C + + Common code 001 isvalidfilename VC/MFC determine valid file name

Source: Internet
Author: User



/*
File name check is not required for extension \ n
1 Length of 0\n
2 directory part not correct \ n
3 file name contains illegal characters \ n
4 If the extension is not *, check that the extension matches \ n
*/
BOOL Ccommonfunc::s_isvalidfilename (CString &strp, CString &strext)
{
LN Add 2005/05/18 Begin
BOOL ret;
Delete unintended 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
& Nbsp; 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
}

C + + Common code 001 isvalidfilename VC/MFC determine valid file name

Related Article

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.