Skip to content

Commit f2540b6

Browse files
Fea, 添加YY::Base::IO::Path
1 parent 53c5ffc commit f2540b6

File tree

8 files changed

+807
-9
lines changed

8 files changed

+807
-9
lines changed

UnitTest/PathUnitTest.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "ToStringHelper.h"
2+
#include "CppUnitTest.h"
3+
4+
#include <YY/Base/IO/Path.h>
5+
6+
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
7+
using namespace YY;
8+
9+
namespace UnitTest
10+
{
11+
TEST_CLASS(Path)
12+
{
13+
public:
14+
TEST_METHOD(Combine)
15+
{
16+
Assert::AreEqual(YY::Path::Combine(_S("C:\\"), _S("mydir"), _S("test.txt")), _S("C:\\mydir\\test.txt"));
17+
18+
Assert::AreEqual(YY::Path::Combine(_S("C:\\"), _S("D:\\SSS"), _S("mydir"), _S("test.txt")), _S("D:\\SSS\\mydir\\test.txt"));
19+
20+
Assert::AreEqual(YY::Path::Combine(_S("C:\\"), _S("\\\\TTT\\SSS"), _S("mydir"), _S("test.txt")), _S("\\\\TTT\\SSS\\mydir\\test.txt"));
21+
}
22+
};
23+
}

UnitTest/StringUnitTest.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
#include "CppUnitTest.h"
22
#include "ToStringHelper.h"
33

4-
#include <atlstr.h>
5-
#include <Windows.h>
6-
#include <tchar.h>
7-
#include <string>
8-
#include <atltypes.h>
9-
104
#include <YY/Base/Strings/NString.h>
115

126
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
@@ -174,6 +168,9 @@ namespace UnitTest
174168
_szTemp.Remove(5, 2);
175169
Assert::AreEqual(_szTemp, _S("23678"));
176170

171+
_szTemp.Remove(3);
172+
Assert::AreEqual(_szTemp, _S("236"));
173+
177174
_szTemp.Remove(0);
178175
Assert::AreEqual(_szTemp, _S(""));
179176
}

UnitTest/UnitTest.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@
179179
<ClCompile Include="BitMapUnitTest.cpp" />
180180
<ClCompile Include="DynamicArrayUnitTest.cpp" />
181181
<ClCompile Include="ObserverPtrUnitTest.cpp" />
182+
<ClCompile Include="PathUnitTest.cpp" />
182183
<ClCompile Include="SpanUnitTest.cpp" />
183184
<ClCompile Include="StringUnitTest.cpp" />
184185
<ClCompile Include="TaskRunnerUnitTest.cpp" />

include/YY/Base/IO/Path.h

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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)

include/YY/Base/Strings/String.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ namespace YY
167167
_Field_z_ char_t* szString;
168168

169169
public:
170-
explicit StringBase() noexcept
170+
explicit constexpr StringBase() noexcept
171171
: szString(StringData::GetEmtpyStringData()->GetStringBuffer())
172172
{
173173
}
@@ -1172,7 +1172,7 @@ namespace YY
11721172
return _pNewStringData;
11731173
}
11741174

1175-
static _Ret_notnull_ StringData* __YYAPI GetEmtpyStringData()
1175+
static constexpr _Ret_notnull_ StringData* __YYAPI GetEmtpyStringData() noexcept
11761176
{
11771177
struct StaticStringData
11781178
{
@@ -1187,7 +1187,7 @@ namespace YY
11871187
return const_cast<StringData*>(&g_EmptyStringData.Base);
11881188
}
11891189

1190-
_Ret_z_ char_t* __YYAPI GetStringBuffer()
1190+
constexpr _Ret_z_ char_t* __YYAPI GetStringBuffer() noexcept
11911191
{
11921192
return reinterpret_cast<char_t*>(this + 1);
11931193
}

src/YY.Base.vcxitems

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
<ProjectCapability Include="SourceItemsFromImports" />
1515
</ItemGroup>
1616
<ItemGroup>
17+
<ClCompile Include="$(MSBuildThisFileDirectory)YY\Base\IO\Path.cpp">
18+
<PrecompiledHeader>NotUsing</PrecompiledHeader>
19+
</ClCompile>
1720
<ClCompile Include="$(MSBuildThisFileDirectory)YY\Base\Time\TimeZone.cpp">
1821
<PrecompiledHeader>NotUsing</PrecompiledHeader>
1922
</ClCompile>
@@ -61,6 +64,7 @@
6164
</ClCompile>
6265
</ItemGroup>
6366
<ItemGroup>
67+
<ClInclude Include="$(MSBuildThisFileDirectory)..\include\YY\Base\IO\Path.h" />
6468
<ClInclude Include="$(MSBuildThisFileDirectory)..\include\YY\Base\Time\TimeZone.h" />
6569
<ClInclude Include="$(MSBuildThisFileDirectory)..\include\YY\Base\Utils\AutoCleanup.h" />
6670
<ClInclude Include="$(MSBuildThisFileDirectory)\..\include\YY\Base\Containers\Array.h" />

src/YY.Base.vcxitems.filters

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@
7373
<Filter Include="资源文件">
7474
<UniqueIdentifier>{f6e926ce-f565-470c-8c1f-a29246361bc9}</UniqueIdentifier>
7575
</Filter>
76+
<Filter Include="源文件\YY\Base\IO">
77+
<UniqueIdentifier>{a7a25633-a86d-4ecd-8d49-206521c9e98a}</UniqueIdentifier>
78+
</Filter>
7679
<Filter Include="源文件\YY\Base\Time">
7780
<UniqueIdentifier>{c1cc024f-013f-4121-ac07-1c108bb052ec}</UniqueIdentifier>
7881
</Filter>
@@ -123,6 +126,9 @@
123126
<ClCompile Include="$(MSBuildThisFileDirectory)\YY\Base\Time\DataTime.cpp">
124127
<Filter>源文件\YY\Base\Time</Filter>
125128
</ClCompile>
129+
<ClCompile Include="$(MSBuildThisFileDirectory)YY\Base\IO\Path.cpp">
130+
<Filter>源文件\YY\Base\IO</Filter>
131+
</ClCompile>
126132
</ItemGroup>
127133
<ItemGroup>
128134
<ClInclude Include="$(MSBuildThisFileDirectory)\..\include\YY\Base\Containers\Array.h">
@@ -296,6 +302,9 @@
296302
<ClInclude Include="$(MSBuildThisFileDirectory)\..\include\YY\Base\Time\DataTime.h">
297303
<Filter>头文件\YY\Base\Time</Filter>
298304
</ClInclude>
305+
<ClInclude Include="$(MSBuildThisFileDirectory)..\include\YY\Base\IO\Path.h">
306+
<Filter>头文件\YY\Base\IO</Filter>
307+
</ClInclude>
299308
<ClInclude Include="$(MSBuildThisFileDirectory)..\include\YY\Base\Utils\AutoCleanup.h">
300309
<Filter>头文件\YY\Base\Utils</Filter>
301310
</ClInclude>

0 commit comments

Comments
 (0)