forked from scummvm/scummvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.cpp
162 lines (130 loc) · 3.56 KB
/
path.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
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* 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 Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "common/path.h"
namespace Common {
Path::Path(const Path &path) {
_str = path.rawString();
}
Path::Path(const char *str, char separator) {
set(str, separator);
}
Path::Path(const String &str, char separator) {
set(str.c_str(), separator);
}
String Path::toString(char separator) const {
String res;
for (const char *ptr = _str.c_str(); *ptr; ptr++) {
if (*ptr == DIR_SEPARATOR)
res += separator;
else
res += *ptr;
}
return res;
}
bool Path::operator==(const Path &x) const {
return _str == x.rawString();
}
bool Path::operator!=(const Path &x) const {
return _str != x.rawString();
}
bool Path::empty() const {
return _str.empty();
}
Path &Path::operator=(const Path &path) {
_str = path.rawString();
return *this;
}
Path &Path::operator=(const char *str) {
set(str);
return *this;
}
Path &Path::operator=(const String &str) {
set(str.c_str());
return *this;
}
void Path::set(const char *str, char separator) {
_str.clear();
appendInPlace(str, separator);
}
Path &Path::appendInPlace(const Path &x) {
_str += x.rawString();
return *this;
}
Path &Path::appendInPlace(const String &str, char separator) {
appendInPlace(str.c_str(), separator);
return *this;
}
Path &Path::appendInPlace(const char *str, char separator) {
for (; *str; str++) {
if (*str == separator)
_str += DIR_SEPARATOR;
else
_str += *str;
}
return *this;
}
Path Path::append(const Path &x) const {
Path temp(*this);
temp.appendInPlace(x);
return temp;
}
Path Path::append(const String &str, char separator) const {
return append(str.c_str(), separator);
}
Path Path::append(const char *str, char separator) const {
Path temp(*this);
temp.appendInPlace(str, separator);
return temp;
}
Path &Path::joinInPlace(const Path &x) {
if (x.empty())
return *this;
if (!_str.empty() && _str.lastChar() != DIR_SEPARATOR && x.rawString().firstChar() != DIR_SEPARATOR)
_str += DIR_SEPARATOR;
_str += x.rawString();
return *this;
}
Path &Path::joinInPlace(const String &str, char separator) {
return joinInPlace(str.c_str(), separator);
}
Path &Path::joinInPlace(const char *str, char separator) {
if (*str == '\0')
return *this;
if (!_str.empty() && _str.lastChar() != DIR_SEPARATOR && *str != separator)
_str += DIR_SEPARATOR;
appendInPlace(str, separator);
return *this;
}
Path Path::join(const Path &x) const {
Path temp(*this);
temp.joinInPlace(x);
return temp;
}
Path Path::join(const String &str, char separator) const {
return join(str.c_str(), separator);
}
Path Path::join(const char *str, char separator) const {
Path temp(*this);
temp.joinInPlace(str, DIR_SEPARATOR);
return temp;
}
} // End of namespace Common