Main Page   Namespace List   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>
00028 #include <string>
00029 #include <iterator>
00030 
00031 #include <libicq2000/Translator.h>
00032 
00033 class Buffer {
00034  public:
00035   typedef unsigned int size_type;
00036   
00037   enum endian { BIG, LITTLE };
00038 
00039   struct marker {
00040     size_type position;
00041     endian endianness;
00042     int size;
00043   };
00044 
00045  private:
00046   typedef std::vector<unsigned char>::iterator iterator;
00047 
00048   std::vector<unsigned char> m_data;
00049   endian m_endn;
00050   size_type m_out_pos;
00051   ICQ2000::Translator *m_translator;
00052 
00053  public:
00054   Buffer(ICQ2000::Translator *translator);
00055   Buffer(const unsigned char *d, unsigned int size, ICQ2000::Translator *translator); 
00056   // construct from an array
00057   Buffer(Buffer& b, unsigned int start, unsigned int data_len); // construct by copying from another Buffer
00058 
00059   unsigned int size() const { return m_data.size(); }
00060   unsigned int pos() const { return m_out_pos; }
00061   unsigned int remains() const { return m_data.size() - m_out_pos; }
00062 
00063   iterator begin() { return m_data.begin(); }
00064   iterator end() { return m_data.end(); }
00065 
00066   void clear();
00067   bool empty();
00068   void advance(unsigned int ad) { m_out_pos += ad; }
00069   bool beforeEnd() const { return (m_out_pos < m_data.size()); }
00070   void setPos(unsigned int o) { m_out_pos = o; }
00071   void chopOffBuffer(Buffer& b, unsigned int sz);
00072 
00073   void setEndianness(endian e);
00074   void setBigEndian();
00075   void setLittleEndian();
00076 
00077   marker getAutoSizeShortMarker();
00078   marker getAutoSizeIntMarker();
00079   void setAutoSizeMarker(const marker& m);
00080 
00081   Buffer& operator<<(unsigned char);
00082   Buffer& operator<<(unsigned short);
00083   Buffer& operator<<(unsigned int);
00084   Buffer& operator<<(signed char l) { return (*this) << (unsigned char)l; }
00085   Buffer& operator<<(signed short l) { return (*this) << (unsigned short)l; }
00086   Buffer& operator<<(signed int l) { return (*this) << (unsigned int)l; }
00087   Buffer& operator<<(const std::string&);
00088 
00089   Buffer& operator>>(unsigned char&);
00090   Buffer& operator>>(unsigned short&);
00091   Buffer& operator>>(unsigned int&);
00092   Buffer& operator>>(signed char& l) { return (*this) >> (unsigned char&)l; }
00093   Buffer& operator>>(signed short& l) { return (*this) >> (unsigned short&)l; }
00094   Buffer& operator>>(signed int& l) { return (*this) >> (unsigned int&)l; }
00095   Buffer& operator>>(std::string&);
00096 
00097   void Pack(const unsigned char *d, unsigned int size);
00098   void Pack(const std::string& s);
00099   void PackUint16StringNull(const std::string& s);
00100   void PackUint16TranslatedNull(const std::string& s);
00101   void PackByteString(const std::string& s);
00102   void UnpackCRLFString(std::string& s);
00103 
00104   void Unpack(std::string& s, unsigned int size);
00105   void Unpack(unsigned char *const d, unsigned int size);
00106   unsigned char UnpackChar();
00107   void UnpackUint32String(std::string& s);
00108   void UnpackUint16StringNull(std::string& s);
00109   void UnpackUint16TranslatedNull(std::string& s);
00110   void UnpackByteString(std::string& s);
00111 
00112   unsigned char& operator[](unsigned int p);
00113 
00114   void setTranslator(ICQ2000::Translator *translator);
00115   void ServerToClient(std::string& szString);
00116   void ClientToServer(std::string& szString);
00117   std::string ServerToClientCC(const std::string& szString);
00118   std::string ClientToServerCC(const std::string& szString);
00119   void ServerToClient(char &_cChar);
00120   void ClientToServer(char &_cChar);
00121 
00122   void dump(std::ostream& out);
00123 };
00124 
00125 std::ostream& operator<<(std::ostream&,Buffer&);
00126 
00127 #endif

Generated on Sun Jul 21 10:57:32 2002 for libicq2000 by doxygen1.2.16