1
0
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:
Gymnasiast
2022-01-05 15:00:16 +01:00
parent de867827a1
commit e4496c41bd
6 changed files with 29 additions and 42 deletions

View File

@@ -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;
};

View File

@@ -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();

View File

@@ -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()
{

View File

@@ -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];

View File

@@ -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);

View File

@@ -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
}