6#ifndef MATERIALX_FILE_H
7#define MATERIALX_FILE_H
16MATERIALX_NAMESPACE_BEGIN
19using FilePathVec = vector<FilePath>;
21extern MX_FORMAT_API
const string PATH_LIST_SEPARATOR;
22extern MX_FORMAT_API
const string MATERIALX_SEARCH_PATH_ENV_VAR;
41 FormatNative = FormatWindows
43 FormatNative = FormatPosix
54 bool operator==(
const FilePath& rhs)
const
56 return _vec == rhs._vec &&
59 bool operator!=(
const FilePath& rhs)
const
61 return !(*
this == rhs);
76 assign(str ?
string(str) : EMPTY_STRING);
80 operator string()
const
86 void assign(
const string& str);
89 string asString(Format format = FormatNative)
const;
100 return _type != TypeRelative;
111 return _vec[_vec.size() - 1];
121 parent._vec.pop_back();
129 const string& baseName = getBaseName();
130 size_t i = baseName.rfind(
'.');
131 return i != string::npos ? baseName.substr(i + 1) : EMPTY_STRING;
137 assign(asString() +
"." + ext);
145 string& baseName = _vec[_vec.size() - 1];
146 size_t i = baseName.rfind(
'.');
147 if (i != string::npos)
149 baseName = baseName.substr(0, i);
188 bool isDirectory()
const;
191 FilePathVec getFilesInDirectory(
const string& extension)
const;
194 FilePathVec getSubDirectories()
const;
197 void createDirectory()
const;
200 bool setCurrentPath();
221 using Iterator = FilePathVec::iterator;
222 using ConstIterator = FilePathVec::const_iterator;
232 FileSearchPath(
const string& searchPath,
const string& sep = PATH_LIST_SEPARATOR)
234 for (
const string& path : splitString(searchPath, sep))
244 string asString(
const string& sep = PATH_LIST_SEPARATOR)
const
247 for (
size_t i = 0; i < _paths.size(); i++)
250 if (i + 1 < _paths.size())
261 _paths.push_back(path);
267 for (
const FilePath& path : searchPath)
269 _paths.push_back(path);
276 _paths.insert(_paths.begin(), path);
288 return _paths.size();
294 return _paths.empty();
300 return _paths[index];
306 return _paths[index];
315 if (_paths.empty() || filename.
isEmpty())
323 FilePath combined = path / filename;
336 Iterator begin() {
return _paths.begin(); }
337 ConstIterator begin()
const {
return _paths.begin(); }
339 Iterator end() {
return _paths.end(); }
340 ConstIterator end()
const {
return _paths.end(); }
351MATERIALX_NAMESPACE_END
MX_FORMAT_API FileSearchPath getEnvironmentPath(const string &sep=PATH_LIST_SEPARATOR)
Return a FileSearchPath object from search path environment variable.
Definition: File.cpp:374
vector< string > StringVec
A vector of strings.
Definition: Library.h:57
A generic file path, supporting both syntactic and file system operations.
Definition: File.h:27
bool isAbsolute() const
Return true if the given path is absolute.
Definition: File.h:98
size_t size() const
Return the number of strings in the path.
Definition: File.h:159
void removeExtension()
Remove the file extension, if any, from the given path.
Definition: File.h:141
void addExtension(const string &ext)
Add a file extension to the given path.
Definition: File.h:135
FilePath(const char *str)
Construct a path from a C-style string.
Definition: File.h:74
FilePath(const string &str)
Construct a path from a standard string.
Definition: File.h:68
const string & getBaseName() const
Return the base name of the given path, with leading directory information removed.
Definition: File.h:105
const string & operator[](size_t index) const
Return the const string at the given index.
Definition: File.h:171
bool exists() const
Return true if the given path exists on the file system.
Definition: File.cpp:156
FilePath getParentPath() const
Return the parent directory of the given path, if any.
Definition: File.h:116
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
string getExtension() const
Return the file extension of the given path.
Definition: File.h:127
A sequence of file paths, which may be queried to find the first instance of a given filename on the ...
Definition: File.h:219
FilePath & operator[](size_t index)
Return the path at the given index.
Definition: File.h:298
size_t size() const
Return the number of paths in the sequence.
Definition: File.h:286
const FilePath & operator[](size_t index) const
Return the const path at the given index.
Definition: File.h:304
void append(const FileSearchPath &searchPath)
Append the given search path to the sequence.
Definition: File.h:265
void prepend(const FilePath &path)
Prepend the given path to the sequence.
Definition: File.h:274
FileSearchPath(const string &searchPath, const string &sep=PATH_LIST_SEPARATOR)
Construct a search path from a string.
Definition: File.h:232
void clear()
Clear all paths from the sequence.
Definition: File.h:280
bool isEmpty() const
Return true if the search path is empty.
Definition: File.h:292
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:313
string asString(const string &sep=PATH_LIST_SEPARATOR) const
Convert this sequence to a string using the given separator.
Definition: File.h:244
void append(const FilePath &path)
Append the given path to the sequence.
Definition: File.h:259