Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

wvstring.h

Go to the documentation of this file.
00001 /*
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  * It leaves out many of the notational conveniences provided by other
00007  * string classes, because they waste too much CPU time and space.
00008  * 
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) const WvString &__wvs_##n = __wvs_n
00031 #define WVSTRING_FORMAT_DECL const WvString &__wvs_format, \
00032                 const WvString &__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 WvString;
00048 
00049 // WvStringBuf used for char* strings that have not been cloned.
00050 extern WvStringBuf __wvs_nb;
00051 
00052 // just an empty string
00053 extern const WvString __wvs_n;
00054 
00055 
00056 struct WvStringBuf
00057 {
00058     size_t size;        // string length - if zero, use strlen!!
00059     unsigned links;     // number of WvStrings using this buf.
00060     char data[1];       // optional room for extra string data
00061 };
00062 
00063 
00064 // the _actual_ space taken by a WvStringBuf, without the data[] array
00065 // (which is variable-sized, not really 1 byte)
00066 #define WVSTRINGBUF_SIZE(s) (s->data - (char *)s)
00067 
00098 class WvString
00099 {
00100     WvStringBuf *buf;
00101     char *str;
00102     
00103     void unlink();
00104     void link(WvStringBuf *_buf, const char *_str);
00105     
00106     WvStringBuf *alloc(size_t size);
00107     void newbuf(size_t size);
00108 
00109 public:
00116     WvString();
00117     void setsize(size_t i);
00118 
00122     WvString(const WvString &s);
00123 
00127     WvString(const char *_str);
00128 
00133     WvString(int i); // auto-render int 'i' into a string
00134 
00138     static void do_format(WvString &output, char *format, const WvString **a);
00139     
00156     WvString(WVSTRING_FORMAT_DECL)
00157     {
00158         const WvString *x[20];
00159         
00160         if (&__wvs_a0  != &__wvs_n) x[ 0] = &__wvs_a0;
00161         if (&__wvs_a1  != &__wvs_n) x[ 1] = &__wvs_a1;
00162         if (&__wvs_a2  != &__wvs_n) x[ 2] = &__wvs_a2;
00163         if (&__wvs_a3  != &__wvs_n) x[ 3] = &__wvs_a3;
00164         if (&__wvs_a4  != &__wvs_n) x[ 4] = &__wvs_a4;
00165         if (&__wvs_a5  != &__wvs_n) x[ 5] = &__wvs_a5;
00166         if (&__wvs_a6  != &__wvs_n) x[ 6] = &__wvs_a6;
00167         if (&__wvs_a7  != &__wvs_n) x[ 7] = &__wvs_a7;
00168         if (&__wvs_a8  != &__wvs_n) x[ 8] = &__wvs_a8;
00169         if (&__wvs_a9  != &__wvs_n) x[ 9] = &__wvs_a9;
00170         if (&__wvs_a10 != &__wvs_n) x[10] = &__wvs_a10;
00171         if (&__wvs_a11 != &__wvs_n) x[11] = &__wvs_a11;
00172         if (&__wvs_a12 != &__wvs_n) x[12] = &__wvs_a12;
00173         if (&__wvs_a13 != &__wvs_n) x[13] = &__wvs_a13;
00174         if (&__wvs_a14 != &__wvs_n) x[14] = &__wvs_a14;
00175         if (&__wvs_a15 != &__wvs_n) x[15] = &__wvs_a15;
00176         if (&__wvs_a16 != &__wvs_n) x[16] = &__wvs_a16;
00177         if (&__wvs_a17 != &__wvs_n) x[17] = &__wvs_a17;
00178         if (&__wvs_a18 != &__wvs_n) x[18] = &__wvs_a18;
00179         if (&__wvs_a19 != &__wvs_n) x[19] = &__wvs_a19;
00180         
00181         buf = NULL;
00182         do_format(*this, __wvs_format.str, x);
00183     }
00184     
00185     ~WvString();
00186     
00187     void append(const WvString &s);
00188     void append(WVSTRING_FORMAT_DECL)
00189         { append(WvString(WVSTRING_FORMAT_CALL)); }
00190     size_t len() const;
00191 
00192     WvString &operator= (const WvString &s2);
00193     
00197     WvString &unique();
00198 
00199     // string comparison
00200     bool operator== (const WvString &s2) const;
00201     bool operator!= (const WvString &s2) const;
00202     bool operator== (const char *s2) const;
00203     bool operator!= (const char *s2) const;
00204     
00208     bool operator! () const;
00209 
00210     // pointer arithmetic
00211     const char *operator+ (int i) const
00212         { return str + i; }
00213     const char *operator- (int i) const
00214         { return str - i; }
00215     
00219     operator const char*() const
00220         { return str; }
00221     
00227     const char *cstr() const
00228         { return str; }
00229     
00233     char *edit()
00234         { return unique().str; }
00235     
00240     int num() const
00241         { return str ? atoi(str) : 0; }
00242     
00243 };
00244 
00245 
00246 inline bool operator== (const char *s1, const WvString &s2)
00247 {
00248     return s2 == s1;
00249 }
00250 
00251 
00252 inline bool operator!= (const char *s1, const WvString &s2)
00253 {
00254     return s2 != s1;
00255 }
00256 
00257 
00258 #endif // __WVSTRING_H

Generated on Sat Aug 24 21:37:02 2002 for WvStreams by doxygen1.2.15