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
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)
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.
vector< string > StringVec
A vector of strings.
Definition: Library.h:55
MX_CORE_API StringVec splitString(const string &str, const string &sep)
Split a string into a vector of substrings using the given set of separator characters.
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
void createDirectory() const
Create a directory on the file system at the given path.
FilePathVec getSubDirectories() const
Return a vector of all directories at or beneath the given path.
FilePath(const char *str)
Construct a path from a C-style string.
Definition: File.h:74
static FilePath getCurrentPath()
Return the current working directory of the file system.
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
FilePath operator/(const FilePath &rhs) const
Concatenate two paths with a directory separator, returning the combined path.
string asString(Format format=FormatNative) const
Return this path as a standard string with the given format.
void assign(const string &str)
Assign a path from a standard string.
bool isDirectory() const
Return true if the given path is a directory on the file system.
FilePathVec getFilesInDirectory(const string &extension) const
Return a vector of all files in the given directory with the given extension.
bool exists() const
Return true if the given path exists on the file system.
static FilePath getModulePath()
Return the directory containing the executable module.
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
FilePath getNormalized() const
Return a normalized version of the given path, collapsing current path and parent path references so ...
string getExtension() const
Return the file extension of the given path.
Definition: File.h:127
bool setCurrentPath()
Set the current working directory of the file system.
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