Page 1 of 2

Check if Dir is Subset of a path

Posted: Tue Feb 26, 2013 9:39 am
by evstevemd
I wanted to know if path1 let say /home/stefano/Documents/SomeFolder is a subset of path /home/stefano
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

Re: Check if Dir is Subset of a path

Posted: Wed May 01, 2013 4:54 pm
by eranon
Interesting, but what if your path to test contains a ".." (for example, coming from a previous wxFileName::MakeRelativePath). So, maybe you should simply : 1) uniformize the path separator in both passed paths, 2) check if second path is a substring at begin of the first one, 3) count the number of ".." in the second path, then remove as many levels as required to see if it doesn't go above the first path. However, thanks for sharing.

Re: Check if Dir is Subset of a path

Posted: Wed May 01, 2013 4:59 pm
by evstevemd
eranon wrote:Interesting, but what if your path to test contains a ".." (for example, coming from a previous wxFileName::MakeRelativePath). So, maybe you should simply : 1) uniformize the path separator in both passed paths, 2) check if second path is a substring at begin of the first one, 3) count the number of ".." in the second path, then remove as many levels as required to see if it doesn't go above the first path. However, thanks for sharing.
Its free you can post the better version :p

Re: Check if Dir is Subset of a path

Posted: Wed May 01, 2013 5:08 pm
by eranon
If I've reached your post its because I wonder if there was something existing in wxWidgets, evstevemd. So, I've not any code to give you right now and I'll write it only if nothing already exist. I've searched a little bit in the meanwhile and found this thread : https://groups.google.com/forum/?fromgr ... -OWJKSPEUk, where it's said by Vadim Zeitlin that the best way would be to begin from MakeRelativeTo(), then check for ".." at begin of returned path. So, all the way you take, you have to check for "..".

Re: Check if Dir is Subset of a path

Posted: Wed May 01, 2013 5:12 pm
by evstevemd
eranon wrote:If I've reached your post its because I wonder if there was something existing in wxWidgets, evstevemd. So, I've not any code to give you right now and I'll write it only if nothing already exist. I've searched a little bit in the meanwhile and found this thread : https://groups.google.com/forum/?fromgr ... -OWJKSPEUk, where it's said by Vadim Zeitlin that the best way would be to begin from MakeRelativeTo(), then check for ".." at begin of returned path. So, all the way you take, you have to check for "..".
Ok I thought you was doing +ve criticism on my code. No?
What are you finding? I will read the link later and see if I can do modifications!

Re: Check if Dir is Subset of a path

Posted: Wed May 01, 2013 6:02 pm
by eranon
Criticism ? Hum, no, I don't come in a forum to do free criticism : enough of wars in the world ;) No, it was just a track, thinking out loud with you...

Well, I've not found already written code, so I've written my own (not tested in full):

Code: Select all

bool IsSubPath(wxString strRefPath, wxString strPathToTest)
{
    // Determine if path-to-test is a subpath of the ref-path
    wxFileName fn(strPathToTest);

    if (!fn.MakeRelativeTo(strRefPath))
        return false;

    if (fn.GetFullPath().Find("..") == 0)
        return false;

    return true;
}
Of course, if you or someone see something wrong (or forgotten), don't hesitate ^o^

--
EDIT : renamed this function as IsSubDir() in my project

Re: Check if Dir is Subset of a path

Posted: Thu May 02, 2013 9:37 am
by evstevemd
eranon wrote: EDIT : renamed this function as IsSubDir() in my project
So its working fine?

Re: Check if Dir is Subset of a path

Posted: Thu May 02, 2013 12:16 pm
by eranon
Yes, from what I've seen until now, it seems to be OK.

Re: Check if Dir is Subset of a path

Posted: Wed May 15, 2013 1:29 am
by samsam598
eranon wrote:Yes, from what I've seen until now, it seems to be OK.
It helps.Thanks.

In the source Find function

Code: Select all

if(fn.GetFullPath().Find("..")==0)
May I ask does it mak any difference if I write Find(wxT("..")) instead of Find("..")?

When I tried below:

Code: Select all

 if(IsSubPath("c:\\小说","c:\\小说\\第一章"))
        return true;
It returns false actually,but

Code: Select all

 if(IsSubPath(wxT("c:\\小说"),wxT("c:\\小说\\第一章")))
        return true;
It does return true.
I am using wx2.9.5 unicode built under windows.Were I correct,the manual said we can write strings as "string",no wxT("") needed any more.But why it makes difference here?

Thanks.

Re: Check if Dir is Subset of a path

Posted: Sat May 18, 2013 9:32 pm
by evstevemd
I would suggest wxT as it is safer and time tested.
The plain "" Should work and if not file a bug report!

Re: Check if Dir is Subset of a path

Posted: Mon Jul 08, 2013 3:01 pm
by evstevemd
The method does not work in some situations and gives true where it should be false, so I falled back to my method which works fantastic! =D>

Re: Check if Dir is Subset of a path

Posted: Mon Jul 08, 2013 8:53 pm
by eranon
Can you explain a case where it fails. Since I'm using it in my own project, I'm interested to know in detail, of course.

Re: Check if Dir is Subset of a path

Posted: Tue Jul 09, 2013 7:40 am
by evstevemd
eranon wrote:Can you explain a case where it fails. Since I'm using it in my own project, I'm interested to know in detail, of course.
I had paths that were alsmost the same and it would return true for all of them so I guess it is limited to the level it compares.
It caused a bug that was very hard to detect, thank God I did =D>

Re: Check if Dir is Subset of a path

Posted: Tue Jul 09, 2013 2:59 pm
by eranon
Since my function, inspired by a words from Vadim Zeitlin himself, relay on the working of wxFileName ::MakeRelativeTo(), if you found a bug, you should fill-in a bug report...

Re: Check if Dir is Subset of a path

Posted: Tue Jul 09, 2013 7:50 pm
by evstevemd
eranon wrote:Since my function, inspired by a words from Vadim Zeitlin himself, relay on the working of wxFileName ::MakeRelativeTo(), if you found a bug, you should fill-in a bug report...
I have no enough time to make tests. Since my function works I will not test it now. But you can try it such long paths that differ only last nam
soething like
/home/eranon/dev/cpp/tryit/project/proj1
/home/eranon/dev/cpp/tryit/project/proj2