Imported Upstream version 9.20
[platform/upstream/7zip.git] / CPP / Common / ListFileUtils.cpp
1 // Common/ListFileUtils.cpp\r
2 \r
3 #include "StdAfx.h"\r
4 \r
5 #include "MyWindows.h"\r
6 #include "../Windows/FileIO.h"\r
7 \r
8 #include "ListFileUtils.h"\r
9 #include "StringConvert.h"\r
10 #include "UTFConvert.h"\r
11 \r
12 static const char kQuoteChar     = '\"';\r
13 static void RemoveQuote(UString &s)\r
14 {\r
15   if (s.Length() >= 2)\r
16     if (s[0] == kQuoteChar && s[s.Length() - 1] == kQuoteChar)\r
17       s = s.Mid(1, s.Length() - 2);\r
18 }\r
19 \r
20 bool ReadNamesFromListFile(LPCWSTR fileName, UStringVector &resultStrings, UINT codePage)\r
21 {\r
22   NWindows::NFile::NIO::CInFile file;\r
23   if (!file.Open(fileName))\r
24     return false;\r
25   UInt64 length;\r
26   if (!file.GetLength(length))\r
27     return false;\r
28   if (length > ((UInt32)1 << 31))\r
29     return false;\r
30   AString s;\r
31   char *p = s.GetBuffer((int)length + 1);\r
32   UInt32 processed;\r
33   if (!file.Read(p, (UInt32)length, processed))\r
34     return false;\r
35   p[(UInt32)length] = 0;\r
36   s.ReleaseBuffer();\r
37   file.Close();\r
38 \r
39   UString u;\r
40   #ifdef CP_UTF8\r
41   if (codePage == CP_UTF8)\r
42   {\r
43     if (!ConvertUTF8ToUnicode(s, u))\r
44       return false;\r
45   }\r
46   else\r
47   #endif\r
48     u = MultiByteToUnicodeString(s, codePage);\r
49   if (!u.IsEmpty())\r
50   {\r
51     if (u[0] == 0xFEFF)\r
52       u.Delete(0);\r
53   }\r
54 \r
55   UString t;\r
56   for (int i = 0; i < u.Length(); i++)\r
57   {\r
58     wchar_t c = u[i];\r
59     if (c == L'\n' || c == 0xD)\r
60     {\r
61       t.Trim();\r
62       RemoveQuote(t);\r
63       if (!t.IsEmpty())\r
64         resultStrings.Add(t);\r
65       t.Empty();\r
66     }\r
67     else\r
68       t += c;\r
69   }\r
70   t.Trim();\r
71   RemoveQuote(t);\r
72   if (!t.IsEmpty())\r
73     resultStrings.Add(t);\r
74   return true;\r
75 }\r