00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef SWBUF_H
00023 #define SWBUF_H
00024
00025 #include <defs.h>
00026 #include <stdlib.h>
00027
00028 SWORD_NAMESPACE_START
00029
00030
00031 #define JUNKBUFSIZE 8191
00032
00036 class SWDLLEXPORT SWBuf {
00037 char *buf;
00038 char *end;
00039 char fillByte;
00040 unsigned int allocSize;
00041 static char *nullStr;
00042 static char junkBuf[JUNKBUFSIZE];
00043
00044 inline void assureSize(unsigned int newsize) {
00045 if (newsize > allocSize) {
00046 allocSize = newsize + 5;
00047 end = (char *)(end - buf);
00048 buf = (char *)realloc(buf, allocSize);
00049 end = (buf + (unsigned int)end);
00050 }
00051 }
00052 void init();
00053
00054 public:
00060 SWBuf(const char *initVal = 0);
00061
00067 SWBuf(char initVal);
00068
00074 SWBuf(const SWBuf &other);
00075
00076 inline void setFillByte(char ch) { fillByte = ch; }
00077 inline char getFillByte() { return fillByte; }
00078
00082 virtual ~SWBuf();
00083
00087 inline const char *c_str() const{ return buf; }
00088
00093 inline char &charAt(unsigned int pos) { return ((pos <= (unsigned int)(end - buf)) ? buf[pos] : nullStr[0]); }
00094
00099 inline unsigned int size() const { return length(); }
00100
00105 inline unsigned int length() const { return end - buf; }
00106
00112 void set(const char *newVal);
00113
00119 void set(const SWBuf &newVal);
00120
00125 void setSize(unsigned int len);
00126
00132 void append(const char *str);
00133
00139 void append(const SWBuf &str);
00140
00146 inline void append(char ch) {
00147 assureSize((end-buf) + 2);
00148 *end = ch;
00149 end++;
00150 *end = 0;
00151 }
00152
00160 void appendFormatted(const char *format, ...);
00161
00162 inline char *getRawData() { return buf; }
00163
00164 inline operator const char *() const { return c_str(); }
00165 inline char &operator[](unsigned int pos) { return charAt(pos); }
00166 inline char &operator[](int pos) { return charAt((unsigned int)pos); }
00167 inline SWBuf &operator =(const char *newVal) { set(newVal); return *this; }
00168 inline SWBuf &operator =(const SWBuf &other) { set(other); return *this; }
00169 inline SWBuf &operator +=(const char *str) { append(str); return *this; }
00170 inline SWBuf &operator +=(char ch) { append(ch); return *this; }
00171 inline SWBuf &operator -=(unsigned int len) { setSize(length()-len); return *this; }
00172 inline SWBuf &operator --(int) { operator -=(1); return *this; }
00173 inline SWBuf operator +(const SWBuf &other) const {
00174 SWBuf retVal = buf;
00175 retVal += other;
00176 return retVal;
00177 }
00178 inline SWBuf operator +(char ch) const { return (*this) + SWBuf(ch); }
00179 };
00180
00181
00182
00183 SWORD_NAMESPACE_END
00184 #endif