00001 #ifndef FILEUTIL_HEADER_
00002 #define FILEUTIL_HEADER_
00003
00004 #include "headers.h"
00005
00007 template <class T> inline void put(std::ostream &os,T i);
00009 template <class T> inline T get(std::istream &is);
00010
00011
00012
00013 template<> inline void put(std::ostream &os,Uchar i) {
00014 os.put(i);
00015 }
00016 template<> inline Uchar get(std::istream &is) {
00017 return is.get();
00018 }
00019
00020 template<> inline void put(std::ostream &os,Uint16 i) {
00021 put<Uchar>(os,i/256);
00022 put<Uchar>(os,i%256);
00023 }
00024 template<> inline Uint16 get(std::istream &is) {
00025 Uint16 res= get<Uchar>(is);
00026 return res*256+get<Uchar>(is);
00027 }
00028
00029 template<> inline void put(std::ostream &os,Uint32 i) {
00030 put<Uchar>(os,i/(256*256*256));
00031 put<Uchar>(os,i/(256*256)%256);
00032 put<Uchar>(os,i/256%256);
00033 put<Uchar>(os,i%256);
00034 }
00035 template<> inline Uint32 get(std::istream &is) {
00036 Uint32 res= 0;
00037 for (int i=0; i<4; ++i)
00038 res= res*256+get<Uchar>(is);
00039 return res;
00040 }
00041
00042 template<> inline void put(std::ostream &os,float f) {
00043 ASSERT( sizeof(float)==4 );
00044 os.write( (const char*)&f, sizeof(float) );
00045 }
00046 template<> inline float get(std::istream &is) {
00047 ASSERT( sizeof(float)==4 );
00048 float result;
00049 is.read( (char*)&result, sizeof(float) );
00050 return result;
00051 }
00052
00054 inline int float01ToBits(Real f,int bitCount) {
00055 int result= (int)std::ldexp(f,bitCount);
00056 return result==powers[bitCount] ? result-1 : result;
00057 }
00059 inline Real float01FromBits(int bits,int bitCount) {
00060 return std::ldexp( Real(bits)+Real(0.5), -bitCount);
00061 }
00062
00063
00065 class BitWriter {
00067 int buffer, bufbits;
00069 std::ostream &os;
00070 public:
00072 BitWriter(std::ostream &stream)
00073 : buffer(0), bufbits(0), os(stream) {}
00075 ~BitWriter()
00076 { flush(); }
00078 void putBits(int val,int bits) {
00079 ASSERT( bits>=0 && 0<=val && val<powers[bits] );
00080 if (!bits)
00081 return;
00082 buffer+= powers[bufbits]*val;
00083 bufbits+= bits;
00084 while (bufbits>=8) {
00085 bufbits-= 8;
00086 os.put(buffer%256);
00087 buffer/= 256;
00088 }
00089 }
00091 void flush() {
00092 if (bufbits)
00093 os.put(buffer);
00094 buffer= bufbits= 0;
00095 }
00096 };
00097
00099 class BitReader {
00101 int buffer, bufbits;
00103 std::istream &is;
00104 public:
00106 BitReader(std::istream &stream)
00107 : buffer(0), bufbits(0), is(stream) {}
00109 int getBits(int bits) {
00110 ASSERT(bits>=0);
00111 if (!bits)
00112 return 0;
00113 while (bufbits<bits) {
00114 buffer+= powers[bufbits]*Uchar(is.get());
00115 bufbits+= 8;
00116 }
00117 int result= buffer%powers[bits];
00118 buffer/= powers[bits];
00119 bufbits-= bits;
00120 return result;
00121 }
00123 void flush() {
00124 buffer= bufbits= 0;
00125 }
00126 };
00127
00129 inline bool file2string(const char *name,std::string &result) {
00130 using namespace std;
00131 ifstream file( name, ios_base::binary|ios_base::in );
00132 if (!file.good())
00133 return false;
00134
00135 file.seekg(0,ios::end);
00136 int length= file.tellg();
00137 if (!length)
00138 return false;
00139
00140 string res;
00141 res.resize(length);
00142 file.seekg(0,ios::beg);
00143 file.read(&res[0],length);
00144 if (file.gcount()!=length)
00145 return false;
00146 swap(res,result);
00147 return true;
00148 }
00149
00150 #endif // FILEUTIL_HEADER_