Check if Dir is Subset of a path Topic is solved

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2408
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Check if Dir is Subset of a path

Post 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
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
User avatar
eranon
Can't get richer than this
Can't get richer than this
Posts: 867
Joined: Sun May 13, 2012 11:42 pm
Location: France
Contact:

Re: Check if Dir is Subset of a path

Post 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.
[Ind. dev. - wxWidgets 3.0/3.1 under "Win 7 64-bit, TDM64-GCC" + "OS X 10.9, LLVM Clang"]
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2408
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Check if Dir is Subset of a path

Post 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
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
User avatar
eranon
Can't get richer than this
Can't get richer than this
Posts: 867
Joined: Sun May 13, 2012 11:42 pm
Location: France
Contact:

Re: Check if Dir is Subset of a path

Post 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 "..".
[Ind. dev. - wxWidgets 3.0/3.1 under "Win 7 64-bit, TDM64-GCC" + "OS X 10.9, LLVM Clang"]
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2408
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Check if Dir is Subset of a path

Post 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!
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
User avatar
eranon
Can't get richer than this
Can't get richer than this
Posts: 867
Joined: Sun May 13, 2012 11:42 pm
Location: France
Contact:

Re: Check if Dir is Subset of a path

Post 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
[Ind. dev. - wxWidgets 3.0/3.1 under "Win 7 64-bit, TDM64-GCC" + "OS X 10.9, LLVM Clang"]
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2408
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Check if Dir is Subset of a path

Post by evstevemd »

eranon wrote: EDIT : renamed this function as IsSubDir() in my project
So its working fine?
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
User avatar
eranon
Can't get richer than this
Can't get richer than this
Posts: 867
Joined: Sun May 13, 2012 11:42 pm
Location: France
Contact:

Re: Check if Dir is Subset of a path

Post by eranon »

Yes, from what I've seen until now, it seems to be OK.
[Ind. dev. - wxWidgets 3.0/3.1 under "Win 7 64-bit, TDM64-GCC" + "OS X 10.9, LLVM Clang"]
samsam598
Super wx Problem Solver
Super wx Problem Solver
Posts: 340
Joined: Mon Oct 06, 2008 12:55 pm

Re: Check if Dir is Subset of a path

Post 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.
Regards,
Sam
-------------------------------------------------------------------
Windows 10 64bit
VS Community 2019
msys2-mingw13.2.0 C::B character set: UTF-8/GBK(Chinese)
wxWidgets 3.3/3.2.4 Unicode Mono Static gcc static build
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2408
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Check if Dir is Subset of a path

Post by evstevemd »

I would suggest wxT as it is safer and time tested.
The plain "" Should work and if not file a bug report!
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2408
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Check if Dir is Subset of a path

Post 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>
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
User avatar
eranon
Can't get richer than this
Can't get richer than this
Posts: 867
Joined: Sun May 13, 2012 11:42 pm
Location: France
Contact:

Re: Check if Dir is Subset of a path

Post 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.
[Ind. dev. - wxWidgets 3.0/3.1 under "Win 7 64-bit, TDM64-GCC" + "OS X 10.9, LLVM Clang"]
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2408
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Check if Dir is Subset of a path

Post 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>
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
User avatar
eranon
Can't get richer than this
Can't get richer than this
Posts: 867
Joined: Sun May 13, 2012 11:42 pm
Location: France
Contact:

Re: Check if Dir is Subset of a path

Post 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...
[Ind. dev. - wxWidgets 3.0/3.1 under "Win 7 64-bit, TDM64-GCC" + "OS X 10.9, LLVM Clang"]
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2408
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Check if Dir is Subset of a path

Post 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
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
Post Reply