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

more/io/binary_io.h

Go to the documentation of this file.
00001 //  Copyright (C) 2000--2002  Petter Urkedal
00002 //
00003 //  This file is free software; you can redistribute it and/or modify
00004 //  it under the terms of the GNU General Public License as published by
00005 //  the Free Software Foundation; either version 2 of the License, or
00006 //  (at your option) any later version.
00007 //
00008 //  This file is distributed in the hope that it will be useful,
00009 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 //  GNU General Public License for more details.
00012 //
00013 //  You should have received a copy of the GNU General Public License
00014 //  along with this program; if not, write to the Free Software
00015 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00016 //
00017 //  As a special exception, you may use this file as part of a free
00018 //  software library without restriction.  Specifically, if other files
00019 //  instantiate templates or use macros or inline functions from this
00020 //  file, or you compile this file and link it with other files to
00021 //  produce an executable, this file does not by itself cause the
00022 //  resulting executable to be covered by the GNU General Public
00023 //  License.  This exception does not however invalidate any other
00024 //  reasons why the executable file might be covered by the GNU General
00025 //  Public License.
00026 
00027 //  $Id: binary_io.h,v 1.2 2002/09/01 12:01:36 petter_urkedal Exp $
00028 
00029 
00030 //  Status: experimental
00031 
00032 
00033 #include <string>
00034 #include <iostream>
00035 
00036 #ifndef MORE_BINARY_IO_H
00037 #define MORE_BINARY_IO_H
00038 
00039 namespace more {
00040 namespace io {
00041 
00042   //  ---/  Sequential Streaming of Bits  /---
00043 
00044   /** \class bit_writer binary_io.h more/io/binary_io.h
00045    ** Utility class to write one bit at a time to a stream. */
00046   template< typename OutputStream >
00047     struct bit_writer {
00048         typedef OutputStream stream_type;
00049         typedef typename OutputStream::char_type char_type;
00050         bit_writer(stream_type& os_)
00051             : os(os_), bit(1 << 8*sizeof(char_type)), ch(0) {}
00052         ~bit_writer() {
00053             if (bit != (1 << 8*sizeof(char_type)))
00054                 os.put(ch);
00055         }
00056         void put(int x) {
00057             if (bit == 1) {
00058                 os.put(ch);
00059                 bit = 1 << 8*sizeof(char_type);
00060                 ch = 0;
00061             }
00062             bit >>= 1;
00063             if (x) ch |= bit;
00064         }
00065       private:
00066         stream_type& os;
00067         int bit;
00068         char_type ch;
00069     };
00070 
00071   /** \class bit_reader binary_io.h more/io/binary_io.h
00072    ** Utility class to read one bit at a time from a stream. */
00073   template< typename InputStream >
00074     struct bit_reader {
00075         typedef typename InputStream::char_type char_type;
00076         typedef InputStream stream_type;
00077         bit_reader(stream_type& is_) : is(is_), bit(1), ch(0) {}
00078         int get() {
00079             if (bit == 1) {
00080                 ch = is.get();
00081                 bit = 1 << 8*sizeof(char_type);
00082             }
00083             bit >>= 1;
00084             return (ch & bit)? 1 : 0;
00085         }
00086       private:
00087         stream_type& is;
00088         int bit;
00089         char_type ch;
00090     };
00091 
00092 
00093   //  ---/  Binary IO for Integers  /---
00094 
00095   //  These are used to write integral _numbers_ to a binary stream.
00096   //  Do not use these for raw data or bitmasks, as they pack the data
00097   //  assuming that small number occur more frequently than large.
00098 
00099   void binary_write(std::ostream& os, char x);
00100   void binary_read(std::istream& is, char& x);
00101 
00102   void binary_write(std::ostream& os, signed char x);
00103   void binary_write(std::ostream& os, unsigned char x);
00104   void binary_read(std::istream& is, signed char& x);
00105   void binary_read(std::istream& is, unsigned char& x);
00106 
00107   void binary_write(std::ostream& os, short x);
00108   void binary_write(std::ostream& os, unsigned short x);
00109   void binary_read(std::istream& is, short& x);
00110   void binary_read(std::istream& is, unsigned short& x);
00111 
00112   void binary_write(std::ostream& os, int x);
00113   void binary_write(std::ostream& os, unsigned int x);
00114   void binary_read(std::istream& is, int& x);
00115   void binary_read(std::istream& is, unsigned int& x);
00116 
00117   void binary_write(std::ostream& os, long x);
00118   void binary_write(std::ostream& os, unsigned long x);
00119   void binary_read(std::istream& is, long& x);
00120   void binary_read(std::istream& is, unsigned long& x);
00121 
00122 #ifdef MORE_HAVE_LONG_LONG
00123   void binary_write(std::ostream& os, long long x);
00124   void binary_write(std::ostream& os, unsigned long long x);
00125   void binary_read(std::istream& is, long long& x);
00126   void binary_read(std::istream& is, unsigned long long& x);
00127 #endif
00128 
00129   // bool
00130 
00131   inline void binary_write(std::ostream& os, bool x)
00132     { binary_write(os, x? 1 : 0); }
00133   inline void binary_read(std::istream& is, bool& x)
00134     { int i; binary_read(is, i); x = i; }
00135 
00136   // FP
00137 
00138   void binary_write(std::ostream& os, float const& x,
00139                     int prec = std::numeric_limits<float>::digits);
00140   void binary_write(std::ostream& os, double const& x,
00141                     int prec = std::numeric_limits<double>::digits);
00142   void binary_write(std::ostream& os, long double const& x,
00143                     int prec = std::numeric_limits<long double>::digits);
00144   void binary_read(std::istream& is, float& x);
00145   void binary_read(std::istream& is, double& x);
00146   void binary_read(std::istream& is, long double& x);
00147 
00148   // strings
00149 
00150   void binary_write(std::ostream&, std::string);
00151   void binary_read(std::istream&, std::string&);
00152 
00153   void binary_write_raw(std::ostream&, void const*, size_t);
00154   //  Read raw block of known size, checking the size:
00155   void binary_read_raw(std::istream&, void*, size_t);
00156   //  Read raw block of unknown size:
00157   size_t binary_read_raw_size(std::istream&);
00158   void binary_read_raw_data(std::istream&, void*, size_t);
00159 
00160 
00161   // Binary IO of floats
00162 
00163   typedef long double big_float;
00164 
00165   void binary_put_fp(std::ostream& os, big_float x, int prec);
00166   big_float binary_get_fp(std::istream& is);
00167 
00168 
00169   /*X Dump the rest of the binary encoded \a is to \a os. */
00170   void binary_dump(std::istream& is, std::ostream& os, bool print_values_p);
00171 
00172 }
00173 #if defined(MORE_BACKWARD) && MORE_BACKWARD < 20010615
00174 using namespace io;
00175 #endif
00176 }
00177 
00178 #endif

Generated on Sat Sep 7 19:11:15 2002 for more with Doxygen 1.2.13.1. Doxygen 1.2.13.1 is written and copyright 1997-2002 by Dimitri van Heesch.