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