This function checks if a file name is valid or not. I think it's not good enough for integration (not optimized, maybe I forgot about some checks), so need your comments.
static
int ReservedName( const char * name )
{
char * reservedFileNames[] = { "CON", "PRN", "AUX", "NUL",
"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
"LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9" };
char * reservedFileNamesWithDot[] = { "CON.", "PRN.", "AUX.", "NUL.",
"COM1.", "COM2.", "COM3.", "COM4.", "COM5.", "COM6.", "COM7.", "COM8.", "COM9.",
"LPT1.", "LPT2.", "LPT3.", "LPT4.", "LPT5.", "LPT6.", "LPT7.", "LPT8.", "LPT9." };
int i;
int n = sizeof( reservedFileNames ) / sizeof( reservedFileNames[0] );
for( i = 0; i < n; i++ )
{
if( stricmp( reservedFileNames[i], name ) == 0 )
{
return 1;
}else{
if( strnicmp( reservedFileNamesWithDot[i], name, strlen( reservedFileNamesWithDot[i] ) ) == 0 )
{
return 1;
}
}
}
return 0;
}
static
int GetCharsNumInPath( IN const char * path )
{
int i, cnt;
for( i = 0; path[i]; i++ )
{
if( ( path[i] < 0x80 ) || ( path[i] > 0xbf ) )
cnt++;
}
return cnt;
}
int ValidName( const char * name )
{
if( name != NULL )
{
if( GetCharsNumInPath( path ) <= MAX_PATH )
{
if( ! ReservedName( name ) )
{
int i;
int n = strlen( path );
for( i = 0; i < n; i++ )
{
switch( path[i] )
{
// ? " / < > * |
// these characters can not be used in file or folder names
//
case '<':
case '>':
case '/':
case '\\':
case '\"':
case '?':
case '*':
case '|':
return 0;
case ' ':
case '.':
{
if( ( i + 1 == n ) )
{
return 0;
}else{
continue;
}
}
}
}
return 1;
}else{ // if( ! ReservedName( name ) )
return 0;
}
}else{ // if( GetCharsNumInPath( IN path ) <= MAX_PATH_LEN )
return 0;
}
}else{ // if( name != NULL )
return 0;
}
}