1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "common/path.h"
24 
25 namespace Common {
26 
Path(const Path & path)27 Path::Path(const Path &path) {
28 	_str = path.rawString();
29 }
30 
Path(const char * str,char separator)31 Path::Path(const char *str, char separator) {
32 	set(str, separator);
33 }
34 
Path(const String & str,char separator)35 Path::Path(const String &str, char separator) {
36 	set(str.c_str(), separator);
37 }
38 
toString(char separator) const39 String Path::toString(char separator) const {
40 	String res;
41 	for (const char *ptr = _str.c_str(); *ptr; ptr++) {
42 		if (*ptr == DIR_SEPARATOR)
43 			res += separator;
44 		else
45 			res += *ptr;
46 	}
47 	return res;
48 }
49 
operator ==(const Path & x) const50 bool Path::operator==(const Path &x) const {
51 	return _str == x.rawString();
52 }
53 
operator !=(const Path & x) const54 bool Path::operator!=(const Path &x) const {
55 	return _str != x.rawString();
56 }
57 
empty() const58 bool Path::empty() const {
59 	return _str.empty();
60 }
61 
operator =(const Path & path)62 Path &Path::operator=(const Path &path) {
63 	_str = path.rawString();
64 	return *this;
65 }
66 
operator =(const char * str)67 Path &Path::operator=(const char *str) {
68 	set(str);
69 	return *this;
70 }
71 
operator =(const String & str)72 Path &Path::operator=(const String &str) {
73 	set(str.c_str());
74 	return *this;
75 }
76 
set(const char * str,char separator)77 void Path::set(const char *str, char separator) {
78 	_str.clear();
79 	appendInPlace(str, separator);
80 }
81 
appendInPlace(const Path & x)82 Path &Path::appendInPlace(const Path &x) {
83 	_str += x.rawString();
84 	return *this;
85 }
86 
appendInPlace(const String & str,char separator)87 Path &Path::appendInPlace(const String &str, char separator) {
88 	appendInPlace(str.c_str(), separator);
89 	return *this;
90 }
91 
appendInPlace(const char * str,char separator)92 Path &Path::appendInPlace(const char *str, char separator) {
93 	for (; *str; str++) {
94 		if (*str == separator)
95 			_str += DIR_SEPARATOR;
96 		else
97 			_str += *str;
98 	}
99 	return *this;
100 }
101 
append(const Path & x) const102 Path Path::append(const Path &x) const {
103 	Path temp(*this);
104 	temp.appendInPlace(x);
105 	return temp;
106 }
107 
append(const String & str,char separator) const108 Path Path::append(const String &str, char separator) const {
109 	return append(str.c_str(), separator);
110 }
111 
append(const char * str,char separator) const112 Path Path::append(const char *str, char separator) const {
113 	Path temp(*this);
114 	temp.appendInPlace(str, separator);
115 	return temp;
116 }
117 
joinInPlace(const Path & x)118 Path &Path::joinInPlace(const Path &x) {
119 	if (x.empty())
120 		return *this;
121 
122 	if (!_str.empty() && _str.lastChar() != DIR_SEPARATOR && x.rawString().firstChar() != DIR_SEPARATOR)
123 		_str += DIR_SEPARATOR;
124 
125 	_str += x.rawString();
126 
127 	return *this;
128 }
129 
joinInPlace(const String & str,char separator)130 Path &Path::joinInPlace(const String &str, char separator) {
131 	return joinInPlace(str.c_str(), separator);
132 }
133 
joinInPlace(const char * str,char separator)134 Path &Path::joinInPlace(const char *str, char separator) {
135 	if (*str == '\0')
136 		return *this;
137 
138 	if (!_str.empty() && _str.lastChar() != DIR_SEPARATOR && *str != separator)
139 		_str += DIR_SEPARATOR;
140 
141 	appendInPlace(str, separator);
142 
143 	return *this;
144 }
145 
join(const Path & x) const146 Path Path::join(const Path &x) const {
147 	Path temp(*this);
148 	temp.joinInPlace(x);
149 	return temp;
150 }
151 
join(const String & str,char separator) const152 Path Path::join(const String &str, char separator) const {
153 	return join(str.c_str(), separator);
154 }
155 
join(const char * str,char separator) const156 Path Path::join(const char *str, char separator) const {
157 	Path temp(*this);
158 	temp.joinInPlace(str, DIR_SEPARATOR);
159 	return temp;
160 }
161 
162 } // End of namespace Common
163