forked from vsg-dev/VulkanSceneGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileSystem.cpp
346 lines (295 loc) · 9.43 KB
/
FileSystem.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
336
337
338
339
340
341
342
343
344
345
346
/* <editor-fold desc="MIT License">
Copyright(c) 2018 Robert Osfield
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
</editor-fold> */
#include <vsg/io/FileSystem.h>
#include <vsg/io/Logger.h>
#include <vsg/io/Options.h>
#include <vsg/io/stream.h>
#include <cstdio>
#if defined(WIN32) && !defined(__CYGWIN__)
# include <cstdlib>
# include <direct.h>
# include <io.h>
// cctype is needed for tolower()
# include <cctype>
# include <windows.h>
# ifdef _MSC_VER
# ifndef PATH_MAX
# define PATH_MAX MAX_PATH
# endif
# endif
#else
# include <errno.h>
# include <sys/stat.h>
# include <unistd.h>
#endif
#ifdef __APPLE__
# include <TargetConditionals.h>
# include <libgen.h>
# include <mach-o/dyld.h>
#endif
#include <limits.h>
using namespace vsg;
#if defined(_MSC_VER)
const char envPathDelimiter = ';';
#else
const char envPathDelimiter = ':';
#endif
std::string vsg::getEnv(const char* env_var)
{
#if defined(_MSC_VER)
char env_value[4096];
std::size_t len;
if (auto error = getenv_s(&len, env_value, sizeof(env_value) - 1, env_var); error != 0 || len == 0)
{
return {};
}
#else
const char* env_value = getenv(env_var);
if (env_value == nullptr) return {};
#endif
return std::string(env_value);
}
Paths vsg::getEnvPaths(const char* env_var)
{
if (!env_var) return {};
#if defined(_MSC_VER)
char env_value[4096];
std::size_t len;
if (auto error = getenv_s(&len, env_value, sizeof(env_value) - 1, env_var); error != 0 || len == 0)
{
return {};
}
#else
const char* env_value = getenv(env_var);
if (env_value == nullptr) return {};
#endif
Paths filepaths;
std::string paths(env_value);
std::string::size_type start = 0;
std::string::size_type end;
while ((end = paths.find_first_of(envPathDelimiter, start)) != std::string::npos)
{
filepaths.push_back(paths.substr(start, end - start));
start = end + 1;
}
std::string lastPath(paths, start, std::string::npos);
if (!lastPath.empty())
filepaths.push_back(lastPath);
return filepaths;
}
bool vsg::fileExists(const Path& path)
{
#if defined(_MSC_VER)
return _waccess(path.c_str(), 0) == 0;
#else
return access(path.c_str(), F_OK) == 0;
#endif
}
Path vsg::filePath(const Path& path)
{
if (trailingRelativePath(path)) return path;
auto slash = path.find_last_of(Path::separators);
if (slash != vsg::Path::npos)
{
return path.substr(0, slash);
}
else
{
return {};
}
}
Path vsg::fileExtension(const Path& path)
{
auto dot = path.find_last_of('.');
if (dot == Path::npos || (dot + 1) == path.size()) return {};
auto slash = path.find_last_of(Path::separators);
if (slash != Path::npos && dot < slash) return {};
return path.substr(dot);
}
Path vsg::lowerCaseFileExtension(const Path& path)
{
Path ext = fileExtension(path);
for (auto& c : ext) c = std::tolower(c);
return ext;
}
Path vsg::simpleFilename(const Path& path)
{
if (trailingRelativePath(path)) return {};
auto dot = path.find_last_of('.');
auto slash = path.find_last_of(Path::separators);
if (slash != Path::npos)
{
if ((dot == Path::npos) || (dot < slash))
return path.substr(slash + 1);
else
return path.substr(slash + 1, dot - slash - 1);
}
else
{
if (dot == Path::npos)
return path;
else
return path.substr(0, dot);
}
}
bool vsg::trailingRelativePath(const Path& path)
{
if (path == ".") return true;
if (path == "..") return true;
if (path.size() >= 2)
{
if (path.compare(path.size() - 2, 2, "/.") == 0) return true;
if (path.compare(path.size() - 2, 2, "\\.") == 0) return true;
if (path.size() >= 3)
{
if (path.compare(path.size() - 3, 3, "/..") == 0) return true;
if (path.compare(path.size() - 3, 3, "\\..") == 0) return true;
}
}
return false;
}
Path vsg::removeExtension(const Path& path)
{
if (trailingRelativePath(path)) return path;
auto dot = path.find_last_of('.');
if (dot == Path::npos) return path;
auto slash = path.find_last_of(Path::separators);
if (slash != Path::npos && dot < slash)
return path;
else if (dot > 1)
return path.substr(0, dot);
else
return {};
}
Path vsg::findFile(const Path& filename, const Paths& paths)
{
for (auto path : paths)
{
Path fullpath = path / filename;
if (fileExists(fullpath))
{
return fullpath;
}
}
return {};
}
Path vsg::findFile(const Path& filename, const Options* options)
{
if (options)
{
// if Options has a findFileCallback use it
if (options->findFileCallback) return options->findFileCallback(filename, options);
if (!options->paths.empty())
{
// if appropriate use the filename directly if it exists.
if (options->checkFilenameHint == Options::CHECK_ORIGINAL_FILENAME_EXISTS_FIRST && fileExists(filename)) return filename;
// search for the file if the in the specific paths.
if (auto path = findFile(filename, options->paths)) return path;
// if appropriate use the filename directly if it exists.
if (options->checkFilenameHint == Options::CHECK_ORIGINAL_FILENAME_EXISTS_LAST && fileExists(filename))
return filename;
else
return {};
}
}
return fileExists(filename) ? filename : Path();
}
bool vsg::makeDirectory(const Path& path)
{
std::vector<vsg::Path> directoriesToCreate;
Path trimmed_path = path;
while (trimmed_path && !vsg::fileExists(trimmed_path))
{
directoriesToCreate.push_back(trimmed_path);
trimmed_path = vsg::filePath(trimmed_path);
}
for (auto itr = directoriesToCreate.rbegin(); itr != directoriesToCreate.rend(); ++itr)
{
vsg::Path directory_to_create = *itr;
if (directory_to_create.size() == 2 && directory_to_create[1] == ':')
{
// ignore a C: style drive prefixes
continue;
}
#if defined(_MSC_VER)
if (int status = _wmkdir(directory_to_create.c_str()); status != 0)
#elif defined(__MINGW32__)
if (int status = mkdir(directory_to_create.c_str()); status != 0)
#else // POSIX
if (int status = mkdir(directory_to_create.c_str(), 0755); status != 0)
#endif
{
if (errno != EEXIST)
{
// quietly ignore a mkdir on a file that already exists as this can happen safely during a filling in a filecache.
debug("mkdir(", directory_to_create, ") failed. errno = ", errno);
}
return false;
}
}
return true;
}
Path vsg::executableFilePath()
{
Path path;
#if defined(WIN32)
TCHAR buf[PATH_MAX + 1];
DWORD result = GetModuleFileName(NULL, buf, static_cast<DWORD>(std::size(buf) - 1));
if (result && result < std::size(buf))
path = buf;
#elif defined(__linux__)
// TODO need to handle case where executable filename is longer than PATH_MAX
// See https://stackoverflow.com/questions/5525668/how-to-implement-readlink-to-find-the-path
char buf[PATH_MAX + 1];
ssize_t len = ::readlink("/proc/self/exe", buf, sizeof(buf) - 1);
if (len != -1)
{
buf[len] = '\0';
path = buf;
}
#elif defined(__APPLE__)
# if TARGET_OS_MAC
char realPathName[PATH_MAX + 1];
char buf[PATH_MAX + 1];
uint32_t size = (uint32_t)sizeof(buf);
if (!_NSGetExecutablePath(buf, &size))
{
realpath(buf, realPathName);
path = realPathName;
}
# elif TARGET_IPHONE_SIMULATOR
// iOS, tvOS, or watchOS Simulator
// Not currently implemented
# elif TARGET_OS_MACCATALYST
// Mac's Catalyst (ports iOS API into Mac, like UIKit).
// Not currently implemented
# elif TARGET_OS_IPHONE
// iOS, tvOS, or watchOS device
// Not currently implemented
# else
# error "Unknown Apple platform"
# endif
#elif defined(__ANDROID__)
// Not currently implemented
#endif
return path;
}
FILE* vsg::fopen(const Path& path, const char* mode)
{
#if defined(_MSC_VER)
std::wstring wMode;
convert_utf(mode, wMode);
FILE* file = nullptr;
auto errorNo = _wfopen_s(&file, path.c_str(), wMode.c_str());
if (errorNo == 0)
return file;
else
return nullptr;
#else
return ::fopen(path.c_str(), mode);
#endif
}