Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   Related Pages  

swbuf.h

00001 /******************************************************************************
00002 *  swbuf.h  - code for SWBuf used as a transport and utility for data buffers
00003 *
00004 * $Id: swbuf_8h-source.html,v 1.1 2003/02/28 21:21:55 mgruner Exp $
00005 *
00006 * Copyright 2003 CrossWire Bible Society (http://www.crosswire.org)
00007 *       CrossWire Bible Society
00008 *       P. O. Box 2528
00009 *       Tempe, AZ  85280-2528
00010 *
00011 * This program is free software; you can redistribute it and/or modify it
00012 * under the terms of the GNU General Public License as published by the
00013 * Free Software Foundation version 2.
00014 *
00015 * This program is distributed in the hope that it will be useful, but
00016 * WITHOUT ANY WARRANTY; without even the implied warranty of
00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 * General Public License for more details.
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; }       // be careful!  Probably setSize needs to be called in conjunction before and maybe after
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

Generated on Fri Feb 28 22:01:38 2003 for The Sword Project by doxygen1.3-rc3