ListFileUtils.cpp 1.4 KB

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