MaterialX 1.38.2
File.h
Go to the documentation of this file.
1//
2// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
3// All rights reserved. See LICENSE.txt for license.
4//
5
6#ifndef MATERIALX_FILE_H
7#define MATERIALX_FILE_H
8
11
13
14#include <MaterialXCore/Util.h>
15
16namespace MaterialX
17{
18
19class FilePath;
20using FilePathVec = vector<FilePath>;
21
22extern MX_FORMAT_API const string PATH_LIST_SEPARATOR;
23extern MX_FORMAT_API const string MATERIALX_SEARCH_PATH_ENV_VAR;
24
27class MX_FORMAT_API FilePath
28{
29 public:
30 enum Type
31 {
32 TypeRelative = 0,
33 TypeAbsolute = 1,
34 TypeNetwork = 2
35 };
36
37 enum Format
38 {
39 FormatWindows = 0,
40 FormatPosix = 1,
41 #if defined(_WIN32)
42 FormatNative = FormatWindows
43 #else
44 FormatNative = FormatPosix
45 #endif
46 };
47
48 public:
49 FilePath() :
50 _type(TypeRelative)
51 {
52 }
53 ~FilePath() { }
54
55 bool operator==(const FilePath& rhs) const
56 {
57 return _vec == rhs._vec &&
58 _type == rhs._type;
59 }
60 bool operator!=(const FilePath& rhs) const
61 {
62 return !(*this == rhs);
63 }
64
67
69 FilePath(const string& str)
70 {
71 assign(str);
72 }
73
75 FilePath(const char* str)
76 {
77 assign(str ? string(str) : EMPTY_STRING);
78 }
79
81 operator string() const
82 {
83 return asString();
84 }
85
87 void assign(const string& str);
88
90 string asString(Format format = FormatNative) const;
91
93 bool isEmpty() const
94 {
95 return _vec.empty();
96 }
97
99 bool isAbsolute() const
100 {
101 return _type != TypeRelative;
102 }
103
106 const string& getBaseName() const
107 {
108 if (isEmpty())
109 {
110 return EMPTY_STRING;
111 }
112 return _vec[_vec.size() - 1];
113 }
114
118 {
119 FilePath parent(*this);
120 if (!parent.isEmpty())
121 {
122 parent._vec.pop_back();
123 }
124 return parent;
125 }
126
128 string getExtension() const
129 {
130 const string& baseName = getBaseName();
131 size_t i = baseName.rfind('.');
132 return i != string::npos ? baseName.substr(i + 1) : EMPTY_STRING;
133 }
134
136 void addExtension(const string& ext)
137 {
138 assign(asString() + "." + ext);
139 }
140
143 {
144 if (!isEmpty())
145 {
146 string& baseName = _vec[_vec.size() - 1];
147 size_t i = baseName.rfind('.');
148 if (i != string::npos)
149 {
150 baseName = baseName.substr(0, i);
151 }
152 }
153 }
154
157 FilePath operator/(const FilePath& rhs) const;
158
160 size_t size() const
161 {
162 return _vec.size();
163 }
164
166 string operator[](size_t index)
167 {
168 return _vec[index];
169 }
170
172 const string& operator[](size_t index) const
173 {
174 return _vec[index];
175 }
176
180
182 bool exists() const;
183
185 bool isDirectory() const;
186
188 FilePathVec getFilesInDirectory(const string& extension) const;
189
191 FilePathVec getSubDirectories() const;
192
194 void createDirectory() const;
195
197
199 static FilePath getCurrentPath();
200
202 static FilePath getModulePath();
203
204 private:
205 StringVec _vec;
206 Type _type;
207};
208
212class MX_FORMAT_API FileSearchPath
213{
214 public:
215 using Iterator = FilePathVec::iterator;
216 using ConstIterator = FilePathVec::const_iterator;
217
218 public:
220 {
221 }
222 ~FileSearchPath() { }
223
229 FileSearchPath(const string& searchPath, const string& sep = PATH_LIST_SEPARATOR)
230 {
231 for (const string& path : splitString(searchPath, sep))
232 {
233 if (!path.empty())
234 {
235 append(FilePath(path));
236 }
237 }
238 }
239
241 string asString(const string& sep = PATH_LIST_SEPARATOR) const
242 {
243 string str;
244 for (size_t i = 0; i < _paths.size(); i++)
245 {
246 str += _paths[i];
247 if (i + 1 < _paths.size())
248 {
249 str += sep;
250 }
251 }
252 return str;
253 }
254
256 void append(const FilePath& path)
257 {
258 _paths.push_back(path);
259 }
260
262 void append(const FileSearchPath& searchPath)
263 {
264 for (const FilePath& path : searchPath)
265 {
266 _paths.push_back(path);
267 }
268 }
269
271 void prepend(const FilePath& path)
272 {
273 _paths.insert(_paths.begin(), path);
274 }
275
277 void clear()
278 {
279 _paths.clear();
280 }
281
283 size_t size() const
284 {
285 return _paths.size();
286 }
287
289 bool isEmpty() const
290 {
291 return _paths.empty();
292 }
293
295 FilePath& operator[](size_t index)
296 {
297 return _paths[index];
298 }
299
301 const FilePath& operator[](size_t index) const
302 {
303 return _paths[index];
304 }
305
310 FilePath find(const FilePath& filename) const
311 {
312 if (_paths.empty() || filename.isEmpty())
313 {
314 return filename;
315 }
316 if (!filename.isAbsolute())
317 {
318 for (const FilePath& path : _paths)
319 {
320 FilePath combined = path / filename;
321 if (combined.exists())
322 {
323 return combined;
324 }
325 }
326 }
327 return filename;
328 }
329
332
333 Iterator begin() { return _paths.begin(); }
334 ConstIterator begin() const { return _paths.begin(); }
335
336 Iterator end() { return _paths.end(); }
337 ConstIterator end() const { return _paths.end(); }
338
340
341 private:
342 FilePathVec _paths;
343};
344
346MX_FORMAT_API FileSearchPath getEnvironmentPath(const string& sep = PATH_LIST_SEPARATOR);
347
348} // namespace MaterialX
349
350#endif
vector< string > StringVec
A vector of strings.
Definition: Library.h:56
Utility methods.
Macros for declaring imported and exported symbols.
A generic file path, supporting both syntactic and file system operations.
Definition: File.h:28
bool isAbsolute() const
Return true if the given path is absolute.
Definition: File.h:99
size_t size() const
Return the number of strings in the path.
Definition: File.h:160
void removeExtension()
Remove the file extension, if any, from the given path.
Definition: File.h:142
void addExtension(const string &ext)
Add a file extension to the given path.
Definition: File.h:136
FilePath(const char *str)
Construct a path from a C-style string.
Definition: File.h:75
FilePath(const string &str)
Construct a path from a standard string.
Definition: File.h:69
const string & getBaseName() const
Return the base name of the given path, with leading directory information removed.
Definition: File.h:106
const string & operator[](size_t index) const
Return the const string at the given index.
Definition: File.h:172
bool exists() const
Return true if the given path exists on the file system.
Definition: File.cpp:130
FilePath getParentPath() const
Return the parent directory of the given path, if any.
Definition: File.h:117
string operator[](size_t index)
Return the string at the given index.
Definition: File.h:166
bool isEmpty() const
Return true if the given path is empty.
Definition: File.h:93
string getExtension() const
Return the file extension of the given path.
Definition: File.h:128
A sequence of file paths, which may be queried to find the first instance of a given filename on the ...
Definition: File.h:213
FilePath & operator[](size_t index)
Return the path at the given index.
Definition: File.h:295
size_t size() const
Return the number of paths in the sequence.
Definition: File.h:283
const FilePath & operator[](size_t index) const
Return the const path at the given index.
Definition: File.h:301
void append(const FileSearchPath &searchPath)
Append the given search path to the sequence.
Definition: File.h:262
void prepend(const FilePath &path)
Prepend the given path to the sequence.
Definition: File.h:271
FileSearchPath(const string &searchPath, const string &sep=PATH_LIST_SEPARATOR)
Construct a search path from a string.
Definition: File.h:229
void clear()
Clear all paths from the sequence.
Definition: File.h:277
bool isEmpty() const
Return true if the search path is empty.
Definition: File.h:289
FilePath find(const FilePath &filename) const
Given an input filename, iterate through each path in this sequence, returning the first combined pat...
Definition: File.h:310
string asString(const string &sep=PATH_LIST_SEPARATOR) const
Convert this sequence to a string using the given separator.
Definition: File.h:241
void append(const FilePath &path)
Append the given path to the sequence.
Definition: File.h:256