1 #ifndef FILEUTIL_HEADER_
2 #define FILEUTIL_HEADER_
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
) {
16 template<> inline Uchar
get(std::istream
&is
) {
20 template<> inline void put(std::ostream
&os
,Uint16 i
) {
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);
35 template<> inline Uint32
get(std::istream
&is
) {
37 for (int i
=0; i
<4; ++i
)
38 res
= res
*256+get
<Uchar
>(is
);
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 );
49 is
.read( (char*)&result
, sizeof(float) );
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 */
66 /** Buffered bits, number of buffered bits */
68 /** Used output (byte)stream */
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 */
78 void putBits(int val
,int bits
) {
79 ASSERT( bits
>0 && 0<=val
&& val
<powers
[bits
] );
80 buffer
+= powers
[bufbits
]*val
;
88 /** Flushes the buffer - sends it to the stream */
96 /** Stream bit-reader - automated buffer for reading single bits */
98 /** Buffered bits, number of buffered bits */
100 /** Used input (byte)stream */
103 /** Constructor just associates the object with the given stream */
104 BitReader(std::istream
&stream
)
105 : buffer(0), bufbits(0), is(stream
) {}
107 int getBits(int bits
) {
109 while (bufbits
<bits
) {
110 buffer
+= powers
[bufbits
]*Uchar(is
.get());
113 int result
= buffer
%powers
[bits
];
114 buffer
/= powers
[bits
];
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
) {
127 ifstream
file( name
, ios_base::binary
|ios_base::in
);
131 file
.seekg(0,ios::end
);
132 int length
= file
.tellg();
138 file
.seekg(0,ios::beg
);
139 file
.read(&res
[0],length
);
140 if (file
.gcount()!=length
)
146 #endif // FILEUTIL_HEADER_