Umasoft
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
vtString.h
1 //
2 // vtString.h
3 //
4 // Copyright (c) 2001-2009 Virtual Terrain Project
5 // Free for all uses, see license.txt for details.
6 //
7 
8 #ifndef VTSTRINGH
9 #define VTSTRINGH
10 
11 #include <string.h>
12 #include <stdarg.h>
13 #include <string>
14 #include <vector>
15 
16 #include "config_vtdata.h"
17 
18 #if SUPPORT_WSTRING
19 class wstring2;
20 #endif
21 
22 #ifdef WIN32
23 # define WIN_UNIX_CDECL __cdecl
24 #else
25  /* UNIX doesn't have competing calling conventions to deal with */
26 # define WIN_UNIX_CDECL
27 #endif
28 
29 #ifdef WIN32
30 # define stricmp _stricmp // MBCS/Unicode aware
31 #else
32 # define stricmp strcasecmp
33 #endif
34 
35 // pointer to const char
36 typedef char *pchar;
37 typedef const char *pcchar;
38 typedef const uchar *pcuchar;
39 
41 {
42  int nRefs; // reference count
43  int nDataLength; // length of data (including terminator)
44  int nAllocLength; // length of allocation
45 
46  char* data() // char* to managed data
47  { return (char*)(this+1); }
48 };
49 
54 class vtString
55 {
56 public:
57 // Constructors
58 
59  // constructs empty vtString
60  vtString();
61  // copy constructor
62  vtString(const vtString& stringSrc);
63  // from an ANSI string (converts to char)
64  vtString(pcchar lpsz);
65  // from ucharacters
66  vtString(const uchar* psz);
67  // subset of characters from an ANSI string (converts to char)
68  vtString(pcchar lpch, int nLength);
69 
70  // Attributes & Operations
71 
72  // get data length
73  int GetLength() const;
74  // TRUE if zero length
75  bool IsEmpty() const;
76  // clear contents to empty
77  void Empty();
78 
79  // return single character at zero-based index
80  char GetAt(int nIndex) const;
81  // return single character at zero-based index
82  char operator[](int nIndex) const;
83  // set a single character at zero-based index
84  void SetAt(int nIndex, char ch);
85  // return pointer to const string
86  operator pcchar() const;
87 
88  // overloaded assignment
89 
90  // ref-counted copy from another vtString
91  const vtString& operator=(const vtString& stringSrc);
92  // copy string content from ANSI string (converts to char)
93  const vtString& operator=(pcchar lpsz);
94  // copy string content from uchars
95  const vtString& operator=(const uchar* psz);
96 
97  // string concatenation
98 
99  // concatenate a ANSI string
100  const vtString& operator+=(pcchar lpsz);
101  // concatenate from another vtString
102  const vtString& operator+=(const vtString& string);
103  // concatenate a single character
104  const vtString& operator+=(char ch);
105  // concatenate from a buffer with length
106  void Concat(pcchar buffer, size_t length);
107 
108  friend vtString operator+(const vtString& string1, const vtString& string2);
109  friend vtString operator+(const vtString& string, char ch);
110  friend vtString operator+(char ch, const vtString& string);
111  friend vtString operator+(const vtString& string, pcchar lpsz);
112  friend vtString operator+(pcchar lpsz, const vtString& string);
113 
114  // string comparison
115 
116  // straight character comparison
117  int Compare(pcchar lpsz) const;
118  // compare ignoring case
119  int CompareNoCase(pcchar lpsz) const;
120  // test if the string is an integer (with possible sign)
121  bool IsNumber() const;
122  // test if it matches a pattern which can include * and ?
123  bool Matches(pcchar lpsz) const;
124 
125  // simple sub-string extraction
126 
127  // return nCount characters starting at zero-based nFirst
128  vtString Mid(int nFirst, int nCount) const;
129  // return all characters starting at zero-based nFirst
130  vtString Mid(int nFirst) const;
131  // return first nCount characters in string
132  vtString Left(int nCount) const;
133  // return nCount characters from end of string
134  vtString Right(int nCount) const;
135 
136  // upper/lower/reverse conversion
137 
138  // NLS aware conversion to uppercase
139  void MakeUpper();
140  // NLS aware conversion to lowercase
141  void MakeLower();
142  // reverse string right-to-left
143  void MakeReverse();
144 
145  // trimming whitespace (either side)
146 
147  // remove whitespace starting from right edge
148  void TrimRight();
149  // remove whitespace starting from left side
150  void TrimLeft();
151 
152  // searching
153 
154  // find character starting at left, -1 if not found
155  int Find(char ch) const;
156  // find character starting at right
157  int ReverseFind(char ch) const;
158  // find character starting at zero-based index and going right
159  int Find(char ch, int nStart) const;
160  // find first instance of any character in passed string
161  int FindOneOf(pcchar lpszCharSet) const;
162  // find first instance of substring
163  int Find(pcchar szSub) const;
164  // find first instance of substring starting at zero-based index
165  int Find(pcchar szSub, int nStart) const;
166 
167  // advanced manipulation
168 
169  // replace occurrences of chOld with chNew
170  int Replace(char chOld, char chNew);
171  // replace occurrences of strOld with strNew
172  int Replace(const char *strOld, const char *strNew, bool bReplaceAll = false);
173 
174  // remove occurrences of chRemove
175  int Remove(char chRemove);
176  // insert character at zero-based index; concatenates
177  // if index is past end of string
178  int Insert(int nIndex, char ch);
179  // insert substring at zero-based index; concatenates
180  // if index is past end of string
181  int Insert(int nIndex, pcchar pstr);
182  // delete nCount characters starting at zero-based index
183  int Delete(int nIndex, int nCount = 1);
184 
185  // simple formatting
186  // printf-like formatting using passed string
187  void WIN_UNIX_CDECL Format(pcchar lpszFormat, ...);
188  // printf-like formatting using variable arguments parameter
189  void FormatV(pcchar lpszFormat, va_list argList);
190  // produce a string suitable for passing in a URL
191  void FormatForURL(const char *szInput);
192  vtString FormatForURL() const;
193 
194  // formatting for localization (uses FormatMessage API)
195  // format using FormatMessage API on passed string
196  void WIN_UNIX_CDECL FormatMessage(pcchar lpszFormat, ...);
197 
198  // Access to string implementation buffer as "C" character array
199  // get pointer to modifiable buffer at least as long as nMinBufLength
200  pchar GetBuffer(int nMinBufLength);
201  // release buffer, setting length to nNewLength (or to first nul if -1)
202  void ReleaseBuffer(int nNewLength = -1);
203  // get pointer to modifiable buffer exactly as long as nNewLength
204  pchar GetBufferSetLength(int nNewLength);
205  // release memory allocated to but unused by string
206  void FreeExtra();
207 
208  // Use LockBuffer/UnlockBuffer to turn refcounting off
209 
210  // turn refcounting back on
211  pchar LockBuffer();
212  // turn refcounting off
213  void UnlockBuffer();
214 
215 #if SUPPORT_WSTRING
216  wstring2 UTF8ToWideString();
217 #endif
219 
220 // Implementation
221 public:
222  ~vtString();
223  int GetAllocLength() const;
224 
225 protected:
226  pchar m_pchData; // pointer to ref counted string data
227 
228  // implementation helpers
229  vtStringData* GetData() const;
230  void Init();
231  void AllocCopy(vtString& dest, int nCopyLen, int nCopyIndex, int nExtraLen) const;
232  void AllocBuffer(int nLen);
233  void AssignCopy(int nSrcLen, pcchar szSrcData);
234  void ConcatCopy(int nSrc1Len, pcchar szSrc1Data, int nSrc2Len, pcchar szSrc2Data);
235  void ConcatInPlace(int nSrcLen, pcchar szSrcData);
236  void CopyBeforeWrite();
237  void AllocBeforeWrite(int nLen);
238  void Release();
239  static void Release(vtStringData* pData);
240  static size_t SafeStrlen(pcchar lpsz);
241  static void FreeData(vtStringData* pData);
242  };
243 
244 // Compare helpers
245 bool operator==(const vtString& s1, const vtString& s2);
246 bool operator==(const vtString& s1, pcchar s2);
247 bool operator==(pcchar s1, const vtString& s2);
248 bool operator!=(const vtString& s1, const vtString& s2);
249 bool operator!=(const vtString& s1, pcchar s2);
250 bool operator!=(pcchar s1, const vtString& s2);
251 bool operator<(const vtString& s1, const vtString& s2);
252 bool operator<(const vtString& s1, pcchar s2);
253 bool operator<(pcchar s1, const vtString& s2);
254 bool operator>(const vtString& s1, const vtString& s2);
255 bool operator>(const vtString& s1, pcchar s2);
256 bool operator>(pcchar s1, const vtString& s2);
257 bool operator<=(const vtString& s1, const vtString& s2);
258 bool operator<=(const vtString& s1, pcchar s2);
259 bool operator<=(pcchar s1, const vtString& s2);
260 bool operator>=(const vtString& s1, const vtString& s2);
261 bool operator>=(const vtString& s1, pcchar s2);
262 bool operator>=(pcchar s1, const vtString& s2);
263 
264 // Globals
265 extern pcchar _vtPchNil;
266 #define vtEmptyString ((vtString&)*(vtString*)&_vtPchNil)
267 
268 //
269 // vtString inlines
270 //
271 inline vtStringData* vtString::GetData() const
272  { return ((vtStringData*)m_pchData)-1; }
273 inline void vtString::Init()
274  { m_pchData = vtEmptyString.m_pchData; }
275 inline vtString::vtString()
276  { m_pchData = vtEmptyString.m_pchData; }
277 inline vtString::vtString(const uchar* lpsz)
278  { Init(); *this = (pcchar)lpsz; }
279 inline const vtString& vtString::operator=(const uchar* lpsz)
280  { *this = (pcchar)lpsz; return *this; }
281 
282 inline int vtString::GetLength() const
283  { return GetData()->nDataLength; }
284 inline int vtString::GetAllocLength() const
285  { return GetData()->nAllocLength; }
286 inline bool vtString::IsEmpty() const
287  { return GetData()->nDataLength == 0; }
288 inline vtString::operator pcchar() const
289  { return m_pchData; }
290 inline size_t vtString::SafeStrlen(pcchar lpsz)
291  { return (lpsz == NULL) ? 0 : strlen(lpsz); }
292 
293 // string comparison
294 inline int vtString::Compare(pcchar lpsz) const
295  { return strcmp(m_pchData, lpsz); } // MBCS/Unicode aware
296 inline int vtString::CompareNoCase(pcchar lpsz) const
297  { return stricmp(m_pchData, lpsz); }
298 
299 inline char vtString::GetAt(int nIndex) const
300 {
301  return m_pchData[nIndex];
302 }
303 inline char vtString::operator[](int nIndex) const
304 {
305  // same as GetAt
306  return m_pchData[nIndex];
307 }
308 inline bool operator==(const vtString& s1, const vtString& s2)
309  { return s1.Compare(s2) == 0; }
310 inline bool operator==(const vtString& s1, pcchar s2)
311  { return s1.Compare(s2) == 0; }
312 inline bool operator==(pcchar s1, const vtString& s2)
313  { return s2.Compare(s1) == 0; }
314 inline bool operator!=(const vtString& s1, const vtString& s2)
315  { return s1.Compare(s2) != 0; }
316 inline bool operator!=(const vtString& s1, pcchar s2)
317  { return s1.Compare(s2) != 0; }
318 inline bool operator!=(pcchar s1, const vtString& s2)
319  { return s2.Compare(s1) != 0; }
320 inline bool operator<(const vtString& s1, const vtString& s2)
321  { return s1.Compare(s2) < 0; }
322 inline bool operator<(const vtString& s1, pcchar s2)
323  { return s1.Compare(s2) < 0; }
324 inline bool operator<(pcchar s1, const vtString& s2)
325  { return s2.Compare(s1) > 0; }
326 inline bool operator>(const vtString& s1, const vtString& s2)
327  { return s1.Compare(s2) > 0; }
328 inline bool operator>(const vtString& s1, pcchar s2)
329  { return s1.Compare(s2) > 0; }
330 inline bool operator>(pcchar s1, const vtString& s2)
331  { return s2.Compare(s1) < 0; }
332 inline bool operator<=(const vtString& s1, const vtString& s2)
333  { return s1.Compare(s2) <= 0; }
334 inline bool operator<=(const vtString& s1, pcchar s2)
335  { return s1.Compare(s2) <= 0; }
336 inline bool operator<=(pcchar s1, const vtString& s2)
337  { return s2.Compare(s1) >= 0; }
338 inline bool operator>=(const vtString& s1, const vtString& s2)
339  { return s1.Compare(s2) >= 0; }
340 inline bool operator>=(const vtString& s1, pcchar s2)
341  { return s1.Compare(s2) >= 0; }
342 inline bool operator>=(pcchar s1, const vtString& s2)
343  { return s2.Compare(s1) <= 0; }
344 
345 // helpers
346 vtString EscapeStringForXML(const char *input);
347 void EscapeStringForXML(const std::string &input, std::string &output);
348 
349 #if SUPPORT_WSTRING
350 void EscapeStringForXML(const std::wstring &input, std::string &output);
351 void EscapeStringForXML(const std::wstring &input, std::wstring &output);
352 #endif
353 
354 vtString UTF8ToLocal(const char *string_utf8);
355 
356 
358 // wstring2
359 
360 #if SUPPORT_WSTRING
361 
362 #define MAX_WSTRING2_SIZE 4096
363 
379 class wstring2 : public std::wstring
380 {
381 public:
382  wstring2();
383  wstring2(const wchar_t *__s);
384  wstring2(const char *__s);
385  const char *mb_str() const; // multibyte string in local encoding
386 
387  size_t from_utf8(const char *in);
388  const char *to_utf8() const;
389 
390 private:
391  static char s_buffer[MAX_WSTRING2_SIZE];
392 };
393 
394 #endif // SUPPORT_WSTRING
395 
396 
400 #define vtStringArray std::vector<vtString>
401 
403 void vtTokenize(char *buf, const char *delim, vtStringArray &tokens);
404 
406 int vtFindString(const vtStringArray &arr, const char *find);
407 
409 vtString vtConcatArray(const vtStringArray &arr, const char delim);
410 
412 void vtExtractArray(const vtString &input, vtStringArray &arr, const char delim);
413 
414 #endif // VTSTRINGH
415