other-licenses/7zstub/src/Windows/FileName.cpp

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     1 // Windows/FileName.cpp
     3 #include "StdAfx.h"
     5 #include "Windows/FileName.h"
     6 #include "Common/Wildcard.h"
     8 namespace NWindows {
     9 namespace NFile {
    10 namespace NName {
    12 static const wchar_t kDiskDelimiter = L':';
    14 /*
    15 static bool IsCharAPrefixDelimiter(wchar_t c)
    16   { return (c == kDirDelimiter || c == kDiskDelimiter); }
    17 */
    19 void NormalizeDirPathPrefix(CSysString &dirPath)
    20 {
    21   if (dirPath.IsEmpty())
    22     return;
    23   if (dirPath.ReverseFind(kDirDelimiter) != dirPath.Length() - 1)
    24     dirPath += kDirDelimiter;
    25 }
    27 #ifndef _UNICODE
    28 void NormalizeDirPathPrefix(UString &dirPath)
    29 {
    30   if (dirPath.IsEmpty())
    31     return;
    32   if (dirPath.ReverseFind(wchar_t(kDirDelimiter)) != dirPath.Length() - 1)
    33     dirPath += wchar_t(kDirDelimiter);
    34 }
    35 #endif
    37 namespace NPathType
    38 {
    39   EEnum GetPathType(const UString &path)
    40   {
    41     if (path.Length() <= 2)
    42       return kLocal;
    43     if (path[0] == kDirDelimiter && path[1] == kDirDelimiter)
    44       return kUNC;
    45     return kLocal;
    46   }
    47 }
    49 void CParsedPath::ParsePath(const UString &path)
    50 {
    51   int curPos = 0;
    52   switch (NPathType::GetPathType(path))
    53   {
    54     case NPathType::kLocal:
    55     {
    56       int posDiskDelimiter = path.Find(kDiskDelimiter);
    57       if(posDiskDelimiter >= 0)
    58       {
    59         curPos = posDiskDelimiter + 1;
    60         if (path.Length() > curPos)
    61           if(path[curPos] == kDirDelimiter)
    62             curPos++;
    63       }
    64       break;
    65     }
    66     case NPathType::kUNC:
    67     {
    68       int curPos = path.Find(kDirDelimiter, 2);
    69       if(curPos < 0)
    70         curPos = path.Length();
    71       else
    72         curPos++;
    73     }
    74   }
    75   Prefix = path.Left(curPos);
    76   SplitPathToParts(path.Mid(curPos), PathParts);
    77 }
    79 UString CParsedPath::MergePath() const
    80 {
    81   UString result = Prefix;
    82   for(int i = 0; i < PathParts.Size(); i++)
    83   {
    84     if (i != 0)
    85       result += kDirDelimiter;
    86     result += PathParts[i];
    87   }
    88   return result;
    89 }
    91 const wchar_t kExtensionDelimiter = L'.';
    93 void SplitNameToPureNameAndExtension(const UString &fullName, 
    94     UString &pureName, UString &extensionDelimiter, UString &extension)
    95 {
    96   int index = fullName.ReverseFind(kExtensionDelimiter);
    97   if (index < 0)
    98   {
    99     pureName = fullName;
   100     extensionDelimiter.Empty();
   101     extension.Empty();
   102   }
   103   else
   104   {
   105     pureName = fullName.Left(index);
   106     extensionDelimiter = kExtensionDelimiter;
   107     extension = fullName.Mid(index + 1);
   108   }
   109 }
   111 }}}

mercurial