|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <YY/Base/YY.h> |
| 4 | +#include <YY/Base/Strings/String.h> |
| 5 | + |
| 6 | +#pragma pack(push, __YY_PACKING) |
| 7 | + |
| 8 | +/* |
| 9 | +一开始Path被设计为类似于Java的跨平台的路径处理类,但是这样做的话其他打开路径的API不太方便直接传递裸C风格指针。 |
| 10 | +
|
| 11 | +目前暂时设计为根.NET类似的工具类,提供一些静态方法来处理路径字符串。 |
| 12 | +
|
| 13 | +# Windows平台: |
| 14 | + 相对路径 |
| 15 | + - 当前驱动器的相对路径:\mydir\test.txt |
| 16 | + - 当前目录的相对路径:mydir\test.txt |
| 17 | + 传统Dos路径 |
| 18 | + - 驱动器根目录的绝对文件路径:C:\mydir\test.txt |
| 19 | + Dos设备路径 |
| 20 | + - \\?\C:\mydir\test.txt |
| 21 | + - \\?\Volume{GUID}\mydir\test.txt |
| 22 | + - UNC形式:\\?\UNC\ComputerName\SharedFolder\mydir\test.txt |
| 23 | + UNC路径: |
| 24 | + - \\ComputerName\SharedFolder\mydir\test.txt |
| 25 | +
|
| 26 | +# Linux/Unix平台: |
| 27 | + 相对路径 |
| 28 | + - 当前目录的相对路径:mydir/test.txt |
| 29 | + 绝对路径 |
| 30 | + - /mydir/test.txt |
| 31 | +*/ |
| 32 | + |
| 33 | +#pragma push_macro("GetCurrentDirectory") |
| 34 | +#ifdef GetCurrentDirectory |
| 35 | +#undef GetCurrentDirectory |
| 36 | +#endif |
| 37 | + |
| 38 | +namespace YY |
| 39 | +{ |
| 40 | + namespace Base |
| 41 | + { |
| 42 | + namespace IO |
| 43 | + { |
| 44 | + class Path |
| 45 | + { |
| 46 | + public: |
| 47 | +#if defined(WIN32) || defined(_WIN32) |
| 48 | + static constexpr uchar_t kAltPathSeparatorChar = '/'; |
| 49 | + static constexpr uchar_t kDirectorySeparatorChar = '\\'; |
| 50 | + static constexpr uchar_t kPathSeparatorChar = ';'; |
| 51 | + static constexpr uchar_t kVolumeSeparatorChar = ':'; |
| 52 | + |
| 53 | + static constexpr uchar_t kDirectorySeparatorChars[] = { kDirectorySeparatorChar, kAltPathSeparatorChar }; |
| 54 | + static constexpr uchar_t kInvalidFileNameChars[] = { '\"', '<', '>', '|', ':', '*', '?', '\\', '/', '\0', '\x1', '\x2', '\x3', '\x4', '\x5', '\x6', '\a', '\b', '\t', '\n', '\v', '\f', '\r', '\xE', '\xF', '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19', '\x1A', '\x1B', '\x1C', '\x1D', '\x1E', '\x1F' }; |
| 55 | + static constexpr uchar_t kInvalidPathChars[] = { '|', '\0', '\x1', '\x2', '\x3', '\x4', '\x5', '\x6', '\a', '\b', '\t', '\n', '\v', '\f', '\r', '\xE', '\xF', '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19', '\x1A', '\x1B', '\x1C', '\x1D', '\x1E', '\x1F' }; |
| 56 | +#elif defined(__linux__) || defined(__unix__) |
| 57 | + static constexpr uchar_t kAltPathSeparatorChar = '/'; |
| 58 | + static constexpr uchar_t kDirectorySeparatorChar = '/'; |
| 59 | + static constexpr uchar_t kPathSeparatorChar = ';'; |
| 60 | + static constexpr uchar_t kVolumeSeparatorChar = '/'; |
| 61 | + |
| 62 | + static constexpr uchar_t kDirectorySeparatorChars[] = { kDirectorySeparatorChar }; |
| 63 | + static constexpr uchar_t kInvalidFileNameChars[] = { '\"', '<', '>', '|', ':', '*', '?', '\\', '/', '\0', '\x1', '\x2', '\x3', '\x4', '\x5', '\x6', '\a', '\b', '\t', '\n', '\v', '\f', '\r', '\xE', '\xF', '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19', '\x1A', '\x1B', '\x1C', '\x1D', '\x1E', '\x1F' }; |
| 64 | + static constexpr uchar_t kInvalidPathChars[] = { '|', '\0', '\x1', '\x2', '\x3', '\x4', '\x5', '\x6', '\a', '\b', '\t', '\n', '\v', '\f', '\r', '\xE', '\xF', '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19', '\x1A', '\x1B', '\x1C', '\x1D', '\x1E', '\x1F' }; |
| 65 | +#elif defined(__APPLE__) |
| 66 | + static constexpr uchar_t kAltPathSeparatorChar = '/'; |
| 67 | + static constexpr uchar_t kDirectorySeparatorChar = '/'; |
| 68 | + static constexpr uchar_t kPathSeparatorChar = ';'; |
| 69 | + static constexpr uchar_t kVolumeSeparatorChar = ':'; |
| 70 | + |
| 71 | + static constexpr uchar_t kDirectorySeparatorChars[] = { kDirectorySeparatorChar }; |
| 72 | + static constexpr uchar_t kInvalidFileNameChars[] = { '\"', '<', '>', '|', ':', '*', '?', '\\', '/', '\0', '\x1', '\x2', '\x3', '\x4', '\x5', '\x6', '\a', '\b', '\t', '\n', '\v', '\f', '\r', '\xE', '\xF', '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19', '\x1A', '\x1B', '\x1C', '\x1D', '\x1E', '\x1F' }; |
| 73 | + static constexpr uchar_t kInvalidPathChars[] = { '|', '\0', '\x1', '\x2', '\x3', '\x4', '\x5', '\x6', '\a', '\b', '\t', '\n', '\v', '\f', '\r', '\xE', '\xF', '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19', '\x1A', '\x1B', '\x1C', '\x1D', '\x1E', '\x1F' }; |
| 74 | +#else |
| 75 | +#error "unknow system" |
| 76 | +#endif |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// 获取路径的根部分。请注意,根路径可以是绝对路径(即完全限定路径)或相对路径。绝对根路径是从驱动器根目录到特定目录的完全限定路径。 |
| 80 | + /// 相对根路径指定驱动器,但其完全限定路径是针对当前目录解析的。常见结果如下: |
| 81 | + /// \mydir\test.txt -> \ |
| 82 | + /// C:\mydir\test.txt -> C:\ |
| 83 | + /// \\?\C:\mydir\test.txt -> \\?\C:\ |
| 84 | + /// \\?\Volume{GUID}\mydir\test.txt -> \\?\Volume{GUID}\ |
| 85 | + /// \\?\UNC\ComputerName\SharedFolder\mydir\test.txt -> \\?\UNC\ComputerName\SharedFolder\ |
| 86 | + /// \\ComputerName\SharedFolder\mydir\test.txt -> \\ComputerName\SharedFolder\ |
| 87 | + /// </summary> |
| 88 | + /// <param name="_sPath">要提取根路径的路径字符串视图。</param> |
| 89 | + /// <returns>路径字符串的根路径部分,类型为 uStringView。</returns> |
| 90 | + static uStringView __YYAPI GetPathRoot(const uStringView& _sPath); |
| 91 | + |
| 92 | + static uString __YYAPI GetPathRoot(uString _szPath); |
| 93 | + |
| 94 | + static bool __YYAPI IsPathRooted(const uStringView& _sPath); |
| 95 | + |
| 96 | + static bool __YYAPI RemoveFileSpec(uStringView& _sPath); |
| 97 | + |
| 98 | + static bool __YYAPI RemoveFileSpec(uString& _szPath); |
| 99 | + |
| 100 | + static uStringView __YYAPI GetFileName(const uStringView& _sPath); |
| 101 | + |
| 102 | + static uString __YYAPI GetFileName(uString _szPath); |
| 103 | + |
| 104 | + static uStringView __YYAPI GetExtension(const uStringView& _sPath); |
| 105 | + |
| 106 | + static uString __YYAPI GetExtension(uString _szPath); |
| 107 | + |
| 108 | + static uStringView __YYAPI GetFileNameWithoutExtension(const uStringView& _sPath); |
| 109 | + |
| 110 | + static uString __YYAPI GetFileNameWithoutExtension(uString _szPath); |
| 111 | + |
| 112 | + static uStringView __YYAPI GetDirectoryPath(uStringView _sPath); |
| 113 | + |
| 114 | + static uString __YYAPI GetDirectoryPath(uString _szPath); |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// 判断路径是否为完全限定路径(或UNC路径)。 |
| 118 | + /// </summary> |
| 119 | + /// <param name="_sPath">路径。</param> |
| 120 | + /// <returns>如果路径是完全限定路径,则返回 true;否则返回 false。</returns> |
| 121 | + static bool __YYAPI IsPathFullyQualified(const uStringView& _sPath); |
| 122 | + |
| 123 | + static bool __YYAPI Append(uString& _szPath, const uStringView& _sPathAppend); |
| 124 | + |
| 125 | + template<typename... Args> |
| 126 | + static bool __YYAPI Append(uString& _szPath, const uStringView& _sFirstPathAppend, Args&&... _OtherPaths) |
| 127 | + { |
| 128 | + Append(_szPath, _sFirstPathAppend); |
| 129 | + return Append(_szPath, std::forward<Args>(_OtherPaths)...); |
| 130 | + } |
| 131 | + |
| 132 | + template<typename... Args> |
| 133 | + static uString __YYAPI Combine(uString _szPath1, Args&&... _OtherPaths) |
| 134 | + { |
| 135 | + Append(_szPath1, std::forward<Args>(_OtherPaths)...); |
| 136 | + return _szPath1; |
| 137 | + } |
| 138 | + |
| 139 | + static uString __YYAPI GetFullPath(const uStringView& _sPath); |
| 140 | + |
| 141 | + static uString __YYAPI GetCurrentDirectory(); |
| 142 | + }; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + using namespace YY::Base::IO; |
| 147 | +} |
| 148 | + |
| 149 | +#pragma pop_macro("GetCurrentDirectory") |
| 150 | + |
| 151 | +#pragma pack(pop) |
0 commit comments