forked from vsg-dev/VulkanSceneGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystem.cpp
More file actions
238 lines (204 loc) · 6.88 KB
/
FileSystem.cpp
File metadata and controls
238 lines (204 loc) · 6.88 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
/* <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/Options.h>
#if defined(WIN32) && !defined(__CYGWIN__)
# include <cstdlib>
# include <direct.h>
# include <io.h>
// cctype is needed for tolower()
# include <cctype>
#else
# include <sys/stat.h>
# include <unistd.h>
#endif
#include <iostream>
using namespace vsg;
const char UNIX_PATH_SEPARATOR = '/';
const char WINDOWS_PATH_SEPARATOR = '\\';
const char* const PATH_SEPARATORS = "/\\";
#if defined(WIN32) && !defined(__CYGWIN__)
const char delimiterNative = WINDOWS_PATH_SEPARATOR;
const char delimiterForeign = UNIX_PATH_SEPARATOR;
const char envPathDelimiter = ';';
#else
const char delimiterNative = UNIX_PATH_SEPARATOR;
const char delimiterForeign = WINDOWS_PATH_SEPARATOR;
const char envPathDelimiter = ':';
#endif
Paths vsg::getEnvPaths(const char* env_var)
{
Paths filepaths;
if (!env_var) return filepaths;
#if defined(WIN32) && !defined(__CYGWIN__)
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 filepaths;
}
#else
const char* env_value = getenv(env_var);
if (env_value == nullptr) return filepaths;
#endif
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(WIN32)
return _access(path.c_str(), 0) == 0;
#else
return access(path.c_str(), F_OK) == 0;
#endif
}
Path vsg::filePath(const Path& path)
{
std::string::size_type slash = path.find_last_of(PATH_SEPARATORS);
if (slash != std::string::npos)
{
return path.substr(0, slash);
}
else
{
return Path();
}
}
Path vsg::fileExtension(const Path& path)
{
std::string::size_type dot = path.find_last_of('.');
std::string::size_type slash = path.find_last_of(PATH_SEPARATORS);
if (dot == std::string::npos || (slash != std::string::npos && dot < slash)) return Path();
return path.substr(dot + 1);
}
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)
{
std::string::size_type dot = path.find_last_of('.');
std::string::size_type slash = path.find_last_of(PATH_SEPARATORS);
if (slash != std::string::npos)
{
if ((dot == std::string::npos) || (dot < slash))
return path.substr(slash + 1);
else
return path.substr(slash + 1, dot - slash - 1);
}
else
{
if (dot == std::string::npos)
return path;
else
return path.substr(0, dot);
}
}
Path vsg::removeExtension(const Path& path)
{
std::string::size_type dot = path.find_last_of('.');
std::string::size_type slash = path.find_last_of(PATH_SEPARATORS);
if (dot == std::string::npos || (slash != std::string::npos && dot < slash))
return path;
else if (dot > 1)
return path.substr(0, dot);
else
return {};
}
Path vsg::concatPaths(const Path& left, const Path& right)
{
if (left.empty())
{
return (right);
}
char lastChar = left[left.size() - 1];
if (lastChar == delimiterNative)
{
return left + right;
}
else if (lastChar == delimiterForeign)
{
return left.substr(0, left.size() - 1) + delimiterNative + right;
}
else // lastChar != a delimiter
{
return left + delimiterNative + right;
}
}
Path vsg::findFile(const Path& filename, const Paths& paths)
{
for (auto path : paths)
{
Path fullpath = concatPaths(path, filename);
if (fileExists(fullpath))
{
return fullpath;
}
}
return {};
}
Path vsg::findFile(const Path& filename, const Options* options)
{
if (options && !options->paths.empty())
{
if (options->checkFilenameHint == Options::CHECK_ORIGINAL_FILENAME_EXISTS_FIRST && fileExists(filename)) return filename;
if (auto path = findFile(filename, options->paths); !path.empty()) return path;
if (options->checkFilenameHint == Options::CHECK_ORIGINAL_FILENAME_EXISTS_LAST && fileExists(filename))
return filename;
else
return {};
}
else
{
return fileExists(filename) ? filename : Path();
}
}
bool vsg::makeDirectory(const Path& path)
{
std::vector<vsg::Path> directoriesToCreate;
Path trimmed_path = path;
while (!trimmed_path.empty() && !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(WIN32) && !defined(__CYGWIN__)
if (int status = _mkdir(directory_to_create.c_str()); status != 0)
{
std::cerr << " _mkdir(" << directory_to_create << ") failed. status = " << status << std::endl;
return false;
}
#else
if (int status = mkdir(directory_to_create.c_str(), 0755); status != 0)
{
std::cerr << " mkdir(" << directory_to_create << ") failed. status = " << status << std::endl;
return false;
}
#endif
}
return true;
}