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