stringvector.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include "./stringvector.h"
  2. #include <strsafe.h>
  3. StringVector::StringVector(size_t cchAllocate, int cchAllocateStep) : pointers(NULL), ptCount(0), ptAllocated(0)
  4. {
  5. if (cchAllocate)
  6. {
  7. cchBuffer = cchAllocate;
  8. buffer = (wchar_t*)HeapAlloc(GetProcessHeap(), NULL, cchBuffer * sizeof(wchar_t));
  9. }
  10. else
  11. {
  12. cchBuffer = 0;
  13. buffer = NULL;
  14. }
  15. tail = buffer;
  16. allocateStep = cchAllocateStep;
  17. }
  18. StringVector::~StringVector(void)
  19. {
  20. if (buffer)
  21. {
  22. HANDLE hHeap = GetProcessHeap();
  23. HeapFree(hHeap, NULL, buffer);
  24. cchBuffer = 0;
  25. tail = NULL;
  26. buffer = NULL;
  27. if (pointers)
  28. {
  29. HeapFree(hHeap, NULL, pointers);
  30. ptCount = 0;
  31. ptAllocated =0;
  32. pointers = NULL;
  33. }
  34. }
  35. }
  36. size_t StringVector::Count(void) { return ptCount; }
  37. size_t StringVector::GetCbAllocated(void){ return cchBuffer * sizeof(wchar_t); }
  38. size_t StringVector::GetCchAllocated(void) { return cchBuffer; }
  39. LPCWSTR StringVector::GetString(size_t index) { return (index >= 0 && index < ptCount) ? buffer + pointers[index].offset : NULL; }
  40. int StringVector::GetStringLength(size_t index) { return (index >= 0 && index < ptCount) ? pointers[index].length : -1; }
  41. void StringVector::Clear(void)
  42. {
  43. tail = buffer;
  44. ptCount = 0;
  45. }
  46. int StringVector::SetAllocateStep(int cchNewStep)
  47. {
  48. if (cchNewStep < 1) return allocateStep;
  49. int tmp = allocateStep;
  50. allocateStep = cchNewStep;
  51. return tmp;
  52. }
  53. size_t StringVector::Add(const wchar_t *entry, int cchLen)
  54. {
  55. if (!entry) return -1;
  56. if (cchLen < 0) cchLen = lstrlenW(entry);
  57. if (cchLen == 0) return -1;
  58. size_t cchNeed = (size_t)(tail - buffer) + cchLen + 2;
  59. if (cchBuffer < cchNeed)
  60. {
  61. while ( cchBuffer < cchNeed) cchBuffer += allocateStep;
  62. size_t offset_tail = (size_t)(tail - buffer);
  63. buffer = (wchar_t*)( (buffer) ? HeapReAlloc(GetProcessHeap(), NULL, buffer, cchBuffer*sizeof(wchar_t)) : HeapAlloc(GetProcessHeap(), NULL, cchBuffer*sizeof(wchar_t)));
  64. tail = buffer + offset_tail;
  65. }
  66. if (S_OK != StringCchCopyNW(tail, cchBuffer - (size_t)(tail - buffer), entry, cchLen)) return -1;
  67. if (ptCount == ptAllocated)
  68. {
  69. ptAllocated += 24;
  70. pointers = (HRECORD)( (pointers) ? HeapReAlloc(GetProcessHeap(), NULL, pointers, ptAllocated*sizeof(RECORD)) : HeapAlloc(GetProcessHeap(), NULL, ptAllocated*sizeof(RECORD)));
  71. if (pointers == NULL)
  72. {
  73. DWORD err = GetLastError();
  74. err += 1;
  75. }
  76. }
  77. pointers[ptCount].offset = tail - buffer;
  78. pointers[ptCount].length = cchLen;
  79. tail += cchLen + 1;
  80. ptCount++;
  81. return ptCount -1;
  82. }
  83. BOOL StringVector::Remove(size_t index)
  84. {
  85. if (index < 0 || index >= ptCount) return FALSE;
  86. ptCount--;
  87. for (size_t i = index; i < ptCount; i++) pointers[i] = pointers[i + 1];
  88. return TRUE;
  89. }
  90. void StringVector::TrimCount(size_t newCount)
  91. {
  92. if (newCount >= ptCount) return;
  93. if (newCount <= 0) { Clear(); return; }
  94. tail = buffer + pointers[newCount].offset;
  95. ptCount = newCount;
  96. }
  97. size_t StringVector::FindString(LCID lcid, LPCWSTR string, int cchLen, BOOL igonreCase)
  98. {
  99. if (!string) return -1;
  100. if (cchLen < 0) cchLen = lstrlenW(string);
  101. if (cchLen == 0) return -1;
  102. for (size_t i = 0; i < ptCount; i ++)
  103. {
  104. if ((cchLen == pointers[i].length) &&
  105. ( string == (buffer + pointers[i].offset) || CSTR_EQUAL == CompareStringW(lcid, (igonreCase) ? NORM_IGNORECASE : NULL,
  106. buffer + pointers[i].offset,
  107. cchLen,
  108. string,
  109. cchLen)))
  110. return i;
  111. }
  112. return -1;
  113. }