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!
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
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;
}
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"]
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?
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"]
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?
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"]
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
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
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?
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"]
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?