1 // Archive/Common/ItemNameUtils.cpp
2
3 #include "StdAfx.h"
4
5 #include "ItemNameUtils.h"
6
7 namespace NArchive {
8 namespace NItemName {
9
10 static const wchar_t kOsPathSepar = WCHAR_PATH_SEPARATOR;
11
12 #if WCHAR_PATH_SEPARATOR != L'/'
13 static const wchar_t kUnixPathSepar = L'/';
14 #endif
15
ReplaceSlashes_OsToUnix(UString & name)16 void ReplaceSlashes_OsToUnix
17 #if WCHAR_PATH_SEPARATOR != L'/'
18 (UString &name)
19 {
20 name.Replace(kOsPathSepar, kUnixPathSepar);
21 }
22 #else
23 (UString &) {}
24 #endif
25
26
GetOsPath(const UString & name)27 UString GetOsPath(const UString &name)
28 {
29 #if WCHAR_PATH_SEPARATOR != L'/'
30 UString newName = name;
31 newName.Replace(kUnixPathSepar, kOsPathSepar);
32 return newName;
33 #else
34 return name;
35 #endif
36 }
37
38
GetOsPath_Remove_TailSlash(const UString & name)39 UString GetOsPath_Remove_TailSlash(const UString &name)
40 {
41 if (name.IsEmpty())
42 return UString();
43 UString newName = GetOsPath(name);
44 if (newName.Back() == kOsPathSepar)
45 newName.DeleteBack();
46 return newName;
47 }
48
49
ReplaceToOsSlashes_Remove_TailSlash(UString & name,bool useBackslashReplacement)50 void ReplaceToOsSlashes_Remove_TailSlash(UString &name, bool
51 #if WCHAR_PATH_SEPARATOR != L'/'
52 useBackslashReplacement
53 #endif
54 )
55 {
56 if (name.IsEmpty())
57 return;
58
59 #if WCHAR_PATH_SEPARATOR != L'/'
60 {
61 // name.Replace(kUnixPathSepar, kOsPathSepar);
62 const unsigned len = name.Len();
63 for (unsigned i = 0; i < len; i++)
64 {
65 wchar_t c = name[i];
66 if (c == L'/')
67 c = WCHAR_PATH_SEPARATOR;
68 else if (useBackslashReplacement && c == L'\\')
69 c = WCHAR_IN_FILE_NAME_BACKSLASH_REPLACEMENT; // WSL scheme
70 else
71 continue;
72 name.ReplaceOneCharAtPos(i, c);
73 }
74 }
75 #endif
76
77 if (name.Back() == kOsPathSepar)
78 name.DeleteBack();
79 }
80
81
NormalizeSlashes_in_FileName_for_OsPath(wchar_t * name,unsigned len)82 void NormalizeSlashes_in_FileName_for_OsPath(wchar_t *name, unsigned len)
83 {
84 for (unsigned i = 0; i < len; i++)
85 {
86 wchar_t c = name[i];
87 if (c == L'/')
88 c = L'_';
89 #if WCHAR_PATH_SEPARATOR != L'/'
90 else if (c == L'\\')
91 c = WCHAR_IN_FILE_NAME_BACKSLASH_REPLACEMENT; // WSL scheme
92 #endif
93 else
94 continue;
95 name[i] = c;
96 }
97 }
98
NormalizeSlashes_in_FileName_for_OsPath(UString & name)99 void NormalizeSlashes_in_FileName_for_OsPath(UString &name)
100 {
101 NormalizeSlashes_in_FileName_for_OsPath(name.GetBuf(), name.Len());
102 }
103
104
HasTailSlash(const AString & name,UINT codePage)105 bool HasTailSlash(const AString &name, UINT
106 #if defined(_WIN32) && !defined(UNDER_CE)
107 codePage
108 #endif
109 )
110 {
111 if (name.IsEmpty())
112 return false;
113 char c;
114 #if defined(_WIN32) && !defined(UNDER_CE)
115 if (codePage != CP_UTF8)
116 c = *CharPrevExA((WORD)codePage, name, name.Ptr(name.Len()), 0);
117 else
118 #endif
119 {
120 c = name.Back();
121 }
122 return (c == '/');
123 }
124
125
126 #ifndef _WIN32
WinPathToOsPath(const UString & name)127 UString WinPathToOsPath(const UString &name)
128 {
129 UString newName = name;
130 newName.Replace(L'\\', WCHAR_PATH_SEPARATOR);
131 return newName;
132 }
133 #endif
134
135 }}
136