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.
|
|
|
|
*
|
2021-12-26 17:47:58 +00:00
|
|
|
* 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
|
2021-12-26 17:47:58 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2021-08-01 23:30:21 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common/path.h"
|
2022-12-09 03:30:14 +00:00
|
|
|
#include "common/tokenizer.h"
|
|
|
|
#include "common/punycode.h"
|
2022-12-09 04:11:33 +00:00
|
|
|
#include "common/hash-str.h"
|
2021-08-01 23:30:21 +00:00
|
|
|
|
2022-12-09 05:19:23 +00:00
|
|
|
const char DIR_SEPARATOR = '\x1f'; // unit separator
|
|
|
|
|
2021-08-01 23:30:21 +00:00
|
|
|
namespace Common {
|
|
|
|
|
|
|
|
Path::Path(const Path &path) {
|
2022-12-09 20:03:10 +00:00
|
|
|
_str = path._str;
|
2021-08-01 23:30:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Path::Path(const char *str, char separator) {
|
2021-08-06 23:25:14 +00:00
|
|
|
set(str, separator);
|
2021-08-01 23:30:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Path::Path(const String &str, char separator) {
|
2022-04-21 17:59:13 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-04-21 17:59:13 +00:00
|
|
|
Path Path::getParent() const {
|
|
|
|
if (_str.size() < 2)
|
|
|
|
return Path();
|
|
|
|
size_t separatorPos = _str.findLastOf(DIR_SEPARATOR, _str.size() - 2);
|
2022-12-09 03:30:14 +00:00
|
|
|
if (separatorPos == String::npos)
|
2022-04-21 17:59:13 +00:00
|
|
|
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);
|
2022-12-09 03:30:14 +00:00
|
|
|
if (separatorPos == String::npos)
|
2022-04-21 17:59:13 +00:00
|
|
|
return *this;
|
|
|
|
return Path(_str.substr(separatorPos + 1), DIR_SEPARATOR);
|
|
|
|
}
|
|
|
|
|
2022-12-09 04:01:36 +00:00
|
|
|
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 {
|
2022-12-09 20:03:10 +00:00
|
|
|
return _str == x._str;
|
2021-08-01 23:30:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Path::operator!=(const Path &x) const {
|
2022-12-09 20:03:10 +00:00
|
|
|
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) {
|
2022-12-09 20:03:10 +00:00
|
|
|
_str = path._str;
|
2021-08-01 23:30:21 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Path &Path::operator=(const char *str) {
|
2021-08-06 23:25:14 +00:00
|
|
|
set(str);
|
2021-08-01 23:30:21 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Path &Path::operator=(const String &str) {
|
2021-08-06 23:25:14 +00:00
|
|
|
set(str.c_str());
|
2021-08-01 23:30:21 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-08-06 23:25:14 +00:00
|
|
|
void Path::set(const char *str, char separator) {
|
|
|
|
_str.clear();
|
|
|
|
appendInPlace(str, separator);
|
2021-08-01 23:30:21 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 23:25:14 +00:00
|
|
|
Path &Path::appendInPlace(const Path &x) {
|
2022-12-09 20:03:10 +00:00
|
|
|
_str += x._str;
|
2021-08-01 23:30:21 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-08-06 23:25:14 +00:00
|
|
|
Path &Path::appendInPlace(const String &str, char separator) {
|
2022-04-21 17:59:13 +00:00
|
|
|
if (separator == DIR_SEPARATOR)
|
|
|
|
_str += str;
|
|
|
|
else
|
|
|
|
appendInPlace(str.c_str(), separator);
|
2021-08-01 23:30:21 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-08-06 23:25:14 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-08-06 23:25:14 +00:00
|
|
|
Path Path::append(const Path &x) const {
|
|
|
|
Path temp(*this);
|
|
|
|
temp.appendInPlace(x);
|
|
|
|
return temp;
|
2021-08-01 23:30:21 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 23:25:14 +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
|
|
|
}
|
|
|
|
|
2021-08-06 23:25:14 +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;
|
|
|
|
}
|
|
|
|
|
2021-08-06 23:25:14 +00:00
|
|
|
Path &Path::joinInPlace(const Path &x) {
|
|
|
|
if (x.empty())
|
|
|
|
return *this;
|
|
|
|
|
2022-12-09 20:03:10 +00:00
|
|
|
if (!_str.empty() && _str.lastChar() != DIR_SEPARATOR && x._str.firstChar() != DIR_SEPARATOR)
|
2021-08-06 23:25:14 +00:00
|
|
|
_str += DIR_SEPARATOR;
|
|
|
|
|
2022-12-09 20:03:10 +00:00
|
|
|
_str += x._str;
|
2021-08-06 23:25:14 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-08-06 23:25:14 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-12-09 03:30:14 +00:00
|
|
|
Path Path::punycodeDecode() const {
|
2022-12-09 20:03:10 +00:00
|
|
|
StringTokenizer tok(_str, String(DIR_SEPARATOR));
|
2022-12-09 03:30:14 +00:00
|
|
|
String res;
|
|
|
|
|
|
|
|
while (!tok.empty()) {
|
|
|
|
res += punycode_decodefilename(tok.nextToken());
|
|
|
|
if (!tok.empty())
|
|
|
|
res += DIR_SEPARATOR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Path(res, DIR_SEPARATOR);
|
|
|
|
}
|
|
|
|
|
2022-12-09 05:14:03 +00:00
|
|
|
String Path::getIdentifierString() const {
|
2022-12-09 20:03:10 +00:00
|
|
|
StringTokenizer tok(_str, String(DIR_SEPARATOR));
|
2022-12-09 05:14:03 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-12-09 03:30:14 +00:00
|
|
|
Path Path::punycodeEncode() const {
|
2022-12-09 20:03:10 +00:00
|
|
|
StringTokenizer tok(_str, String(DIR_SEPARATOR));
|
2022-12-09 03:30:14 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-12-09 04:03:47 +00:00
|
|
|
bool Path::matchPattern(const Path& pattern) const {
|
|
|
|
// Prevent wildcards from matching the directory separator.
|
|
|
|
const char wildcardExclusions[] = { DIR_SEPARATOR, '\0' };
|
|
|
|
|
2022-12-09 05:14:03 +00:00
|
|
|
return getIdentifierString().matchString(pattern.getIdentifierString(), true, wildcardExclusions);
|
2022-12-09 04:03:47 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 04:11:33 +00:00
|
|
|
bool Path::IgnoreCaseAndMac_EqualsTo::operator()(const Path& x, const Path& y) const {
|
2022-12-09 05:14:03 +00:00
|
|
|
return x.getIdentifierString().equalsIgnoreCase(y.getIdentifierString());
|
2022-12-09 04:11:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint Path::IgnoreCaseAndMac_Hash::operator()(const Path& x) const {
|
2022-12-09 05:14:03 +00:00
|
|
|
return hashit_lower(x.getIdentifierString().c_str());
|
2022-12-09 04:11:33 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 23:30:21 +00:00
|
|
|
} // End of namespace Common
|