Changeset View
Changeset View
Standalone View
Standalone View
source/lib/path.h
/* Copyright (C) 2020 Wildfire Games. | /* Copyright (C) 2021 Wildfire Games. | ||||
* | * | ||||
* Permission is hereby granted, free of charge, to any person obtaining | * Permission is hereby granted, free of charge, to any person obtaining | ||||
* a copy of this software and associated documentation files (the | * a copy of this software and associated documentation files (the | ||||
* "Software"), to deal in the Software without restriction, including | * "Software"), to deal in the Software without restriction, including | ||||
* without limitation the rights to use, copy, modify, merge, publish, | * without limitation the rights to use, copy, modify, merge, publish, | ||||
* distribute, sublicense, and/or sell copies of the Software, and to | * distribute, sublicense, and/or sell copies of the Software, and to | ||||
* permit persons to whom the Software is furnished to do so, subject to | * permit persons to whom the Software is furnished to do so, subject to | ||||
* the following conditions: | * the following conditions: | ||||
▲ Show 20 Lines • Show All 74 Lines • ▼ Show 20 Lines | public: | ||||
Path(const Path& p) | Path(const Path& p) | ||||
: path(p.path) | : path(p.path) | ||||
{ | { | ||||
DetectSeparator(); | DetectSeparator(); | ||||
} | } | ||||
Path(const char* p) | Path(const char* p) | ||||
: path((const unsigned char*)p, (const unsigned char*)p+strlen(p)) | : path(wstring_from_utf8(p)) | ||||
// interpret bytes as unsigned; makes no difference for ASCII, | |||||
// and ensures OsPath on Unix will only contain values 0 <= c < 0x100 | |||||
{ | { | ||||
DetectSeparator(); | DetectSeparator(); | ||||
} | } | ||||
Path(const wchar_t* p) | Path(const wchar_t* p) | ||||
: path(p, p+wcslen(p)) | : path(p, p+wcslen(p)) | ||||
{ | { | ||||
DetectSeparator(); | DetectSeparator(); | ||||
} | } | ||||
Path(const std::string& s) | Path(const std::string& s) | ||||
: path((const unsigned char*)s.c_str(), (const unsigned char*)s.c_str()+s.length()) | : path(wstring_from_utf8(s)) | ||||
{ | { | ||||
DetectSeparator(); | DetectSeparator(); | ||||
} | } | ||||
Path(const std::wstring& s) | Path(const std::wstring& s) | ||||
: path(s) | : path(s) | ||||
{ | { | ||||
DetectSeparator(); | DetectSeparator(); | ||||
Show All 19 Lines | public: | ||||
/** | /** | ||||
* Return a UTF-8 version of the path, in a human-readable but potentially | * Return a UTF-8 version of the path, in a human-readable but potentially | ||||
* lossy form. It is *not* safe to take this string and construct a new | * lossy form. It is *not* safe to take this string and construct a new | ||||
* Path object from it (it may fail for some non-ASCII paths) - it should | * Path object from it (it may fail for some non-ASCII paths) - it should | ||||
* only be used for displaying paths to users. | * only be used for displaying paths to users. | ||||
*/ | */ | ||||
std::string string8() const | std::string string8() const | ||||
{ | { | ||||
Status err; | // We don't support non-BMP characters so we can assume it's simply a wstring. | ||||
#if !OS_WIN | return utf8_from_wstring(path); | ||||
// On Unix, assume paths consisting of 8-bit charactes saved in this wide string. | |||||
std::string spath(path.begin(), path.end()); | |||||
// Return it if it's valid UTF-8 | |||||
wstring_from_utf8(spath, &err); | |||||
if(err == INFO::OK) | |||||
return spath; | |||||
// Otherwise assume ISO-8859-1 and let utf8_from_wstring treat each character as a Unicode code point. | |||||
#endif | |||||
// On Windows, paths are UTF-16 strings. We don't support non-BMP characters so we can assume it's simply a wstring. | |||||
return utf8_from_wstring(path, &err); | |||||
} | } | ||||
bool operator<(const Path& rhs) const | bool operator<(const Path& rhs) const | ||||
{ | { | ||||
return path < rhs.path; | return path < rhs.path; | ||||
} | } | ||||
bool operator==(const Path& rhs) const | bool operator==(const Path& rhs) const | ||||
▲ Show 20 Lines • Show All 160 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator