gaystring.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include "gaystring.h"
  2. GayString::GayString(const char *initial)
  3. {
  4. len = 0;
  5. m_buf = NULL;
  6. m_alloc = 0;
  7. if (initial && *initial) Set(initial);
  8. }
  9. GayString::~GayString()
  10. {
  11. if ( m_buf )
  12. free( m_buf );
  13. }
  14. void GayString::Set(const char *value)
  15. {
  16. if (!value) value="";
  17. len = strlen(value);
  18. Grow(len + 1);
  19. strncpy(m_buf, value, len);
  20. m_buf[len] = 0;
  21. }
  22. char *GayString::Get() { return m_buf ? m_buf : (char*)""; }
  23. void GayString::Append(const char *append)
  24. {
  25. size_t oldsize = len;
  26. len += strlen(append);
  27. Grow(len + 1);
  28. strncpy(m_buf + oldsize, append, len - oldsize);
  29. m_buf[len] = 0;
  30. }
  31. void GayString::Grow(size_t newsize)
  32. {
  33. if (m_alloc < newsize)
  34. {
  35. size_t old_m_alloc = m_alloc;
  36. m_alloc = newsize + 32;
  37. char *new_m_buf = (char*)realloc(m_buf, m_alloc*sizeof(char));
  38. if (!new_m_buf)
  39. {
  40. new_m_buf = (char*)malloc(m_alloc*sizeof(char));
  41. if (!new_m_buf)
  42. {
  43. m_alloc = old_m_alloc;
  44. }
  45. else
  46. {
  47. memcpy(new_m_buf, m_buf, old_m_alloc*sizeof(char));
  48. free(m_buf);
  49. m_buf = new_m_buf;
  50. }
  51. }
  52. else m_buf = new_m_buf;
  53. }
  54. }
  55. void GayString::Compact()
  56. {
  57. if (m_buf)
  58. {
  59. size_t old_m_alloc = m_alloc;
  60. //m_alloc = strlen(m_buf) + 1;
  61. m_alloc = len + 1;
  62. char* new_m_buf = (char*)realloc(m_buf, m_alloc*sizeof(char));
  63. if (!new_m_buf)
  64. {
  65. new_m_buf = (char*)malloc(m_alloc*sizeof(char));
  66. if (!new_m_buf)
  67. {
  68. if (m_alloc > old_m_alloc)
  69. m_alloc = old_m_alloc;
  70. }
  71. else
  72. {
  73. if (m_alloc > old_m_alloc)
  74. m_alloc = old_m_alloc;
  75. memcpy(new_m_buf, m_buf, old_m_alloc*sizeof(char));
  76. free(m_buf);
  77. m_buf = new_m_buf;
  78. }
  79. }
  80. else m_buf = new_m_buf;
  81. }
  82. }
  83. size_t GayString::Length() { return len; }
  84. /* */
  85. GayStringW::GayStringW(const wchar_t *initial)
  86. {
  87. len = 0;
  88. m_buf = NULL;
  89. m_alloc = 0;
  90. if (initial && *initial) Set(initial);
  91. }
  92. GayStringW::~GayStringW()
  93. {
  94. free(m_buf);
  95. }
  96. void GayStringW::Set(const wchar_t *value)
  97. {
  98. if (!value) value=L"";
  99. len = wcslen(value);
  100. Grow(len + 1);
  101. wcsncpy(m_buf, value, len);
  102. m_buf[len] = 0;
  103. }
  104. const wchar_t *GayStringW::Get() { return m_buf ? m_buf : L""; }
  105. void GayStringW::Append(const wchar_t *append)
  106. {
  107. size_t oldsize = len;
  108. len += wcslen(append);
  109. Grow(len + 1);
  110. wcsncpy(m_buf + oldsize, append, len - oldsize);
  111. m_buf[len] = 0;
  112. }
  113. void GayStringW::Grow(size_t newsize)
  114. {
  115. if (m_alloc < newsize)
  116. {
  117. size_t old_m_alloc = m_alloc;
  118. m_alloc = newsize + 32;
  119. wchar_t *new_m_buf = (wchar_t*)realloc(m_buf, m_alloc*sizeof(wchar_t));
  120. if (!new_m_buf)
  121. {
  122. new_m_buf = (wchar_t*)malloc(m_alloc*sizeof(wchar_t));
  123. if (!new_m_buf)
  124. {
  125. m_alloc = old_m_alloc;
  126. }
  127. else
  128. {
  129. memcpy(new_m_buf, m_buf, old_m_alloc*sizeof(wchar_t));
  130. free(m_buf);
  131. m_buf = new_m_buf;
  132. }
  133. }
  134. else m_buf = new_m_buf;
  135. }
  136. }
  137. void GayStringW::Compact()
  138. {
  139. if (m_buf)
  140. {
  141. size_t old_m_alloc = m_alloc;
  142. m_alloc = len + 1;
  143. wchar_t* new_m_buf = (wchar_t*)realloc(m_buf, m_alloc*sizeof(wchar_t));
  144. if (!new_m_buf)
  145. {
  146. new_m_buf = (wchar_t*)malloc(m_alloc*sizeof(wchar_t));
  147. if (!new_m_buf)
  148. {
  149. if (m_alloc > old_m_alloc)
  150. m_alloc = old_m_alloc;
  151. }
  152. else
  153. {
  154. if (m_alloc > old_m_alloc)
  155. m_alloc = old_m_alloc;
  156. memcpy(new_m_buf, m_buf, old_m_alloc*sizeof(wchar_t));
  157. free(m_buf);
  158. m_buf = new_m_buf;
  159. }
  160. }
  161. else m_buf = new_m_buf;
  162. }
  163. }
  164. size_t GayStringW::Length() { return len; }