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

buffer.h

00001 /*
00002  * Buffer class header
00003  *
00004  * Copyright (C) 2001 Barnaby Gray <barnaby@beedesign.co.uk>.
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00019  *
00020  */
00021 
00022 #ifndef BUFFER_H
00023 #define BUFFER_H
00024 
00025 #include <vector>
00026 #include <iostream>
00027 #include <iomanip.h>
00028 #include <string>
00029 #include <iterator>
00030 
00031 #include <libicq2000/Translator.h>
00032 
00033 using std::vector;
00034 using std::string;
00035 using std::ostream;
00036 
00037 using ICQ2000::Translator;
00038 
00039 class Buffer {
00040  public:
00041   typedef unsigned int size_type;
00042   
00043   enum endian { BIG, LITTLE };
00044 
00045   struct marker {
00046     size_type position;
00047     endian endianness;
00048     int size;
00049   };
00050 
00051  private:
00052   typedef vector<unsigned char>::iterator iterator;
00053 
00054   vector<unsigned char> m_data;
00055   endian m_endn;
00056   size_type m_out_pos;
00057   Translator *m_translator;
00058 
00059  public:
00060   Buffer(Translator *translator);
00061   Buffer(const unsigned char *d, int size, Translator *translator); 
00062   // construct from an array
00063   Buffer(Buffer& b, unsigned int start, unsigned int data_len); // construct by copying from another Buffer
00064 
00065   unsigned int size() const { return m_data.size(); }
00066   unsigned int pos() const { return m_out_pos; }
00067   unsigned int remains() const { return m_data.size() - m_out_pos; }
00068 
00069   iterator begin() { return m_data.begin(); }
00070   iterator end() { return m_data.end(); }
00071 
00072   void clear();
00073   bool empty();
00074   void advance(unsigned int ad) { m_out_pos += ad; }
00075   bool beforeEnd() const { return (m_out_pos < m_data.size()); }
00076   void setPos(unsigned int o) { m_out_pos = o; }
00077   void chopOffBuffer(Buffer& b, unsigned int sz);
00078 
00079   void setEndianness(endian e);
00080   void setBigEndian();
00081   void setLittleEndian();
00082 
00083   marker getAutoSizeShortMarker();
00084   marker getAutoSizeIntMarker();
00085   void setAutoSizeMarker(const marker& m);
00086 
00087   Buffer& operator<<(unsigned char);
00088   Buffer& operator<<(unsigned short);
00089   Buffer& operator<<(unsigned int);
00090   Buffer& operator<<(signed char l) { return (*this) << (unsigned char)l; }
00091   Buffer& operator<<(signed short l) { return (*this) << (unsigned short)l; }
00092   Buffer& operator<<(signed int l) { return (*this) << (unsigned int)l; }
00093   Buffer& operator<<(const string&);
00094 
00095   Buffer& operator>>(unsigned char&);
00096   Buffer& operator>>(unsigned short&);
00097   Buffer& operator>>(unsigned int&);
00098   Buffer& operator>>(string&);
00099 
00100   void Pack(const unsigned char *d, int size);
00101   void Pack(const string& s);
00102   void PackUint16StringNull(const string& s);
00103   void PackByteString(const string& s);
00104 
00105   void Unpack(string& s, int size);
00106   void Unpack(unsigned char *const d, int size);
00107   unsigned char UnpackChar();
00108   void UnpackUint32String(string& s);
00109   void UnpackUint16StringNull(string& s);
00110   void UnpackByteString(string& s);
00111 
00112   unsigned char& operator[](unsigned int p);
00113 
00114   void setTranslator(Translator *translator);
00115   void ServerToClient(string& szString);
00116   void ClientToServer(string& szString);
00117   void ServerToClient(char &_cChar);
00118   void ClientToServer(char &_cChar);
00119 
00120   void dump(ostream& out);
00121 };
00122 
00123 ostream& operator<<(ostream&,Buffer&);
00124 
00125 #endif

Generated on Tue Jan 15 12:24:34 2002 for libicq2000 by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001