From 86572cc1f3aef6c21ba8cb960c8caa20e8ef8b25 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Thu, 29 Dec 2005 01:20:33 +0000 Subject: [PATCH] add MakeValidFilename --- stepmania/src/RageUtil.cpp | 38 ++++++++++++++++++++++++++++++++++++++ stepmania/src/RageUtil.h | 1 + 2 files changed, 39 insertions(+) diff --git a/stepmania/src/RageUtil.cpp b/stepmania/src/RageUtil.cpp index 5e85205188..010fd6fdd6 100644 --- a/stepmania/src/RageUtil.cpp +++ b/stepmania/src/RageUtil.cpp @@ -705,6 +705,44 @@ RString GetFileNameWithoutExtension( const RString &sPath ) return sFName; } +void MakeValidFilename( CString &sName ) +{ + wstring wsName = RStringToWstring( sName ); + wstring wsInvalid = L"/\\:*?\"<>|"; + for( unsigned i = 0; i < wsName.size(); ++i ) + { + wchar_t w = wsName[i]; + if( w >= 32 && + w < 126 && + wsInvalid.find_first_of(w) == wsInvalid.npos ) + continue; + + if( w == L'"' ) + { + wsName[i] = L'\''; + continue; + } + + /* + * We could replace with closest matches in ASCII: convert the character to UTF-8 + * NFD (decomposed) (maybe NFKD?), and see if the first character is ASCII. + * + * This is useless for non-Western languages, since we'll replace the whole filename. + */ + wsName[i] = '_'; + } + + sName = WStringToRString( wsName ); + if( Basename(sName).size() > 128 ) + { + /* The filename is too long. Truncate it, but leave the extension alone. */ + RString sExt = GetExtension( sName ); + SetExtension( sName, "" ); + sName.erase( 128 ); + SetExtension( sName, sExt ); + } +} + int g_argc = 0; char **g_argv = NULL; diff --git a/stepmania/src/RageUtil.h b/stepmania/src/RageUtil.h index 711c13e5fd..fa750f17a6 100644 --- a/stepmania/src/RageUtil.h +++ b/stepmania/src/RageUtil.h @@ -264,6 +264,7 @@ void splitpath( const RString &Path, RString &Dir, RString &Filename, RString &E RString SetExtension( const RString &path, const RString &ext ); RString GetExtension( const RString &sPath ); RString GetFileNameWithoutExtension( const RString &sPath ); +void MakeValidFilename( CString &sName ); typedef int longchar; extern const wchar_t INVALID_CHAR;