]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/pk3man/pk3str.h
* added pk3man and fixed it to compile for latest radiant
[xonotic/netradiant.git] / contrib / pk3man / pk3str.h
1 #ifndef __STR__
2 #define __STR__
3 //
4 // class Str
5 // loose replacement for CString from MFC
6 //
7 //#include "cmdlib.h"
8 //#include <glib.h>
9 #include <string.h>
10 #include <ctype.h>
11
12 #ifdef WIN32
13 #define strcasecmp strcmpi
14 #endif
15
16 //char* __StrDup(char* pStr);
17 //char* __StrDup(const char* pStr);
18
19 #define __StrDup strdup
20
21 #ifdef __linux__
22 #define strcmpi strcasecmp
23 #define stricmp strcasecmp
24 #define strnicmp strncasecmp
25
26 inline char* strlwr(char* string)
27 {
28         char *cp;
29         for (cp = string; *cp; ++cp)
30         {
31                 if ('A' <= *cp && *cp <= 'Z')
32                 *cp += 'a' - 'A';
33         }
34
35         return string;
36 }
37
38 inline char* strupr(char* string)
39 {
40         char *cp;
41         for (cp = string; *cp; ++cp)
42         {
43                 if ('a' <= *cp && *cp <= 'z')
44                 *cp += 'A' - 'a';
45         }
46
47         return string;
48 }
49 #endif
50
51 static char *g_pStrWork = NULL;
52
53 class Str
54 {
55 protected:
56         bool m_bIgnoreCase;
57         char *m_pStr;
58
59 public:
60         Str()
61         {
62                 m_bIgnoreCase = true;
63                 m_pStr = new char[1];
64                 m_pStr[0] = '\0';
65         }
66
67         Str(char *p)
68         {
69                 m_bIgnoreCase = true;
70                 m_pStr = __StrDup(p);
71         }
72
73         Str(const char *p)
74         {
75                 m_bIgnoreCase = true;
76                 m_pStr = __StrDup(p);
77         }
78
79         Str(const char c)
80         {
81                 m_bIgnoreCase = true;
82                 m_pStr = new char[2];
83                 m_pStr[0] = c;
84                 m_pStr[1] = '\0';
85         }
86
87         void Deallocate()
88         {
89                 delete []m_pStr;
90                 m_pStr = NULL;
91         }
92
93         void Allocate(int n)
94         {
95                 Deallocate();
96                 m_pStr = new char[n];
97         }
98
99         const char* GetBuffer()
100         {
101                 return m_pStr;
102         }
103
104         void MakeEmpty()
105         {
106                 Deallocate();
107                 m_pStr = __StrDup("");
108         }
109
110         ~Str()
111         {
112                 Deallocate();
113                 delete []g_pStrWork;
114                 g_pStrWork = NULL;
115         }
116
117         void MakeLower()
118         {
119                 if (m_pStr)
120                 {
121                         strlwr(m_pStr);
122                 }
123         }
124
125         void MakeUpper()
126         {
127                 if (m_pStr)
128                 {
129                         strupr(m_pStr);
130                 }
131         }
132
133         void TrimRight()
134         {
135                 char* lpsz = m_pStr;
136                 char* lpszLast = NULL;
137                 while (*lpsz != '\0')
138                 {
139                         if (isspace(*lpsz))
140                         {
141                                 if (lpszLast == NULL)
142                                         lpszLast = lpsz;
143                         }
144                         else
145                                 lpszLast = NULL;
146                                 lpsz++;
147                         }
148
149                         if (lpszLast != NULL)
150                         {
151                                 // truncate at trailing space start
152                                 *lpszLast = '\0';
153                         }
154         }
155
156         void TrimLeft()
157         {
158                 // find first non-space character
159                 char* lpsz = m_pStr;
160                 while (isspace(*lpsz))
161                         lpsz++;
162
163                 // fix up data and length
164                 int nDataLength = GetLength() - (lpsz - m_pStr);
165                 memmove(m_pStr, lpsz, (nDataLength+1));
166         }
167
168         int Find(const char *p)
169         {
170                 char *pf = strstr(m_pStr, p);
171                 return (pf) ? (pf - m_pStr) : -1;
172         }
173
174         // search starting at a given offset
175         int Find(const char *p, int offset)
176         {
177                 char *pf = strstr(m_pStr+offset, p);
178                 return (pf) ? (pf - m_pStr) : -1;
179         }
180
181         int Find(const char ch)
182         {
183                 char *pf = strchr (m_pStr, ch);
184                 return (pf) ? (pf - m_pStr) : -1;
185         }
186
187         int ReverseFind(const char ch)
188         {
189                 char *pf = strrchr(m_pStr, ch);
190                 return (pf) ? (pf - m_pStr) : -1;
191         }
192
193         int CompareNoCase (const char* str) const
194         {
195                 return strcasecmp (m_pStr, str);
196         }
197
198         int GetLength()
199         {
200                 return (m_pStr) ? strlen(m_pStr) : 0;
201         }
202
203         const char* Left(int n)
204         {
205                 delete []g_pStrWork;
206                 if (n > 0)
207                 {
208                         g_pStrWork = new char[n+1];
209                         strncpy(g_pStrWork, m_pStr, n);
210                         g_pStrWork[n] = '\0';
211                 }
212                 else
213                 {
214                         g_pStrWork = "";
215                         g_pStrWork = new char[1];
216                         g_pStrWork[0] = '\0';
217                 }
218                 return g_pStrWork;
219         }
220
221         const char* Right(int n)
222         {
223                 delete []g_pStrWork;
224                 if (n > 0)
225                 {
226                         g_pStrWork = new char[n+1];
227                         int nStart = GetLength() - n;
228                         strncpy(g_pStrWork, &m_pStr[nStart], n);
229                         g_pStrWork[n] = '\0';
230                 }
231                 else
232                 {
233                         g_pStrWork = new char[1];
234                         g_pStrWork[0] = '\0';
235                 }
236                 return g_pStrWork;
237         }
238
239         const char* Mid(int nFirst) const
240         {
241                 return Mid(nFirst, strlen (m_pStr) - nFirst);
242         }
243
244         const char* Mid(int first, int n) const
245         {
246                 delete []g_pStrWork;
247                 if (n > 0)
248                 {
249                         g_pStrWork = new char[n+1];
250                         strncpy(g_pStrWork, m_pStr+first, n);
251                         g_pStrWork[n] = '\0';
252                 }
253                 else
254                 {
255                         g_pStrWork = "";
256                         g_pStrWork = new char[1];
257                         g_pStrWork[0] = '\0';
258                 }
259                 return g_pStrWork;
260         }
261
262 #ifdef __G_LIB_H__
263         void Format(const char* fmt, ...)
264         {
265                 va_list args;
266                 char *buffer;
267
268                 va_start (args, fmt);
269                 buffer = g_strdup_vprintf (fmt, args);
270                 va_end (args);
271
272                 delete[] m_pStr;
273                 m_pStr = __StrDup(buffer);
274                 g_free (buffer);
275         }
276 #else
277         void Format(const char* fmt, ...)
278         {
279                 va_list args;
280                 m_pStr = new char[1024];
281
282                 va_start (args, fmt);
283                 vsprintf (m_pStr, fmt, args);
284                 va_end (args);
285         }
286 #endif
287
288         void SetAt(int n, char ch)
289         {
290                 if (n >= 0 && n < GetLength ())
291                         m_pStr[n] = ch;
292         }
293
294         // NOTE: unlike CString, this looses the pointer
295         void ReleaseBuffer(int n = -1)
296         {
297                 if (n == -1)
298                 n = GetLength ();
299
300                 char* tmp = m_pStr;
301                 tmp[n] = '\0';
302                 m_pStr = __StrDup(tmp);
303                 delete []tmp;
304         }
305
306         char* GetBufferSetLength(int n)
307         {
308                 if (n < 0)
309                         n = 0;
310
311                 char *p = new char[n+1];
312                 strncpy (p, m_pStr, n);
313                 p[n] = '\0';
314                 delete []m_pStr;
315                 m_pStr = p;
316                 return m_pStr;
317         }
318
319         //  char& operator *() { return *m_pStr; }
320         //  char& operator *() const { return *const_cast<Str*>(this)->m_pStr; }
321         operator void*() { return m_pStr; }
322         operator char*() { return m_pStr; }
323         operator const char*(){ return reinterpret_cast<const char*>(m_pStr); }
324         operator unsigned char*() { return reinterpret_cast<unsigned char*>(m_pStr); }
325         operator const unsigned char*() { return reinterpret_cast<const unsigned char*>(m_pStr); }
326         Str& operator =(const Str& rhs)
327         {
328                 if (&rhs != this)
329                 {
330                         delete[] m_pStr;
331                         m_pStr = __StrDup(rhs.m_pStr);
332                 }
333                 return *this;
334         }
335
336         Str& operator =(const char* pStr)
337         {
338                 if (m_pStr != pStr)
339                 {
340                         delete[] m_pStr;
341                         m_pStr = __StrDup(pStr);
342                 }
343                 return *this;
344         }
345
346         Str& operator +=(const char ch)
347         {
348                 int len = GetLength ();
349                 char *p = new char[len + 1 + 1];
350
351                 if (m_pStr)
352                 {
353                         strcpy(p, m_pStr);
354                         delete[] m_pStr;
355                 }
356
357                 m_pStr = p;
358                 m_pStr[len] = ch;
359                 m_pStr[len+1] = '\0';
360
361                 return *this;
362         }
363
364         Str& operator +=(const char *pStr)
365         {
366                 if (pStr)
367                 {
368                         if (m_pStr)
369                         {
370                                 char *p = new char[strlen(m_pStr) + strlen(pStr) + 1];
371                                 strcpy(p, m_pStr);
372                                 strcat(p, pStr);
373                                 delete[] m_pStr;
374                                 m_pStr = p;
375                         }
376                         else
377                         {
378                                 m_pStr = __StrDup(pStr);
379                         }
380                 }
381                 return *this;
382         }
383
384         bool operator ==(const Str& rhs) const { return (m_bIgnoreCase) ? stricmp(m_pStr, rhs.m_pStr) == 0 : strcmp(m_pStr, rhs.m_pStr) == 0; }
385         bool operator ==(char* pStr) const { return (m_bIgnoreCase) ? stricmp(m_pStr, pStr) == 0 : strcmp(m_pStr, pStr) == 0; }
386         bool operator ==(const char* pStr) const { return (m_bIgnoreCase) ? stricmp(m_pStr, pStr) == 0 : strcmp(m_pStr, pStr) == 0; }
387         bool operator !=(Str& rhs) const { return (m_bIgnoreCase) ? stricmp(m_pStr, rhs.m_pStr) != 0 : strcmp(m_pStr, rhs.m_pStr) != 0; }
388         bool operator !=(char* pStr) const { return (m_bIgnoreCase) ? stricmp(m_pStr, pStr) != 0 : strcmp(m_pStr, pStr) != 0; }
389         bool operator !=(const char* pStr) const { return (m_bIgnoreCase) ? stricmp(m_pStr, pStr) != 0 : strcmp(m_pStr, pStr) != 0; }
390         char& operator [](int nIndex) { return m_pStr[nIndex]; }
391         char& operator [](int nIndex) const { return m_pStr[nIndex]; }
392         const char GetAt (int nIndex) { return m_pStr[nIndex]; }
393 };
394
395 #endif