strlist.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include "rar.hpp"
  2. StringList::StringList()
  3. {
  4. Reset();
  5. }
  6. void StringList::Reset()
  7. {
  8. Rewind();
  9. StringData.Reset();
  10. StringsCount=0;
  11. SavePosNumber=0;
  12. }
  13. void StringList::AddStringA(const char *Str)
  14. {
  15. Array<wchar> StrW(strlen(Str));
  16. CharToWide(Str,&StrW[0],StrW.Size());
  17. AddString(&StrW[0]);
  18. }
  19. void StringList::AddString(const wchar *Str)
  20. {
  21. if (Str==NULL)
  22. Str=L"";
  23. size_t PrevSize=StringData.Size();
  24. StringData.Add(wcslen(Str)+1);
  25. wcscpy(&StringData[PrevSize],Str);
  26. StringsCount++;
  27. }
  28. bool StringList::GetStringA(char *Str,size_t MaxLength)
  29. {
  30. Array<wchar> StrW(MaxLength);
  31. if (!GetString(&StrW[0],StrW.Size()))
  32. return false;
  33. WideToChar(&StrW[0],Str,MaxLength);
  34. return true;
  35. }
  36. bool StringList::GetString(wchar *Str,size_t MaxLength)
  37. {
  38. wchar *StrPtr;
  39. if (!GetString(&StrPtr))
  40. return false;
  41. wcsncpyz(Str,StrPtr,MaxLength);
  42. return true;
  43. }
  44. #ifndef SFX_MODULE
  45. bool StringList::GetString(wchar *Str,size_t MaxLength,int StringNum)
  46. {
  47. SavePosition();
  48. Rewind();
  49. bool RetCode=true;
  50. while (StringNum-- >=0)
  51. if (!GetString(Str,MaxLength))
  52. {
  53. RetCode=false;
  54. break;
  55. }
  56. RestorePosition();
  57. return RetCode;
  58. }
  59. #endif
  60. wchar* StringList::GetString()
  61. {
  62. wchar *Str;
  63. GetString(&Str);
  64. return Str;
  65. }
  66. bool StringList::GetString(wchar **Str)
  67. {
  68. if (CurPos>=StringData.Size()) // No more strings left unprocessed.
  69. {
  70. if (Str!=NULL)
  71. *Str=NULL;
  72. return false;
  73. }
  74. wchar *CurStr=&StringData[CurPos];
  75. CurPos+=wcslen(CurStr)+1;
  76. if (Str!=NULL)
  77. *Str=CurStr;
  78. return true;
  79. }
  80. void StringList::Rewind()
  81. {
  82. CurPos=0;
  83. }
  84. #ifndef SFX_MODULE
  85. bool StringList::Search(const wchar *Str,bool CaseSensitive)
  86. {
  87. SavePosition();
  88. Rewind();
  89. bool Found=false;
  90. wchar *CurStr;
  91. while (GetString(&CurStr))
  92. {
  93. if (Str!=NULL && CurStr!=NULL)
  94. if ((CaseSensitive ? wcscmp(Str,CurStr):wcsicomp(Str,CurStr))!=0)
  95. continue;
  96. Found=true;
  97. break;
  98. }
  99. RestorePosition();
  100. return Found;
  101. }
  102. #endif
  103. #ifndef SFX_MODULE
  104. void StringList::SavePosition()
  105. {
  106. if (SavePosNumber<ASIZE(SaveCurPos))
  107. {
  108. SaveCurPos[SavePosNumber]=CurPos;
  109. SavePosNumber++;
  110. }
  111. }
  112. #endif
  113. #ifndef SFX_MODULE
  114. void StringList::RestorePosition()
  115. {
  116. if (SavePosNumber>0)
  117. {
  118. SavePosNumber--;
  119. CurPos=SaveCurPos[SavePosNumber];
  120. }
  121. }
  122. #endif