WvStreams
wvbuf.h
00001 /* -*- Mode: C++ -*-
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
00004  *
00005  * Specializations of the generic buffering API and a few new buffers.
00006  */
00007 #ifndef __WVBUFFER_H
00008 #define __WVBUFFER_H
00009  
00010 #include "wvstring.h"
00011 #include "wvbufbase.h"
00012 
00013 /***** Specialization for 'unsigned char' buffers *****/
00014 
00021 template <>
00022 class WvBufBase<unsigned char> :
00023     public WvBufBaseCommonImpl<unsigned char>
00024 {
00025 public:
00026     explicit WvBufBase(WvBufStore *store) :
00027         WvBufBaseCommonImpl<unsigned char>(store) { }
00028 
00033     void putstr(WvStringParm str);
00034     void putstr(WVSTRING_FORMAT_DECL)
00035         { putstr(WvString(WVSTRING_FORMAT_CALL)); }
00036 
00047     WvString getstr();
00048 
00055     WvString getstr(size_t len);
00056 
00057     /*** Get/put characters as integer values ***/
00058 
00066     int getch()
00067         { return int(get()); }
00068 
00076     void putch(int ch)
00077         { put((unsigned char)ch); }
00078 
00087     int peekch(int offset = 0)
00088         { return int(peek(offset)); }
00089     
00097     size_t strchr(int ch);
00098 
00106     size_t match(const void *bytelist, size_t numbytes)
00107         { return _match(bytelist, numbytes, false); }
00108         
00115     size_t match(const char *chlist)
00116         { return match(chlist, strlen(chlist)); }
00117 
00125     size_t notmatch(const void *bytelist, size_t numbytes)
00126         { return _match(bytelist, numbytes, true); }
00127 
00134     size_t notmatch(const char *chlist)
00135         { return notmatch(chlist, strlen(chlist)); }
00136 
00137     /*** Overload put() and move() to accept void pointers ***/
00138     
00139     void put(unsigned char value)
00140         { WvBufBaseCommonImpl<unsigned char>::put(value); }
00141     void put(const void *data, size_t count)
00142         { WvBufBaseCommonImpl<unsigned char>::put(
00143             (const unsigned char*)data, count); }
00144     void move(void *data, size_t count)
00145         { WvBufBaseCommonImpl<unsigned char>::move(
00146             (unsigned char*)data, count); }
00147     void poke(void *data, int offset, size_t count)
00148         { WvBufBaseCommonImpl<unsigned char>::poke(
00149             (unsigned char*)data, offset, count); }
00150 
00151 private:
00152     // moved here to avoid ambiguities between the match variants
00153     size_t _match(const void *bytelist, size_t numbytes, bool reverse);
00154 };
00155 
00156 
00157 
00158 /***** Declarations for some commonly used memory buffers *****/
00159 
00164 class WvInPlaceBuf : public WvInPlaceBufBase<unsigned char>
00165 {
00166 public:
00167     WvInPlaceBuf(void *_data, size_t _avail, size_t _size,
00168         bool _autofree = false) :
00169         WvInPlaceBufBase<unsigned char>((unsigned char*)_data,
00170             _avail, _size, _autofree) { }
00171     explicit WvInPlaceBuf(size_t _size) :
00172         WvInPlaceBufBase<unsigned char>(_size) { }
00173     WvInPlaceBuf() :
00174         WvInPlaceBufBase<unsigned char>() { }
00175     void reset(void *_data, size_t _avail, size_t _size,
00176         bool _autofree = false)
00177     {
00178         WvInPlaceBufBase<unsigned char>::reset(
00179             (unsigned char*)_data, _avail, _size, _autofree);
00180     }
00181 };
00182 
00187 class WvConstInPlaceBuf : public WvConstInPlaceBufBase<unsigned char>
00188 {
00189 public:
00190     WvConstInPlaceBuf(const void *_data, size_t _avail) :
00191         WvConstInPlaceBufBase<unsigned char>(
00192             (const unsigned char*)_data, _avail) { }
00193     WvConstInPlaceBuf() :
00194         WvConstInPlaceBufBase<unsigned char>() { }
00195     void reset(const void *_data, size_t _avail)
00196     {
00197         WvConstInPlaceBufBase<unsigned char>::reset(
00198             (const unsigned char*)_data, _avail);
00199     }
00200 };
00201 
00206 class WvCircularBuf : public WvCircularBufBase<unsigned char>
00207 {
00208 public:
00209     WvCircularBuf(void *_data, size_t _avail, size_t _size,
00210         bool _autofree = false) :
00211         WvCircularBufBase<unsigned char>((unsigned char*)_data,
00212             _avail, _size, _autofree) { }
00213     explicit WvCircularBuf(size_t _size) :
00214         WvCircularBufBase<unsigned char>(_size) { }
00215     WvCircularBuf() :
00216         WvCircularBufBase<unsigned char>() { }
00217     void reset(void *_data, size_t _avail, size_t _size,
00218         bool _autofree = false)
00219     {
00220         WvCircularBufBase<unsigned char>::reset(
00221             (unsigned char*)_data, _avail, _size, _autofree);
00222     }
00223 };
00224 
00226 typedef WvBufBase<unsigned char> WvBuf;
00227 
00229 typedef WvDynBufBase<unsigned char> WvDynBuf;
00230 
00232 typedef WvNullBufBase<unsigned char> WvNullBuf;
00233 
00235 typedef WvBufCursorBase<unsigned char> WvBufCursor;
00236 
00238 typedef WvBufViewBase<unsigned char> WvBufView;
00239 
00241 class WvConstStringBuffer : public WvConstInPlaceBuf
00242 {
00243     WvString xstr;
00244 
00245 public:
00251     explicit WvConstStringBuffer(WvStringParm _str);
00252 
00254     WvConstStringBuffer();
00255 
00261     void reset(WvStringParm _str);
00262 
00268     WvString str()
00269         { return xstr; }
00270 };
00271 
00272 #endif // __WVBUFFER_H