00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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
00043
00044
00045
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
00072
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
00094
00095
00096
00097
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
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
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
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
00155 void binary_read_raw(std::istream&, void*, size_t);
00156
00157 size_t binary_read_raw_size(std::istream&);
00158 void binary_read_raw_data(std::istream&, void*, size_t);
00159
00160
00161
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
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