scummvm/common/path.cpp

265 lines
5.9 KiB
C++
Raw Normal View History

2021-08-01 23:30:21 +00:00
/* 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 3 of the License, or
* (at your option) any later version.
2021-08-01 23:30:21 +00:00
*
* 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, see <http://www.gnu.org/licenses/>.
2021-08-01 23:30:21 +00:00
*
*/
#include "common/path.h"
#include "common/tokenizer.h"
#include "common/punycode.h"
#include "common/hash-str.h"
2021-08-01 23:30:21 +00:00
const char DIR_SEPARATOR = '\x1f'; // unit separator
2021-08-01 23:30:21 +00:00
namespace Common {
Path::Path(const Path &path) {
_str = path._str;
2021-08-01 23:30:21 +00:00
}
Path::Path(const char *str, char separator) {
set(str, separator);
2021-08-01 23:30:21 +00:00
}
Path::Path(const String &str, char separator) {
if (separator == DIR_SEPARATOR)
_str = str;
else
set(str.c_str(), separator);
2021-08-01 23:30:21 +00:00
}
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;
}
Path Path::getParent() const {
if (_str.size() < 2)
return Path();
size_t separatorPos = _str.findLastOf(DIR_SEPARATOR, _str.size() - 2);
if (separatorPos == String::npos)
return Path();
return Path(_str.substr(0, separatorPos + 1), DIR_SEPARATOR);
}
Path Path::getLastComponent() const {
if (_str.size() < 2)
return *this;
size_t separatorPos = _str.findLastOf(DIR_SEPARATOR, _str.size() - 2);
if (separatorPos == String::npos)
return *this;
return Path(_str.substr(separatorPos + 1), DIR_SEPARATOR);
}
Path Path::appendComponent(const String &x) const {
if (x.empty())
return *this;
String str = _str;
if (!str.empty() && str.lastChar() != DIR_SEPARATOR)
str += DIR_SEPARATOR;
str += x;
return Path(str, DIR_SEPARATOR);
}
2021-08-01 23:30:21 +00:00
bool Path::operator==(const Path &x) const {
return _str == x._str;
2021-08-01 23:30:21 +00:00
}
bool Path::operator!=(const Path &x) const {
return _str != x._str;
2021-08-01 23:30:21 +00:00
}
bool Path::empty() const {
return _str.empty();
}
Path &Path::operator=(const Path &path) {
_str = path._str;
2021-08-01 23:30:21 +00:00
return *this;
}
Path &Path::operator=(const char *str) {
set(str);
2021-08-01 23:30:21 +00:00
return *this;
}
Path &Path::operator=(const String &str) {
set(str.c_str());
2021-08-01 23:30:21 +00:00
return *this;
}
void Path::set(const char *str, char separator) {
_str.clear();
appendInPlace(str, separator);
2021-08-01 23:30:21 +00:00
}
Path &Path::appendInPlace(const Path &x) {
_str += x._str;
2021-08-01 23:30:21 +00:00
return *this;
}
Path &Path::appendInPlace(const String &str, char separator) {
if (separator == DIR_SEPARATOR)
_str += str;
else
appendInPlace(str.c_str(), separator);
2021-08-01 23:30:21 +00:00
return *this;
}
Path &Path::appendInPlace(const char *str, char separator) {
for (; *str; str++) {
if (*str == separator)
_str += DIR_SEPARATOR;
else
_str += *str;
}
2021-08-01 23:30:21 +00:00
return *this;
}
Path Path::append(const Path &x) const {
Path temp(*this);
temp.appendInPlace(x);
return temp;
2021-08-01 23:30:21 +00:00
}
Path Path::append(const String &str, char separator) const {
return append(str.c_str(), separator);
2021-08-01 23:30:21 +00:00
}
Path Path::append(const char *str, char separator) const {
Path temp(*this);
temp.appendInPlace(str, separator);
2021-08-01 23:30:21 +00:00
return temp;
}
Path &Path::joinInPlace(const Path &x) {
if (x.empty())
return *this;
if (!_str.empty() && _str.lastChar() != DIR_SEPARATOR && x._str.firstChar() != DIR_SEPARATOR)
_str += DIR_SEPARATOR;
_str += x._str;
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);
2021-08-01 23:30:21 +00:00
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);
2021-08-01 23:30:21 +00:00
return temp;
}
Path Path::punycodeDecode() const {
StringTokenizer tok(_str, String(DIR_SEPARATOR));
String res;
while (!tok.empty()) {
res += punycode_decodefilename(tok.nextToken());
if (!tok.empty())
res += DIR_SEPARATOR;
}
return Path(res, DIR_SEPARATOR);
}
String Path::getIdentifierString() const {
StringTokenizer tok(_str, String(DIR_SEPARATOR));
String res;
while (!tok.empty()) {
String part = punycode_decodefilename(tok.nextToken());
for (uint i = 0; i < part.size(); i++)
if (part[i] == '/')
res += ':';
else
res += part[i];
if (!tok.empty())
res += DIR_SEPARATOR;
}
return res;
}
Path Path::punycodeEncode() const {
StringTokenizer tok(_str, String(DIR_SEPARATOR));
String res;
while (!tok.empty()) {
String part = tok.nextToken();
if (punycode_needEncode(part))
res += punycode_encodefilename(part);
else
res += part;
if (!tok.empty())
res += DIR_SEPARATOR;
}
return Path(res, DIR_SEPARATOR);
}
bool Path::matchPattern(const Path& pattern) const {
// Prevent wildcards from matching the directory separator.
const char wildcardExclusions[] = { DIR_SEPARATOR, '\0' };
return getIdentifierString().matchString(pattern.getIdentifierString(), true, wildcardExclusions);
}
bool Path::IgnoreCaseAndMac_EqualsTo::operator()(const Path& x, const Path& y) const {
return x.getIdentifierString().equalsIgnoreCase(y.getIdentifierString());
}
uint Path::IgnoreCaseAndMac_Hash::operator()(const Path& x) const {
return hashit_lower(x.getIdentifierString().c_str());
}
2021-08-01 23:30:21 +00:00
} // End of namespace Common