FileSystem: Add SanitizeFilename() overload for std::string

This commit is contained in:
Connor McLaughlin 2021-02-21 03:45:40 +10:00
parent d97a107b62
commit 322f1492b2
2 changed files with 12 additions and 1 deletions

View File

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

View File

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