vsg: include/vsg/io/Path.h Source File

vsg 1.1.3 VulkanSceneGraph library

Loading...
Searching...
No Matches
Path.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2022 Robert Osfield
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8
9The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
11THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12
13</editor-fold> */
14
15#include <vsg/core/Object.h>
16#include <vsg/io/convert_utf.h>
17
18#include <map>
19#include <string>
20
21namespace vsg
22{
23
24 enum FileType
25 {
26 FILE_NOT_FOUND = 0,
27 REGULAR_FILE,
28 DIRECTORY
29 };
30
33 class VSG_DECLSPEC Path
34 {
35 public:
36#if defined(_MSC_VER) || defined(__MINGW32__)
37 using value_type = wchar_t;
38 static constexpr value_type windows_separator = L'\\';
39 static constexpr value_type posix_separator = L'/';
40 static constexpr value_type preferred_separator = windows_separator;
41 static constexpr value_type alternate_separator = posix_separator;
42 static constexpr const value_type* separators = L"/\\";
43#else
44 using value_type = char;
45 static constexpr value_type windows_separator = '\\';
46 static constexpr value_type posix_separator = '/';
47 static constexpr value_type preferred_separator = posix_separator;
48 static constexpr value_type alternate_separator = windows_separator;
49 static constexpr const value_type* separators = "/\\";
50#endif
51 using string_type = std::basic_string<value_type>;
52
53 using size_type = size_t;
54 using reference = value_type&;
55 using const_reference = const value_type&;
56 using iterator = string_type::iterator;
57 using const_iterator = string_type::const_iterator;
58 using pointer = value_type*;
59 using const_pointer = const value_type*;
60
61 static const size_type npos = static_cast<size_type>(-1);
62
63 Path();
64 Path(const Path& path);
65 Path(const std::string& str);
66 Path(const char* str);
67 Path(const std::wstring& str);
68 Path(const wchar_t* str);
69
70 iterator begin() { return _string.begin(); }
71 iterator end() { return _string.end(); }
72 const_iterator begin() const { return _string.begin(); }
73 const_iterator end() const { return _string.end(); }
74
75 Path& assign(const Path& path);
76 Path& assign(const std::string& str);
77 Path& assign(const char* str);
78 Path& assign(const std::wstring& str);
79 Path& assign(const wchar_t* str);
80
81 Path& operator=(const Path& path) { return assign(path); }
82 Path& operator=(const std::string& str) { return assign(str); }
83 Path& operator=(const char* str) { return assign(str); }
84 Path& operator=(const std::wstring& str) { return assign(str); }
85 Path& operator=(const wchar_t* str) { return assign(str); }
86
87 int compare(const Path& rhs) const { return _string.compare(rhs._string); }
88 int compare(size_type pos, size_type n, const Path& rhs) const { return _string.compare(pos, n, rhs._string); }
89
90 int compare(const char* rhs) const { return _string.compare(convert_utf<string_type>(rhs)); }
91 int compare(const wchar_t* rhs) const { return _string.compare(convert_utf<string_type>(rhs)); }
92 int compare(size_type pos, size_type n, const char* rhs) const { return _string.compare(pos, n, convert_utf<string_type>(rhs)); }
93 int compare(size_type pos, size_type n, const wchar_t* rhs) const { return _string.compare(pos, n, convert_utf<string_type>(rhs)); }
94
95 bool operator==(const Path& rhs) const { return compare(rhs) == 0; }
96 bool operator!=(const Path& rhs) const { return compare(rhs) != 0; }
97 bool operator<(const Path& rhs) const { return compare(rhs) < 0; }
98
99 bool operator==(const char* rhs) const { return compare(convert_utf<string_type>(rhs)) == 0; }
100 bool operator!=(const char* rhs) const { return compare(convert_utf<string_type>(rhs)) != 0; }
101
102 bool operator==(const wchar_t* rhs) const { return compare(convert_utf<string_type>(rhs)) == 0; }
103 bool operator!=(const wchar_t* rhs) const { return compare(convert_utf<string_type>(rhs)) != 0; }
104
105 explicit operator bool() const noexcept { return !_string.empty(); }
106 bool empty() const { return _string.empty(); }
107 size_type size() const { return _string.size(); }
108 size_type length() const { return _string.size(); }
109
110 inline std::string string() const
111 {
112 std::string dest;
113 convert_utf(_string, dest);
114 return dest;
115 }
116 inline std::wstring wstring() const
117 {
118 std::wstring dest;
119 convert_utf(_string, dest);
120 return dest;
121 }
122
123 inline const string_type& native() const noexcept { return _string; }
124 inline operator const string_type&() const noexcept { return _string; }
125 inline const value_type* c_str() const noexcept { return _string.c_str(); }
126#if defined(__MINGW32__)
127 inline operator const value_type*() const noexcept
128 {
129 return _string.c_str();
130 }
131#endif
132
133 reference operator[](size_type pos)
134 {
135 return _string[pos];
136 }
137 const_reference operator[](size_type pos) const { return _string[pos]; }
138
139 void clear() noexcept { _string.clear(); }
140 void swap(Path& rhs) noexcept { return _string.swap(rhs._string); }
141
143 Path& concat(const Path& path)
144 {
145 _string.append(path._string);
146 return *this;
147 }
148
150 Path& concat(char c)
151 {
152 _string.push_back(c);
153 return *this;
154 }
155
157 Path& operator+=(const Path& path) { return concat(path); }
158
160 Path& append(const Path& path);
161
163 Path& operator/=(const Path& path) { return append(path); }
164
165 Path substr(size_type pos, size_type len = Path::npos) const { return Path(_string.substr(pos, len)); }
166
167 size_type find(const Path& s, size_type pos = 0) const { return _string.find(s._string, pos); }
168 size_type find(const char* s, size_type pos = 0) const { return _string.find(convert_utf<string_type>(s), pos); }
169 size_type find(const wchar_t* s, size_type pos = 0) const { return _string.find(convert_utf<string_type>(s), pos); }
170
171 size_type find_first_of(const Path& s, size_type pos = 0) const { return _string.find_first_of(s._string, pos); }
172 size_type find_first_of(const char* s, size_type pos = 0) const { return find_first_of(convert_utf<string_type>(s), pos); }
173 size_type find_first_of(const char c, size_type pos = 0) const { return find_first_of(convert_utf<string_type>(c), pos); }
174 size_type find_first_of(const wchar_t* s, size_type pos = 0) const { return find_first_of(convert_utf<string_type>(s), pos); }
175 size_type find_first_of(const wchar_t c, size_type pos = 0) const { return find_first_of(convert_utf<string_type>(c), pos); }
176
177 size_type find_last_of(const Path& s, size_type pos = npos) const { return _string.find_last_of(s._string, pos); }
178 size_type find_last_of(const char* s, size_type pos = npos) const { return find_last_of(convert_utf<string_type>(s), pos); }
179 size_type find_last_of(const char c, size_type pos = npos) const { return find_last_of(convert_utf<string_type>(c), pos); }
180 size_type find_last_of(const wchar_t* s, size_type pos = npos) const { return find_last_of(convert_utf<string_type>(s), pos); }
181 size_type find_last_of(const wchar_t c, size_type pos = npos) const { return find_last_of(convert_utf<string_type>(c), pos); }
182
183 Path& replace(size_type pos, size_type n, const Path& str);
184 Path& replace(size_type pos, size_type n, const std::string& str);
185 Path& replace(size_type pos, size_type n, const std::wstring& str);
186 Path& replace(size_type pos, size_type n, const char* str);
187 Path& replace(size_type pos, size_type n, const wchar_t* str);
188
189 Path& erase(size_t pos = 0, size_t len = Path::npos);
190
191 FileType type() const;
192
193 Path lexically_normal() const;
194
195 protected:
196 string_type _string;
197 };
198 VSG_type_name(vsg::Path);
199
201 inline Path operator+(const Path& lhs, const Path& rhs)
202 {
203 Path path(lhs);
204 return path.concat(rhs);
205 }
206
208 inline Path operator/(const Path& lhs, const Path& rhs)
209 {
210 Path path(lhs);
211 return path /= rhs;
212 }
213
214 using Paths = std::vector<Path>;
215 using PathObjects = std::map<Path, ref_ptr<Object>>;
216
218 extern VSG_DECLSPEC Path filePath(const Path& path);
219
221 extern VSG_DECLSPEC Path fileExtension(const Path& path);
222
225 extern VSG_DECLSPEC Path lowerCaseFileExtension(const Path& path, bool pruneExtras = true);
226
228 extern VSG_DECLSPEC Path simpleFilename(const Path& path);
229
231 extern VSG_DECLSPEC bool trailingRelativePath(const Path& path);
232
234 extern VSG_DECLSPEC Path removeExtension(const Path& path);
235
236} // namespace vsg
vsg::Path
Definition Path.h:34
vsg::Path::concat
Path & concat(const Path &path)
directly add to end of path without a path separator
Definition Path.h:143
vsg::Path::append
Path & append(const Path &path)
add to end of path with path separator
vsg::Path::operator/=
Path & operator/=(const Path &path)
add to end of path with path separator
Definition Path.h:163
vsg::Path::concat
Path & concat(char c)
directly add to end of path without a path separator
Definition Path.h:150
vsg::Path::operator+=
Path & operator+=(const Path &path)
directly add to end of path without a path separator
Definition Path.h:157
vsg::allocator_affinity_nodes
std container adapter for allocating with MEMORY_AFFINITY_NODES
Definition Allocator.h:138