/
nativepath.cpp
184 lines (155 loc) · 4.77 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
/**
* @file nativepath.cpp
* File paths for the native file system.
*
* @authors Copyright © 2012 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2012 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 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 General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include "de/NativePath"
#include "de/App"
#include <QDir>
#ifdef WIN32
# define DIR_SEPARATOR QChar('\\')
#else
# define DIR_SEPARATOR QChar('/')
# ifdef UNIX
# include <sys/types.h>
# include <pwd.h>
# endif
#endif
namespace de {
static QString toNative(const QString& s)
{
// This will resolve parent references (".."), multiple separators
// (hello//world), and self-references (".").
return QDir::toNativeSeparators(QDir::cleanPath(s));
}
NativePath::NativePath() : Path()
{}
NativePath::NativePath(const QString &str) : Path(toNative(str), DIR_SEPARATOR)
{}
NativePath::NativePath(const char *nullTerminatedCStr)
: Path(toNative(nullTerminatedCStr), DIR_SEPARATOR)
{
}
NativePath::NativePath(const char* cStr, dsize length)
: Path(toNative(QString::fromUtf8(cStr, length)), DIR_SEPARATOR)
{}
NativePath& NativePath::operator = (const QString& str)
{
set(toNative(str), DIR_SEPARATOR);
return *this;
}
NativePath& NativePath::operator = (const char *nullTerminatedCStr)
{
return (*this) = QString(nullTerminatedCStr);
}
NativePath NativePath::concatenatePath(const NativePath& nativePath) const
{
if(nativePath.isAbsolute()) return nativePath;
return toString().concatenatePath(nativePath, QChar(DIR_SEPARATOR));
}
NativePath NativePath::concatenatePath(const QString& nativePath) const
{
return concatenatePath(NativePath(nativePath));
}
NativePath NativePath::operator / (const NativePath& nativePath) const
{
return concatenatePath(nativePath);
}
NativePath NativePath::operator / (const QString& str) const
{
return *this / NativePath(str);
}
NativePath NativePath::operator / (const char *nullTerminatedCStr) const
{
return *this / NativePath(nullTerminatedCStr);
}
NativePath NativePath::fileNamePath() const
{
return String(*this).fileNamePath(DIR_SEPARATOR);
}
bool NativePath::isAbsolute() const
{
return QDir::isAbsolutePath(expand());
}
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;
const String 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;
// Hide relative directives like '}'
if(result.length() > 1 && (result.first() == '}' || result.first() == '>'))
{
result = result.mid(1);
}
// If within our the base directory cut out the base path.
if(QDir::isAbsolutePath(result))
{
NativePath basePath = App::app().nativeBasePath();
if(result.beginsWith(basePath))
{
result = result.mid(basePath.length() + 1);
}
}
return result;
}
String NativePath::withSeparators(QChar sep) const
{
return Path::withSeparators(sep);
}
NativePath NativePath::workPath()
{
return QDir::currentPath();
}
}