MiKTeX 26.5-next
A scalable TeX distribution
Loading...
Searching...
No Matches
PathName.h
Go to the documentation of this file.
1
13
14#pragma once
15
16#include <miktex/Util/config.h>
17
18#include <ostream>
19#include <string>
20#include <vector>
21
22#include <miktex/Util/CharBuffer>
23#include <miktex/Util/PathNameUtil>
24
25#include "OptionSet.h"
26
27MIKTEX_UTIL_BEGIN_NAMESPACE;
28
51
54
55enum class DisplayPathNameOption
56{
57};
58
59typedef OptionSet<DisplayPathNameOption> DisplayPathNameOptions;
60
62class PathName :
63 public MiKTeX::Util::CharBuffer<char, MIKTEX_UTIL_PATHNAME_SIZE>
64{
65
66protected:
67
68 typedef CharBuffer<char, MIKTEX_UTIL_PATHNAME_SIZE> Base;
69
70public:
71
72 PathName() = default;
73 PathName(const PathName& other) = default;
74 PathName& operator=(const PathName& other) = default;
75 PathName(PathName&& other) noexcept = default;
76 PathName& operator=(PathName&& other) noexcept = default;
77 virtual ~PathName() noexcept = default;
78
81 explicit PathName(const char* path) :
82 Base(path)
83 {
84 }
85
88 explicit PathName(const wchar_t* path) :
89 Base(path)
90 {
91 }
92
95 explicit PathName(const std::string& path) :
96 Base(path)
97 {
98 }
99
102 explicit PathName(const std::wstring& path) :
103 Base(path)
104 {
105 }
106
107 PathName(size_t n) = delete;
108
109 PathName& operator=(const char* path)
110 {
111 Base::operator=(path);
112 return *this;
113 }
114
115 PathName& operator=(const wchar_t* path)
116 {
117 Base::operator=(path);
118 return *this;
119 }
120
121 PathName& operator=(const std::string& path)
122 {
123 Base::operator=(path);
124 return *this;
125 }
126
127 PathName& operator=(const std::wstring& path)
128 {
129 Base::operator=(path);
130 return *this;
131 }
132
135 MIKTEXUTILTHISAPI(std::size_t) GetHash() const;
136
137 static MIKTEXUTILCEEAPI(std::vector<std::string>) Split(const PathName& path);
138
139
140 PathName GetDirectoryName() const
141 {
142 std::string directoryName;
143 std::string fileNameWithoutExtension;
144 std::string extension;
145 Split(*this, directoryName, fileNameWithoutExtension, extension);
146 return PathName(directoryName);
147 }
148
149 PathName GetFileName() const
150 {
151 std::string directoryName;
152 std::string fileNameWithoutExtension;
153 std::string extension;
154 Split(*this, directoryName, fileNameWithoutExtension, extension);
155 return PathName(fileNameWithoutExtension + extension);
156 }
157
158 PathName GetFileNameWithoutExtension() const
159 {
160 std::string directoryName;
161 std::string fileNameWithoutExtension;
162 std::string extension;
163 Split(*this, directoryName, fileNameWithoutExtension, extension);
164 return PathName(fileNameWithoutExtension);
165 }
166
169 PathName& RemoveFileSpec()
170 {
171 return CutOffLastComponent();
172 }
173
177 {
178 Base::Set(GetFileName());
179 return *this;
180 }
181
182 MIKTEXUTILTHISAPI(PathName&) SetToHomeDirectory();
183
184 MIKTEXUTILTHISAPI(PathName&) SetToLockDirectory();
185
188 MIKTEXUTILTHISAPI(PathName&) SetToCurrentDirectory();
189
192 MIKTEXUTILTHISAPI(PathName&) SetToTempDirectory();
193
196 MIKTEXUTILTHISAPI(PathName&) SetToTempFile();
197
201 MIKTEXUTILTHISAPI(PathName&) SetToTempFile(const PathName& directory);
202
205 MIKTEXUTILTHISAPI(PathName) GetMountPoint() const;
206
207 MIKTEXUTILTHISAPI(PathName&) Convert(ConvertPathNameOptions options);
208
211 PathName& Clean()
212 {
213 return Convert({ ConvertPathNameOption::CleanUp });
214 }
215
218 PathName& ConvertToUnix()
219 {
220 return Convert({ ConvertPathNameOption::ToUnix });
221 }
222
223 PathName ToUnix() const
224 {
225 PathName result = *this;
226 result.Convert({ ConvertPathNameOption::ToUnix });
227 return result;
228 }
229
232 PathName& ConvertToDos()
233 {
234 return Convert({ ConvertPathNameOption::ToDos });
235 }
236
237 PathName ToDos() const
238 {
239 PathName result = *this;
240 result.Convert({ ConvertPathNameOption::ToDos });
241 return result;
242 }
243
244#if defined(MIKTEX_WINDOWS)
245 PathName ToExtendedLengthPathName() const
246 {
247 PathName result = *this;
248 result.Convert({ ConvertPathNameOption::ToExtendedLengthPathName });
249 return result;
250 }
251#endif
252
253 std::wstring ToWideCharString() const
254 {
255 return MiKTeX::Util::StringUtil::UTF8ToWideChar(ToString());
256 }
257
258#if defined(MIKTEX_WINDOWS)
259 std::wstring ToNativeString() const
260 {
261 return ToExtendedLengthPathName().ToWideCharString();
262 }
263#else
264 std::string ToNativeString() const
265 {
266 return ToString();
267 }
268#endif
269
270 MIKTEXUTILTHISAPI(std::string) ToDisplayString(DisplayPathNameOptions options = {}) const;
271
275 {
276#if defined(MIKTEX_WINDOWS)
278#else
279 return *this;
280#endif
281 }
282
283 bool IsFullyQualified() const
284 {
285 return MiKTeX::Util::PathNameUtil::IsFullyQualifiedPath(ToString());
286 }
287
288 bool IsAbsolute() const
289 {
290 return MiKTeX::Util::PathNameUtil::IsAbsolutePath(ToString());
291 }
292
293 PathName& Canonicalize()
294 {
295 return Convert({ ConvertPathNameOption::Canonicalize });
296 }
297
301 {
303 }
304
305 bool HasExtension() const
306 {
307 return !GetExtension().empty();
308 }
309
313 bool HasExtension(const std::string& extension_) const
314 {
315 if (extension_.empty())
316 {
317 return false;
318 }
319 std::string currentExtension = GetExtension();
320 if (currentExtension.empty())
321 {
322 return false;
323 }
324 auto extension = extension_;
325 if (extension[0] == '.')
326 {
327 extension.erase(0, 1);
328 }
329 if (currentExtension[0] == '.')
330 {
331 currentExtension.erase(0, 1);
332 }
333 return PathName::Equals(PathName(currentExtension), PathName(extension));
334 }
335
339 MIKTEXUTILTHISAPI(std::string) GetExtension() const;
340
346 MIKTEXUTILTHISAPI(PathName&) SetExtension(const std::string& extension, bool override);
347
352 PathName& SetExtension(const std::string& extension)
353 {
354 return SetExtension(extension, true);
355 }
356
357 PathName& AppendExtension(const std::string& extension)
358 {
359 if (!HasExtension(extension))
360 {
361 if (!extension.empty() && extension[0] != '.')
362 {
363 Base::Append('.');
364 }
365 Base::Append(extension);
366 }
367 return *this;
368 }
369
373 {
374 std::size_t l = GetLength();
375 return l > 0 && (MiKTeX::Util::PathNameUtil::IsDirectoryDelimiter(Base::operator[](l - 1)));
376 }
377
383 PathName& Append(const std::string& s, bool appendDirectoryDelimiter)
384 {
385 if (appendDirectoryDelimiter && !Empty() && !s.empty() && !MiKTeX::Util::PathNameUtil::IsDirectoryDelimiter(s[0]))
386 {
387 AppendDirectoryDelimiter();
388 }
389 Base::Append(s);
390 return *this;
391 }
392
396 PathName& AppendComponent(const std::string& component)
397 {
398 return Append(component, true);
399 }
400
404 PathName& operator/=(const std::string& component)
405 {
406 return AppendComponent(component);
407 }
408
411 MIKTEXUTILTHISAPI(PathName&) CutOffLastComponent(bool allowSelfCutting);
412
416 {
417 return CutOffLastComponent(false);
418 }
419
422 MIKTEXUTILTHISAPI(PathName&) AppendDirectoryDelimiter();
423
424#if defined(MIKTEX_WINDOWS)
425 MIKTEXUTILTHISAPI(PathName&) AppendAltDirectoryDelimiter();
426#endif
427
428 bool IsExplicitlyRelative() const
429 {
430 return MiKTeX::Util::PathNameUtil::IsExplicitlyRelative(ToString());
431 }
432
440 static MIKTEXUTILCEEAPI(int) ComparePrefixes(const PathName& path1, const PathName& path2, std::size_t count);
441
449 static MIKTEXUTILCEEAPI(int) Compare(const PathName& path1, const PathName& path2);
450
451 static bool Equals(const PathName& path1, const PathName& path2)
452 {
453 return Compare(path1, path2) == 0;
454 }
455
460 static MIKTEXUTILCEEAPI(bool) Match(const std::string& pattern, const PathName& path);
461
462private:
463
464 static MIKTEXUTILCEEAPI(void) Split(const PathName& path, std::string& directoryName, std::string& fileNameWithoutExtension, std::string& extension);
465
466};
467
468inline bool operator<(const PathName& lhs, const PathName& rhs)
469{
470 return PathName::Compare(lhs, rhs) < 0;
471}
472
477inline bool operator==(const PathName& lhs, const PathName& rhs)
478{
479 return PathName::Equals(lhs, rhs);
480}
481
486inline bool operator!=(const PathName& lhs, const PathName& rhs)
487{
488 return !PathName::Equals(lhs, rhs);
489}
490
491inline PathName operator/(const PathName& lhs, const std::string& rhs)
492{
493 PathName result = lhs;
494 result /= rhs;
495 return result;
496}
497
498inline std::ostream& operator<<(std::ostream& os, const PathName& path)
499{
500 return os << path.ToDisplayString();
501}
502
503MIKTEX_UTIL_END_NAMESPACE;
504
505namespace std
506{
507 template<> struct hash<MiKTeX::Util::PathName>
508 {
509 std::size_t operator()(const MiKTeX::Util::PathName& path) const
510 {
511 return path.GetHash();
512 }
513 };
514}
OptionSet class.
Instances of this class store characters.
Definition CharBuffer.h:30
bool Empty() const
Definition CharBuffer.h:209
std::basic_string< char > ToString() const
Definition CharBuffer.h:242
Definition OptionSet.h:26
Instances of this class can be used to store path names.
Definition PathName.h:64
PathName & MakeFullyQualified()
Definition PathName.h:300
bool EndsWithDirectoryDelimiter() const
Definition PathName.h:372
PathName & operator/=(const std::string &component)
Definition PathName.h:404
static int ComparePrefixes(const PathName &path1, const PathName &path2, std::size_t count)
PathName & AppendComponent(const std::string &component)
Definition PathName.h:396
bool HasExtension(const std::string &extension_) const
Definition PathName.h:313
MIKTEXUTILTHISAPI(PathName &) CutOffLastComponent(bool allowSelfCutting)
MIKTEXUTILTHISAPI(PathName &) SetToTempDirectory()
MIKTEXUTILTHISAPI(PathName &) SetExtension(const std PathName SetExtension)(const std::string &extension)
Definition PathName.h:352
MIKTEXUTILTHISAPI(std::string) GetExtension() const
PathName(const std::wstring &path)
Definition PathName.h:102
PathName & CutOffLastComponent()
Definition PathName.h:415
static int Compare(const PathName &path1, const PathName &path2)
MIKTEXUTILTHISAPI(PathName &) AppendDirectoryDelimiter()
PathName(const wchar_t *path)
Definition PathName.h:88
MIKTEXUTILTHISAPI(std::size_t) GetHash() const
PathName & ConvertToUnix()
Definition PathName.h:218
MIKTEXUTILTHISAPI(PathName &) SetToTempFile(const PathName &directory)
PathName & RemoveFileSpec()
Definition PathName.h:169
PathName(const std::string &path)
Definition PathName.h:95
MIKTEXUTILTHISAPI(PathName &) SetToCurrentDirectory()
static bool Match(const std::string &pattern, const PathName &path)
MIKTEXUTILTHISAPI(PathName &) SetToTempFile()
MIKTEXUTILTHISAPI(PathName) GetMountPoint() const
PathName & TransformForComparison()
Definition PathName.h:274
PathName & Append(const std::string &s, bool appendDirectoryDelimiter)
Definition PathName.h:383
PathName & Clean()
Definition PathName.h:211
PathName & ConvertToDos()
Definition PathName.h:232
PathName(const char *path)
Definition PathName.h:81
PathName & RemoveDirectorySpec()
Definition PathName.h:176
static bool IsDirectoryDelimiter(int ch)
Definition PathNameUtil.h:73
ConvertPathNameOption
Path name conversion option enum class.
Definition PathName.h:31
@ MakeUpper
Replaces small letters with their capital letter counterpart.
Definition PathName.h:39
@ CleanUp
Clean up the path name.
Definition PathName.h:49
@ Canonicalize
Resolve symbolic links.
Definition PathName.h:47
@ MakeFullyQualified
Makes the path name fully qualified.
Definition PathName.h:45
@ ToDos
Replaces slashes with backslashes.
Definition PathName.h:35
@ ToUnix
Replaces backslashes with slashes.
Definition PathName.h:33
@ MakeLower
Replaces capital letters with their small letter counterpart.
Definition PathName.h:37
@ ToExtendedLengthPathName
Prefix with \?\ to create an extended-length path name.
Definition PathName.h:42
OptionSet< ConvertPathNameOption > ConvertPathNameOptions
Path name conversion options.
Definition PathName.h:53
bool operator==(const PathName &lhs, const PathName &rhs)
Definition PathName.h:477
bool operator!=(const PathName &lhs, const PathName &rhs)
Definition PathName.h:486