MLString.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "main.h"
  2. #include ".\MLString.h"
  3. #include <strsafe.h>
  4. #define ALLOCATION_STEP 16
  5. void* MLString::heap = GetProcessHeap();
  6. MLString::MLString(void) : buffer(NULL), allocated(0), cchLen(0)
  7. {
  8. }
  9. MLString::MLString(const wchar_t* string) : buffer(NULL), allocated(0), cchLen(0)
  10. {
  11. if (string) Append(string, lstrlenW(string));
  12. }
  13. MLString::MLString(unsigned int cchBuffer) : buffer(NULL), allocated(0), cchLen(0)
  14. {
  15. Allocate(cchBuffer);
  16. }
  17. MLString::MLString(const MLString &copy) : buffer(NULL), allocated(0)
  18. {
  19. cchLen = copy.cchLen;
  20. Allocate(cchLen + 1);
  21. CopyMemory(buffer, copy.buffer, cchLen*sizeof(wchar_t));
  22. }
  23. MLString::~MLString(void)
  24. {
  25. if (buffer)
  26. {
  27. HeapFree(heap, NULL, buffer);
  28. allocated = 0;
  29. cchLen = 0;
  30. buffer = NULL;
  31. }
  32. }
  33. HRESULT MLString::Append(const wchar_t* string, unsigned int cchLength)
  34. {
  35. if (allocated <= cchLen + cchLength)
  36. {
  37. do { allocated += ALLOCATION_STEP; } while(allocated < cchLen + cchLength + 1);
  38. buffer = (wchar_t*) ((!buffer) ? HeapAlloc(heap, NULL, allocated*sizeof(wchar_t)) :
  39. HeapReAlloc(heap, NULL, buffer, allocated*sizeof(wchar_t)));
  40. }
  41. if (!buffer) return ERROR_OUTOFMEMORY;
  42. CopyMemory(buffer + cchLen, string, cchLength*sizeof(wchar_t));
  43. cchLen += cchLength;
  44. buffer[cchLen] = 0x0000;
  45. return S_OK;
  46. }
  47. HRESULT MLString::Set(const wchar_t* string, unsigned int cchLength)
  48. {
  49. cchLen = 0;
  50. return Append(string, cchLength);
  51. }
  52. HRESULT MLString::Allocate(unsigned int cchNewSize)
  53. {
  54. if (cchNewSize <= cchLen) return ERROR_BAD_LENGTH;
  55. if (allocated >= cchNewSize) return S_OK;
  56. allocated = cchNewSize;
  57. buffer = (wchar_t*) ((!buffer) ? HeapAlloc(heap, NULL, allocated*sizeof(wchar_t)) :
  58. HeapReAlloc(heap, NULL, buffer, allocated*sizeof(wchar_t)));
  59. return (buffer) ? S_OK : ERROR_OUTOFMEMORY;
  60. }
  61. void MLString::Compact(void)
  62. {
  63. if (!buffer) return;
  64. if (0 == cchLen)
  65. {
  66. HeapFree(heap, NULL, buffer);
  67. allocated = 0;
  68. cchLen = 0;
  69. buffer = NULL;
  70. }
  71. else
  72. {
  73. allocated = cchLen + 1;
  74. buffer = (wchar_t*)HeapReAlloc(heap, NULL, buffer, allocated*sizeof(wchar_t));
  75. }
  76. }
  77. HRESULT MLString::Format(const wchar_t *format, ...)
  78. {
  79. va_list argList;
  80. va_start(argList, format);
  81. HRESULT retCode;
  82. size_t remaining = 0;
  83. retCode = (allocated) ? StringCchVPrintfExW(buffer, allocated, NULL, &remaining, STRSAFE_NULL_ON_FAILURE, format, argList) : STRSAFE_E_INSUFFICIENT_BUFFER;
  84. while (STRSAFE_E_INSUFFICIENT_BUFFER == retCode)
  85. {
  86. int attempt = 1;
  87. allocated += ALLOCATION_STEP*attempt;
  88. attempt++;
  89. buffer = (wchar_t*) ((!buffer) ? HeapAlloc(heap, NULL, allocated*sizeof(wchar_t)) :
  90. HeapReAlloc(heap, NULL, buffer, allocated*sizeof(wchar_t)));
  91. retCode = StringCchVPrintfExW(buffer, allocated, NULL, &remaining, STRSAFE_NULL_ON_FAILURE, format, argList);
  92. }
  93. va_end(argList);
  94. cchLen = (S_OK == retCode) ? allocated - (unsigned int)remaining : 0;
  95. return retCode;
  96. }
  97. HRESULT MLString::CopyTo(MLString *destination)
  98. {
  99. HRESULT retCode = destination->Allocate(allocated);
  100. if (S_OK != retCode) return retCode;
  101. destination->cchLen = cchLen;
  102. CopyMemory(destination->buffer, buffer, cchLen*sizeof(wchar_t));
  103. return S_OK;
  104. }