C ++ common code 001 IsValidFileName VC/MFC determine a valid file name, isvalidfilenamemfc
Zookeeper
/*
File Name check \ n without extension requirements
1 is 0 \ n
2. The directory is incorrect. \ n
3. The file name contains invalid characters \ n
4. If the extension is not *, check whether the extension complies with \ n.
*/
BOOL CCommonFunc: S_IsValidFileName (CString & strP, CString & strExt)
{
// LN add 2005/05/18 begin
BOOL ret;
// Delete unintentional characters at the beginning and end
StrP. TrimLeft ();
StrP. TrimRight ();
// Check length> 0
Int length = strP. GetLength ();
If (length <= 0)
{
Return FALSE;
}
// Check last \ or/position
Int pos = 0;
Int pos1 = strP. ReverseFind ('\\');
Int pos2 = strP. ReverseFind ('/');
// Get max pos
If (pos1> pos2)
{
Pos = pos1;
}
Else
{
Pos = pos2;
}
// Check pos
If (pos> = 0 & pos <length)
{
// Valid dir
Ret = S_IsDirectory (strP. Left (pos ));
If (ret = TRUE)
{
CString strT = strP. Right (length-pos-1 );
// Valid file name
If (strT.
FindOneOf (_ T ("\\/:,;*? \ "<> |") =-1
& StrT. GetLength ()> 0)
{
// Check fdb
If (strExt. CompareNoCase (_ T ("*") = 0)
{
Return TRUE;
}
If (strP. Right (strExt. GetLength ()).
CompareNoCase (strExt )! = 0)
{
StrP + = strExt;
}
Return TRUE;
}
}
Return FALSE;
}
Return TRUE;
// LN add 2005/05/18 end
}