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 // TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
3 // All rights reserved. See LICENSE.txt for license.
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  /// @}
177  /// @name File System Operations
178  /// @{
179 
180  /// Return true if the given path exists on the file system.
181  bool exists() const;
182 
183  /// Return true if the given path is a directory on the file system.
184  bool isDirectory() const;
185 
186  /// Return a vector of all files in the given directory with the given extension.
187  FilePathVec getFilesInDirectory(const string& extension) const;
188 
189  /// Return a vector of all directories at or beneath the given path.
190  FilePathVec getSubDirectories() const;
191 
192  /// Create a directory on the file system at the given path.
193  void createDirectory() const;
194 
195  /// Set the current working directory of the file system.
196  bool setCurrentPath();
197 
198  /// @}
199 
200  /// Return the current working directory of the file system.
201  static FilePath getCurrentPath();
202 
203  /// Return the directory containing the executable module.
204  static FilePath getModulePath();
205 
206  private:
207  StringVec _vec;
208  Type _type;
209 };
210 
211 /// @class FileSearchPath
212 /// A sequence of file paths, which may be queried to find the first instance
213 /// of a given filename on the file system.
215 {
216  public:
217  using Iterator = FilePathVec::iterator;
218  using ConstIterator = FilePathVec::const_iterator;
219 
220  public:
222  {
223  }
225 
226  /// Construct a search path from a string.
227  /// @param searchPath A string containing a sequence of file paths joined
228  /// by separator characters.
229  /// @param sep The set of separator characters used in the search path.
230  /// Defaults to the PATH_LIST_SEPARATOR character.
231  FileSearchPath(const string& searchPath, const string& sep = PATH_LIST_SEPARATOR)
232  {
233  for (const string& path : splitString(searchPath, sep))
234  {
235  if (!path.empty())
236  {
237  append(FilePath(path));
238  }
239  }
240  }
241 
242  /// Convert this sequence to a string using the given separator.
243  string asString(const string& sep = PATH_LIST_SEPARATOR) const
244  {
245  string str;
246  for (size_t i = 0; i < _paths.size(); i++)
247  {
248  str += _paths[i];
249  if (i + 1 < _paths.size())
250  {
251  str += sep;
252  }
253  }
254  return str;
255  }
256 
257  /// Append the given path to the sequence.
258  void append(const FilePath& path)
259  {
260  _paths.push_back(path);
261  }
262 
263  /// Append the given search path to the sequence.
264  void append(const FileSearchPath& searchPath)
265  {
266  for (const FilePath& path : searchPath)
267  {
268  _paths.push_back(path);
269  }
270  }
271 
272  /// Prepend the given path to the sequence.
273  void prepend(const FilePath& path)
274  {
275  _paths.insert(_paths.begin(), path);
276  }
277 
278  /// Clear all paths from the sequence.
279  void clear()
280  {
281  _paths.clear();
282  }
283 
284  /// Return the number of paths in the sequence.
285  size_t size() const
286  {
287  return _paths.size();
288  }
289 
290  /// Return true if the search path is empty.
291  bool isEmpty() const
292  {
293  return _paths.empty();
294  }
295 
296  /// Return the path at the given index.
298  {
299  return _paths[index];
300  }
301 
302  /// Return the const path at the given index.
303  const FilePath& operator[](size_t index) const
304  {
305  return _paths[index];
306  }
307 
308  /// Given an input filename, iterate through each path in this sequence,
309  /// returning the first combined path found on the file system.
310  /// On success, the combined path is returned; otherwise the original
311  /// filename is returned unmodified.
313  {
314  if (_paths.empty() || filename.isEmpty())
315  {
316  return filename;
317  }
318  if (!filename.isAbsolute())
319  {
320  for (const FilePath& path : _paths)
321  {
322  FilePath combined = path / filename;
323  if (combined.exists())
324  {
325  return combined;
326  }
327  }
328  }
329  return filename;
330  }
331 
332  /// @name Iterators
333  /// @{
334 
335  Iterator begin() { return _paths.begin(); }
336  ConstIterator begin() const { return _paths.begin(); }
337 
338  Iterator end() { return _paths.end(); }
339  ConstIterator end() const { return _paths.end(); }
340 
341  /// @}
342 
343  private:
344  FilePathVec _paths;
345 };
346 
347 /// Return a FileSearchPath object from search path environment variable.
349 
351 
352 #endif
ConstIterator begin() const
Definition: File.h:336
string asString(const string &sep=PATH_LIST_SEPARATOR) const
Convert this sequence to a string using the given separator.
Definition: File.h:243
bool isEmpty() const
Return true if the search path is empty.
Definition: File.h:291
FileSearchPath()
Definition: File.h:221
GT_API const UT_StringHolder filename
~FileSearchPath()
Definition: File.h:224
Definition: File.h:26
~FilePath()
Definition: File.h:52
FilePath & operator[](size_t index)
Return the path at the given index.
Definition: File.h:297
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:23
vector< string > StringVec
A vector of strings.
Definition: Library.h:57
Iterator begin()
Definition: File.h:335
#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:217
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:285
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
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
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:339
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
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
Type
Definition: File.h:29
void prepend(const FilePath &path)
Prepend the given path to the sequence.
Definition: File.h:273
void append(const FilePath &path)
Append the given path to the sequence.
Definition: File.h:258
void clear()
Clear all paths from the sequence.
Definition: File.h:279
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:231
Iterator end()
Definition: File.h:338
FilePath find(const FilePath &filename) const
Definition: File.h:312
FilePathVec::const_iterator ConstIterator
Definition: File.h:218
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:24
const FilePath & operator[](size_t index) const
Return the const path at the given index.
Definition: File.h:303
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:264
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.