Directory Format
I''m working on a win32 application and I was wondering if there was an API call to validate a directory. I don''t care if the directory exists or not, I just want to know if the format of the entered directory is valid.
I''ve looked in the MSDN library but I haven''t had much luck locating any API call to do this and right now I''m more interested in working on the program rather than writting a function to make sure the string is of valid format to be a directory.
Thanks for any help
--Andrew
Thanks for the reply however, I''ve seen that function before and that tells you if the path is valid only if the directory exists - I don''t care if it does or doesn''t. Does any one know of a function to do this?
thanks
--Andrew
thanks
--Andrew
Here, I wrote one. It may not be perfect, since I may have screwed up the ''allowed characters'' part (I found the information online, but I didn''t put much effort into insuring its correctness). It''s easy to make it handle unix-style directory characters (which Windows doesn''t mind as far as I know), by adding a check for ''/'' in all the places that check against ''\\''. It seems to work, but it isn''t heavily tested.
int IsValidWindowsPath(const char *Path) { const char DirChar = ''\\''; const char Allowed[] = { ''$'', ''%'', ''\'''', ''`'', ''-'', ''@'', ''{'', ''}'', ''~'', ''!'', ''#'', ''('', '')'', ''&'', ''_'', ''^'', ''+'', '','', ''.'', ''='', ''['', '']'', '' '' }; unsigned int a; /* Handle Drive Dealies */ if(Path[1] == '':'') { if(tolower(Path[0]) < ''a'' || tolower(Path[0]) > ''z'' || Path[2] != DirChar) { return 0; } Path += 3; } while(*Path) { if(Path[0] == ''.'') { /* Handle Current Directory and Parent Directory References */ if(Path[1] == DirChar || Path[1] == ''\0'') Path += 3; else if(Path[1] == ''.'') if(Path[2] == DirChar || Path[2] == ''\0'') Path += 4; else return 0; else return 0; } else { while(Path[0] != DirChar && Path[1] != ''\0'') { if(tolower(Path[0]) < ''a'' || tolower(Path[0]) > ''z'') { for(a=0; a<sizeof(Allowed); ++a) { if(Path[0] == Allowed[a]) goto continue_2; } return 0; } continue_2:; ++Path; } if(Path != ''\0'') ++Path; } } return 1;}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement