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

wvbuffer.h

Go to the documentation of this file.
00001 /*
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
00004  * 
00005  * Declarations for WvMiniBuffer (a statically-sized data buffer with
00006  * get/put functions) and WvBuffer (a dynamically-sized buffer made from
00007  * a list of WvMiniBuffers).
00008  */
00009 #ifndef __WVBUFFER_H
00010 #define __WVBUFFER_H
00011 
00012 #include <string.h>
00013 #include "wvlinklist.h"
00014 #include "wvstring.h"
00015 #include <string.h>
00016 
00017 
00018 class WvMiniBuffer
00019 {
00020     unsigned char *buffer, *head, *tail;
00021     size_t size;
00022     
00023 public:
00024     WvMiniBuffer(size_t _size)
00025         { buffer = head = tail = new unsigned char[(size = _size) + 16]; }
00026     ~WvMiniBuffer()
00027         { delete buffer; }
00028     
00029     /*
00030      * return number of bytes total/used/left in minibuffer. Note that
00031      * used+left != total, because bytes cannot be "recycled" until we do a
00032      * zap().  That is, the sequence put(15); get(15); causes used() to
00033      * return the same value as before, and free() to be reduced by 15 bytes.
00034      */
00035     size_t total() const
00036         { return size; }
00037     size_t used() const
00038         { return tail - head; }
00039     size_t free() const
00040         { return buffer + size - tail; }
00041     
00042     /*
00043      * remove all data from the minibuffer by setting head/tail to buffer
00044      * start
00045      */
00046     void zap()
00047        { head = tail = buffer; }
00048     
00049     
00050     /*
00051      * NO ERROR CHECKING in any of the following!!
00052      */
00053    
00054 
00055     /*
00056      * return a pointer to the next 'num' bytes in the minibuffer; valid
00057      * until buffer is zapped.
00058      */
00059     unsigned char *get(size_t num)
00060         { return (head += num) - num; }
00061     
00062     /*
00063      * Reverse a previous get() operation, making the last 'num' bytes read
00064      * available for a subsequent get().
00065      */
00066     void unget(size_t num)
00067         { head -= num; }
00068     
00069     /*
00070      * allocate the next 'num' bytes of the minibuffer (presumably for
00071      * writing)
00072      */
00073     unsigned char *alloc(size_t num)
00074         { return (tail += num) - num; }
00075 
00076     /*
00077      * Reverse a previous alloc() operation, making the last 'num' bytes
00078      * allocated available for a subsequent alloc() or put().
00079      */
00080     void unalloc(size_t num)
00081         { tail -= num; }
00082     
00083     /*
00084      * copy the given data into the next 'num' bytes of the minibuffer.
00085      */
00086     void put(const void *data, size_t num)
00087         { memcpy(alloc(num), data, num); }
00088     
00089     /*
00090      * return the number of bytes that must be retrieved with get() in order
00091      * to find the first instance of 'ch'.  A return value of 0 means that
00092      * there is no 'ch' in the minibuffer.
00093      */
00094     size_t strchr(unsigned char ch) const;
00095     size_t strchr(char ch) const
00096         { return strchr((unsigned char)ch); }
00097     
00098     /*
00099      * Count the number of leading bytes that match any in chlist.
00100      * If reverse==true, match bytes that are _not_ in chlist.
00101      */
00102     size_t match(const unsigned char chlist[], size_t numch,
00103                    bool reverse = false) const;
00104     size_t match(const char chlist[], bool reverse = false) const
00105         { return match((const unsigned char *)chlist,
00106                          strlen(chlist), reverse); }
00107 };
00108 
00109 
00110 DeclareWvList(WvMiniBuffer);
00111 
00112 
00113 class WvBuffer
00114 {
00115     WvMiniBufferList list;
00116     size_t inuse;
00117     
00118 public:
00119     WvBuffer()
00120         { inuse = 0; }
00121     
00122     size_t used() const
00123         { return inuse; }
00124 
00125     /*
00126      * Clear the entire buffer.
00127      */
00128     void zap();
00129     
00130     /*
00131      * Return the next 'num' bytes in the buffer.  Pointer is valid until
00132      * next zap() or get().  Returns NULL if there are not enough bytes
00133      * in the buffer.
00134      */
00135     unsigned char *get(size_t num);
00136     
00137     /*
00138      * Undo all or part of the previous get().  You can unget() up to the
00139      * number of bytes you did in the last get(), assuming you have not done
00140      * any other buffer operations in the meantime.
00141      */
00142     void unget(size_t num);
00143     
00144     /*
00145      * allocate 'num' bytes in the buffer and return a pointer to its start.
00146      * Pointer is valid until next zap() or get().
00147      */
00148     unsigned char *alloc(size_t num);
00149 
00150     /*
00151      * unallocate the last 'num' bytes in the buffer that were previously
00152      * allocated using alloc() or put().  They are then available for a
00153      * subsequent alloc() or put().
00154      */
00155     void unalloc(size_t num);
00156     
00157     /* 
00158      * copy 'buf' into the next 'num' bytes of buffer.
00159      */
00160     void put(const void *buf, size_t num);
00161     
00162     /*
00163      * copy a WvString into the buffer, not including the terminating nul.
00164      */
00165     void put(const WvString &str);
00166     
00167     /*
00168      * _move_ (not copy) the contents of another WvBuffer into this buffer.
00169      * This is done by physically taking the WvMiniBuffer objects from one
00170      * buffer and adding them at the end of this one.
00171      */
00172     void merge(WvBuffer &buf);
00173     
00174     /*
00175      * Return the entire buffer as a nul-terminated WvString.  If the buffer
00176      * contains nul characters, they'll seem to terinate the string.
00177      */
00178     WvString getstr();
00179 
00180     /*
00181      * return the number of bytes that would have to be read to find the
00182      * first character 'ch', or zero if 'ch' is not in the buffer.
00183      */
00184     size_t strchr(unsigned char ch);
00185     size_t strchr(char ch)
00186         { return strchr((unsigned char)ch); }
00187 
00188     /*
00189      * return the number of leading bytes that match any in chlist.
00190      * If reverse==true, match bytes that are NOT in chlist.
00191      */
00192     size_t match(const unsigned char chlist[], size_t numch,
00193                    bool reverse = false);
00194     size_t match(const char chlist[], bool reverse = false)
00195         { return match((const unsigned char *)chlist, strlen(chlist),
00196                          reverse); }
00197     
00198     int num_of_bufs()
00199         { return list.count(); }
00200 };
00201 
00202 
00203 #endif // __WVBUFFER_H

Generated on Sat Aug 24 21:09:33 2002 for WvStreams by doxygen1.2.15