gstring.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #ifndef __GSTRING_SMART_STRING_CLASS__
  2. #define __GSTRING_SMART_STRING_CLASS__ 1
  3. // This is just a streamlined version of std::string.
  4. // It cleans itself up automatically, it can copy itself,
  5. // test for equality, etc.
  6. // and you can use .c_str() to get the string const, so it's
  7. // sytactically the same (usage-wise) as std::string.
  8. // Written by Ryan Geiss.
  9. #include <windows.h>
  10. class GString
  11. {
  12. public:
  13. GString()
  14. {
  15. m_data = new wchar_t[1];
  16. m_data[0] = 0;
  17. m_size = 1;
  18. }
  19. GString(const wchar_t* src)
  20. {
  21. m_data = NULL;
  22. m_size = 0;
  23. operator=(src);
  24. }
  25. GString(const GString &src_string)
  26. {
  27. m_data = NULL;
  28. m_size = 0;
  29. operator=(src_string);
  30. }
  31. ~GString()
  32. {
  33. delete m_data; // note: delete is safe on NULL ptrs
  34. m_data = NULL;
  35. m_size = 0;
  36. }
  37. inline GString& operator=(const wchar_t* src)
  38. {
  39. if (src != m_data) // don't do anything on "x = x.c_str();"
  40. {
  41. delete m_data; // note: delete is safe on NULL ptrs
  42. if (src)
  43. {
  44. m_size = wcslen(src)+1;
  45. m_data = new wchar_t[m_size];
  46. memcpy(m_data, src, m_size*2);
  47. }
  48. else
  49. {
  50. m_size = 1;
  51. m_data = new wchar_t[1];
  52. m_data[0] = 0;
  53. }
  54. }
  55. return *this;
  56. }
  57. inline GString& operator=(const GString &src_string)
  58. {
  59. if (&src_string != this) // don't do anything on "x = x;"
  60. {
  61. if (src_string.GetSize() != m_size) //optimization
  62. {
  63. delete m_data;
  64. m_size = src_string.GetSize();
  65. m_data = new wchar_t[m_size];
  66. }
  67. memcpy(m_data, src_string.c_str(), m_size*2);
  68. }
  69. return *this;
  70. }
  71. inline wchar_t operator[](int index) const
  72. {
  73. return m_data[index];
  74. }
  75. inline bool operator==(const wchar_t* comp)
  76. {
  77. return (wcscmp(m_data,comp) == 0);
  78. }
  79. inline bool operator==(const GString &comp)
  80. {
  81. if (m_size != comp.m_size) // shortcut
  82. return false;
  83. return (wcscmp(m_data,comp.c_str()) == 0); //return operator==(comp.m_data);
  84. }
  85. inline const wchar_t* c_str() const
  86. {
  87. return m_data;
  88. }
  89. // This is actually unsafe, but we need it for convenience, unless we really
  90. // feel like copying all data twice. BE WARNED! When this class reallocates
  91. // memory, all old references to _data are invalid!
  92. //operator const char*() const { return _data; }
  93. inline int GetSize() const //in bytes - including terminating NULL char.
  94. {
  95. return m_size;
  96. }
  97. inline int GetLength() const
  98. {
  99. return (m_size >= 1) ? m_size-1 : 0;
  100. }
  101. private:
  102. wchar_t* m_data;
  103. int m_size;
  104. };
  105. class GStringA
  106. {
  107. public:
  108. GStringA()
  109. {
  110. m_data = new char[1];
  111. m_data[0] = 0;
  112. m_size = 1;
  113. }
  114. GStringA(const char* src)
  115. {
  116. m_data = NULL;
  117. m_size = 0;
  118. operator=(src);
  119. }
  120. GStringA(const GStringA &src_string)
  121. {
  122. m_data = NULL;
  123. m_size = 0;
  124. operator=(src_string);
  125. }
  126. ~GStringA()
  127. {
  128. delete m_data; // note: delete is safe on NULL ptrs
  129. m_data = NULL;
  130. m_size = 0;
  131. }
  132. inline GStringA& operator=(const char* src)
  133. {
  134. if (src != m_data) // don't do anything on "x = x.c_str();"
  135. {
  136. delete m_data; // note: delete is safe on NULL ptrs
  137. if (src)
  138. {
  139. m_size = strlen(src)+1;
  140. m_data = new char[m_size];
  141. memcpy(m_data, src, m_size);
  142. }
  143. else
  144. {
  145. m_size = 1;
  146. m_data = new char[1];
  147. m_data[0] = 0;
  148. }
  149. }
  150. return *this;
  151. }
  152. inline GStringA& operator=(const GStringA &src_string)
  153. {
  154. if (&src_string != this) // don't do anything on "x = x;"
  155. {
  156. if (src_string.GetSize() != m_size) //optimization
  157. {
  158. delete m_data;
  159. m_size = src_string.GetSize();
  160. m_data = new char[m_size];
  161. }
  162. memcpy(m_data, src_string.c_str(), m_size);
  163. }
  164. return *this;
  165. }
  166. inline char operator[](int index) const
  167. {
  168. return m_data[index];
  169. }
  170. inline bool operator==(const char* comp)
  171. {
  172. return (strcmp(m_data,comp) == 0);
  173. }
  174. inline bool operator==(const GStringA &comp)
  175. {
  176. if (m_size != comp.m_size) // shortcut
  177. return false;
  178. return (strcmp(m_data,comp.c_str()) == 0); //return operator==(comp.m_data);
  179. }
  180. inline const char* c_str() const
  181. {
  182. return m_data;
  183. }
  184. // This is actually unsafe, but we need it for convenience, unless we really
  185. // feel like copying all data twice. BE WARNED! When this class reallocates
  186. // memory, all old references to _data are invalid!
  187. //operator const char*() const { return _data; }
  188. inline int GetSize() const //in bytes - including terminating NULL char.
  189. {
  190. return m_size;
  191. }
  192. inline int GetLength() const
  193. {
  194. return (m_size >= 1) ? m_size-1 : 0;
  195. }
  196. private:
  197. char* m_data;
  198. int m_size;
  199. };
  200. #endif