Writing - included generated graphs in the repo.
[fic.git] / fileUtil.h
blob550a81f30d3d178087ad318971bb5cbe8e714190
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 Uint32 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 if (!bits)
81 return;
82 buffer+= powers[bufbits]*val;
83 bufbits+= bits;
84 while (bufbits>=8) {
85 bufbits-= 8;
86 os.put(buffer%256);
87 buffer/= 256;
90 /** Flushes the buffer - sends it to the stream */
91 void flush() {
92 if (bufbits)
93 os.put(buffer);
94 buffer= bufbits= 0;
98 /** Stream bit-reader - automated buffer for reading single bits */
99 class BitReader {
100 /** Buffered bits, number of buffered bits */
101 int buffer, bufbits;
102 /** Used input (byte)stream */
103 std::istream &is;
104 public:
105 /** Constructor just associates the object with the given stream */
106 BitReader(std::istream &stream)
107 : buffer(0), bufbits(0), is(stream) {}
108 /** Reads bits */
109 int getBits(int bits) {
110 ASSERT(bits>=0);
111 if (!bits)
112 return 0;
113 while (bufbits<bits) {
114 buffer+= powers[bufbits]*Uchar(is.get());
115 bufbits+= 8;
117 int result= buffer%powers[bits];
118 buffer/= powers[bits];
119 bufbits-= bits;
120 return result;
122 /** Clears buffer */
123 void flush() {
124 buffer= bufbits= 0;
128 /** Reads a whole file into std::string (\p result), returns \p true on success */
129 inline bool file2string(const char *name,std::string &result) {
130 using namespace std;
131 ifstream file( name, ios_base::binary|ios_base::in );
132 if (!file.good())
133 return false;
135 file.seekg(0,ios::end);
136 int length= file.tellg();
137 if (!length)
138 return false;
140 string res;
141 res.resize(length);
142 file.seekg(0,ios::beg);
143 file.read(&res[0],length);
144 if (file.gcount()!=length)
145 return false;
146 swap(res,result);
147 return true;
150 #endif // FILEUTIL_HEADER_