00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef _CSTRING__H
00024 #define _CSTRING__H
00025
00026
00027
00028
00029
00030
00031 #ifdef HAVE_STDIO_H
00032 #undef HAVE_STDIO_H
00033 #endif
00034 #ifdef HAVE_STRING_H
00035 #undef HAVE_STRING_H
00036 #endif
00037 #ifdef HAVE_STDLIB_H
00038 #undef HAVE_STDLIB_H
00039 #endif
00040
00041 #ifdef _WIN32
00042 #define HAVE_STDIO_H 1
00043 #define HAVE_STDARG_H 1
00044 #define HAVE_CTYPE_H 1
00045 #define HAVE_STRING_H 1
00046 #define HAVE_STDLIB_H 1
00047 #else
00048 #include "config.h"
00049 #endif
00050
00051 #ifdef HAVE_STDIO_H
00052 #include <stdio.h>
00053 #endif
00054 #ifdef HAVE_STDARG_H
00055 #include <stdarg.h>
00056 #endif
00057 #ifdef HAVE_CTYPE_H
00058 #include <ctype.h>
00059 #endif
00060 #ifdef HAVE_STRING_H
00061 #include <string.h>
00062 #endif
00063 #ifdef HAVE_STDLIB_H
00064 #include <stdlib.h>
00065 #endif
00066 #ifdef HAVE_UNISTD_H
00067 #include <unistd.h>
00068 #endif
00069
00070 #if !HAVE_SETENV
00071 extern "C" {
00072 int setenv (const char *name, const char *value, int overwrite);
00073 }
00074 #endif
00075
00077 #define CString CString_
00078
00079 #ifndef CSTR_WASTE_LIMIT
00080
00081 #define CSTR_WASTE_LIMIT 32
00082 #endif
00083
00084 #ifndef CSTR_REUSE_ALLOC
00085
00086 #define CSTR_REUSE_ALLOC 16
00087 #endif
00088
00102 typedef void (*CSTRING_CRIT) (void *hLock, bool setLock);
00103
00115 extern void CString_init (void *hLock = NULL, CSTRING_CRIT locker = NULL);
00116
00125 extern void CString_term ();
00126
00127 #ifdef CSTR_PROFL_USAGE
00128
00134 extern void CString_stat ();
00142 extern void CString_statReset ();
00143 #endif
00144
00146 class CString_ {
00147 friend class CString_support;
00148 private:
00150 char *s;
00152 size_t l;
00154 size_t r;
00155 public:
00156 void MakeLower();
00157 void MakeUpper();
00159 CString_ ();
00161 CString_ (const char *psz);
00163 CString_ (const unsigned char *psz);
00165 CString_ (const CString_ & stringSrc);
00167 CString_ (const char *psz, const size_t siz);
00169 CString_ (const unsigned char *psz, const size_t siz);
00171 const CString_ & operator = (const CString_ & stringSrc);
00173 const CString_ & operator = (const char *psz);
00175 const CString_ & operator = (const unsigned char *psz);
00177 ~CString_ ();
00179 void Empty ();
00181 const CString_ & operator += (const CString_ & string);
00183 const CString_ & operator += (char ch);
00185 const CString_ & operator += (const char * psz);
00187 friend CString_ operator + (const CString_ & string, const char * lpsz);
00188
00190 operator const char * () const {
00191 return s;
00192 };
00193 void Format (const char *fmt, ...);
00195 int CompareNoCase (const CString_ &string) const
00196 {
00197 #ifdef _WIN32
00198 return stricmp (s, string.s);
00199 #else
00200 return strcasecmp (s, string.s);
00201 #endif
00202 };
00204 int Compare (const CString_ &string) const
00205 {
00206 return strcmp (s, string.s);
00207 };
00209 int GetLength () const
00210 {
00211 return l;
00212 };
00214 bool IsEmpty ()
00215 {
00216 return l == 0;
00217 };
00219 char GetAt (int p)
00220 {
00221 if (p >= 0 && p <= (int) l) {
00222 return s [p];
00223 }
00224 return 0;
00225 };
00227 void SetAt (int p, char ch)
00228 {
00229 if (p >= 0 && p <= (int) l) {
00230 s [p] = ch;
00231 }
00232 };
00233 CString_ Mid (int nFirst, int nCount = -1) const;
00235 int Find (int ch, int startAt = 0) const {
00236 if (startAt < 0 || startAt >= (int) l) return -1;
00237 char *s2 = strchr (s + startAt, ch);
00238 if (s2) return s2 - s;
00239 return -1;
00240 };
00242 int Find (const char *str, int startAt = 0) const {
00243 if (startAt < 0 || startAt >= (int) l) return -1;
00244 char *s2 = strstr (s + startAt, str);
00245 if (s2) return s2 - s;
00246 return -1;
00247 };
00249 int ReverseFind (int ch) const {
00250 char *s2 = strrchr (s, ch);
00251 if (s2) return s2 - s;
00252 return -1;
00253 };
00255 CString_ Left (int nCount) const {
00256 if (nCount < 0) nCount = 0;
00257 return Mid (0, nCount);
00258 };
00260 CString_ Right (int nCount) const {
00261 if (nCount < 0) nCount = 0;
00262 return Mid (GetLength () - nCount);
00263 };
00264 };
00265
00266
00268 inline bool operator == (const CString_ & s1, const CString_ & s2) {
00269 return s1.Compare (s2) == 0;
00270 }
00271
00273 inline bool operator == (const CString_ & s1, const char * s2) {
00274 return s1.Compare (s2) == 0;
00275 }
00276
00278 inline bool operator == (const char * s1, const CString_ & s2) {
00279 return s2.Compare (s1) == 0;
00280 }
00281
00283 inline bool operator != (const CString_ & s1, const CString_ & s2) {
00284 return s1.Compare (s2) != 0;
00285 }
00286
00287
00289 inline bool operator != (const CString_ & s1, const char * s2) {
00290 return s1.Compare (s2) != 0;
00291 }
00292
00293
00295 inline bool operator != (const char * s1, const CString_ & s2) {
00296 return s2.Compare (s1) != 0;
00297 }
00298
00299
00301 inline bool operator < (const CString_ & s1, const CString_ & s2) {
00302 return s1.Compare (s2) < 0;
00303 }
00304
00306 inline bool operator < (const CString_ & s1, const char * s2) {
00307 return s1.Compare (s2) < 0;
00308 }
00309
00311 inline bool operator < (const char * s1, const CString_ & s2) {
00312 return s2.Compare (s1) > 0;
00313 }
00314
00316 inline bool operator > (const CString_ & s1, const CString_ & s2) {
00317 return s1.Compare (s2) > 0;
00318 }
00319
00321 inline bool operator > (const CString_ & s1, const char * s2) {
00322 return s1.Compare (s2) > 0;
00323 }
00324
00326 inline bool operator > (const char * s1, const CString_ & s2) {
00327 return s2.Compare (s1) < 0;
00328 }
00329
00331 inline bool operator <= (const CString_ & s1, const CString_ & s2) {
00332 return s1.Compare (s2) <= 0;
00333 }
00334
00336 inline bool operator <= (const CString_ & s1, const char * s2) {
00337 return s1.Compare (s2) <= 0;
00338 }
00339
00341 inline bool operator <= (const char * s1, const CString_ & s2) {
00342 return s2.Compare (s1) >= 0;
00343 }
00344
00346 inline bool operator >= (const CString_ & s1, const CString_ & s2) {
00347 return s1.Compare (s2) >= 0;
00348 }
00349
00351 inline bool operator >= (const CString_ & s1, const char * s2) {
00352 return s1.Compare (s2) >= 0;
00353 }
00354
00356 inline bool operator >= (const char * s1, const CString_ & s2) {
00357 return s2.Compare (s1) <= 0;
00358 }
00359
00360 #endif // defined _CSTRING__H
00361