Various small fixes in documentation and settings.
[fic.git] / fileUtil.h
blob4bdf7ecfe9fb0a8c892707e2acf85babbdabfee7
1 #ifndef FILEUTIL_HEADER_
2 #define FILEUTIL_HEADER_
4 #include "headers.h"
6 /** Stores a value in a stream, specialized for various types (portably) */
7 template <class T> inline void put(std::ostream &os,T i);
8 /** Reconstructs a value from a stream, see ::put function */
9 template <class T> inline T get(std::istream &is);
13 template<> inline void put(std::ostream &os,Uchar i) {
14 os.put(i);
16 template<> inline Uchar get(std::istream &is) {
17 return is.get();
20 template<> inline void put(std::ostream &os,Uint16 i) {
21 put<Uchar>(os,i/256);
22 put<Uchar>(os,i%256);
24 template<> inline Uint16 get(std::istream &is) {
25 Uint16 res= get<Uchar>(is);
26 return res*256+get<Uchar>(is);
29 template<> inline void put(std::ostream &os,Uint32 i) {
30 put<Uchar>(os,i/(256*256*256));
31 put<Uchar>(os,i/(256*256)%256);
32 put<Uchar>(os,i/256%256);
33 put<Uchar>(os,i%256);
35 template<> inline Uint32 get(std::istream &is) {
36 Uint16 res= 0;
37 for (int i=0; i<4; ++i)
38 res= res*256+get<Uchar>(is);
39 return res;
42 template<> inline void put(std::ostream &os,float f) {
43 ASSERT( sizeof(float)==4 );
44 os.write( (const char*)&f, sizeof(float) );
46 template<> inline float get(std::istream &is) {
47 ASSERT( sizeof(float)==4 );
48 float result;
49 is.read( (char*)&result, sizeof(float) );
50 return result;
53 /** Converts a floating-point value from interval [0,1] to a bit sequence */
54 inline int float01ToBits(Real f,int bitCount) {
55 int result= (int)std::ldexp(f,bitCount);
56 return result==powers[bitCount] ? result-1 : result;
58 /** Converts a bit sequnce to a floating-point value, the inverse to ::float01ToBits function */
59 inline Real float01FromBits(int bits,int bitCount) {
60 return std::ldexp( Real(bits)+Real(0.5), -bitCount);
64 /** Stream bit-writer - automated buffer for writing single bits */
65 class BitWriter {
66 /** Buffered bits, number of buffered bits */
67 int buffer, bufbits;
68 /** Used output (byte)stream */
69 std::ostream &os;
70 public:
71 /** Constructor just associates the object with the given stream */
72 BitWriter(std::ostream &stream)
73 : buffer(0), bufbits(0), os(stream) {}
74 /** Destructor only flushes the buffer */
75 ~BitWriter()
76 { flush(); }
77 /** Puts bits */
78 void putBits(int val,int bits) {
79 ASSERT( bits>0 && 0<=val && val<powers[bits] );
80 buffer+= powers[bufbits]*val;
81 bufbits+= bits;
82 while (bufbits>=8) {
83 bufbits-= 8;
84 os.put(buffer%256);
85 buffer/= 256;
88 /** Flushes the buffer - sends it to the stream */
89 void flush() {
90 if (bufbits)
91 os.put(buffer);
92 buffer= bufbits= 0;
96 /** Stream bit-reader - automated buffer for reading single bits */
97 class BitReader {
98 /** Buffered bits, number of buffered bits */
99 int buffer, bufbits;
100 /** Used input (byte)stream */
101 std::istream &is;
102 public:
103 /** Constructor just associates the object with the given stream */
104 BitReader(std::istream &stream)
105 : buffer(0), bufbits(0), is(stream) {}
106 /** Reads bits */
107 int getBits(int bits) {
108 ASSERT(bits>0);
109 while (bufbits<bits) {
110 buffer+= powers[bufbits]*Uchar(is.get());
111 bufbits+= 8;
113 int result= buffer%powers[bits];
114 buffer/= powers[bits];
115 bufbits-= bits;
116 return result;
118 /** Clears buffer */
119 void flush() {
120 buffer= bufbits= 0;
124 /** Reads a whole file into std::string (\p result), returns \p true on success */
125 inline bool file2string(const char *name,std::string &result) {
126 using namespace std;
127 ifstream file( name, ios_base::binary|ios_base::in );
128 if (!file.good())
129 return false;
131 file.seekg(0,ios::end);
132 int length= file.tellg();
133 if (!length)
134 return false;
136 string res;
137 res.resize(length);
138 file.seekg(0,ios::beg);
139 file.read(&res[0],length);
140 if (file.gcount()!=length)
141 return false;
142 swap(res,result);
143 return true;
146 #endif // FILEUTIL_HEADER_