1 // ExtractingFilePath.cpp
4 #include "ExtractingFilePath.h"
6 static UString ReplaceIncorrectChars(const UString &s)
10 for (int i = 0; i < s.Length(); i++)
13 if (c < 0x20 || c == '*' || c == '?' || c == '<' || c == '>' || c == '|' || c == ':' || c == '"')
24 static const wchar_t *g_ReservedNames[] =
26 L"CON", L"PRN", L"AUX", L"NUL"
29 static bool CheckTail(const UString &name, int len)
31 int dotPos = name.Find(L'.');
33 dotPos = name.Length();
34 UString s = name.Left(dotPos);
36 return (s.Length() != len);
39 static bool CheckNameNum(const UString &name, const wchar_t *reservedName)
41 int len = MyStringLen(reservedName);
42 if (name.Length() <= len)
44 if (name.Left(len).CompareNoCase(reservedName) != 0)
46 wchar_t c = name[len];
47 if (c < L'0' || c > L'9')
49 return CheckTail(name, len + 1);
52 static bool IsSupportedName(const UString &name)
54 for (int i = 0; i < sizeof(g_ReservedNames) / sizeof(g_ReservedNames[0]); i++)
56 const wchar_t *reservedName = g_ReservedNames[i];
57 int len = MyStringLen(reservedName);
58 if (name.Length() < len)
60 if (name.Left(len).CompareNoCase(reservedName) != 0)
62 if (!CheckTail(name, len))
65 if (!CheckNameNum(name, L"COM"))
67 return CheckNameNum(name, L"LPT");
71 static UString GetCorrectFileName(const UString &path)
73 if (path == L".." || path == L".")
75 return ReplaceIncorrectChars(path);
78 void MakeCorrectPath(UStringVector &pathParts)
80 for (int i = 0; i < pathParts.Size();)
82 UString &s = pathParts[i];
83 s = GetCorrectFileName(s);
89 if (!IsSupportedName(s))
90 s = (UString)L"_" + s;