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

wvbuf.h

Go to the documentation of this file.
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 "wvbufbase.h"
00011 #include "wvstring.h"
00012 
00013 /***** Specialization for 'unsigned char' buffers *****/
00014 
00015 /**
00016  * Specialization of WvBufBase for unsigned char type
00017  * buffers intended for use with raw memory buffers.
00018  * Refines the interface to add support for untyped pointers.
00019  * Adds some useful string operations.
00020  */
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 
00029     /**
00030      * Copies a WvString into the buffer, excluding the null-terminator.
00031      * "str" is the string
00032      */
00033     void putstr(WvStringParm str);
00034     void putstr(WVSTRING_FORMAT_DECL)
00035         { putstr(WvString(WVSTRING_FORMAT_CALL)); }
00036 
00037     /**
00038      * Returns the entire buffer as a null-terminated WvString.
00039      * 
00040      * If the buffer contains null characters, they will seem to
00041      * prematurely terminate the string.
00042      * 
00043      * After this operation, ungettable() >= length of the string.
00044      * 
00045      * Returns: the buffer contents as a string
00046      */
00047     WvString getstr();
00048 
00049     /**
00050      * Returns the first len characters in the buffer.
00051      *
00052      * This is equivalent to doing a get(len), but returns it as a WvString
00053      * instead of as an unsigned char *.
00054      */
00055     WvString getstr(size_t len);
00056 
00057     /*** Get/put characters as integer values ***/
00058 
00059     /**
00060      * Returns a single character from the buffer as an int.
00061      * 
00062      * The same constraints apply as for get(1).
00063      * 
00064      * Returns: the character
00065      */
00066     int getch()
00067         { return int(get()); }
00068 
00069     /**
00070      * Puts a single character into the buffer as an int.
00071      * 
00072      * The same constraints apply as for alloc(1).
00073      * 
00074      * "ch" is the character
00075      */
00076     void putch(int ch)
00077         { put((unsigned char)ch); }
00078 
00079     /**
00080      * Peeks a single character from the buffer as an int.
00081      * 
00082      * The same constraints apply as for peek(offset, 1).
00083      * 
00084      * "offset" is the offset
00085      * Returns: the character
00086      */
00087     int peekch(int offset = 0)
00088         { return int(peek(offset)); }
00089     
00090     /**
00091      * Returns the number of characters that would have to be read
00092      * to find the first instance of the character.
00093      * "ch" is the character
00094      * Returns: the number of bytes, or zero if the character is not
00095      *         in the buffer
00096      */
00097     size_t strchr(int ch);
00098 
00099     /**
00100      * Returns the number of leading buffer elements that match
00101      * any of those in the list.
00102      * "bytelist" is the list bytes to search for
00103      * "numbytes" is the number of bytes in the list
00104      * Returns: the number of leading buffer elements that match
00105      */
00106     size_t match(const void *bytelist, size_t numbytes)
00107         { return _match(bytelist, numbytes, false); }
00108         
00109     /**
00110      * Returns the number of leading buffer elements that match
00111      * any of those in the list.
00112      * "chlist" is a string of characters to search for
00113      * Returns: the number of leading buffer elements that match
00114      */
00115     size_t match(const char *chlist)
00116         { return match(chlist, strlen(chlist)); }
00117 
00118     /**
00119      * Returns the number of leading buffer elements that do not
00120      * match any of those in the list.
00121      * "bytelist" is the list bytes to search for
00122      * "numbytes" is the number of bytes in the list
00123      * Returns: the number of leading buffer elements that don't match
00124      */
00125     size_t notmatch(const void *bytelist, size_t numbytes)
00126         { return _match(bytelist, numbytes, true); }
00127 
00128     /**
00129      * Returns the number of leading buffer elements that do not
00130      * match any of those in the list.
00131      * "chlist" is a string of characters to search for
00132      * Returns: the number of leading buffer elements that don't match
00133      */
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 
00160 /**
00161  * The in place raw memory buffer type.
00162  * Refines the interface to add support for untyped pointers.
00163  */
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 
00183 /**
00184  * The const in place raw memory buffer type.
00185  * Refines the interface to add support for untyped pointers.
00186  */
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 
00202 /**
00203  * The circular in place raw memory buffer type.
00204  * Refines the interface to add support for untyped pointers.
00205  */
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 
00225 /** The base raw memory buffer type. */
00226 typedef WvBufBase<unsigned char> WvBuf;
00227 
00228 /** The dynamically resizing raw memory buffer type. */
00229 typedef WvDynBufBase<unsigned char> WvDynBuf;
00230 
00231 /** The empty raw memory buffer type. */
00232 typedef WvNullBufBase<unsigned char> WvNullBuf;
00233 
00234 /** The raw memory buffer cursor type. */
00235 typedef WvBufCursorBase<unsigned char> WvBufCursor;
00236 
00237 /** The raw memory buffer view type. */
00238 typedef WvBufViewBase<unsigned char> WvBufView;
00239 
00240 /** A raw memory read-only buffer backed by a constant WvString */
00241 class WvConstStringBuffer : public WvConstInPlaceBuf
00242 {
00243     WvString xstr;
00244 
00245 public:
00246     /**
00247      * Creates a new buffer backed by a constant string.
00248      *
00249      * "_str" is the string
00250      */
00251     explicit WvConstStringBuffer(WvStringParm _str);
00252 
00253     /** Creates a new empty buffer backed by a null string. */
00254     WvConstStringBuffer();
00255 
00256     /**
00257      * Resets the buffer contents to a new string.
00258      *
00259      * "_str" is the new string
00260      */
00261     void reset(WvStringParm _str);
00262 
00263     /**
00264      * Returns the string that backs the array
00265      *
00266      * Returns: the string
00267      */
00268     WvString str()
00269         { return xstr; }
00270 };
00271 
00272 #endif // __WVBUFFER_H

Generated on Wed Dec 15 15:08:10 2004 for WvStreams by  doxygen 1.3.9.1