views:

999

answers:

4

Given two file path strings with potentially different casing and slashes ('\' vs '/'), is there a quick way (that does not involve writing my own function) to normalize both paths to the same form, or at least to test them for equivalence?

I'm restricted to WinAPI and standard C++. All files are local.

A: 

To write the least amount of code, you could write a binary predicate that compares to chars as equal if they differ only in case or are both one of the slashes. Then use the stl algorithm equal to do the comparison, and pass in the predicate.

camh
+1  A: 

There are odd cases. For example, "c:\windows..\data\myfile.txt" is the same as "c:\data.\myfile.txt" and "c:\data\myfile.txt". You can have any number of "\.\" and "\..\" in there. You might look into the Windows API function GetFullPathName. It might do canonicalization for you.

Jim Mischel
+2  A: 

Depending on whether the paths could be relative, or contain "..", or junction points, or UNC paths this may be more difficult than you think. The best way might be to use the GetFileInformationByHandle() function as in this answer.

Edit: I agree with the comment by RBerteig that this may become hard to impossible to do if the paths are not pointing to a local file. Any comment on how to safely handle this case would be greatly appreciated.

mghie
As long as the two paths resolve to files on the same computer, then it looks like GetFileInformationByHandle() is the right answer. If they resolve to different computers, I don't see a guarantee, and I don't see a trivial way to get one, either. It isn't necessarily easy to test for this.
RBerteig
All files are local in my case, so this works.
Alex B
+7  A: 

May I suggest PathCanonicalize?

jeffamaphone
It doesn't look like that addresses either Junction points or UNC paths... but it does look useful to know about.
RBerteig
That's the method I was looking for. Not GetFullPathName.
Jim Mischel