forked from AcademySoftwareFoundation/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
File.cpp
335 lines (302 loc) · 7.58 KB
/
File.cpp
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//
// 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
#if defined(__APPLE__)
#include <mach-o/dyld.h>
#endif
#include <array>
#include <cctype>
#include <cerrno>
#include <cstring>
namespace MaterialX
{
const string VALID_SEPARATORS = "/\\";
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)
{
_type = TypeRelative;
_vec = splitString(str, VALID_SEPARATORS);
if (!str.empty())
{
if (str[0] == PREFERRED_SEPARATOR_POSIX)
{
_type = TypeAbsolute;
}
else if (str.size() >= 2)
{
if (std::isalpha(str[0]) && str[1] == ':')
{
_type = TypeAbsolute;
}
else if (str[0] == '\\' && str[1] == '\\')
{
_type = TypeNetwork;
}
}
}
}
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.");
}
FilePath combined(*this);
for (const string& str : rhs._vec)
{
combined._vec.push_back(str);
}
return combined;
}
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.emplace_back(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 (path == "." || path == "..")
{
continue;
}
auto d_type = entry->d_type;
FilePath newDir = *this / path;
if (d_type == DT_UNKNOWN)
{
if (newDir.isDirectory())
{
d_type = DT_DIR;
}
}
if (d_type == DT_DIR)
{
FilePath newDir = *this / path;
FilePathVec newDirs = newDir.getSubDirectories();
dirs.insert(dirs.end(), newDirs.begin(), newDirs.end());
}
}
closedir(dir);
}
#endif
return dirs;
}
void FilePath::createDirectory() const
{
#if defined(_WIN32)
_mkdir(asString().c_str());
#else
mkdir(asString().c_str(), 0777);
#endif
}
FilePath FilePath::getCurrentPath()
{
#if defined(_WIN32)
std::array<char, MAX_PATH> buf;
if (!GetCurrentDirectory(MAX_PATH, buf.data()))
{
throw Exception("Error in getCurrentPath: " + std::to_string(GetLastError()));
}
return FilePath(buf.data());
#else
std::array<char, PATH_MAX> buf;
if (getcwd(buf.data(), PATH_MAX) == NULL)
{
throw Exception("Error in getCurrentPath: " + string(strerror(errno)));
}
return FilePath(buf.data());
#endif
}
FilePath FilePath::getModulePath()
{
#if defined(_WIN32)
vector<char> buf(MAX_PATH);
while (true)
{
uint32_t reqSize = GetModuleFileName(NULL, buf.data(), (uint32_t) buf.size());
if (!reqSize)
{
throw Exception("Error in getModulePath: " + std::to_string(GetLastError()));
}
else if ((size_t) reqSize >= buf.size())
{
buf.resize(buf.size() * 2);
}
else
{
return FilePath(buf.data()).getParentPath();
}
}
#elif defined(__APPLE__)
vector<char> buf(PATH_MAX);
while (true)
{
uint32_t reqSize = buf.size();
if (_NSGetExecutablePath(buf.data(), &reqSize) == -1)
{
buf.resize((size_t) reqSize);
}
else
{
return FilePath(buf.data()).getParentPath();
}
}
#else
vector<char> buf(PATH_MAX);
while (true)
{
ssize_t reqSize = readlink("/proc/self/exe", buf.data(), buf.size());
if (reqSize == -1)
{
throw Exception("Error in getModulePath: " + string(strerror(errno)));
}
else if ((size_t) reqSize >= buf.size())
{
buf.resize(buf.size() * 2);
}
else
{
buf.data()[reqSize] = '\0';
return FilePath(buf.data()).getParentPath();
}
}
#endif
}
FileSearchPath getEnvironmentPath(const string& sep)
{
string searchPathEnv = getEnviron(MATERIALX_SEARCH_PATH_ENV_VAR);
return FileSearchPath(searchPathEnv, sep);
}
} // namespace MaterialX