6#ifndef MATERIALX_FILE_H
7#define MATERIALX_FILE_H
20using FilePathVec = vector<FilePath>;
22extern MX_FORMAT_API
const string PATH_LIST_SEPARATOR;
23extern MX_FORMAT_API
const string MATERIALX_SEARCH_PATH_ENV_VAR;
42 FormatNative = FormatWindows
44 FormatNative = FormatPosix
55 bool operator==(
const FilePath& rhs)
const
57 return _vec == rhs._vec &&
60 bool operator!=(
const FilePath& rhs)
const
62 return !(*
this == rhs);
77 assign(str ?
string(str) : EMPTY_STRING);
81 operator string()
const
87 void assign(
const string& str);
90 string asString(Format format = FormatNative)
const;
101 return _type != TypeRelative;
112 return _vec[_vec.size() - 1];
122 parent._vec.pop_back();
130 const string& baseName = getBaseName();
131 size_t i = baseName.rfind(
'.');
132 return i != string::npos ? baseName.substr(i + 1) : EMPTY_STRING;
138 assign(asString() +
"." + ext);
146 string& baseName = _vec[_vec.size() - 1];
147 size_t i = baseName.rfind(
'.');
148 if (i != string::npos)
150 baseName = baseName.substr(0, i);
185 bool isDirectory()
const;
188 FilePathVec getFilesInDirectory(
const string& extension)
const;
191 FilePathVec getSubDirectories()
const;
194 void createDirectory()
const;
215 using Iterator = FilePathVec::iterator;
216 using ConstIterator = FilePathVec::const_iterator;
229 FileSearchPath(
const string& searchPath,
const string& sep = PATH_LIST_SEPARATOR)
231 for (
const string& path : splitString(searchPath, sep))
241 string asString(
const string& sep = PATH_LIST_SEPARATOR)
const
244 for (
size_t i = 0; i < _paths.size(); i++)
247 if (i + 1 < _paths.size())
258 _paths.push_back(path);
264 for (
const FilePath& path : searchPath)
266 _paths.push_back(path);
273 _paths.insert(_paths.begin(), path);
285 return _paths.size();
291 return _paths.empty();
297 return _paths[index];
303 return _paths[index];
312 if (_paths.empty() || filename.
isEmpty())
320 FilePath combined = path / filename;
333 Iterator begin() {
return _paths.begin(); }
334 ConstIterator begin()
const {
return _paths.begin(); }
336 Iterator end() {
return _paths.end(); }
337 ConstIterator end()
const {
return _paths.end(); }
346MX_FORMAT_API FileSearchPath getEnvironmentPath(
const string& sep = PATH_LIST_SEPARATOR);
vector< string > StringVec
A vector of strings.
Definition: Library.h:56
A generic file path, supporting both syntactic and file system operations.
Definition: File.h:28
bool isAbsolute() const
Return true if the given path is absolute.
Definition: File.h:99
size_t size() const
Return the number of strings in the path.
Definition: File.h:160
void removeExtension()
Remove the file extension, if any, from the given path.
Definition: File.h:142
void addExtension(const string &ext)
Add a file extension to the given path.
Definition: File.h:136
FilePath(const char *str)
Construct a path from a C-style string.
Definition: File.h:75
FilePath(const string &str)
Construct a path from a standard string.
Definition: File.h:69
const string & getBaseName() const
Return the base name of the given path, with leading directory information removed.
Definition: File.h:106
const string & operator[](size_t index) const
Return the const string at the given index.
Definition: File.h:172
bool exists() const
Return true if the given path exists on the file system.
Definition: File.cpp:130
FilePath getParentPath() const
Return the parent directory of the given path, if any.
Definition: File.h:117
string operator[](size_t index)
Return the string at the given index.
Definition: File.h:166
bool isEmpty() const
Return true if the given path is empty.
Definition: File.h:93
string getExtension() const
Return the file extension of the given path.
Definition: File.h:128
A sequence of file paths, which may be queried to find the first instance of a given filename on the ...
Definition: File.h:213
FilePath & operator[](size_t index)
Return the path at the given index.
Definition: File.h:295
size_t size() const
Return the number of paths in the sequence.
Definition: File.h:283
const FilePath & operator[](size_t index) const
Return the const path at the given index.
Definition: File.h:301
void append(const FileSearchPath &searchPath)
Append the given search path to the sequence.
Definition: File.h:262
void prepend(const FilePath &path)
Prepend the given path to the sequence.
Definition: File.h:271
FileSearchPath(const string &searchPath, const string &sep=PATH_LIST_SEPARATOR)
Construct a search path from a string.
Definition: File.h:229
void clear()
Clear all paths from the sequence.
Definition: File.h:277
bool isEmpty() const
Return true if the search path is empty.
Definition: File.h:289
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:310
string asString(const string &sep=PATH_LIST_SEPARATOR) const
Convert this sequence to a string using the given separator.
Definition: File.h:241
void append(const FilePath &path)
Append the given path to the sequence.
Definition: File.h:256