string.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "string.h"
  2. #include <windows.h>
  3. #include <strsafe.h>
  4. using namespace tagz_;
  5. string::string()
  6. {
  7. data = 0;
  8. size = 0;
  9. used = 0;
  10. }
  11. void string::AddDBChar(LPTSTR c)
  12. {
  13. LPTSTR end = CharNext(c);
  14. while (c != end)
  15. AddChar(*c++);
  16. }
  17. void string::AddChar(TCHAR c)
  18. {
  19. if (!data)
  20. {
  21. size = 512;
  22. data = (LPTSTR)calloc(size, sizeof(TCHAR));
  23. if (!data) size = 0;
  24. used = 0;
  25. }
  26. else if (size == used)
  27. {
  28. size <<= 1;
  29. LPTSTR newData = (LPTSTR)realloc((LPTSTR)data, size * sizeof(TCHAR));
  30. if (!newData)
  31. {
  32. free(data);
  33. data = (LPTSTR)calloc(size, sizeof(TCHAR));
  34. if (!data) size = 0;
  35. used = 0;
  36. }
  37. else
  38. {
  39. data = newData;
  40. memset(data+used, 0, (size - used) * sizeof(TCHAR));
  41. }
  42. }
  43. if (data)
  44. data[used++] = c;
  45. }
  46. void string::AddInt(int i)
  47. {
  48. TCHAR simpleInt[16] = {0};
  49. StringCchPrintf(simpleInt, 16, TEXT("%i"), i);
  50. AddString(simpleInt);
  51. }
  52. void string::AddString(LPCTSTR z)
  53. {
  54. while (z && *z)
  55. {
  56. AddChar(*z);
  57. z++;
  58. }
  59. }
  60. void string::AddString(string & s)
  61. {
  62. AddString(s.Peek());
  63. }
  64. string::~string()
  65. {
  66. if (data) free(data);
  67. }
  68. LPTSTR string::GetBuf()
  69. {
  70. if (!data)
  71. return NULL;
  72. LPTSTR r = (LPTSTR)realloc(data, (used + 1) * sizeof(TCHAR));
  73. if (!r)
  74. {
  75. free(data);
  76. data = 0;
  77. size = used + 1;
  78. r = (LPTSTR)calloc((used + 1), sizeof(TCHAR));
  79. if (!r) size = 0;
  80. }
  81. if (r) r[used] = 0;
  82. data = 0;
  83. return r;
  84. }
  85. TCHAR string::operator[](size_t i)
  86. {
  87. if (!data || i >= used)
  88. return 0;
  89. else
  90. return data[i];
  91. }
  92. size_t string::Len()
  93. {
  94. return data ? used : 0;
  95. }
  96. void string::Reset()
  97. {
  98. if (data)
  99. {
  100. free(data);
  101. data = 0;
  102. }
  103. size = 0;
  104. used = 0;
  105. }
  106. LPCTSTR string::Peek()
  107. {
  108. AddChar(0);
  109. used--;
  110. return data;
  111. }