CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Mar 2003
    Posts
    53

    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

  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    I am not aware of such a function...so I would guess you would come up with your own one...

  3. #3
    Join Date
    Mar 2003
    Posts
    53
    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

  4. #4
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    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;
    }

  5. #5
    Join Date
    Mar 2003
    Posts
    53
    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

  6. #6
    Join Date
    Mar 2003
    Posts
    53
    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;
    }

  7. #7
    Join Date
    Jan 2001
    Posts
    588
    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.

  8. #8
    Join Date
    Feb 2003
    Location
    South Africa
    Posts
    2
    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...

  9. #9
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Quote Originally Posted by carribus
    CString is no longer MFC specific
    Was that part of the joke?

  10. #10
    Join Date
    Mar 2003
    Posts
    53
    Can't rewrite it 'cause I don't gots time!

  11. #11
    Join Date
    Dec 2003
    Location
    Montreal
    Posts
    58
    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured