forked from AcademySoftwareFoundation/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
File.h
330 lines (275 loc) · 7.92 KB
/
File.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
//
// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//
#ifndef MATERIALX_FILE_H
#define MATERIALX_FILE_H
/// @file
/// Cross-platform support for file and search paths
#include <MaterialXCore/Util.h>
namespace MaterialX
{
class FilePath;
using FilePathVec = vector<FilePath>;
extern const string PATH_LIST_SEPARATOR;
extern const string MATERIALX_SEARCH_PATH_ENV_VAR;
/// @class FilePath
/// A generic file path, supporting both syntactic and file system operations.
class FilePath
{
public:
enum Type
{
TypeRelative = 0,
TypeAbsolute = 1,
TypeNetwork = 2
};
enum Format
{
FormatWindows = 0,
FormatPosix = 1,
#if defined(_WIN32)
FormatNative = FormatWindows
#else
FormatNative = FormatPosix
#endif
};
public:
FilePath() :
_type(TypeRelative)
{
}
~FilePath() { }
bool operator==(const FilePath& rhs) const
{
return _vec == rhs._vec &&
_type == rhs._type;
}
bool operator!=(const FilePath& rhs) const
{
return !(*this == rhs);
}
/// @name Syntactic Operations
/// @{
/// Construct a path from a standard string.
FilePath(const string& str)
{
assign(str);
}
/// Construct a path from a C-style string.
FilePath(const char* str)
{
assign(str ? string(str) : EMPTY_STRING);
}
/// Convert a path to a standard string.
operator string() const
{
return asString();
}
/// Assign a path from a standard string.
void assign(const string& str);
/// Return this path as a standard string with the given format.
string asString(Format format = FormatNative) const;
/// Return true if the given path is empty.
bool isEmpty() const
{
return _vec.empty();
}
/// Return true if the given path is absolute.
bool isAbsolute() const
{
return _type != TypeRelative;
}
/// Return the base name of the given path, with leading directory
/// information removed.
const string& getBaseName() const
{
if (isEmpty())
{
return EMPTY_STRING;
}
return _vec[_vec.size() - 1];
}
/// Return the parent directory of the given path, if any. If no
/// parent directory is present, then the empty path is returned.
FilePath getParentPath() const
{
FilePath parent(*this);
if (!parent.isEmpty())
{
parent._vec.pop_back();
}
return parent;
}
/// Return the file extension of the given path.
string getExtension() const
{
const string& baseName = getBaseName();
size_t i = baseName.rfind('.');
return i != string::npos ? baseName.substr(i + 1) : EMPTY_STRING;
}
/// Add a file extension to the given path.
void addExtension(const string& ext)
{
assign(asString() + "." + ext);
}
/// Remove the file extension, if any, from the given path.
void removeExtension()
{
if (!isEmpty())
{
string& baseName = _vec[_vec.size() - 1];
size_t i = baseName.rfind('.');
if (i != string::npos)
{
baseName = baseName.substr(0, i);
}
}
}
/// Concatenate two paths with a directory separator, returning the
/// combined path.
FilePath operator/(const FilePath& rhs) const;
/// @}
/// @name File System Operations
/// @{
/// Return true if the given path exists on the file system.
bool exists() const;
/// Return true if the given path is a directory on the file system.
bool isDirectory() const;
/// Return a vector of all files in the given directory with the given extension.
FilePathVec getFilesInDirectory(const string& extension) const;
/// Return a vector of all directories at or beneath the given path.
FilePathVec getSubDirectories() const;
/// Create a directory on the file system at the given path.
void createDirectory() const;
/// @}
/// Return the current working directory of the file system.
static FilePath getCurrentPath();
/// Return the directory containing the executable module.
static FilePath getModulePath();
private:
StringVec _vec;
Type _type;
};
/// @class FileSearchPath
/// A sequence of file paths, which may be queried to find the first instance
/// of a given filename on the file system.
class FileSearchPath
{
public:
using Iterator = FilePathVec::iterator;
using ConstIterator = FilePathVec::const_iterator;
public:
FileSearchPath()
{
}
~FileSearchPath() { }
/// Construct a search path from a string.
/// @param searchPath A string containing a sequence of file paths joined
/// by separator characters.
/// @param sep The set of separator characters used in the search path.
/// Defaults to the PATH_LIST_SEPARATOR character.
FileSearchPath(const string& searchPath, const string& sep = PATH_LIST_SEPARATOR)
{
for (const string& path : splitString(searchPath, sep))
{
if (!path.empty())
{
append(FilePath(path));
}
}
}
/// Convert this sequence to a string using the given separator.
string asString(const string& sep = PATH_LIST_SEPARATOR) const
{
string str;
for (size_t i = 0; i < _paths.size(); i++)
{
str += _paths[i];
if (i + 1 < _paths.size())
{
str += sep;
}
}
return str;
}
/// Append the given path to the sequence.
void append(const FilePath& path)
{
_paths.push_back(path);
}
/// Append the given search path to the sequence.
void append(const FileSearchPath& searchPath)
{
for (const FilePath& path : searchPath)
{
_paths.push_back(path);
}
}
/// Prepend the given path to the sequence.
void prepend(const FilePath& path)
{
_paths.insert(_paths.begin(), path);
}
/// Clear all paths from the sequence.
void clear()
{
_paths.clear();
}
/// Return the number of paths in the sequence.
size_t size() const
{
return _paths.size();
}
/// Return true if the search path is empty.
bool isEmpty() const
{
return _paths.empty();
}
/// Return the path at the given index.
FilePath& operator[](size_t index)
{
return _paths[index];
}
/// Return the const path at the given index.
const FilePath& operator[](size_t index) const
{
return _paths[index];
}
/// Given an input filename, iterate through each path in this sequence,
/// returning the first combined path found on the file system.
/// On success, the combined path is returned; otherwise the original
/// filename is returned unmodified.
FilePath find(const FilePath& filename) const
{
if (_paths.empty() || filename.isEmpty())
{
return filename;
}
if (!filename.isAbsolute())
{
for (const FilePath& path : _paths)
{
FilePath combined = path / filename;
if (combined.exists())
{
return combined;
}
}
}
return filename;
}
/// @name Iterators
/// @{
Iterator begin() { return _paths.begin(); }
ConstIterator begin() const { return _paths.begin(); }
Iterator end() { return _paths.end(); }
ConstIterator end() const { return _paths.end(); }
/// @}
private:
FilePathVec _paths;
};
/// Return a FileSearchPath object from search path environment variable.
FileSearchPath getEnvironmentPath(const string& sep = PATH_LIST_SEPARATOR);
} // namespace MaterialX
#endif