Here is my code, I use it to verify whether an input path is correct or not. I assume that the input path is encoded in UTF8. Is there any way to make it better?
#define MAX_PATH_LEN MAX_PATH
#define IN
#define OUT
typedef int Bool_T;
static
int GetCharsNumInPath( IN const char * path )
{
int cnt;
int i;
int n = strlen( path );
for( i = 0; i < n; i++ )
{
if( ( path[i] < 0x80 ) || ( path[i] > 0xbf ) )
cnt++;
}
return cnt;
}
Bool_T PathValid( IN const char * path )
{
if( path != NULL )
{
if( GetCharsNumInPath( IN path ) <= MAX_PATH_LEN )
{
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 '|':
return false;
// Can meet only between a local disk letter and full path
// for example D:\folder\file.txt
//
case ':':
{
if( i != 1 )
{
return false;
}else{
break;
}
}
// Space and point can not be the last character of a file or folder names
//
case ' ':
case '.':
{
if( ( i + 1 == n ) || ( path[i+1] == PATH_SEPERATOR_CHAR ) )
{
return false;
}else{
break;
}
}
// two backslashes can not go straight
//
case PATH_SEPERATOR_CHAR:
{
if( i > 0 && path[i - 1] == PATH_SEPERATOR_CHAR )
{
return false;
}else{
break;
}
}
}
}
return true;
}else{ // if( GetCharsNumInPath( IN path ) <= MAX_PATH_LEN )
LOG_ERROR( "PathValid FAILURE --> path is too long" );
return false;
}
}else{ // if( path != NULL )
// wrong argument
//
return false;
}
}