MaterialX 1.39.0
Loading...
Searching...
No Matches
File.h
Go to the documentation of this file.
1//
2// Copyright Contributors to the MaterialX Project
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#ifndef MATERIALX_FILE_H
7#define MATERIALX_FILE_H
8
11
13
14#include <MaterialXCore/Util.h>
15
16MATERIALX_NAMESPACE_BEGIN
17
18class FilePath;
19using FilePathVec = vector<FilePath>;
20
21extern MX_FORMAT_API const string PATH_LIST_SEPARATOR;
22extern MX_FORMAT_API const string MATERIALX_SEARCH_PATH_ENV_VAR;
23
26class MX_FORMAT_API FilePath
27{
28 public:
29 enum Type
30 {
31 TypeRelative = 0,
32 TypeAbsolute = 1,
33 TypeNetwork = 2
34 };
35
36 enum Format
37 {
38 FormatWindows = 0,
39 FormatPosix = 1,
40#if defined(_WIN32)
41 FormatNative = FormatWindows
42#else
43 FormatNative = FormatPosix
44#endif
45 };
46
47 public:
48 FilePath() :
49 _type(TypeRelative)
50 {
51 }
52 ~FilePath() { }
53
54 bool operator==(const FilePath& rhs) const
55 {
56 return _vec == rhs._vec &&
57 _type == rhs._type;
58 }
59 bool operator!=(const FilePath& rhs) const
60 {
61 return !(*this == rhs);
62 }
63
66
68 FilePath(const string& str)
69 {
70 assign(str);
71 }
72
74 FilePath(const char* str)
75 {
76 assign(str ? string(str) : EMPTY_STRING);
77 }
78
80 operator string() const
81 {
82 return asString();
83 }
84
86 void assign(const string& str);
87
89 string asString(Format format = FormatNative) const;
90
92 bool isEmpty() const
93 {
94 return _vec.empty();
95 }
96
98 bool isAbsolute() const
99 {
100 return _type != TypeRelative;
101 }
102
105 const string& getBaseName() const
106 {
107 if (isEmpty())
108 {
109 return EMPTY_STRING;
110 }
111 return _vec[_vec.size() - 1];
112 }
113
117 {
118 FilePath parent(*this);
119 if (!parent.isEmpty())
120 {
121 parent._vec.pop_back();
122 }
123 return parent;
124 }
125
127 string getExtension() const
128 {
129 const string& baseName = getBaseName();
130 size_t i = baseName.rfind('.');
131 return i != string::npos ? baseName.substr(i + 1) : EMPTY_STRING;
132 }
133
135 void addExtension(const string& ext)
136 {
137 assign(asString() + "." + ext);
138 }
139
142 {
143 if (!isEmpty())
144 {
145 string& baseName = _vec[_vec.size() - 1];
146 size_t i = baseName.rfind('.');
147 if (i != string::npos)
148 {
149 baseName = baseName.substr(0, i);
150 }
151 }
152 }
153
156 FilePath operator/(const FilePath& rhs) const;
157
159 size_t size() const
160 {
161 return _vec.size();
162 }
163
165 string operator[](size_t index)
166 {
167 return _vec[index];
168 }
169
171 const string& operator[](size_t index) const
172 {
173 return _vec[index];
174 }
175
179
183
185 bool exists() const;
186
188 bool isDirectory() const;
189
191 FilePathVec getFilesInDirectory(const string& extension) const;
192
194 FilePathVec getSubDirectories() const;
195
197 void createDirectory() const;
198
201
203
206
209
210 private:
211 StringVec _vec;
212 Type _type;
213};
214
218class MX_FORMAT_API FileSearchPath
219{
220 public:
221 using Iterator = FilePathVec::iterator;
222 using ConstIterator = FilePathVec::const_iterator;
223
224 public:
225 FileSearchPath() = default;
226
232 FileSearchPath(const string& searchPath, const string& sep = PATH_LIST_SEPARATOR)
233 {
234 for (const string& path : splitString(searchPath, sep))
235 {
236 if (!path.empty())
237 {
238 append(FilePath(path));
239 }
240 }
241 }
242
244 string asString(const string& sep = PATH_LIST_SEPARATOR) const
245 {
246 string str;
247 for (size_t i = 0; i < _paths.size(); i++)
248 {
249 str += _paths[i];
250 if (i + 1 < _paths.size())
251 {
252 str += sep;
253 }
254 }
255 return str;
256 }
257
259 void append(const FilePath& path)
260 {
261 _paths.push_back(path);
262 }
263
265 void append(const FileSearchPath& searchPath)
266 {
267 for (const FilePath& path : searchPath)
268 {
269 _paths.push_back(path);
270 }
271 }
272
274 void prepend(const FilePath& path)
275 {
276 _paths.insert(_paths.begin(), path);
277 }
278
280 void clear()
281 {
282 _paths.clear();
283 }
284
286 size_t size() const
287 {
288 return _paths.size();
289 }
290
292 bool isEmpty() const
293 {
294 return _paths.empty();
295 }
296
298 FilePath& operator[](size_t index)
299 {
300 return _paths[index];
301 }
302
304 const FilePath& operator[](size_t index) const
305 {
306 return _paths[index];
307 }
308
313 FilePath find(const FilePath& filename) const
314 {
315 if (_paths.empty() || filename.isEmpty())
316 {
317 return filename;
318 }
319 if (!filename.isAbsolute())
320 {
321 for (const FilePath& path : _paths)
322 {
323 FilePath combined = path / filename;
324 if (combined.exists())
325 {
326 return combined;
327 }
328 }
329 }
330 return filename;
331 }
332
335
336 Iterator begin() { return _paths.begin(); }
337 ConstIterator begin() const { return _paths.begin(); }
338
339 Iterator end() { return _paths.end(); }
340 ConstIterator end() const { return _paths.end(); }
341
343
344 private:
345 FilePathVec _paths;
346};
347
349MX_FORMAT_API FileSearchPath getEnvironmentPath(const string& sep = PATH_LIST_SEPARATOR);
350
351MATERIALX_NAMESPACE_END
352
353#endif
MX_FORMAT_API FileSearchPath getEnvironmentPath(const string &sep=PATH_LIST_SEPARATOR)
Return a FileSearchPath object from search path environment variable.
vector< string > StringVec
A vector of strings.
Definition: Library.h:57
Utility methods.
MX_CORE_API StringVec splitString(const string &str, const string &sep)
Split a string into a vector of substrings using the given set of separator characters.
Macros for declaring imported and exported symbols.
A generic file path, supporting both syntactic and file system operations.
Definition: File.h:27
bool isAbsolute() const
Return true if the given path is absolute.
Definition: File.h:98
size_t size() const
Return the number of strings in the path.
Definition: File.h:159
void removeExtension()
Remove the file extension, if any, from the given path.
Definition: File.h:141
void addExtension(const string &ext)
Add a file extension to the given path.
Definition: File.h:135
void createDirectory() const
Create a directory on the file system at the given path.
FilePathVec getSubDirectories() const
Return a vector of all directories at or beneath the given path.
FilePath(const char *str)
Construct a path from a C-style string.
Definition: File.h:74
static FilePath getCurrentPath()
Return the current working directory of the file system.
FilePath(const string &str)
Construct a path from a standard string.
Definition: File.h:68
const string & getBaseName() const
Return the base name of the given path, with leading directory information removed.
Definition: File.h:105
const string & operator[](size_t index) const
Return the const string at the given index.
Definition: File.h:171
FilePath operator/(const FilePath &rhs) const
Concatenate two paths with a directory separator, returning the combined path.
string asString(Format format=FormatNative) const
Return this path as a standard string with the given format.
void assign(const string &str)
Assign a path from a standard string.
bool isDirectory() const
Return true if the given path is a directory on the file system.
FilePathVec getFilesInDirectory(const string &extension) const
Return a vector of all files in the given directory with the given extension.
bool exists() const
Return true if the given path exists on the file system.
static FilePath getModulePath()
Return the directory containing the executable module.
FilePath getParentPath() const
Return the parent directory of the given path, if any.
Definition: File.h:116
string operator[](size_t index)
Return the string at the given index.
Definition: File.h:165
bool isEmpty() const
Return true if the given path is empty.
Definition: File.h:92
FilePath getNormalized() const
Return a normalized version of the given path, collapsing current path and parent path references so ...
string getExtension() const
Return the file extension of the given path.
Definition: File.h:127
bool setCurrentPath()
Set the current working directory of the file system.
A sequence of file paths, which may be queried to find the first instance of a given filename on the ...
Definition: File.h:219
FilePath & operator[](size_t index)
Return the path at the given index.
Definition: File.h:298
size_t size() const
Return the number of paths in the sequence.
Definition: File.h:286
const FilePath & operator[](size_t index) const
Return the const path at the given index.
Definition: File.h:304
void append(const FileSearchPath &searchPath)
Append the given search path to the sequence.
Definition: File.h:265
void prepend(const FilePath &path)
Prepend the given path to the sequence.
Definition: File.h:274
FileSearchPath(const string &searchPath, const string &sep=PATH_LIST_SEPARATOR)
Construct a search path from a string.
Definition: File.h:232
void clear()
Clear all paths from the sequence.
Definition: File.h:280
bool isEmpty() const
Return true if the search path is empty.
Definition: File.h:292
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:313
string asString(const string &sep=PATH_LIST_SEPARATOR) const
Convert this sequence to a string using the given separator.
Definition: File.h:244
void append(const FilePath &path)
Append the given path to the sequence.
Definition: File.h:259