add MakeValidFilename

This commit is contained in:
Glenn Maynard
2005-12-29 01:20:33 +00:00
parent 6177fe5884
commit 86572cc1f3
2 changed files with 39 additions and 0 deletions
+38
View File
@@ -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;
+1
View File
@@ -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;