-
August 17th, 2004, 04:18 AM
#1
Anyone know of a function to...
...return the difference between two absolute paths?
For my project, I need to pass two actual paths to a function so I can set the relative path of the second actual path passed.
For example, if I pass c:\folder1\ and c:\folder1\folder2\folder3, the function will return folder2\folder3 - as this is the difference between the two. This makes it easy to link to my second path relatively from my first path, if you see what I mean.
Thanks for any help,
Jim
-
August 17th, 2004, 04:32 AM
#2
I am not aware of such a function...so I would guess you would come up with your own one...
-
August 17th, 2004, 04:43 AM
#3
Okay Andreas, thanks.
If anyone has already written anything like this, it would be a real help if you could show me the function.
Thanks
Jim
-
August 17th, 2004, 05:08 AM
#4
It's actually quite simple with std::string.
Code:
#include <string>
using namespace std;
string PathDiff(const string &first, const string &second)
{
// Search if the second string contain the first string
if(second.find(first) == 0)
{
// Removing the occurence of the first string in second string.
return second.substr( first.length() );
}
return second;
}
-
August 17th, 2004, 05:19 AM
#5
Cheers Kheun, that's helpful.
What I need is a little more complicated than that though, because the two absolute paths will not always be on the same branch. For example, I might pass c:\folder\folder1, and c:\folder\folder2, in which case the function will need to return ..\folder2 ...etc.
It needs to be able to give the difference in any paths passed.
I am starting to write this myself now and will probably use what you have written as the template for this. I'm a little new to this though so it's not too easy for me.
Thanks
Jim
-
August 17th, 2004, 05:40 AM
#6
Okay, it's a little long-winded, but here is the function I have written to deal with this - if it's of use to anyone else. As I say, I'm a bit new so it's probably not written particularly well, and there may be something I've overlooked, but it seems to work
Cheers,
Jim
Code:
CString AbsolutePathDiff(CString path1, CString path2)
{
//ignore 0 as this will be the drive
int count=1;
int file1check=0, file2check=0;
CString path1buf;
CString path2buf;
CString newpath;
CString dirsback;
newpath.Empty();
dirsback.Empty();
//1. loop through both paths grabbing folders one-by-one
while(TRUE)
{
file1check=AfxExtractSubString(path1buf, path1, count, '\\');
file2check=AfxExtractSubString(path2buf, path2, count, '\\');
//no path left to grab so break out
if(file1check==0 && file2check==0)
{
break;
}
if(file1check==0)
{
newpath.Insert(newpath.GetLength()+1, path2buf);
newpath.Insert(newpath.GetLength()+1, "\\");
}
else if(file2check==0)
{
dirsback.Insert(0, "..\\");
}
else if(path1buf.CompareNoCase(path2buf)!=0)
{
dirsback.Insert(0, "..\\");
newpath.Insert(newpath.GetLength()+1, path2buf);
newpath.Insert(newpath.GetLength()+1, "\\");
}
count++;
}
newpath.Insert(0, dirsback);
return newpath;
}
-
August 17th, 2004, 07:00 AM
#7
Just some advice: I would rewrite your function to use std::string in place of CString. There's no need to lock yourself down to requiring MFC when it's not necessary.
-
August 17th, 2004, 07:23 AM
#8
CString is no longer MFC specific, and why would you want to lock yourself down to a slow std:string class? *joking*
...All good things were meant to be improved...
-
August 17th, 2004, 07:50 AM
#9
Originally Posted by carribus
CString is no longer MFC specific
Was that part of the joke?
-
August 17th, 2004, 08:00 AM
#10
Can't rewrite it 'cause I don't gots time!
-
August 17th, 2004, 11:29 AM
#11
Originally Posted by Jim1981
...return the difference between two absolute paths?
For my project, I need to pass two actual paths to a function so I can set the relative path of the second actual path passed.
For example, if I pass c:\folder1\ and c:\folder1\folder2\folder3, the function will return folder2\folder3 - as this is the difference between the two. This makes it easy to link to my second path relatively from my first path, if you see what I mean.
If you are programming for Windows paltforms, try the PathRelativePathTo() which is part of the SHLWAPI.dll (IE 5.0+). These are the shell APIs for IE, they are documented in MSDN, or see the shlwapi.h file in your include directory (VC++ installations).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|