32 #ifndef PLANCK_BSTREAM_H 33 #define PLANCK_BSTREAM_H 50 union { uint16 i16; uint8 i8; } tmp;
52 big_end = (tmp.i8==0);
54 operator bool()
const {
return big_end; }
59 template<
size_t size>
inline void byteswap_helper__ (
char *);
60 template<>
inline void byteswap_helper__<1> (
char *)
62 template<>
inline void byteswap_helper__<2> (
char *val)
67 template<>
inline void byteswap_helper__<4> (
char *val)
70 swap (val[0],val[3]); swap (val[1],val[2]);
72 template<>
inline void byteswap_helper__<8> (
char *val)
75 swap (val[0],val[7]); swap (val[1],val[6]);
76 swap (val[2],val[5]); swap (val[3],val[4]);
81 template<
typename T>
inline void byteswap (T &val)
82 { byteswap_helper__<sizeof(T)> (
reinterpret_cast<char *
> (&val)); }
84 const bool file_is_lsb=big_endian, file_is_msb=!big_endian,
85 file_is_natural=
false;
97 bostream (std::ostream &s_,
bool doswap_=
false)
98 : s(s_), doswap(doswap_) {}
106 if ((
sizeof(T)>1) && doswap)
107 for (
size_t m=0; m<num; ++m)
111 s.write (reinterpret_cast<const char *> (&tmp),
sizeof(T));
114 s.write (reinterpret_cast<const char *> (data), num*
sizeof(T));
128 void setSwap(
bool newswap)
145 : s(s_), doswap(doswap_) {}
151 template<
typename T>
bistream &
get (T *data,
size_t num)
153 s.read (reinterpret_cast<char *> (data), num*
sizeof(T));
154 if ((
sizeof(T)>1) && doswap)
155 for (
size_t m=0; m<num; ++m)
170 void setSwap(
bool newswap)
176 class bofstream:
public std::ofstream
183 bofstream (
const char *fname,
bool doswap_)
184 :
std::ofstream(fname,
std::ios::binary), doswap(doswap_) {}
186 template<
typename T> bofstream &operator<< (
const T &data)
192 write (reinterpret_cast<const char *> (&tmp),
sizeof(T));
195 write (reinterpret_cast<const char *> (&data),
sizeof(T));
198 template<
typename T> bofstream &put (
const T *data,
size_t num)
201 for (
size_t m=0; m<num; ++m)
205 write (reinterpret_cast<const char *> (&tmp),
sizeof(T));
208 write (reinterpret_cast<const char *> (data), num*
sizeof(T));
214 void setSwap(
bool newswap)
220 class bifstream:
public std::ifstream
229 bifstream (
const char *fname,
bool doswap_)
230 :
std::ifstream(fname,
std::ios::binary), doswap(doswap_) {}
232 void open (
const char *fname,
bool doswap_)
235 std::ifstream::open(fname,std::ios::binary);
238 template<
typename T> bifstream &operator>> (T &data)
240 read (reinterpret_cast<char *> (&data),
sizeof(T));
244 template<
typename T> bifstream &
get (T *data,
size_t num)
246 read (reinterpret_cast<char *> (data), num*
sizeof(T));
248 for (
size_t m=0; m<num; ++m)
254 { seekg(0,std::ios::beg); }
255 void skip(std::streamoff nbytes)
256 { seekg(nbytes,std::ios::cur); }
260 void setSwap(
bool newswap)
bistream(std::istream &s_, bool doswap_=false)
bostream(std::ostream &s_, bool doswap_=false)
bostream & operator<<(const T &data)
bistream & operator>>(T &data)
bostream & put(const T *data, size_t num)