str.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1999-2005 Id Software, Inc.
  4. This file is part of Quake III Arena source code.
  5. Quake III Arena source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake III Arena source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Foobar; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. #ifndef __STR__
  19. #define __STR__
  20. //
  21. // class Str
  22. // loose replacement for CString from MFC
  23. //
  24. //#include "cmdlib.h"
  25. #include <string.h>
  26. char* __StrDup(char* pStr);
  27. char* __StrDup(const char* pStr);
  28. static char *g_pStrWork = NULL;
  29. class Str
  30. {
  31. protected:
  32. bool m_bIgnoreCase;
  33. char *m_pStr;
  34. public:
  35. Str()
  36. {
  37. m_bIgnoreCase = true;
  38. m_pStr = NULL;
  39. }
  40. Str(char *p)
  41. {
  42. m_bIgnoreCase = true;
  43. m_pStr = __StrDup(p);
  44. }
  45. Str(const char *p)
  46. {
  47. m_bIgnoreCase = true;
  48. m_pStr = __StrDup(p);
  49. }
  50. void Deallocate()
  51. {
  52. delete []m_pStr;
  53. m_pStr = NULL;
  54. }
  55. void Allocate(int n)
  56. {
  57. Deallocate();
  58. m_pStr = new char[n];
  59. }
  60. const char* GetBuffer()
  61. {
  62. return m_pStr;
  63. }
  64. void MakeEmpty()
  65. {
  66. Deallocate();
  67. m_pStr = __StrDup("");
  68. }
  69. ~Str()
  70. {
  71. Deallocate();
  72. delete []g_pStrWork;
  73. g_pStrWork = NULL;
  74. }
  75. void MakeLower()
  76. {
  77. if (m_pStr)
  78. {
  79. strlwr(m_pStr);
  80. }
  81. }
  82. int Find(const char *p)
  83. {
  84. char *pf = strstr(m_pStr, p);
  85. return (pf) ? (pf - m_pStr) : -1;
  86. }
  87. int GetLength()
  88. {
  89. return (m_pStr) ? strlen(m_pStr) : 0;
  90. }
  91. const char* Left(int n)
  92. {
  93. delete []g_pStrWork;
  94. if (n > 0)
  95. {
  96. g_pStrWork = new char[n+1];
  97. strncpy(g_pStrWork, m_pStr, n);
  98. }
  99. else
  100. {
  101. g_pStrWork = "";
  102. g_pStrWork = new char[1];
  103. g_pStrWork[0] = '\0';
  104. }
  105. return g_pStrWork;
  106. }
  107. const char* Right(int n)
  108. {
  109. delete []g_pStrWork;
  110. if (n > 0)
  111. {
  112. g_pStrWork = new char[n+1];
  113. int nStart = GetLength() - n;
  114. strncpy(g_pStrWork, &m_pStr[nStart], n);
  115. g_pStrWork[n] = '\0';
  116. }
  117. else
  118. {
  119. g_pStrWork = new char[1];
  120. g_pStrWork[0] = '\0';
  121. }
  122. return g_pStrWork;
  123. }
  124. char& operator *() { return *m_pStr; }
  125. char& operator *() const { return *const_cast<Str*>(this)->m_pStr; }
  126. operator void*() { return m_pStr; }
  127. operator char*() { return m_pStr; }
  128. operator const char*(){ return reinterpret_cast<const char*>(m_pStr); }
  129. operator unsigned char*() { return reinterpret_cast<unsigned char*>(m_pStr); }
  130. operator const unsigned char*() { return reinterpret_cast<const unsigned char*>(m_pStr); }
  131. Str& operator =(const Str& rhs)
  132. {
  133. if (&rhs != this)
  134. {
  135. delete[] m_pStr;
  136. m_pStr = __StrDup(rhs.m_pStr);
  137. }
  138. return *this;
  139. }
  140. Str& operator =(const char* pStr)
  141. {
  142. if (m_pStr != pStr)
  143. {
  144. delete[] m_pStr;
  145. m_pStr = __StrDup(pStr);
  146. }
  147. return *this;
  148. }
  149. Str& operator +=(const char *pStr)
  150. {
  151. if (pStr)
  152. {
  153. if (m_pStr)
  154. {
  155. char *p = new char[strlen(m_pStr) + strlen(pStr) + 1];
  156. strcpy(p, m_pStr);
  157. strcat(p, pStr);
  158. delete m_pStr;
  159. m_pStr = p;
  160. }
  161. else
  162. {
  163. m_pStr = __StrDup(pStr);
  164. }
  165. }
  166. return *this;
  167. }
  168. Str& operator +=(const char c)
  169. {
  170. return operator+=(&c);
  171. }
  172. bool operator ==(const Str& rhs) const { return (m_bIgnoreCase) ? stricmp(m_pStr, rhs.m_pStr) == 0 : strcmp(m_pStr, rhs.m_pStr) == 0; }
  173. bool operator ==(char* pStr) const { return (m_bIgnoreCase) ? stricmp(m_pStr, pStr) == 0 : strcmp(m_pStr, pStr) == 0; }
  174. bool operator ==(const char* pStr) const { return (m_bIgnoreCase) ? stricmp(m_pStr, pStr) == 0 : strcmp(m_pStr, pStr) == 0; }
  175. bool operator !=(Str& rhs) const { return (m_bIgnoreCase) ? stricmp(m_pStr, rhs.m_pStr) != 0 : strcmp(m_pStr, rhs.m_pStr) != 0; }
  176. bool operator !=(char* pStr) const { return (m_bIgnoreCase) ? stricmp(m_pStr, pStr) != 0 : strcmp(m_pStr, pStr) != 0; }
  177. bool operator !=(const char* pStr) const { return (m_bIgnoreCase) ? stricmp(m_pStr, pStr) != 0 : strcmp(m_pStr, pStr) != 0; }
  178. char& operator [](int nIndex) { return m_pStr[nIndex]; }
  179. char& operator [](int nIndex) const { return m_pStr[nIndex]; }
  180. };
  181. #endif