MiKTeX 26.5-next
A scalable TeX distribution
Loading...
Searching...
No Matches
PathNameUtil.h
Go to the documentation of this file.
1
13
14#pragma once
15
16#include <miktex/Util/config.h>
17
18#include <string>
19
20MIKTEX_UTIL_BEGIN_NAMESPACE;
21
22class MIKTEXNOVTABLE PathNameUtil
23{
24
25public:
26
27 PathNameUtil() = delete;
28 PathNameUtil(const PathNameUtil& other) = delete;
29 PathNameUtil& operator=(const PathNameUtil& other) = delete;
30 PathNameUtil(PathNameUtil&& other) = delete;
31 PathNameUtil& operator=(PathNameUtil&& other) = delete;
32 ~PathNameUtil() = delete;
33
34 static constexpr char DosDirectoryDelimiter{ '\\' };
35 static constexpr char UnixDirectoryDelimiter{ '/' };
36 static constexpr char DosPathNameDelimiter{ ';' };
37 static constexpr char UnixPathNameDelimiter{ ':' };
38 static constexpr char DosVolumeDelimiter{ ':' };
39
40#if defined(MIKTEX_WINDOWS)
41 static constexpr char AltDirectoryDelimiter{ UnixDirectoryDelimiter };
42#endif
43
44#if defined(MIKTEX_WINDOWS)
45 static constexpr char DirectoryDelimiter{ DosDirectoryDelimiter };
46#elif defined(MIKTEX_UNIX)
47 static constexpr char DirectoryDelimiter{ UnixDirectoryDelimiter };
48#endif
49
50#if defined(MIKTEX_WINDOWS)
51 static constexpr char PathNameDelimiter{ DosPathNameDelimiter };
52#elif defined(MIKTEX_UNIX)
53 static constexpr char PathNameDelimiter{ UnixPathNameDelimiter };
54#endif
55
56#if defined(MIKTEX_WINDOWS)
57 static constexpr char VolumeDelimiter{ DosVolumeDelimiter };
58#endif
59
60 static bool IsDosDriveLetter(int ch)
61 {
62 return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z');
63 }
64
65 static bool IsDosVolumeDelimiter(int ch)
66 {
67 return ch == DosVolumeDelimiter;
68 }
69
73 static bool IsDirectoryDelimiter(int ch)
74 {
75 if (ch == DirectoryDelimiter)
76 {
77 return true;
78 }
79#if defined(MIKTEX_WINDOWS)
80 return ch == AltDirectoryDelimiter;
81#else
82 return false;
83#endif
84 }
85
86 static bool IsExplicitlyRelative(const std::string& path)
87 {
88 if (path.empty())
89 {
90 return false;
91 }
92 else if (path[0] == '.')
93 {
94 return (path.length() > 1 && IsDirectoryDelimiter(path[1])) || (path.length() > 2 && path[1] == '.' && IsDirectoryDelimiter(path[2]));
95 }
96 else
97 {
98 return false;
99 }
100 }
101
102 static bool IsAbsolutePath(const std::string& path)
103 {
104 if (path.empty())
105 {
106 return false;
107 }
108 else if (IsDirectoryDelimiter(path[0]))
109 {
110 // "/xyz/foo.txt", also "//server/xyz/foo.txt"
111 return true;
112 }
113#if defined(MIKTEX_WINDOWS)
114 else if (path.length() > 2 && PathNameUtil::IsDosDriveLetter(path[0]) // "C:\xyz\foo.txt"
115 && IsDosVolumeDelimiter(path[1])
116 && IsDirectoryDelimiter(path[2]))
117 {
118 return true;
119 }
120#endif
121 else
122 {
123 return false;
124 }
125 }
126
127 static bool IsFullyQualifiedPath(const std::string& path)
128 {
129#if defined(MIKTEX_WINDOWS)
130 if (path.length() < 3)
131 {
132 return false;
133 }
134 else if (IsDirectoryDelimiter(path[0]) && IsDirectoryDelimiter(path[1]))
135 {
136 // \\server\xyz\foo.txt
137 return true;
138 }
139 else if (PathNameUtil::IsDosDriveLetter(path[0])
140 && IsDosVolumeDelimiter(path[1])
141 && IsDirectoryDelimiter(path[2]))
142 {
143 // C:\xyz\foo.txt
144 return true;
145 }
146 else
147 {
148 return false;
149 }
150#else
151 return IsAbsolutePath(path);
152#endif
153 }
154
155 static char ToUnix(char ch)
156 {
157 return ch == DosDirectoryDelimiter ? UnixDirectoryDelimiter : ch;
158 }
159
160 static char ToDos(char ch)
161 {
162 return ch == UnixDirectoryDelimiter ? DosDirectoryDelimiter : ch;
163 }
164
165 static void ConvertToUnix(std::string& path)
166 {
167 for (char& ch : path)
168 {
169 ch = ToUnix(ch);
170 }
171 }
172
173 static void ConvertToDos(std::string& path)
174 {
175 for (char& ch : path)
176 {
177 ch = ToDos(ch);
178 }
179 }
180
181#if defined(MIKTEX_WINDOWS)
182 static MIKTEXUTILCEEAPI(std::wstring) ToLengthExtendedPathName(const std::string& path);
183#endif
184};
185
186MIKTEX_UTIL_END_NAMESPACE;
static bool IsDirectoryDelimiter(int ch)
Definition PathNameUtil.h:73
@ ToDos
Replaces slashes with backslashes.
Definition PathName.h:35
@ ToUnix
Replaces backslashes with slashes.
Definition PathName.h:33