OLD | NEW |
| (Empty) |
1 // ExtractingFilePath.cpp | |
2 | |
3 #include "StdAfx.h" | |
4 #include "ExtractingFilePath.h" | |
5 | |
6 static UString ReplaceIncorrectChars(const UString &s) | |
7 { | |
8 #ifdef _WIN32 | |
9 UString res; | |
10 for (int i = 0; i < s.Length(); i++) | |
11 { | |
12 wchar_t c = s[i]; | |
13 if (c < 0x20 || c == '*' || c == '?' || c == '<' || c == '>' || c == '|' ||
c == ':' || c == '"') | |
14 c = '_'; | |
15 res += c; | |
16 } | |
17 res.TrimRight(); | |
18 return res; | |
19 #else | |
20 return s; | |
21 #endif | |
22 } | |
23 | |
24 #ifdef _WIN32 | |
25 static const wchar_t *g_ReservedNames[] = | |
26 { | |
27 L"CON", L"PRN", L"AUX", L"NUL" | |
28 }; | |
29 | |
30 static bool CheckTail(const UString &name, int len) | |
31 { | |
32 int dotPos = name.Find(L'.'); | |
33 if (dotPos < 0) | |
34 dotPos = name.Length(); | |
35 UString s = name.Left(dotPos); | |
36 s.TrimRight(); | |
37 return (s.Length() != len); | |
38 } | |
39 | |
40 static bool CheckNameNum(const UString &name, const wchar_t *reservedName) | |
41 { | |
42 int len = MyStringLen(reservedName); | |
43 if (name.Length() <= len) | |
44 return true; | |
45 if (name.Left(len).CompareNoCase(reservedName) != 0) | |
46 return true; | |
47 wchar_t c = name[len]; | |
48 if (c < L'0' || c > L'9') | |
49 return true; | |
50 return CheckTail(name, len + 1); | |
51 } | |
52 | |
53 static bool IsSupportedName(const UString &name) | |
54 { | |
55 for (int i = 0; i < sizeof(g_ReservedNames) / sizeof(g_ReservedNames[0]); i++) | |
56 { | |
57 const wchar_t *reservedName = g_ReservedNames[i]; | |
58 int len = MyStringLen(reservedName); | |
59 if (name.Length() < len) | |
60 continue; | |
61 if (name.Left(len).CompareNoCase(reservedName) != 0) | |
62 continue; | |
63 if (!CheckTail(name, len)) | |
64 return false; | |
65 } | |
66 if (!CheckNameNum(name, L"COM")) | |
67 return false; | |
68 return CheckNameNum(name, L"LPT"); | |
69 } | |
70 #endif | |
71 | |
72 static UString GetCorrectFileName(const UString &path) | |
73 { | |
74 if (path == L".." || path == L".") | |
75 return UString(); | |
76 return ReplaceIncorrectChars(path); | |
77 } | |
78 | |
79 void MakeCorrectPath(UStringVector &pathParts) | |
80 { | |
81 for (int i = 0; i < pathParts.Size();) | |
82 { | |
83 UString &s = pathParts[i]; | |
84 s = GetCorrectFileName(s); | |
85 if (s.IsEmpty()) | |
86 pathParts.Delete(i); | |
87 else | |
88 { | |
89 #ifdef _WIN32 | |
90 if (!IsSupportedName(s)) | |
91 s = (UString)L"_" + s; | |
92 #endif | |
93 i++; | |
94 } | |
95 } | |
96 } | |
97 | |
OLD | NEW |