mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2025-12-23 15:52:55 +01:00
Upgrade platform_sanitise_filename()
This commit is contained in:
@@ -139,7 +139,7 @@ static std::optional<std::string> screenshot_get_next_path()
|
||||
|
||||
// Generate a path with a `tries` number
|
||||
auto pathComposer = [&screenshotDirectory, &name](int tries) {
|
||||
auto composedFilename = platform_sanitise_filename(
|
||||
auto composedFilename = Platform::SanitiseFilename(
|
||||
name + ((tries > 0) ? " ("s + std::to_string(tries) + ")" : ""s) + ".png");
|
||||
return screenshotDirectory + PATH_SEPARATOR + composedFilename;
|
||||
};
|
||||
|
||||
@@ -50,6 +50,8 @@ namespace Platform
|
||||
|
||||
std::string GetUsername();
|
||||
|
||||
std::string SanitiseFilename(std::string_view originalName);
|
||||
|
||||
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__FreeBSD__)
|
||||
std::string GetEnvironmentPath(const char* name);
|
||||
std::string GetHomePath();
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include "../config/Config.h"
|
||||
#include "../core/FileSystem.hpp"
|
||||
#include "../core/Path.hpp"
|
||||
#include "../core/String.hpp"
|
||||
#include "../drawing/Drawing.h"
|
||||
#include "../drawing/LightFX.h"
|
||||
#include "../localisation/Currency.h"
|
||||
@@ -106,6 +107,24 @@ namespace Platform
|
||||
std::string combinedPath = Path::ResolveCasing(Path::Combine(path, "Data", "g1.dat"));
|
||||
return Platform::FileExists(combinedPath);
|
||||
}
|
||||
|
||||
std::string SanitiseFilename(std::string_view originalName)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
static constexpr std::array prohibited = { '<', '>', '*', '\\', ':', '|', '?', '"', '/' };
|
||||
#else
|
||||
static constexpr std::array prohibited = { '/' };
|
||||
#endif
|
||||
auto sanitised = std::string(originalName);
|
||||
std::replace_if(
|
||||
sanitised.begin(), sanitised.end(),
|
||||
[](const std::string::value_type& ch) -> bool {
|
||||
return std::find(prohibited.begin(), prohibited.end(), ch) != prohibited.end();
|
||||
},
|
||||
'_');
|
||||
sanitised = String::Trim(sanitised);
|
||||
return sanitised;
|
||||
}
|
||||
} // namespace Platform
|
||||
|
||||
GamePalette gPalette;
|
||||
@@ -241,21 +260,6 @@ CurrencyType platform_get_currency_value(const char* currCode)
|
||||
return CurrencyType::Pounds;
|
||||
}
|
||||
|
||||
#ifndef _WIN32
|
||||
std::string platform_sanitise_filename(const std::string& path)
|
||||
{
|
||||
static constexpr std::array prohibited = { '/' };
|
||||
auto sanitised = path;
|
||||
std::replace_if(
|
||||
sanitised.begin(), sanitised.end(),
|
||||
[](const std::string::value_type& ch) -> bool {
|
||||
return std::find(prohibited.begin(), prohibited.end(), ch) != prohibited.end();
|
||||
},
|
||||
'_');
|
||||
return sanitised;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef __ANDROID__
|
||||
float platform_get_default_scale()
|
||||
{
|
||||
|
||||
@@ -185,20 +185,6 @@ std::string platform_get_rct2_steam_dir()
|
||||
return "Rollercoaster Tycoon 2";
|
||||
}
|
||||
|
||||
std::string platform_sanitise_filename(const std::string& path)
|
||||
{
|
||||
static constexpr std::array prohibited = { '<', '>', '*', '\\', ':', '|', '?', '"', '/' };
|
||||
auto sanitised = path;
|
||||
std::replace_if(
|
||||
sanitised.begin(), sanitised.end(),
|
||||
[](const std::string::value_type& ch) -> bool {
|
||||
return std::find(prohibited.begin(), prohibited.end(), ch) != prohibited.end();
|
||||
},
|
||||
'_');
|
||||
sanitised = String::Trim(sanitised);
|
||||
return sanitised;
|
||||
}
|
||||
|
||||
uint16_t platform_get_locale_language()
|
||||
{
|
||||
CHAR langCode[4];
|
||||
|
||||
@@ -115,7 +115,6 @@ bool platform_process_is_elevated();
|
||||
bool platform_get_steam_path(utf8* outPath, size_t outSize);
|
||||
std::string platform_get_rct1_steam_dir();
|
||||
std::string platform_get_rct2_steam_dir();
|
||||
std::string platform_sanitise_filename(const std::string&);
|
||||
|
||||
#ifndef NO_TTF
|
||||
bool platform_get_font_path(TTFFontDescriptor* font, utf8* buffer, size_t size);
|
||||
|
||||
@@ -8,21 +8,17 @@
|
||||
*****************************************************************************/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <openrct2/platform/platform.h>
|
||||
#include <openrct2/platform/Platform2.h>
|
||||
|
||||
TEST(platform, sanitise_filename)
|
||||
{
|
||||
ASSERT_EQ("normal-filename.png", Platform::SanitiseFilename("normal-filename.png"));
|
||||
ASSERT_EQ("utf🎱", Platform::SanitiseFilename("utf🎱"));
|
||||
ASSERT_EQ("forbidden_char", Platform::SanitiseFilename("forbidden/char"));
|
||||
ASSERT_EQ("non trimmed", Platform::SanitiseFilename(" non trimmed "));
|
||||
#ifndef _WIN32
|
||||
ASSERT_EQ("normal-filename.png", platform_sanitise_filename("normal-filename.png"));
|
||||
ASSERT_EQ("utf🎱", platform_sanitise_filename("utf🎱"));
|
||||
ASSERT_EQ("forbidden_char", platform_sanitise_filename("forbidden/char"));
|
||||
ASSERT_EQ("forbidden_\\:\"|?*chars", platform_sanitise_filename("forbidden/\\:\"|?*chars"));
|
||||
ASSERT_EQ(" non trimmed ", platform_sanitise_filename(" non trimmed "));
|
||||
ASSERT_EQ("forbidden_\\:\"|?*chars", Platform::SanitiseFilename("forbidden/\\:\"|?*chars"));
|
||||
#else
|
||||
ASSERT_EQ("normal-filename.png", platform_sanitise_filename("normal-filename.png"));
|
||||
ASSERT_EQ("utf🎱", platform_sanitise_filename("utf🎱"));
|
||||
ASSERT_EQ("forbidden_char", platform_sanitise_filename("forbidden/char"));
|
||||
ASSERT_EQ("forbidden_______chars", platform_sanitise_filename("forbidden/\\:\"|?*chars"));
|
||||
ASSERT_EQ("non trimmed", platform_sanitise_filename(" non trimmed "));
|
||||
ASSERT_EQ("forbidden_______chars", Platform::SanitiseFilename("forbidden/\\:\"|?*chars"));
|
||||
#endif
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user