/
nativepath.cpp
295 lines (251 loc) · 7.71 KB
/
nativepath.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
/**
* @file nativepath.cpp
* File paths for the native file system.
*
* @authors Copyright © 2012-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2013 Daniel Swanson <danij@dengine.net>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "de/NativePath"
#include "de/App"
#include <QDir>
#include <QFile>
/**
* @def NATIVE_BASE_SYMBOLIC
*
* Symbol used in "pretty" paths to represent the base directory (the one set
* with -basedir).
*/
#define NATIVE_BASE_SYMBOLIC "(basedir)"
/**
* @def NATIVE_HOME_SYMBOLIC
*
* Symbol used in "pretty" paths to represent the user's native home directory.
* Note that this is not the same thing as App::nativeHomePath(), which returns
* the native location of the Doomsday runtime "/home" path where runtime files
* are stored.
*/
#ifdef WIN32
# define NATIVE_HOME_SYMBOLIC "%HOMEPATH%"
# define DIR_SEPARATOR QChar('\\')
#else
# define NATIVE_HOME_SYMBOLIC "~"
# define DIR_SEPARATOR QChar('/')
# ifdef UNIX
# include <sys/types.h>
# include <pwd.h>
# endif
#endif
namespace de {
static QString toNative(String const &s)
{
// This will resolve parent references (".."), multiple separators
// (hello//world), and self-references (".").
return Path::normalizeString(QDir::cleanPath(s), DIR_SEPARATOR);
}
NativePath::NativePath() : Path()
{}
NativePath::NativePath(String const &str) : Path(toNative(str), DIR_SEPARATOR)
{}
NativePath::NativePath(QString const &qstr) : Path(toNative(qstr), DIR_SEPARATOR)
{}
NativePath::NativePath(char const *nullTerminatedCStr)
: Path(toNative(nullTerminatedCStr), DIR_SEPARATOR)
{}
NativePath::NativePath(char const *cStr, dsize length)
: Path(toNative(String(cStr, length)), DIR_SEPARATOR)
{}
NativePath &NativePath::operator = (String const &str)
{
set(toNative(str), DIR_SEPARATOR);
return *this;
}
NativePath &NativePath::operator = (QString const &str)
{
set(toNative(str), DIR_SEPARATOR);
return *this;
}
NativePath &NativePath::operator = (char const *nullTerminatedCStr)
{
return (*this) = String(nullTerminatedCStr);
}
NativePath NativePath::concatenatePath(NativePath const &nativePath) const
{
if (nativePath.isAbsolute()) return nativePath;
return toString().concatenatePath(nativePath, QChar(DIR_SEPARATOR));
}
NativePath NativePath::concatenatePath(String const &nativePath) const
{
return concatenatePath(NativePath(nativePath));
}
NativePath NativePath::operator / (NativePath const &nativePath) const
{
return concatenatePath(nativePath);
}
NativePath NativePath::operator / (String const &str) const
{
return *this / NativePath(str);
}
NativePath NativePath::operator / (QString const &str) const
{
return *this / NativePath(str);
}
NativePath NativePath::operator / (char const *nullTerminatedCStr) const
{
return *this / NativePath(nullTerminatedCStr);
}
NativePath NativePath::fileNamePath() const
{
return String(*this).fileNamePath(DIR_SEPARATOR);
}
bool NativePath::isAbsolute() const
{
return QDir::isAbsolutePath(expand());
}
bool NativePath::isDirectory() const
{
QFileInfo info(*this);
return info.exists() && info.isDir();
}
NativePath NativePath::expand(bool *didExpand) const
{
if (first() == '>' || first() == '}')
{
if (didExpand) *didExpand = true;
return App::app().nativeBasePath() / toString().mid(1);
}
#ifdef UNIX
else if (first() == '~')
{
if (didExpand) *didExpand = true;
String const path = toString();
int firstSlash = path.indexOf('/');
if (firstSlash > 1)
{
// Parse the user's home directory (from passwd).
QByteArray userName = path.mid(1, firstSlash - 1).toLatin1();
struct passwd *pw = getpwnam(userName);
if (!pw)
{
/// @throws UnknownUserError User is not known.
throw UnknownUserError("NativePath::expand",
String("Unknown user '%1'").arg(QLatin1String(userName)));
}
return NativePath(pw->pw_dir) / path.mid(firstSlash + 1);
}
else
{
// Replace with the HOME path.
return NativePath(QDir::homePath()) / path.mid(2);
}
}
#endif
// No expansion done.
if (didExpand) *didExpand = false;
return *this;
}
String NativePath::pretty() const
{
if (isEmpty()) return *this;
String result = *this;
// Replace relative directives like '}' (used in FS1 only) with a full symbol.
if (result.length() > 1 && (result.first() == '}' || result.first() == '>'))
{
return String(NATIVE_BASE_SYMBOLIC) + DIR_SEPARATOR + result.mid(1);
}
// If within one of the known native directories, cut out the known path,
// replacing it with a symbolic. This retains the absolute nature of the path
// while omitting potentially redundant/verbose information.
if (QDir::isAbsolutePath(result))
{
NativePath basePath = App::app().nativeBasePath();
if (result.beginsWith(basePath))
{
result = NATIVE_BASE_SYMBOLIC + result.mid(basePath.length());
}
else
{
#ifdef MACOSX
NativePath contentsPath = App::app().nativeAppContentsPath();
if (result.beginsWith(contentsPath))
{
return "(app)" + result.mid(contentsPath.length());
}
#endif
#ifndef WIN32 // Windows users are not familiar with a symbolic home path.
NativePath homePath = QDir::homePath(); // actual native home dir, not FS2 "/home"
if (result.beginsWith(homePath))
{
result = NATIVE_HOME_SYMBOLIC + result.mid(homePath.length());
}
#endif
}
}
return result;
}
String NativePath::withSeparators(QChar sep) const
{
return Path::withSeparators(sep);
}
bool NativePath::exists() const
{
if (isEmpty()) return false;
return QFile::exists(toString());
}
bool NativePath::isReadable() const
{
return QFileInfo(toString()).isReadable();
}
static NativePath currentNativeWorkPath;
NativePath NativePath::workPath()
{
if (currentNativeWorkPath.isEmpty())
{
currentNativeWorkPath = QDir::currentPath();
}
return currentNativeWorkPath;
}
bool NativePath::setWorkPath(NativePath const &cwd)
{
if (QDir::setCurrent(cwd))
{
currentNativeWorkPath = cwd;
return true;
}
return false;
}
bool NativePath::exists(NativePath const &nativePath)
{
return QDir::current().exists(nativePath);
}
void NativePath::createPath(NativePath const &nativePath)
{
NativePath parentPath = nativePath.fileNamePath();
if (!parentPath.isEmpty() && !exists(parentPath))
{
createPath(parentPath);
}
QDir::current().mkdir(nativePath);
if (!exists(nativePath))
{
/// @throw CreateDirError Failed to create directory @a nativePath.
throw CreateDirError("NativePath::createPath", "Could not create: " + nativePath);
}
}
QChar NativePath::separator()
{
return DIR_SEPARATOR;
}
} // namespace de