ScummVM API documentation
string.h
1 
2 /* ScummVM - Graphic Adventure Engine
3  *
4  * ScummVM is the legal property of its developers, whose names
5  * are too numerous to list here. Please refer to the COPYRIGHT
6  * file distributed with this source distribution.
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #ifndef BAGEL_BOFLIB_STRING_H
24 #define BAGEL_BOFLIB_STRING_H
25 
26 #include "common/system.h"
27 #include "bagel/boflib/object.h"
28 
29 namespace Bagel {
30 
31 // The buffer size and string len members are shorts, and use the high byte
32 // of the buffer size to tell us if it is stack memory being used.
33 #define mUseStackMem 0x8000
34 #define NORMALIZEBUFFERSIZE() (_nBufferSize & ~mUseStackMem)
35 #define SETBUFFERSIZE(size, usestackmem) (_nBufferSize = (uint16)((size) + ((usestackmem) ? mUseStackMem : 0)))
36 #define USESSTACKMEM() (_nBufferSize & mUseStackMem)
37 
38 class CBofString : public CBofObject {
39 public:
40  // Constructors
44  CBofString();
45 
50  CBofString(const char *pszBuf);
51 
55  CBofString(const CBofString &stringSrc);
56 
62  CBofString(char ch, int nRepeat = 1);
63 
68  CBofString(int nLength);
69 
76  CBofString(char *pszBuff, int pszBuffLen);
77 
81  virtual ~CBofString();
82 
83  // Attributes & Operations
84 
85  int getBufferSize() const {
86  return NORMALIZEBUFFERSIZE();
87  }
88  int getLength() const {
89  return _nLength;
90  }
91  bool isEmpty() const {
92  return _nLength == 0;
93  }
94 
98  void free();
99 
100  void growTo(int nNewSize); // Resize the buffer
101 
102  char getAt(int nIndex); // 0 based
103  char operator[](int nIndex); // same as getAt
104 
105  operator const char *() const {
106  return (const char *)_pszData;
107  }
108  const char *getBuffer() const {
109  return _pszData;
110  }
111 
112  // Hashing support.
113  //
114  int hash() const;
115 
116  // Overloaded assignment
121  void copy(const char *pszSourceBuf);
122 
123  const CBofString &operator=(const CBofString &cStringSrc);
124  const CBofString &operator=(char ch);
125  const CBofString &operator=(const char *psz);
126 
127  // String concatenation
128  const CBofString &operator+=(const CBofString &cString);
129  const CBofString &operator+=(char ch);
130  const CBofString &operator+=(const char *psz);
131 
132  friend CBofString operator+(const CBofString &string1, const CBofString &string2);
133  friend CBofString operator+(const CBofString &string, char ch);
134  friend CBofString operator+(char ch, const CBofString &string);
135  friend CBofString operator+(const CBofString &string, const char *lpsz);
136  friend CBofString operator+(const char *lpsz, const CBofString &string);
137 
138  // String comparison
139  int compare(const char *lpsz) const; // straight character
140  int compareNoCase(const char *lpsz) const; // ignore case
141 
142  // Simple sub-string extraction
143  //
144  CBofString mid(int nFirst, int nCount) const;
145  CBofString mid(int nFirst) const;
146  CBofString left(int nCount) const;
147  CBofString right(int nCount) const;
148 
149  void deleteLastChar();
150 
151  // Upper/lower/reverse conversion
152  void makeUpper();
153 
154  // Searching (return starting index, or -1 if not found)
155  // Look for a specific sub-string
156  int find(const char *lpszSub) const; // like "C" strstr
157  int findNumOccurrences(const char *pszSub);
158 
159  // Search and replace routines
160  void replaceCharAt(int, char);
161  void replaceChar(char chOld, char chNew);
162  void replaceStr(const char *pszOld, const char *pszNew);
163 
164  // Access to string implementation buffer as "C" character array
165  char *getBuffer();
166 
167 protected:
168  // implementation helpers
169 
173  void init();
174 
178  void allocCopy(CBofString &dest, int nCopyLen, int nCopyIndex, int nExtraLen) const;
179 
184  void allocBuffer(int nLen);
185 
186  void concatCopy(int nSrc1Len, const char *lpszSrc1Data, int nSrc2Len, const char *lpszSrc2Data, int nAllocLen = 0);
187  void concatInPlace(int nSrcLen, const char *lpszSrcData);
188  static int safeStrlen(const char *lpsz);
189 
190  // Lengths/sizes in characters
191  // (note: an extra character is always allocated)
192  //
193  char *_pszData; // actual string (zero terminated)
194  uint16 _nLength; // does not include terminating 0
195  uint16 _nBufferSize; // does not include terminating 0
196 };
197 
198 // Inline Comparison operators
199 //
200 inline bool operator==(const CBofString &s1, const CBofString &s2) {
201  return s1.compare(s2) == 0;
202 }
203 
204 inline bool operator==(const CBofString &s1, const char *s2) {
205  return s1.compare(s2) == 0;
206 }
207 
208 inline bool operator==(const char *s1, const CBofString &s2) {
209  return s2.compare(s1) == 0;
210 }
211 
212 inline bool operator!=(const CBofString &s1, const CBofString &s2) {
213  return s1.compare(s2) != 0;
214 }
215 
216 inline bool operator!=(const CBofString &s1, const char *s2) {
217  return s1.compare(s2) != 0;
218 }
219 
220 inline bool operator!=(const char *s1, const CBofString &s2) {
221  return s2.compare(s1) != 0;
222 }
223 
224 inline bool operator<(const CBofString &s1, const CBofString &s2) {
225  return s1.compare(s2) < 0;
226 }
227 
228 inline bool operator<(const CBofString &s1, const char *s2) {
229  return s1.compare(s2) < 0;
230 }
231 
232 inline bool operator<(const char *s1, const CBofString &s2) {
233  return s2.compare(s1) > 0;
234 }
235 
236 inline bool operator>(const CBofString &s1, const CBofString &s2) {
237  return s1.compare(s2) > 0;
238 }
239 
240 inline bool operator>(const CBofString &s1, const char *s2) {
241  return s1.compare(s2) > 0;
242 }
243 
244 inline bool operator>(const char *s1, const CBofString &s2) {
245  return s2.compare(s1) < 0;
246 }
247 
248 inline bool operator<=(const CBofString &s1, const CBofString &s2) {
249  return s1.compare(s2) <= 0;
250 }
251 
252 inline bool operator<=(const CBofString &s1, const char *s2) {
253  return s1.compare(s2) <= 0;
254 }
255 
256 inline bool operator<=(const char *s1, const CBofString &s2) {
257  return s2.compare(s1) >= 0;
258 }
259 
260 inline bool operator>=(const CBofString &s1, const CBofString &s2) {
261  return s1.compare(s2) >= 0;
262 }
263 
264 inline bool operator>=(const CBofString &s1, const char *s2) {
265  return s1.compare(s2) >= 0;
266 }
267 
268 inline bool operator>=(const char *s1, const CBofString &s2) {
269  return s2.compare(s1) <= 0;
270 }
271 
272 } // namespace Bagel
273 
274 #endif
void allocBuffer(int nLen)
Definition: object.h:28
virtual ~CBofString()
void copy(const char *pszSourceBuf)
Definition: string.h:38
Definition: bagel.h:31
void allocCopy(CBofString &dest, int nCopyLen, int nCopyIndex, int nExtraLen) const