Some cleanup in stdDomains.* and documentation changes.
[fic.git] / fileUtil.h
blobeac750816ac3b50fe36c518ec00d96f36ace0fc6
1 #ifndef FILEUTIL_HEADER_
2 #define FILEUTIL_HEADER_
4 #include "headers.h"
6 template <class T> inline void put(std::ostream &os,T i);
7 template <class T> inline T get(std::istream &is);
11 template<> inline void put(std::ostream &os,Uchar i) {
12 os.put(i);
14 template<> inline Uchar get(std::istream &is) {
15 return is.get();
18 template<> inline void put(std::ostream &os,Uint16 i) {
19 put<Uchar>(os,i/256);
20 put<Uchar>(os,i%256);
22 template<> inline Uint16 get(std::istream &is) {
23 Uint16 res= get<Uchar>(is);
24 return res*256+get<Uchar>(is);
27 template<> inline void put(std::ostream &os,Uint32 i) {
28 put<Uchar>(os,i/(256*256*256));
29 put<Uchar>(os,i/(256*256)%256);
30 put<Uchar>(os,i/256%256);
31 put<Uchar>(os,i%256);
33 template<> inline Uint32 get(std::istream &is) {
34 Uint16 res= 0;
35 for (int i=0; i<4; ++i)
36 res= res*256+get<Uchar>(is);
37 return res;
40 template<> inline void put(std::ostream &os,float f) {
41 ASSERT( sizeof(float)==4 );
42 os.write( (const char*)&f, sizeof(float) );
44 template<> inline float get(std::istream &is) {
45 ASSERT( sizeof(float)==4 );
46 float result;
47 is.read( (char*)&result, sizeof(float) );
48 return result;
51 inline int float01ToBits(Real f,int bitCount) {
52 int result= (int)std::ldexp(f,bitCount);
53 return result==powers[bitCount] ? result-1 : result;
55 inline Real float01FromBits(int bits,int bitCount) {
56 return std::ldexp( Real(bits)+Real(0.5), -bitCount);
60 /** Stream bit-writer - automated buffer for writing single bits */
61 class BitWriter {
62 /** Buffered bits, number of buffered bits */
63 int buffer, bufbits;
64 /** Used output (byte)stream */
65 std::ostream &os;
66 public:
67 /** Constructor just associates the object with the given stream */
68 BitWriter(std::ostream &stream)
69 : buffer(0), bufbits(0), os(stream) {}
70 /** Destructor only flushes the buffer */
71 ~BitWriter()
72 { flush(); }
73 /** Puts bits */
74 void putBits(int val,int bits) {
75 ASSERT( bits>0 && 0<=val && val<powers[bits] );
76 buffer+= powers[bufbits]*val;
77 bufbits+= bits;
78 while (bufbits>=8) {
79 bufbits-= 8;
80 os.put(buffer%256);
81 buffer/= 256;
84 /** Flushes the buffer - sends it to the stream */
85 void flush() {
86 if (bufbits)
87 os.put(buffer);
88 buffer= bufbits= 0;
92 /** Stream bit-reader - automated buffer for reading single bits */
93 class BitReader {
94 /** Buffered bits, number of buffered bits */
95 int buffer, bufbits;
96 /** Used input (byte)stream */
97 std::istream &is;
98 public:
99 /** Constructor just associates the object with the given stream */
100 BitReader(std::istream &stream)
101 : buffer(0), bufbits(0), is(stream) {}
102 /** Reads bits */
103 int getBits(int bits) {
104 ASSERT(bits>0);
105 while (bufbits<bits) {
106 buffer+= powers[bufbits]*Uchar(is.get());
107 bufbits+= 8;
109 int result= buffer%powers[bits];
110 buffer/= powers[bits];
111 bufbits-= bits;
112 return result;
114 /** Clears buffer */
115 void flush() {
116 buffer= bufbits= 0;
120 inline bool file2string(const char *name,std::string &result) {
121 using namespace std;
122 ifstream file( name, ios_base::binary|ios_base::in );
123 if (!file.good())
124 return false;
126 file.seekg(0,ios::end);
127 int length= file.tellg();
128 if (!length)
129 return false;
131 string res;
132 res.resize(length);
133 file.seekg(0,ios::beg);
134 file.read(&res[0],length);
135 if (file.gcount()!=length)
136 return false;
137 swap(res,result);
138 return true;
141 #endif // FILEUTIL_HEADER_