-
Notifications
You must be signed in to change notification settings - Fork 347
Expand file tree
/
Copy pathdeFilePath.cpp
More file actions
300 lines (251 loc) · 9.11 KB
/
deFilePath.cpp
File metadata and controls
300 lines (251 loc) · 9.11 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*-------------------------------------------------------------------------
* drawElements C++ Base Library
* -----------------------------
*
* Copyright 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*//*!
* \file
* \brief Filesystem path class.
*//*--------------------------------------------------------------------*/
#include "deFilePath.hpp"
#include <vector>
#include <stdexcept>
#include <sys/stat.h>
#include <sys/types.h>
#if (DE_OS == DE_OS_WIN32)
#define VC_EXTRALEAN
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
#endif
using std::string;
namespace de
{
#if (DE_OS == DE_OS_WIN32)
const std::string FilePath::separator = "\\";
#else
const std::string FilePath::separator = "/";
#endif
FilePath::FilePath(const std::vector<std::string> &components)
{
for (size_t ndx = 0; ndx < components.size(); ndx++)
{
if (!m_path.empty() && !isSeparator(m_path[m_path.size() - 1]))
m_path += separator;
m_path += components[ndx];
}
}
void FilePath::split(std::vector<std::string> &components) const
{
components.clear();
int curCompStart = 0;
int pos;
if (isWinNetPath())
components.push_back(separator + separator);
else if (isRootPath() && !beginsWithDrive())
components.push_back(separator);
for (pos = 0; pos < (int)m_path.length(); pos++)
{
const char c = m_path[pos];
if (isSeparator(c))
{
if (pos - curCompStart > 0)
components.push_back(m_path.substr(curCompStart, pos - curCompStart));
curCompStart = pos + 1;
}
}
if (pos - curCompStart > 0)
components.push_back(m_path.substr(curCompStart, pos - curCompStart));
}
FilePath FilePath::join(const std::vector<std::string> &components)
{
return FilePath(components);
}
FilePath &FilePath::normalize(void)
{
std::vector<std::string> components;
std::vector<std::string> reverseNormalizedComponents;
split(components);
m_path = "";
int numUp = 0;
// Do in reverse order and eliminate any . or .. components
for (int ndx = (int)components.size() - 1; ndx >= 0; ndx--)
{
const std::string &comp = components[ndx];
if (comp == "..")
numUp += 1;
else if (comp == ".")
continue;
else if (numUp > 0)
numUp -= 1; // Skip part
else
reverseNormalizedComponents.push_back(comp);
}
if (isAbsolutePath() && numUp > 0)
throw std::runtime_error("Cannot normalize path: invalid path");
// Prepend necessary ".." components
while (numUp--)
reverseNormalizedComponents.push_back("..");
if (reverseNormalizedComponents.empty() && components.back() == ".")
reverseNormalizedComponents.push_back("."); // Composed of "." components only
*this = join(std::vector<std::string>(reverseNormalizedComponents.rbegin(), reverseNormalizedComponents.rend()));
return *this;
}
FilePath FilePath::normalize(const FilePath &path)
{
return FilePath(path).normalize();
}
std::string FilePath::getBaseName(void) const
{
std::vector<std::string> components;
split(components);
return !components.empty() ? components[components.size() - 1] : std::string("");
}
std::string FilePath::getDirName(void) const
{
std::vector<std::string> components;
split(components);
if (components.size() > 1)
{
components.pop_back();
return FilePath(components).getPath();
}
else if (isAbsolutePath())
return separator;
else
return std::string(".");
}
std::string FilePath::getFileExtension(void) const
{
std::string baseName = getBaseName();
size_t dotPos = baseName.find_last_of('.');
if (dotPos == std::string::npos)
return std::string("");
else
return baseName.substr(dotPos + 1);
}
bool FilePath::exists(void) const
{
FilePath normPath = FilePath::normalize(*this);
struct stat st;
int result = stat(normPath.getPath(), &st);
return result == 0;
}
FilePath::Type FilePath::getType(void) const
{
FilePath normPath = FilePath::normalize(*this);
struct stat st;
int result = stat(normPath.getPath(), &st);
if (result != 0)
return TYPE_UNKNOWN;
int type = st.st_mode & S_IFMT;
if (type == S_IFREG)
return TYPE_FILE;
else if (type == S_IFDIR)
return TYPE_DIRECTORY;
else
return TYPE_UNKNOWN;
}
bool FilePath::beginsWithDrive(void) const
{
for (int ndx = 0; ndx < (int)m_path.length(); ndx++)
{
if (m_path[ndx] == ':' && ndx + 1 < (int)m_path.length() && isSeparator(m_path[ndx + 1]))
return true; // First part is drive letter.
if (isSeparator(m_path[ndx]))
return false;
}
return false;
}
bool FilePath::isAbsolutePath(void) const
{
return isRootPath() || isWinNetPath() || beginsWithDrive();
}
void FilePath_selfTest(void)
{
DE_TEST_ASSERT(!FilePath(".").isAbsolutePath());
DE_TEST_ASSERT(!FilePath("..\\foo").isAbsolutePath());
DE_TEST_ASSERT(!FilePath("foo").isAbsolutePath());
DE_TEST_ASSERT(FilePath("\\foo/bar").isAbsolutePath());
DE_TEST_ASSERT(FilePath("/foo").isAbsolutePath());
DE_TEST_ASSERT(FilePath("\\").isAbsolutePath());
DE_TEST_ASSERT(FilePath("\\\\net\\loc").isAbsolutePath());
DE_TEST_ASSERT(FilePath("C:\\file.txt").isAbsolutePath());
DE_TEST_ASSERT(FilePath("c:/file.txt").isAbsolutePath());
DE_TEST_ASSERT(string(".") == FilePath(".//.").normalize().getPath());
DE_TEST_ASSERT(string(".") == FilePath(".").normalize().getPath());
DE_TEST_ASSERT((string("..") + FilePath::separator + "test") ==
FilePath("foo/../bar/../../test").normalize().getPath());
DE_TEST_ASSERT((FilePath::separator + "foo" + FilePath::separator + "foo.txt") ==
FilePath("/foo\\bar/..\\dir\\..\\foo.txt").normalize().getPath());
DE_TEST_ASSERT((string("c:") + FilePath::separator + "foo" + FilePath::separator + "foo.txt") ==
FilePath("c:/foo\\bar/..\\dir\\..\\foo.txt").normalize().getPath());
DE_TEST_ASSERT((FilePath::separator + FilePath::separator + "foo" + FilePath::separator + "foo.txt") ==
FilePath("\\\\foo\\bar/..\\dir\\..\\foo.txt").normalize().getPath());
DE_TEST_ASSERT(FilePath("foo/bar").getBaseName() == "bar");
DE_TEST_ASSERT(FilePath("foo/bar/").getBaseName() == "bar");
DE_TEST_ASSERT(FilePath("foo\\bar").getBaseName() == "bar");
DE_TEST_ASSERT(FilePath("foo\\bar\\").getBaseName() == "bar");
DE_TEST_ASSERT(FilePath("foo/bar").getDirName() == "foo");
DE_TEST_ASSERT(FilePath("foo/bar/").getDirName() == "foo");
DE_TEST_ASSERT(FilePath("foo\\bar").getDirName() == "foo");
DE_TEST_ASSERT(FilePath("foo\\bar\\").getDirName() == "foo");
DE_TEST_ASSERT(FilePath("/foo/bar/baz").getDirName() == FilePath::separator + "foo" + FilePath::separator + "bar");
}
static void createDirectoryImpl(const char *path)
{
#if (DE_OS == DE_OS_WIN32)
if (!CreateDirectory(path, nullptr))
throw std::runtime_error("Failed to create directory");
#elif (DE_OS == DE_OS_UNIX) || (DE_OS == DE_OS_OSX) || (DE_OS == DE_OS_IOS) || (DE_OS == DE_OS_ANDROID) || \
(DE_OS == DE_OS_SYMBIAN) || (DE_OS == DE_OS_QNX) || (DE_OS == DE_OS_FUCHSIA) || (DE_OS == DE_OS_OHOS)
if (mkdir(path, 0777) != 0)
throw std::runtime_error("Failed to create directory");
#else
#error Implement createDirectoryImpl() for your platform.
#endif
}
void createDirectory(const char *path)
{
FilePath dirPath = FilePath::normalize(path);
FilePath parentPath(dirPath.getDirName());
if (dirPath.exists())
throw std::runtime_error("Destination exists already");
else if (!parentPath.exists())
throw std::runtime_error("Parent directory doesn't exist");
else if (parentPath.getType() != FilePath::TYPE_DIRECTORY)
throw std::runtime_error("Parent is not directory");
createDirectoryImpl(path);
}
void createDirectoryAndParents(const char *path)
{
std::vector<std::string> createPaths;
FilePath curPath(path);
if (curPath.exists())
throw std::runtime_error("Destination exists already");
while (!curPath.exists())
{
createPaths.push_back(curPath.getPath());
std::string parent = curPath.getDirName();
DE_CHECK_RUNTIME_ERR(parent != curPath.getPath());
curPath = FilePath(parent);
}
// Create in reverse order.
for (std::vector<std::string>::const_reverse_iterator parentIter = createPaths.rbegin();
parentIter != createPaths.rend(); parentIter++)
createDirectory(parentIter->c_str());
}
} // namespace de