Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

wvstring.h

00001 /* -*- Mode: C++ -*-
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
00004  *
00005  * Implementation of a simple and efficient printable-string class.
00006  * 
00007  * It leaves out many of the notational conveniences provided by other
00008  * string classes, because they waste too much CPU time and space.
00009  * It does the one thing really missing from char* strings, that is,
00010  * dynamic buffer management.
00011  * 
00012  * The 'str' member is the actual (char*) string.  You should never
00013  * need to access it directly.
00014  */
00015 #ifndef __WVSTRING_H
00016 #define __WVSTRING_H
00017 
00018 #include <string.h>
00019 #include <stdio.h>
00020 #include <stdlib.h>
00021 
00022 
00023 /*
00024  * 1 byte for terminating NUL, 4 more to kludge around libc5+efence
00025  * incompatibility with strcat().
00026  */
00027 #define WVSTRING_EXTRA 5
00028 
00029 
00030 #define __WVS_FORM(n) WvStringParm __wvs_##n = WvFastString::null
00031 #define WVSTRING_FORMAT_DECL WvStringParm __wvs_format, \
00032                 WvStringParm __wvs_a0, \
00033                 __WVS_FORM( a1), __WVS_FORM( a2), __WVS_FORM( a3), \
00034                 __WVS_FORM( a4), __WVS_FORM( a5), __WVS_FORM( a6), \
00035                 __WVS_FORM( a7), __WVS_FORM( a8), __WVS_FORM( a9), \
00036                 __WVS_FORM(a10), __WVS_FORM(a11), __WVS_FORM(a12), \
00037                 __WVS_FORM(a13), __WVS_FORM(a14), __WVS_FORM(a15), \
00038                 __WVS_FORM(a16), __WVS_FORM(a17), __WVS_FORM(a18), \
00039                 __WVS_FORM(a19)
00040 #define WVSTRING_FORMAT_CALL __wvs_format, __wvs_a0, \
00041                 __wvs_a1, __wvs_a2, __wvs_a3, __wvs_a4, __wvs_a5, \
00042                 __wvs_a6, __wvs_a7, __wvs_a8, __wvs_a9, __wvs_a10, \
00043                 __wvs_a11, __wvs_a12, __wvs_a13, __wvs_a14, __wvs_a15, \
00044                 __wvs_a16, __wvs_a17, __wvs_a18, __wvs_a19
00045 
00046 struct WvStringBuf;
00047 class WvFastString;
00048 class WvString;
00049 class QString; // for operator QString()
00050 class QCString;
00051 
00052 // all WvFastString objects are const - they should _only_ be created
00053 // automatically by automatic typecasting in parameter passing.  So let's
00054 // create a handy alias.
00055 typedef const WvFastString &   WvStringParm;
00056 
00057 
00058 
00059 struct WvStringBuf
00060 {
00061     size_t size;        // string length - if zero, use strlen!!
00062     unsigned links;     // number of WvStrings using this buf.
00063     char data[1];       // optional room for extra string data
00064 };
00065 
00066 
00067 // the _actual_ space taken by a WvStringBuf, without the data[] array
00068 // (which is variable-sized, not really 1 byte)
00069 #define WVSTRINGBUF_SIZE(s) (s->data - (char *)s)
00070 
00085 class WvFastString
00086 {
00087     friend class WvString; // so WvString can access members of _other_ objects
00088     
00089 protected:
00090     WvStringBuf *buf;
00091     char *str;
00092     
00093     // WvStringBuf used for char* strings that have not been cloned.
00094     static WvStringBuf nullbuf;
00095     
00096 public:
00097     // a null string, converted to char* as "(nil)"
00098     static const WvFastString null;
00099 
00106     WvFastString();
00107     void setsize(size_t i);
00108     
00115     WvFastString(const WvFastString &s);
00116     WvFastString(const WvString &s);
00117     
00124     WvFastString(const char *_str);
00125     
00130     WvFastString(const QString &s);
00131     WvFastString(const QCString &s);
00132 
00137     WvFastString(short i);
00138     WvFastString(unsigned short i);
00139     WvFastString(int i);
00140     WvFastString(unsigned int i);
00141     WvFastString(long i);
00142     WvFastString(unsigned long i);
00143     WvFastString(long long i);
00144     WvFastString(unsigned long long i);
00145     WvFastString(double i);
00146     
00148     static void do_format(WvFastString &output, const char *format,
00149                           const WvFastString * const *a);
00150     
00151     
00168     WvFastString(WVSTRING_FORMAT_DECL) 
00169     {
00170         const WvFastString *x[20];
00171         
00172         if (&__wvs_a0  != &null) x[ 0] = &__wvs_a0;
00173         if (&__wvs_a1  != &null) x[ 1] = &__wvs_a1;
00174         if (&__wvs_a2  != &null) x[ 2] = &__wvs_a2;
00175         if (&__wvs_a3  != &null) x[ 3] = &__wvs_a3;
00176         if (&__wvs_a4  != &null) x[ 4] = &__wvs_a4;
00177         if (&__wvs_a5  != &null) x[ 5] = &__wvs_a5;
00178         if (&__wvs_a6  != &null) x[ 6] = &__wvs_a6;
00179         if (&__wvs_a7  != &null) x[ 7] = &__wvs_a7;
00180         if (&__wvs_a8  != &null) x[ 8] = &__wvs_a8;
00181         if (&__wvs_a9  != &null) x[ 9] = &__wvs_a9;
00182         if (&__wvs_a10 != &null) x[10] = &__wvs_a10;
00183         if (&__wvs_a11 != &null) x[11] = &__wvs_a11;
00184         if (&__wvs_a12 != &null) x[12] = &__wvs_a12;
00185         if (&__wvs_a13 != &null) x[13] = &__wvs_a13;
00186         if (&__wvs_a14 != &null) x[14] = &__wvs_a14;
00187         if (&__wvs_a15 != &null) x[15] = &__wvs_a15;
00188         if (&__wvs_a16 != &null) x[16] = &__wvs_a16;
00189         if (&__wvs_a17 != &null) x[17] = &__wvs_a17;
00190         if (&__wvs_a18 != &null) x[18] = &__wvs_a18;
00191         if (&__wvs_a19 != &null) x[19] = &__wvs_a19;
00192         
00193         link(&nullbuf, NULL);
00194         do_format(*this, __wvs_format.str, x);
00195     }
00196     
00197     ~WvFastString();
00198     
00199     /*
00200      * Figure out the length of this string.  ==0 if NULL or empty.
00201      */
00202     size_t len() const;
00203 
00204 protected:
00205     // this doesn't exist - it's just here to keep it from being auto-created
00206     // by stupid C++.
00207     WvFastString &operator= (const WvFastString &s2);
00208     
00209     // connect/disconnect ourselves from a WvStringBuf.
00210     void link(WvStringBuf *_buf, const char *_str);
00211     void unlink();
00212     
00213     // allocate new space for buffers - needed only by the (int i) constructor,
00214     // for now.
00215     WvStringBuf *alloc(size_t size);
00216     void newbuf(size_t size);
00217     
00218 public:
00219     // string comparison
00220     bool operator== (WvStringParm s2) const;
00221     bool operator!= (WvStringParm s2) const;
00222     bool operator< (WvStringParm s2) const;
00223     bool operator== (const char *s2) const;
00224     bool operator!= (const char *s2) const;
00225     bool operator< (const char *s2) const;
00226     
00228     bool operator! () const;
00229 
00230     // pointer arithmetic
00231     const char *operator+ (int i) const
00232         { return str + i; }
00233     const char *operator- (int i) const
00234         { return str - i; }
00235     
00237     operator const char*() const
00238         { return str; }
00239     
00245     const char *cstr() const
00246         { return str; }
00247     
00252     operator QString() const;
00253     
00258     int num() const
00259         { return str ? atoi(str) : 0; }
00260     
00262     bool isnull() const
00263         { return str == NULL; }
00264     
00266     const WvFastString &ifnull(WvStringParm defval) const
00267         { return isnull() ? defval : *this; }
00268 };
00269 
00270 
00301 class WvString : public WvFastString
00302 {
00303 public:
00304     // an empty string, converted to char* as ""
00305     static const WvString empty;
00306  
00307     WvString() {} // nothing special needed
00308     WvString(short i) : WvFastString(i) { } // nothing special
00309     WvString(unsigned short i) : WvFastString(i) { } // nothing special
00310     WvString(int i) : WvFastString(i) { } // nothing special
00311     WvString(unsigned int i) : WvFastString(i) { } // nothing special
00312     WvString(long i) : WvFastString(i) { } // nothing special
00313     WvString(unsigned long i) : WvFastString(i) { } // nothing special
00314     WvString(long long i) : WvFastString(i) { } // nothing special
00315     WvString(unsigned long long i) : WvFastString(i) { } // nothing special
00316     WvString(double i) : WvFastString(i) { } // nothing special
00317     
00324     WvString(const WvString &s)
00325         { copy_constructor(s); }
00326     WvString(const WvFastString &s)
00327         { copy_constructor(s); }
00328     
00335     WvString(const char *_str);
00336 
00341     WvString(const QString &);
00342     WvString(const QCString &);
00343 
00344     WvString(WVSTRING_FORMAT_DECL) : WvFastString(WVSTRING_FORMAT_CALL)
00345         { }
00346     
00347     WvString &append(WvStringParm s);
00348     WvString &append(WVSTRING_FORMAT_DECL)
00349         { return append(WvString(WVSTRING_FORMAT_CALL)); }
00350     
00351     WvString &operator= (int i);
00352     WvString &operator= (const WvFastString &s2);
00353     WvString &operator= (const char *s2)
00354         { return *this = WvFastString(s2); }
00355     
00357     WvString &unique();
00358 
00360     char *edit()
00361         { return unique().str; }
00362     
00363 protected:
00364     void copy_constructor(const WvFastString &s);
00365 
00366 };
00367 
00368 
00375 class WvStringStar : public WvFastString
00376 {
00377 public:
00378     WvStringStar(WvStringParm s) : WvFastString(s)
00379         { }
00380     WvFastString *operator -> ()
00381         { return this; }
00382 };
00383 
00384 
00385 inline bool operator== (const char *s1, WvStringParm s2)
00386 {
00387     return s2 == s1;
00388 }
00389 
00390 
00391 inline bool operator!= (const char *s1, WvStringParm s2)
00392 {
00393     return s2 != s1;
00394 }
00395 
00396 #endif // __WVSTRING_H

Generated on Sun Apr 3 14:46:41 2005 for WvStreams by  doxygen 1.4.2