1 // ExtractingFilePath.cpp
\r
5 #include "../../../../C/Types.h"
\r
7 #include "Common/Wildcard.h"
\r
9 #include "ExtractingFilePath.h"
\r
11 static UString ReplaceIncorrectChars(const UString &s)
\r
15 for (int i = 0; i < s.Length(); i++)
\r
18 if (c < 0x20 || c == '*' || c == '?' || c == '<' || c == '>' || c == '|' || c == ':' || c == '"')
\r
23 while (!res.IsEmpty() && res[res.Length() - 1] == '.')
\r
24 res.Delete(res.Length() - 1);
\r
32 static const wchar_t *g_ReservedNames[] =
\r
34 L"CON", L"PRN", L"AUX", L"NUL"
\r
37 static bool CheckTail(const UString &name, int len)
\r
39 int dotPos = name.Find(L'.');
\r
41 dotPos = name.Length();
\r
42 UString s = name.Left(dotPos);
\r
44 return (s.Length() != len);
\r
47 static bool CheckNameNum(const UString &name, const wchar_t *reservedName)
\r
49 int len = MyStringLen(reservedName);
\r
50 if (name.Length() <= len)
\r
52 if (name.Left(len).CompareNoCase(reservedName) != 0)
\r
54 wchar_t c = name[len];
\r
55 if (c < L'0' || c > L'9')
\r
57 return CheckTail(name, len + 1);
\r
60 static bool IsSupportedName(const UString &name)
\r
62 for (int i = 0; i < sizeof(g_ReservedNames) / sizeof(g_ReservedNames[0]); i++)
\r
64 const wchar_t *reservedName = g_ReservedNames[i];
\r
65 int len = MyStringLen(reservedName);
\r
66 if (name.Length() < len)
\r
68 if (name.Left(len).CompareNoCase(reservedName) != 0)
\r
70 if (!CheckTail(name, len))
\r
73 if (!CheckNameNum(name, L"COM"))
\r
75 return CheckNameNum(name, L"LPT");
\r
79 static UString GetCorrectFileName(const UString &path)
\r
81 if (path == L".." || path == L".")
\r
83 return ReplaceIncorrectChars(path);
\r
86 void MakeCorrectPath(UStringVector &pathParts)
\r
88 for (int i = 0; i < pathParts.Size();)
\r
90 UString &s = pathParts[i];
\r
91 s = GetCorrectFileName(s);
\r
93 pathParts.Delete(i);
\r
97 if (!IsSupportedName(s))
\r
98 s = (UString)L"_" + s;
\r
105 UString MakePathNameFromParts(const UStringVector &parts)
\r
108 for (int i = 0; i < parts.Size(); i++)
\r
111 result += WCHAR_PATH_SEPARATOR;
\r
112 result += parts[i];
\r
117 UString GetCorrectFsPath(const UString &path)
\r
119 UString res = GetCorrectFileName(path);
\r
121 if (!IsSupportedName(res))
\r
122 res = (UString)L"_" + res;
\r
127 UString GetCorrectFullFsPath(const UString &path)
\r
129 UStringVector parts;
\r
130 SplitPathToParts(path, parts);
\r
131 for (int i = 0; i < parts.Size(); i++)
\r
133 UString &s = parts[i];
\r
135 while (!s.IsEmpty() && s[s.Length() - 1] == '.')
\r
136 s.Delete(s.Length() - 1);
\r
137 if (!IsSupportedName(s))
\r
138 s = (UString)L"_" + s;
\r
141 return MakePathNameFromParts(parts);
\r