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