MaterialX 1.39.5
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() = default;
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
116 FilePath getParentPath() const
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.resize(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
178 FilePath getNormalized() const;
179
183
185 bool exists() const;
186
188 bool isDirectory() const;
189
192 FilePathVec getFilesInDirectory(const string& extension = EMPTY_STRING) const;
193
195 FilePathVec getSubDirectories() const;
196
199 void createDirectory(bool recursive = false) const;
200
203
205
207 static FilePath getCurrentPath();
208
210 static FilePath getModulePath();
211
212 private:
213 StringVec _vec;
214 Type _type;
215};
216
220class MX_FORMAT_API FileSearchPath
221{
222 public:
223 using Iterator = FilePathVec::iterator;
224 using ConstIterator = FilePathVec::const_iterator;
225
226 public:
227 FileSearchPath() = default;
228
234 FileSearchPath(const string& searchPath, const string& sep = PATH_LIST_SEPARATOR)
235 {
236 for (const string& path : splitString(searchPath, sep))
237 {
238 if (!path.empty())
239 {
240 append(FilePath(path));
241 }
242 }
243 }
244
246 string asString(const string& sep = PATH_LIST_SEPARATOR) const
247 {
248 string str;
249 for (size_t i = 0; i < _paths.size(); i++)
250 {
251 str += _paths[i];
252 if (i + 1 < _paths.size())
253 {
254 str += sep;
255 }
256 }
257 return str;
258 }
259
261 void append(const FilePath& path)
262 {
263 _paths.push_back(path);
264 }
265
267 void append(const FileSearchPath& searchPath)
268 {
269 for (const FilePath& path : searchPath)
270 {
271 _paths.push_back(path);
272 }
273 }
274
276 void prepend(const FilePath& path)
277 {
278 _paths.insert(_paths.begin(), path);
279 }
280
282 void clear()
283 {
284 _paths.clear();
285 }
286
288 size_t size() const
289 {
290 return _paths.size();
291 }
292
294 bool isEmpty() const
295 {
296 return _paths.empty();
297 }
298
300 FilePath& operator[](size_t index)
301 {
302 return _paths[index];
303 }
304
306 const FilePath& operator[](size_t index) const
307 {
308 return _paths[index];
309 }
310
315 FilePath find(const FilePath& filename) const
316 {
317 if (_paths.empty() || filename.isEmpty())
318 {
319 return filename;
320 }
321 if (!filename.isAbsolute())
322 {
323 for (const FilePath& path : _paths)
324 {
325 FilePath combined = path / filename;
326 if (combined.exists())
327 {
328 return combined;
329 }
330 }
331 }
332 return filename;
333 }
334
337
338 Iterator begin() { return _paths.begin(); }
339 ConstIterator begin() const { return _paths.begin(); }
340
341 Iterator end() { return _paths.end(); }
342 ConstIterator end() const { return _paths.end(); }
343
345
346 private:
347 FilePathVec _paths;
348};
349
351MX_FORMAT_API FileSearchPath getEnvironmentPath(const string& sep = PATH_LIST_SEPARATOR);
352
353MATERIALX_NAMESPACE_END
354
355#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:61
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 createDirectory(bool recursive=false) const
Create a directory on the file system at the given path.
void addExtension(const string &ext)
Add a file extension to the given path.
Definition File.h:135
FilePathVec getFilesInDirectory(const string &extension=EMPTY_STRING) const
Return a vector of all files in the given directory with the given extension.
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.
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:221
FilePath & operator[](size_t index)
Return the path at the given index.
Definition File.h:300
size_t size() const
Return the number of paths in the sequence.
Definition File.h:288
const FilePath & operator[](size_t index) const
Return the const path at the given index.
Definition File.h:306
void append(const FileSearchPath &searchPath)
Append the given search path to the sequence.
Definition File.h:267
void prepend(const FilePath &path)
Prepend the given path to the sequence.
Definition File.h:276
FileSearchPath(const string &searchPath, const string &sep=PATH_LIST_SEPARATOR)
Construct a search path from a string.
Definition File.h:234
void clear()
Clear all paths from the sequence.
Definition File.h:282
bool isEmpty() const
Return true if the search path is empty.
Definition File.h:294
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:315
string asString(const string &sep=PATH_LIST_SEPARATOR) const
Convert this sequence to a string using the given separator.
Definition File.h:246
void append(const FilePath &path)
Append the given path to the sequence.
Definition File.h:261