Back to index

courier  0.68.2
buffer.h
Go to the documentation of this file.
00001 #ifndef       buffer_h
00002 #define       buffer_h
00003 
00004 
00005 #include      "config.h"
00006 #include      <string.h>
00007 
00009 //
00010 // Generic text/data buffer.  Not null-terminated by default.
00011 //
00012 // This class is used to store arbitrary text strings.  It is used by
00013 // the lexical scanner to build up text that's recognized as a token,
00014 // and the rest of the code to store strings.
00015 //
00017 
00018 class Buffer {
00019 
00020        unsigned char *buf;
00021        int    bufsize;
00022        int    buflength;
00023 public:
00024        Buffer() : buf(0), bufsize(0), buflength(0)      {}
00025        ~Buffer()     { if (buf)    delete[] buf; }
00026        const unsigned char *Ptr() const { return (buf); }
00027        operator const unsigned char *() const { return (buf); }
00028        operator const char *() const { return ((const char *)buf); }
00029        int    Int(const char * =0) const;
00030        int    Length() const { return (buflength); }
00031        void   Length(int l) { if (l < buflength) buflength=l; }
00032 
00033        Buffer(const Buffer &);            // UNDEFINED
00034        Buffer &operator=(const Buffer &);
00035 
00036        void   push(int c) { if (buflength < bufsize)
00037                             {
00038                                    buf[buflength]=c;
00039                                    ++buflength;
00040                             }
00041                             else   append(c); }
00042        int    pop() { return (buflength ? buf[--buflength]:0); }
00043        int    peek() { return (buflength ? buf[buflength-1]:0); }
00044        void   reset() { buflength=0; }
00045 
00046 private:
00047        void   append(int);
00048 public:
00049        void   append(const void *, int);
00050        void   set(const char *);
00051        void   append(const char *p) { append(p, strlen(p)); }
00052        void   set(unsigned long n) { buflength=0; append(n); }
00053        void   append(unsigned long n);
00054        void   append(double);
00055        Buffer &operator=(const char *p) { set(p); return (*this); }
00056        Buffer &operator += (const Buffer &p) { append(p.buf, p.buflength); return (*this); }
00057        Buffer &operator += (const char *p) { append(p, strlen(p)); return (*this); }
00058        Buffer &operator += (char c) { push(c); return (*this); }
00059        int    operator-(const Buffer &) const;
00060        int    operator-(const char *) const;
00061        int    operator==(const Buffer &b) const
00062               { return ( operator-(b) == 0); }
00063        int    operator==(const char *b) const
00064               { return ( operator-(b) == 0); }
00065 } ;
00066 
00067 #endif