bit/ibstream.h

00001 /***************************************************************************
00002  *   Copyright (C) 2001 by Rick L. Vinyard, Jr.                            *
00003  *   rvinyard@cs.nmsu.edu                                                  *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation version 2.1.                *
00008  *                                                                         *
00009  *   This program is distributed in the hope that it will be useful,       *
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00012  *   GNU General Public License for more details.                          *
00013  *                                                                         *
00014  *   You should have received a copy of the GNU Lesser General Public      *
00015  *   License along with this library; if not, write to the                 *
00016  *   Free Software Foundation, Inc.,                                       *
00017  *   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA              *
00018  ***************************************************************************/
00019 #ifndef BITIBSTREAM_H
00020 #define BITIBSTREAM_H
00021 
00022 #include <istream>
00023 #include <cstring>
00024 #include <byteswap.h>
00025 
00026 #include <bit/bstream.h>
00027 
00028 namespace bit {
00029 
00030 
00037 class ibstream: public bstream {
00038 public:
00042     ibstream();
00043 
00048     ibstream(std::istream& input);
00049 
00053     virtual ~ibstream();
00054 
00062     size_t read_octets(unsigned char* buf, size_t octets);
00063 
00072     size_t read_octets(unsigned char* buf, size_t bufsize, size_t octets);
00073 
00078     size_t read_bits(unsigned char* buf, size_t bits);
00079 
00086     size_t read_bits(unsigned char* buf, size_t bufsize, size_t bits);
00087 
00094     template <typename T>
00095     size_t read_octets(T& t, size_t n=sizeof(T));
00096 
00097     size_t read_octets(uint16_t& t, size_t n=2);
00098     size_t read_octets(uint32_t& t, size_t n=4);
00099     size_t read_octets(uint64_t& t, size_t n=8);
00100     size_t read_octets(int16_t& t, size_t n=2);
00101     size_t read_octets(int32_t& t, size_t n=4);
00102     size_t read_octets(int64_t& t, size_t n=8);
00103 
00104 
00111     template <typename T>
00112     size_t read_bits(T& t, size_t n=sizeof(T)*8);
00113 
00114     size_t read_bits(uint16_t& t, size_t n=2);
00115     size_t read_bits(uint32_t& t, size_t n=4);
00116     size_t read_bits(uint64_t& t, size_t n=8);
00117     size_t read_bits(int16_t& t, size_t n=2);
00118     size_t read_bits(int32_t& t, size_t n=4);
00119     size_t read_bits(int64_t& t, size_t n=8);
00120 
00121 
00122     friend ibstream& operator>>(ibstream& s, const bits& b);
00123     friend ibstream& operator>>(ibstream& s, const octets& o);
00124     friend ibstream& operator>>(ibstream& s, const whole& w);
00125     template <typename T>
00126     friend ibstream& operator>>(ibstream& s, T& t);
00127 
00133     void attach_stream(std::istream& stream, bool clearbits=false);
00134 
00141     void detach_stream(bool clearbits=false);
00142 
00143 protected:
00148     std::istream* m_input;
00149 
00150 };
00151 
00152 template <typename T>
00153 inline        size_t ibstream::read_octets(T& t, size_t n) {
00154   if (!m_input) return 0;
00155 
00156   if (n > sizeof(T))
00157         n = sizeof(T);
00158     return read_octets( (unsigned char*) &t, n, sizeof(T));
00159 }
00160 
00161 template <typename T>
00162 inline size_t ibstream::read_bits(T& t, size_t n) {
00163   if (m_input == NULL && m_numleftoverbits == 0) return 0;
00164 
00165   if (n > sizeof(T) * 8 )
00166         n = sizeof(T) * 8;
00167    return read_bits( (unsigned char*) &t, n, sizeof(T));
00168 }
00169 
00170 inline ibstream& operator>>(ibstream& s, const bits& b) {
00171   s.m_state = bstream::BITS;
00172     s.m_stateval = b.val;
00173     return s;
00174 }
00175 
00176 inline ibstream& operator>>(ibstream& s, const octets& o) {
00177     s.m_state = bstream::OCTETS;
00178     s.m_stateval = o.val;
00179     return s;
00180 }
00181 
00182 inline ibstream& operator>>(ibstream& s, const whole& w) {
00183     s.m_state = bstream::WHOLE;
00184     s.m_stateval = 0;
00185     return s;
00186 }
00187 
00188 
00189 template <typename T>
00190 inline ibstream& operator>>(ibstream& s, T& t) {
00191     switch (s.m_state) {
00192     case bstream::WHOLE:
00193         s.read_octets(t);
00194         break;
00195     case bstream::BITS:
00196         s.read_bits(t, s.m_stateval);
00197         break;
00198     case bstream::OCTETS:
00199         s.read_octets(t, s.m_stateval);
00200         break;
00201     }
00202     return s;
00203 }
00204 
00205 
00206 }; // namespace bit
00207 
00208 #endif

Generated on Tue Mar 13 20:00:01 2007 by  doxygen 1.5.1