C ++ common code 001 IsValidFileName VC/MFC determine a valid file name, isvalidfilenamemfc

Source: Internet
Author: User

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
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.