00001 /*************************************************************************** 00002 * Copyright (C) 2001 by Rick L. Vinyard, Jr. * 00003 * rvinyard@cs.nmsu.edu * 00004 * * 00005 * This file is part of the bit library. * 00006 * * 00007 * The bit library is free software; you can redistribute it and/or * 00008 * modify it under the terms of the GNU General Public License * 00009 * version 3 as published by the Free Software Foundation. * 00010 * * 00011 * The bit 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 * General Public License for more details. * 00015 * * 00016 * You should have received a copy of the GNU General Public License * 00017 * along with this software. If not see <http://www.gnu.org/licenses/>. * 00018 ***************************************************************************/ 00019 #ifndef BSTREAM_H 00020 #define BSTREAM_H 00021 00022 #include <stdexcept> 00023 00029 namespace bit { 00030 00038 class bstream { 00039 public: 00040 bstream(); 00041 00042 virtual ~bstream(); 00043 00044 //bool is_big_endian() { return m_big_endian; } 00045 //bool is_little_endian() { return !m_big_endian; } 00046 bool is_host_big_endian() { 00047 return m_host_big_endian; 00048 } 00049 bool is_host_little_endian() { 00050 return !m_host_big_endian; 00051 } 00052 00053 //void big_endian(bool b=true) { m_big_endian = b; } 00054 //void little_endian(bool b=true) { m_big_endian = !b; } 00055 void host_big_endian(bool b=true) { 00056 m_host_big_endian = b; 00057 } 00058 void host_little_endian(bool b=true) { 00059 m_host_big_endian = !b; 00060 } 00061 00062 typedef enum State {WHOLE, BITS, OCTETS} State; 00063 00064 protected: 00065 //bool m_big_endian; 00066 bool m_host_big_endian; 00067 unsigned char m_leftoverbits; 00068 size_t m_numleftoverbits; 00069 static const int m_masks[8]; 00070 00071 State m_state; 00072 size_t m_stateval; 00073 00074 00075 }; 00076 00077 class bits { 00078 public: 00079 bits(size_t n): val(n) { } 00080 int val; 00081 }; 00082 00083 class octets { 00084 public: 00085 octets(size_t n): val(n) { } 00086 int val; 00087 }; 00088 00089 class whole { 00090 public: 00091 whole() { } 00092 }; 00093 00094 }; // namespace bit 00095 00096 #endif