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() = 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 
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.resize(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  /// If extension is empty all files are returned.
192  FilePathVec getFilesInDirectory(const string& extension = EMPTY_STRING) const;
193 
194  /// Return a vector of all directories at or beneath the given path.
195  FilePathVec getSubDirectories() const;
196 
197  /// Create a directory on the file system at the given path.
198  /// If recursive is true, any missing parent directories will be created as well.
199  void createDirectory(bool recursive = false) const;
200 
201  /// Set the current working directory of the file system.
202  bool setCurrentPath();
203 
204  /// @}
205 
206  /// Return the current working directory of the file system.
207  static FilePath getCurrentPath();
208 
209  /// Return the directory containing the executable module.
210  static FilePath getModulePath();
211 
212  private:
213  StringVec _vec;
214  Type _type;
215 };
216 
217 /// @class FileSearchPath
218 /// A sequence of file paths, which may be queried to find the first instance
219 /// of a given filename on the file system.
221 {
222  public:
223  using Iterator = FilePathVec::iterator;
224  using ConstIterator = FilePathVec::const_iterator;
225 
226  public:
227  FileSearchPath() = default;
228 
229  /// Construct a search path from a string.
230  /// @param searchPath A string containing a sequence of file paths joined
231  /// by separator characters.
232  /// @param sep The set of separator characters used in the search path.
233  /// Defaults to the PATH_LIST_SEPARATOR character.
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 
245  /// Convert this sequence to a string using the given separator.
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 
260  /// Append the given path to the sequence.
261  void append(const FilePath& path)
262  {
263  _paths.push_back(path);
264  }
265 
266  /// Append the given search path to the sequence.
267  void append(const FileSearchPath& searchPath)
268  {
269  for (const FilePath& path : searchPath)
270  {
271  _paths.push_back(path);
272  }
273  }
274 
275  /// Prepend the given path to the sequence.
276  void prepend(const FilePath& path)
277  {
278  _paths.insert(_paths.begin(), path);
279  }
280 
281  /// Clear all paths from the sequence.
282  void clear()
283  {
284  _paths.clear();
285  }
286 
287  /// Return the number of paths in the sequence.
288  size_t size() const
289  {
290  return _paths.size();
291  }
292 
293  /// Return true if the search path is empty.
294  bool isEmpty() const
295  {
296  return _paths.empty();
297  }
298 
299  /// Return the path at the given index.
301  {
302  return _paths[index];
303  }
304 
305  /// Return the const path at the given index.
306  const FilePath& operator[](size_t index) const
307  {
308  return _paths[index];
309  }
310 
311  /// Given an input filename, iterate through each path in this sequence,
312  /// returning the first combined path found on the file system.
313  /// On success, the combined path is returned; otherwise the original
314  /// filename is returned unmodified.
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 
335  /// @name Iterators
336  /// @{
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 
344  /// @}
345 
346  private:
347  FilePathVec _paths;
348 };
349 
350 /// Return a FileSearchPath object from search path environment variable.
352 
354 
355 #endif
ConstIterator begin() const
Definition: File.h:339
string asString(const string &sep=PATH_LIST_SEPARATOR) const
Convert this sequence to a string using the given separator.
Definition: File.h:246
bool isEmpty() const
Return true if the search path is empty.
Definition: File.h:294
GT_API const UT_StringHolder filename
Definition: File.h:26
FilePath & operator[](size_t index)
Return the path at the given index.
Definition: File.h:300
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:25
vector< string > StringVec
A vector of strings.
Definition: Library.h:61
Iterator begin()
Definition: File.h:338
#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:223
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:118
size_t size() const
Return the number of paths in the sequence.
Definition: File.h:288
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:342
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:276
void append(const FilePath &path)
Append the given path to the sequence.
Definition: File.h:261
void clear()
Clear all paths from the sequence.
Definition: File.h:282
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:234
Iterator end()
Definition: File.h:341
FilePath find(const FilePath &filename) const
Definition: File.h:315
FilePathVec::const_iterator ConstIterator
Definition: File.h:224
#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:306
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:934
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:267
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.