OSDN Git Service

Fix paths_GetLongPath to handle trailing . or ..
authorPerry Rapp <elsapo@users.sourceforge.net>
Sun, 24 Aug 2003 23:19:09 +0000 (23:19 +0000)
committerPerry Rapp <elsapo@users.sourceforge.net>
Sun, 24 Aug 2003 23:19:09 +0000 (23:19 +0000)
Src/paths.cpp
Src/readme.txt

index f0c4bd3..cd7025e 100644 (file)
@@ -114,8 +114,8 @@ CString paths_GetLongPath(const CString & sPath, DIRSLASH_TYPE dst)
 {
        int len = sPath.GetLength();
        // ensure it is not a root drive or a UNC path
-       if (len < 2) return sPath;
-       if (sPath[0]=='\\' && sPath[1]=='\\') return sPath;
+       if (len < 1) return sPath;
+       if (sPath[0]=='\\' && len>1 && sPath[1]=='\\') return sPath;
 
        // Now get a working buffer and walk down each directory
        CString sBuffer=sPath; // original path
@@ -129,7 +129,7 @@ CString paths_GetLongPath(const CString & sPath, DIRSLASH_TYPE dst)
                sTemp += paths_GetCurrentDriveUpper();
                sBuffer = sTemp + ':' + sBuffer;
        }
-       else if (sBuffer[1] == ':')
+       else if (len>1 && sBuffer[1] == ':')
        {
                if (!_istalpha(sBuffer[0]))
                        return sPath; // not a valid drive, give up
@@ -175,36 +175,36 @@ CString paths_GetLongPath(const CString & sPath, DIRSLASH_TYPE dst)
        while (ptr)
        {
                end = _tcschr(ptr, '\\');
-               if (!end)
+               // zero-terminate current component
+               // (if we're at end, its already zero-terminated)
+               if (end)
+                       *end = 0;
+               sTemp = sLong + '\\' + ptr;
+               // special handling for . and .. components
+               if (0 == _tcscmp(ptr, _T(".")))
                {
-                       // on last component (probably filename)
-                       sTemp = sLong + '\\' + ptr;
-                       ptr = 0;
+                       // advance to next component (or set ptr==0 to flag end)
+                       ptr = (end ? end+1 : 0);
+                       continue;
                }
-               else
+               if (0 == _tcscmp(ptr, _T("..")))
                {
-                       *end = 0;
-                       sTemp = sLong + _T("\\") + ptr;
-                       if (0 == _tcscmp(ptr, _T(".")))
+                       // back up one component
+                       for (int i = sLong.GetLength()-1; i>=0 && sLong[i]!='\\' && sLong[i]!=':'; --i)
+                               ;
+                       if (i==-1)
                        {
-                               ptr = &end[1];
-                               continue;
+                               return _T("");
                        }
-                       if (0 == _tcscmp(ptr, _T("..")))
-                       {
-                               // back up one component
-                               for (int i = sLong.GetLength()-1; i>=0 && sLong[i]!='\\' && sLong[i]!=':'; --i)
-                                       ;
-                               if (i==-1)
-                               {
-                                       return _T("");
-                               }
-                               sLong = sLong.Left(i);
-                               ptr = &end[1];
-                               continue;
-                       }
-                       ptr = &end[1];
+                       sLong = sLong.Left(i);
+                       // advance to next component (or set ptr==0 to flag end)
+                       ptr = (end ? end+1 : 0);
+                       continue;
                }
+
+               // advance to next component (or set ptr==0 to flag end)
+               ptr = (end ? end+1 : 0);
+
                // (Couldn't get info for just the directory from CFindFile)
                WIN32_FIND_DATA ffd;
                HANDLE h = FindFirstFile(sTemp, &ffd);
index 0979255..6cb2f6d 100644 (file)
@@ -1,3 +1,7 @@
+2003-08-24 Perry
+ Fix paths_GetLongPath to handle trailing . or ..
+  WinMerge: paths.cpp
+
 2003-08-24 Kimmo
  PATCH: [ 792668 ] Move file load code to CMergeDoc
   WinMerge: MainFrm.cpp MergeDoc.h MergeDoc.cpp