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.

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

mercurial