| 1 |
/* |
| 2 |
* mptPathString.h |
| 3 |
* --------------- |
| 4 |
* Purpose: Wrapper class around the platform-native representation of path names. Should be the only type that is used to store path names. |
| 5 |
* Notes : Currently none. |
| 6 |
* Authors: OpenMPT Devs |
| 7 |
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details. |
| 8 |
*/ |
| 9 |
|
| 10 |
|
| 11 |
#pragma once |
| 12 |
|
| 13 |
#include "openmpt/all/BuildSettings.hpp" |
| 14 |
|
| 15 |
#include "mpt/base/detect.hpp" |
| 16 |
#include "mpt/base/namespace.hpp" |
| 17 |
#include "mpt/path/basic_path.hpp" |
| 18 |
#include "mpt/path/native_path.hpp" |
| 19 |
#include "mpt/path/os_path.hpp" |
| 20 |
#include "mpt/string/types.hpp" |
| 21 |
|
| 22 |
#if defined(MODPLUG_TRACKER) |
| 23 |
#include "mpt/string_transcode/transcode.hpp" |
| 24 |
#endif |
| 25 |
|
| 26 |
#include "mptString.h" |
| 27 |
|
| 28 |
#if defined(MODPLUG_TRACKER) && MPT_OS_WINDOWS |
| 29 |
#include <windows.h> |
| 30 |
#endif |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
OPENMPT_NAMESPACE_BEGIN |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
namespace mpt |
| 39 |
{ |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
#if defined(MPT_ENABLE_CHARSET_LOCALE) |
| 44 |
|
| 45 |
using PathString = mpt::native_path; |
| 46 |
|
| 47 |
#define MPT_PATHSTRING_LITERAL(x) MPT_OS_PATH_LITERAL( x ) |
| 48 |
#define MPT_PATHSTRING(x) mpt::PathString::FromNative(MPT_OS_PATH_LITERAL( x )) |
| 49 |
|
| 50 |
#else // !MPT_ENABLE_CHARSET_LOCALE |
| 51 |
|
| 52 |
using PathString = mpt::BasicPathString<mpt::Utf8PathTraits, false>; |
| 53 |
|
| 54 |
#define MPT_PATHSTRING_LITERAL(x) ( x ) |
| 55 |
#define MPT_PATHSTRING(x) mpt::PathString::FromNative( x ) |
| 56 |
|
| 57 |
#endif // MPT_ENABLE_CHARSET_LOCALE |
| 58 |
|
| 59 |
using RawPathString = PathString::raw_path_type; |
| 60 |
|
| 61 |
#define PC_(x) MPT_PATHSTRING_LITERAL(x) |
| 62 |
#define PL_(x) MPT_PATHSTRING_LITERAL(x) |
| 63 |
#define P_(x) MPT_PATHSTRING(x) |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
template <typename T, typename std::enable_if<std::is_same<T, mpt::PathString>::value, bool>::type = true> |
| 68 |
inline mpt::ustring ToUString(const T &x) |
| 69 |
{ |
| 70 |
return x.ToUnicode(); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
#if defined(MODPLUG_TRACKER) |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
#if MPT_OS_WINDOWS |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
// Relative / absolute paths conversion |
| 84 |
|
| 85 |
mpt::PathString AbsolutePathToRelative(const mpt::PathString &p, const mpt::PathString &relativeTo); // similar to std::fs::path::lexically_approximate |
| 86 |
|
| 87 |
mpt::PathString RelativePathToAbsolute(const mpt::PathString &p, const mpt::PathString &relativeTo); |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
#if !MPT_OS_WINDOWS_WINRT |
| 92 |
inline int PathCompareNoCase(const PathString &a, const PathString &b) |
| 93 |
{ |
| 94 |
return lstrcmpi(a.AsNative().c_str(), b.AsNative().c_str()); |
| 95 |
} |
| 96 |
#endif // !MPT_OS_WINDOWS_WINRT |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
#endif // MPT_OS_WINDOWS |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
template <typename Tstring> |
| 105 |
inline Tstring SanitizePathComponent(const Tstring &str) |
| 106 |
{ |
| 107 |
return mpt::transcode<Tstring>(mpt::native_path::FromNative(mpt::transcode<mpt::native_path::raw_path_type>(str)).AsSanitizedComponent().AsNative()); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
#endif // MODPLUG_TRACKER |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
} // namespace mpt |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
OPENMPT_NAMESPACE_END |