forked from AcademySoftwareFoundation/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile.cpp
More file actions
278 lines (248 loc) · 6.18 KB
/
File.cpp
File metadata and controls
278 lines (248 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//
// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//
#include <MaterialXFormat/File.h>
#include <MaterialXFormat/Environ.h>
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <direct.h>
#else
#include <unistd.h>
#include <sys/stat.h>
#include <dirent.h>
#endif
#include <cctype>
#include <cerrno>
#include <climits>
#include <cstring>
namespace MaterialX
{
const string VALID_SEPARATORS_WINDOWS = "/\\";
const string VALID_SEPARATORS_POSIX = "/";
const char PREFERRED_SEPARATOR_WINDOWS = '\\';
const char PREFERRED_SEPARATOR_POSIX = '/';
#if defined(_WIN32)
const string PATH_LIST_SEPARATOR = ";";
#else
const string PATH_LIST_SEPARATOR = ":";
#endif
const string MATERIALX_SEARCH_PATH_ENV_VAR = "MATERIALX_SEARCH_PATH";
//
// FilePath methods
//
void FilePath::assign(const string& str, Format format)
{
_type = TypeRelative;
if (format == FormatWindows)
{
_vec = splitString(str, VALID_SEPARATORS_WINDOWS);
if (str.size() >= 2)
{
if (std::isalpha(str[0]) && str[1] == ':')
{
_type = TypeAbsolute;
}
else if (str[0] == '\\' && str[1] == '\\')
{
_type = TypeNetwork;
}
}
}
else
{
_vec = splitString(str, VALID_SEPARATORS_POSIX);
if (!str.empty() && str[0] == PREFERRED_SEPARATOR_POSIX)
{
_type = TypeAbsolute;
}
}
_format = format;
}
string FilePath::asString(Format format) const
{
string str;
if (format == FormatPosix && isAbsolute())
{
str += "/";
}
else if (format == FormatWindows && _type == TypeNetwork)
{
str += "\\\\";
}
for (size_t i = 0; i < _vec.size(); i++)
{
str += _vec[i];
if (i + 1 < _vec.size())
{
if (format == FormatPosix)
{
str += PREFERRED_SEPARATOR_POSIX;
}
else
{
str += PREFERRED_SEPARATOR_WINDOWS;
}
}
}
return str;
}
FilePath FilePath::operator/(const FilePath& rhs) const
{
if (rhs.isAbsolute())
{
throw Exception("Appended path must be relative.");
}
if (_format != rhs._format)
{
throw Exception("Appended path must have the same format.");
}
FilePath combined(*this);
for (const string& str : rhs._vec)
{
combined._vec.push_back(str);
}
return combined;
}
void FilePath::pop()
{
if (!isEmpty())
{
_vec.pop_back();
}
}
bool FilePath::exists() const
{
#if defined(_WIN32)
uint32_t result = GetFileAttributes(asString().c_str());
return result != INVALID_FILE_ATTRIBUTES;
#else
struct stat sb;
return stat(asString().c_str(), &sb) == 0;
#endif
}
bool FilePath::isDirectory() const
{
#if defined(_WIN32)
uint32_t result = GetFileAttributes(asString().c_str());
if (result == INVALID_FILE_ATTRIBUTES)
return false;
return (result & FILE_ATTRIBUTE_DIRECTORY) != 0;
#else
struct stat sb;
if (stat(asString().c_str(), &sb))
return false;
return S_ISDIR(sb.st_mode);
#endif
}
FilePathVec FilePath::getFilesInDirectory(const string& extension) const
{
FilePathVec files;
#if defined(_WIN32)
WIN32_FIND_DATA fd;
string wildcard = "*." + extension;
HANDLE hFind = FindFirstFile((*this / wildcard).asString().c_str(), &fd);
if (hFind != INVALID_HANDLE_VALUE)
{
do
{
if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
files.push_back(FilePath(fd.cFileName));
}
} while (FindNextFile(hFind, &fd));
FindClose(hFind);
}
#else
DIR* dir = opendir(asString().c_str());
if (dir)
{
while (struct dirent* entry = readdir(dir))
{
if (entry->d_type != DT_DIR && FilePath(entry->d_name).getExtension() == extension)
{
files.push_back(FilePath(entry->d_name));
}
}
closedir(dir);
}
#endif
return files;
}
FilePathVec FilePath::getSubDirectories() const
{
if (!isDirectory())
{
return FilePathVec();
}
FilePathVec dirs { *this };
#if defined(_WIN32)
WIN32_FIND_DATA fd;
string wildcard = "*";
HANDLE hFind = FindFirstFile((*this / wildcard).asString().c_str(), &fd);
if (hFind != INVALID_HANDLE_VALUE)
{
do
{
string path = fd.cFileName;
if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && (path != "." && path != ".."))
{
FilePath newDir = *this / path;
FilePathVec newDirs = newDir.getSubDirectories();
dirs.insert(dirs.end(), newDirs.begin(), newDirs.end());
}
} while (FindNextFile(hFind, &fd));
FindClose(hFind);
}
#else
DIR* dir = opendir(asString().c_str());
if (dir)
{
while (struct dirent* entry = readdir(dir))
{
string path = entry->d_name;
if (entry->d_type == DT_DIR && (path != "." && path != ".."))
{
FilePath newDir = *this / path;
FilePathVec newDirs = newDir.getSubDirectories();
dirs.insert(dirs.end(), newDirs.begin(), newDirs.end());
}
}
closedir(dir);
}
#endif
return dirs;
}
void FilePath::createDirectory()
{
#if defined(_WIN32)
_mkdir(asString().c_str());
#else
mkdir(asString().c_str(), 0777);
#endif
}
FilePath FilePath::getCurrentPath()
{
#if defined(_WIN32)
char buf[MAX_PATH];
if (!GetCurrentDirectory(MAX_PATH, buf))
{
throw Exception("Error in getCurrentPath: " + std::to_string(GetLastError()));
}
return FilePath(buf);
#else
char buf[PATH_MAX];
if (getcwd(buf, PATH_MAX) == NULL)
{
throw Exception("Error in getCurrentPath: " + string(strerror(errno)));
}
return FilePath(buf);
#endif
}
FileSearchPath getEnvironmentPath(const string& sep)
{
string searchPathEnv = getEnviron(MATERIALX_SEARCH_PATH_ENV_VAR);
return FileSearchPath(searchPathEnv, sep);
}
} // namespace MaterialX