strlist.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include "rar.hpp"
  2. StringList::StringList()
  3. {
  4. Reset();
  5. }
  6. void StringList::Reset()
  7. {
  8. Rewind();
  9. StringData.Reset();
  10. StringDataW.Reset();
  11. StringsCount=0;
  12. SavePosNumber=0;
  13. }
  14. void StringList::AddString(const char *Str)
  15. {
  16. AddString(Str,NULL);
  17. }
  18. void StringList::AddString(const wchar *Str)
  19. {
  20. AddString(NULL,Str);
  21. }
  22. void StringList::AddString(const char *Str,const wchar *StrW)
  23. {
  24. if (Str==NULL)
  25. Str="";
  26. if (StrW==NULL)
  27. StrW=L"";
  28. size_t PrevSize=StringData.Size();
  29. StringData.Add(strlen(Str)+1);
  30. strcpy(&StringData[PrevSize],Str);
  31. size_t PrevSizeW=StringDataW.Size();
  32. StringDataW.Add(wcslen(StrW)+1);
  33. wcscpy(&StringDataW[PrevSizeW],StrW);
  34. StringsCount++;
  35. }
  36. bool StringList::GetString(char *Str,size_t MaxLength)
  37. {
  38. return(GetString(Str,NULL,MaxLength));
  39. }
  40. bool StringList::GetString(wchar *Str,size_t MaxLength)
  41. {
  42. return(GetString(NULL,Str,MaxLength));
  43. }
  44. bool StringList::GetString(char *Str,wchar *StrW,size_t MaxLength)
  45. {
  46. char *StrPtr;
  47. wchar *StrPtrW;
  48. if (!GetString(&StrPtr,&StrPtrW))
  49. return(false);
  50. if (Str!=NULL)
  51. strncpy(Str,StrPtr,MaxLength);
  52. if (StrW!=NULL)
  53. wcsncpy(StrW,StrPtrW,MaxLength);
  54. return(true);
  55. }
  56. #ifndef SFX_MODULE
  57. bool StringList::GetString(char *Str,wchar *StrW,size_t MaxLength,int StringNum)
  58. {
  59. SavePosition();
  60. Rewind();
  61. bool RetCode=true;
  62. while (StringNum-- >=0)
  63. if (!GetString(Str,StrW,MaxLength))
  64. {
  65. RetCode=false;
  66. break;
  67. }
  68. RestorePosition();
  69. return(RetCode);
  70. }
  71. #endif
  72. char* StringList::GetString()
  73. {
  74. char *Str;
  75. GetString(&Str,NULL);
  76. return(Str);
  77. }
  78. wchar* StringList::GetStringW()
  79. {
  80. wchar *StrW;
  81. GetString(NULL,&StrW);
  82. return(StrW);
  83. }
  84. bool StringList::GetString(char **Str,wchar **StrW)
  85. {
  86. // First check would be enough, because both buffers grow synchronously,
  87. // but we check both for extra fail proof.
  88. if (CurPos>=StringData.Size() || CurPosW>=StringDataW.Size())
  89. {
  90. // No more strings left unprocessed.
  91. if (Str!=NULL)
  92. *Str=NULL;
  93. if (StrW!=NULL)
  94. *StrW=NULL;
  95. return(false);
  96. }
  97. // We move ASCII and Unicode buffer pointers synchronously.
  98. char *CurStr=&StringData[CurPos];
  99. CurPos+=strlen(CurStr)+1;
  100. if (Str!=NULL)
  101. *Str=CurStr;
  102. wchar *CurStrW=&StringDataW[CurPosW];
  103. CurPosW+=wcslen(CurStrW)+1;
  104. if (StrW!=NULL)
  105. *StrW=CurStrW;
  106. return(true);
  107. }
  108. void StringList::Rewind()
  109. {
  110. CurPos=0;
  111. CurPosW=0;
  112. }
  113. // Return the total size of usual and Unicode characters stored in the list.
  114. size_t StringList::GetCharCount()
  115. {
  116. return(StringData.Size()+StringDataW.Size());
  117. }
  118. #ifndef SFX_MODULE
  119. bool StringList::Search(char *Str,wchar *StrW,bool CaseSensitive)
  120. {
  121. SavePosition();
  122. Rewind();
  123. bool Found=false;
  124. char *CurStr;
  125. wchar *CurStrW;
  126. while (GetString(&CurStr,&CurStrW))
  127. {
  128. if (Str!=NULL && CurStr!=NULL)
  129. if ((CaseSensitive ? strcmp(Str,CurStr):stricomp(Str,CurStr))!=0)
  130. continue;
  131. if (StrW!=NULL && CurStrW!=NULL)
  132. if ((CaseSensitive ? wcscmp(StrW,CurStrW):wcsicomp(StrW,CurStrW))!=0)
  133. continue;
  134. Found=true;
  135. break;
  136. }
  137. RestorePosition();
  138. return(Found);
  139. }
  140. #endif
  141. #ifndef SFX_MODULE
  142. void StringList::SavePosition()
  143. {
  144. if (SavePosNumber<ASIZE(SaveCurPos))
  145. {
  146. SaveCurPos[SavePosNumber]=CurPos;
  147. SaveCurPosW[SavePosNumber]=CurPosW;
  148. SavePosNumber++;
  149. }
  150. }
  151. #endif
  152. #ifndef SFX_MODULE
  153. void StringList::RestorePosition()
  154. {
  155. if (SavePosNumber>0)
  156. {
  157. SavePosNumber--;
  158. CurPos=SaveCurPos[SavePosNumber];
  159. CurPosW=SaveCurPosW[SavePosNumber];
  160. }
  161. }
  162. #endif