From 322f1492b2252c76681ee074e448b00581105a8a Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sun, 21 Feb 2021 03:45:40 +1000 Subject: [PATCH] FileSystem: Add SanitizeFilename() overload for std::string --- src/common/file_system.cpp | 12 +++++++++++- src/common/file_system.h | 1 + 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/common/file_system.cpp b/src/common/file_system.cpp index 6dd6717b8..e136cb411 100644 --- a/src/common/file_system.cpp +++ b/src/common/file_system.cpp @@ -169,7 +169,7 @@ void CanonicalizePath(std::string& path, bool OSPath /*= true*/) static inline bool FileSystemCharacterIsSane(char c, bool StripSlashes) { if (!(c >= 'a' && c <= 'z') && !(c >= 'A' && c <= 'Z') && !(c >= '0' && c <= '9') && c != ' ' && c != ' ' && - c != '_' && c != '-') + c != '_' && c != '-' && c != '.') { if (!StripSlashes && (c == '/' || c == '\\')) return true; @@ -239,6 +239,16 @@ void SanitizeFileName(String& Destination, bool StripSlashes /* = true */) return SanitizeFileName(Destination, Destination, StripSlashes); } +void SanitizeFileName(std::string& Destination, bool StripSlashes /* = true*/) +{ + const std::size_t len = Destination.length(); + for (std::size_t i = 0; i < len; i++) + { + if (!FileSystemCharacterIsSane(Destination[i], StripSlashes)) + Destination[i] = '_'; + } +} + bool IsAbsolutePath(const std::string_view& path) { #ifdef WIN32 diff --git a/src/common/file_system.h b/src/common/file_system.h index d2944797c..e3622b6c7 100644 --- a/src/common/file_system.h +++ b/src/common/file_system.h @@ -138,6 +138,7 @@ String BuildPathRelativeToFile(const char* CurrentFileName, const char* NewFileN void SanitizeFileName(char* Destination, u32 cbDestination, const char* FileName, bool StripSlashes = true); void SanitizeFileName(String& Destination, const char* FileName, bool StripSlashes = true); void SanitizeFileName(String& Destination, bool StripSlashes = true); +void SanitizeFileName(std::string& Destination, bool StripSlashes = true); /// Returns true if the specified path is an absolute path (C:\Path on Windows or /path on Unix). bool IsAbsolutePath(const std::string_view& path);