I remember seeing something like that in wxWidgets but I tried to locate it with no avail. So I rolled a function and decided to share.
If you can improve it, go ahead. Sample usage is shown below
Code: Select all
bool IsPathSubSet(const wxString& thePath, const wxString& strToTest) {
//test if C:/Folder/stefano is subpath of C:/
wxStringTokenizer tkzPath(thePath, wxFileName::GetPathSeparator());
wxStringTokenizer tkzTest(strToTest, wxFileName::GetPathSeparator());
wxString pathSlicer = thePath;
while(tkzPath.HasMoreTokens()) {
//when the path has no more portion to test and all previous paths matched it is a subpath
if(tkzTest.HasMoreTokens() && tkzTest.GetNextToken()==tkzPath.GetNextToken())
continue; //so far it is still looks like subpath, keep testing
else
return false;//subpath is supposed to have at least equal path components
}
return true;
}
Code: Select all
wxString path1 = /home/stefano/folder1/folder2/folder3
wxString path2 = /home/stefano/folder1/folder4/folder5
wxString path=/home/stefano
IsPathSubSet(path, path1); //true
IsPathSubSet(path1, path2);//false