Changeset View
Changeset View
Standalone View
Standalone View
source/lib/path.h
/* Copyright (c) 2013 Wildfire Games | /* Copyright (c) 2017 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 120 Lines • ▼ Show 20 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 | ||||
{ | { | ||||
// TODO: On Unixes, this will only be correct for ASCII or ISO-8859-1 | Status err; | ||||
// encoded paths; we should probably assume UTF-8 encoding by default | #if !OS_WIN | ||||
// (but take care to handle non-valid-UTF-8 paths safely). | // 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; | |||||
return utf8_from_wstring(path); | // 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 162 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator