HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
9 /// @file
10 /// Cross-platform support for file and search paths
11 
12 #include <MaterialXFormat/Export.h>
13 
14 #include <MaterialXCore/Util.h>
15 
17 
18 class FilePath;
19 using FilePathVec = vector<FilePath>;
20 
21 extern MX_FORMAT_API const string PATH_LIST_SEPARATOR;
23 
24 /// @class FilePath
25 /// A generic file path, supporting both syntactic and file system operations.
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:
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 
64  /// @name Syntactic Operations
65  /// @{
66 
67  /// Construct a path from a standard string.
68  FilePath(const string& str)
69  {
70  assign(str);
71  }
72 
73  /// Construct a path from a C-style string.
74  FilePath(const char* str)
75  {
76  assign(str ? string(str) : EMPTY_STRING);
77  }
78 
79  /// Convert a path to a standard string.
80  operator string() const
81  {
82  return asString();
83  }
84 
85  /// Assign a path from a standard string.
86  void assign(const string& str);
87 
88  /// Return this path as a standard string with the given format.
89  string asString(Format format = FormatNative) const;
90 
91  /// Return true if the given path is empty.
92  bool isEmpty() const
93  {
94  return _vec.empty();
95  }
96 
97  /// Return true if the given path is absolute.
98  bool isAbsolute() const
99  {
100  return _type != TypeRelative;
101  }
102 
103  /// Return the base name of the given path, with leading directory
104  /// information removed.
105  const string& getBaseName() const
106  {
107  if (isEmpty())
108  {
109  return EMPTY_STRING;
110  }
111  return _vec[_vec.size() - 1];
112  }
113 
114  /// Return the parent directory of the given path, if any. If no
115  /// parent directory is present, then the empty path is returned.
117  {
118  FilePath parent(*this);
119  if (!parent.isEmpty())
120  {
121  parent._vec.pop_back();
122  }
123  return parent;
124  }
125 
126  /// Return the file extension of the given path.
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 
134  /// Add a file extension to the given path.
135  void addExtension(const string& ext)
136  {
137  assign(asString() + "." + ext);
138  }
139 
140  /// Remove the file extension, if any, from the given path.
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 
154  /// Concatenate two paths with a directory separator, returning the
155  /// combined path.
156  FilePath operator/(const FilePath& rhs) const;
157 
158  /// Return the number of strings in the path.
159  size_t size() const
160  {
161  return _vec.size();
162  }
163 
164  /// Return the string at the given index.
165  string operator[](size_t index)
166  {
167  return _vec[index];
168  }
169 
170  /// Return the const string at the given index.
171  const string& operator[](size_t index) const
172  {
173  return _vec[index];
174  }
175 
176  /// Return a normalized version of the given path, collapsing current path and
177  /// parent path references so that 'a/./b' and 'c/../d/../a/b' become 'a/b'.
178  FilePath getNormalized() const;
179 
180  /// @}
181  /// @name File System Operations
182  /// @{
183 
184  /// Return true if the given path exists on the file system.
185  bool exists() const;
186 
187  /// Return true if the given path is a directory on the file system.
188  bool isDirectory() const;
189 
190  /// Return a vector of all files in the given directory with the given extension.
191  FilePathVec getFilesInDirectory(const string& extension) const;
192 
193  /// Return a vector of all directories at or beneath the given path.
194  FilePathVec getSubDirectories() const;
195 
196  /// Create a directory on the file system at the given path.
197  void createDirectory() const;
198 
199  /// Set the current working directory of the file system.
200  bool setCurrentPath();
201 
202  /// @}
203 
204  /// Return the current working directory of the file system.
205  static FilePath getCurrentPath();
206 
207  /// Return the directory containing the executable module.
208  static FilePath getModulePath();
209 
210  private:
211  StringVec _vec;
212  Type _type;
213 };
214 
215 /// @class FileSearchPath
216 /// A sequence of file paths, which may be queried to find the first instance
217 /// of a given filename on the file system.
219 {
220  public:
221  using Iterator = FilePathVec::iterator;
222  using ConstIterator = FilePathVec::const_iterator;
223 
224  public:
225  FileSearchPath() = default;
226 
227  /// Construct a search path from a string.
228  /// @param searchPath A string containing a sequence of file paths joined
229  /// by separator characters.
230  /// @param sep The set of separator characters used in the search path.
231  /// Defaults to the PATH_LIST_SEPARATOR character.
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 
243  /// Convert this sequence to a string using the given separator.
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 
258  /// Append the given path to the sequence.
259  void append(const FilePath& path)
260  {
261  _paths.push_back(path);
262  }
263 
264  /// Append the given search path to the sequence.
265  void append(const FileSearchPath& searchPath)
266  {
267  for (const FilePath& path : searchPath)
268  {
269  _paths.push_back(path);
270  }
271  }
272 
273  /// Prepend the given path to the sequence.
274  void prepend(const FilePath& path)
275  {
276  _paths.insert(_paths.begin(), path);
277  }
278 
279  /// Clear all paths from the sequence.
280  void clear()
281  {
282  _paths.clear();
283  }
284 
285  /// Return the number of paths in the sequence.
286  size_t size() const
287  {
288  return _paths.size();
289  }
290 
291  /// Return true if the search path is empty.
292  bool isEmpty() const
293  {
294  return _paths.empty();
295  }
296 
297  /// Return the path at the given index.
299  {
300  return _paths[index];
301  }
302 
303  /// Return the const path at the given index.
304  const FilePath& operator[](size_t index) const
305  {
306  return _paths[index];
307  }
308 
309  /// Given an input filename, iterate through each path in this sequence,
310  /// returning the first combined path found on the file system.
311  /// On success, the combined path is returned; otherwise the original
312  /// filename is returned unmodified.
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 
333  /// @name Iterators
334  /// @{
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 
342  /// @}
343 
344  private:
345  FilePathVec _paths;
346 };
347 
348 /// Return a FileSearchPath object from search path environment variable.
350 
352 
353 #endif
ConstIterator begin() const
Definition: File.h:337
string asString(const string &sep=PATH_LIST_SEPARATOR) const
Convert this sequence to a string using the given separator.
Definition: File.h:244
bool isEmpty() const
Return true if the search path is empty.
Definition: File.h:292
GT_API const UT_StringHolder filename
Definition: File.h:26
~FilePath()
Definition: File.h:52
FilePath & operator[](size_t index)
Return the path at the given index.
Definition: File.h:298
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:25
vector< string > StringVec
A vector of strings.
Definition: Library.h:57
Iterator begin()
Definition: File.h:336
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
#define MX_FORMAT_API
Definition: Export.h:18
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
bool isAbsolute() const
Return true if the given path is absolute.
Definition: File.h:98
MATERIALX_NAMESPACE_BEGIN MX_CORE_API const string EMPTY_STRING
MX_FORMAT_API const string PATH_LIST_SEPARATOR
FilePathVec::iterator Iterator
Definition: File.h:221
void addExtension(const string &ext)
Add a file extension to the given path.
Definition: File.h:135
const string & getBaseName() const
Definition: File.h:105
FilePath(const string &str)
Construct a path from a standard string.
Definition: File.h:68
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
Format
Definition: oidn.hpp:16
size_t size() const
Return the number of paths in the sequence.
Definition: File.h:286
void removeExtension()
Remove the file extension, if any, from the given path.
Definition: File.h:141
FilePath(const char *str)
Construct a path from a C-style string.
Definition: File.h:74
Format
Definition: File.h:36
bool operator!=(const FilePath &rhs) const
Definition: File.h:59
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
MX_FORMAT_API FileSearchPath getEnvironmentPath(const string &sep=PATH_LIST_SEPARATOR)
Return a FileSearchPath object from search path environment variable.
ConstIterator end() const
Definition: File.h:340
FilePath()
Definition: File.h:48
string getExtension() const
Return the file extension of the given path.
Definition: File.h:127
MX_FORMAT_API const string MATERIALX_SEARCH_PATH_ENV_VAR
OIIO_UTIL_API bool exists(string_view path) noexcept
const string & operator[](size_t index) const
Return the const string at the given index.
Definition: File.h:171
size_t size() const
Return the number of strings in the path.
Definition: File.h:159
vector< FilePath > FilePathVec
Definition: File.h:19
Type
Definition: File.h:29
void prepend(const FilePath &path)
Prepend the given path to the sequence.
Definition: File.h:274
void append(const FilePath &path)
Append the given path to the sequence.
Definition: File.h:259
void clear()
Clear all paths from the sequence.
Definition: File.h:280
bool operator==(const FilePath &rhs) const
Definition: File.h:54
GLuint index
Definition: glcorearb.h:786
FileSearchPath(const string &searchPath, const string &sep=PATH_LIST_SEPARATOR)
Definition: File.h:232
Iterator end()
Definition: File.h:339
FilePath find(const FilePath &filename) const
Definition: File.h:313
FilePathVec::const_iterator ConstIterator
Definition: File.h:222
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:26
const FilePath & operator[](size_t index) const
Return the const path at the given index.
Definition: File.h:304
FilePath getParentPath() const
Definition: File.h:116
IMATH_HOSTDEVICE constexpr Quat< T > operator/(const Quat< T > &q1, const Quat< T > &q2) IMATH_NOEXCEPT
Quaterion division.
Definition: ImathQuat.h:871
OIIO_UTIL_API std::string extension(string_view filepath, bool include_dot=true) noexcept
void append(const FileSearchPath &searchPath)
Append the given search path to the sequence.
Definition: File.h:265
MX_CORE_API StringVec splitString(const string &str, const string &sep)
bool exists() const
Return true if the given path exists on the file system.