File: bstream.h

package info (click to toggle)
healpix-cxx 3.50.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 3,008 kB
  • sloc: cpp: 13,882; ansic: 5,930; sh: 4,451; makefile: 216
file content (266 lines) | stat: -rw-r--r-- 7,525 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
 *  This file is part of libcxxsupport.
 *
 *  libcxxsupport is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  libcxxsupport is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with libcxxsupport; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/*
 *  libcxxsupport is being developed at the Max-Planck-Institut fuer Astrophysik
 *  and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
 *  (DLR).
 */

/*! \file bstream.h
 *  Classes for binary I/O with streams
 *
 *  Copyright (C) 2010 Max-Planck-Society
 *  \author Martin Reinecke
 */

#ifndef PLANCK_BSTREAM_H
#define PLANCK_BSTREAM_H

#include <iostream>
#include <fstream>
#include <algorithm>
#include "datatypes.h"

/*! An object of this class can be cast to a \a bool, which indicates whether
    the computer architecture is big-endian (true) or little-endian (false). */
class EndianTest__
  {
  private:
    bool big_end;

  public:
    EndianTest__()
      {
      union { uint16 i16; uint8 i8; } tmp;
      tmp.i16 = 1;
      big_end = (tmp.i8==0);
      }
    operator bool() const { return big_end; }
  };

const EndianTest__ big_endian;

template<size_t size> inline void byteswap_helper__ (char *);
template<> inline void byteswap_helper__<1> (char *)
  {}
template<> inline void byteswap_helper__<2> (char *val)
  {
  using namespace std;
  swap (val[0],val[1]);
  }
template<> inline void byteswap_helper__<4> (char *val)
  {
  using namespace std;
  swap (val[0],val[3]); swap (val[1],val[2]);
  }
template<> inline void byteswap_helper__<8> (char *val)
  {
  using namespace std;
  swap (val[0],val[7]); swap (val[1],val[6]);
  swap (val[2],val[5]); swap (val[3],val[4]);
  }

/*! Performs an endianness conversion on \a val.
    \note \a T must be a primitive data type! */
template<typename T> inline void byteswap (T &val)
  { byteswap_helper__<sizeof(T)> (reinterpret_cast<char *> (&val)); }

const bool file_is_lsb=big_endian, file_is_msb=!big_endian,
           file_is_natural=false;

/*! Class for writing binary data to a stream. */
class bostream
  {
  private:
    std::ostream &s;
    bool doswap;

  public:
    /*! Creates a new object which is attached to \a s_ and performs
        endianness conversion if \a doswap_==true. */
    bostream (std::ostream &s_, bool doswap_=false)
      : s(s_), doswap(doswap_) {}

    /*! Writes a binary representation of \a num objects of type \a T
        (stored in \a data) to the attached stream. Endianness conversion
        is performed if requested in the constructor.
        \note \a T must be a primitive data type! */
    template<typename T> bostream &put (const T *data, size_t num)
      {
      if ((sizeof(T)>1) && doswap)
        for (size_t m=0; m<num; ++m)
          {
          T tmp=data[m];
          byteswap (tmp);
          s.write (reinterpret_cast<const char *> (&tmp), sizeof(T));
          }
      else
        s.write (reinterpret_cast<const char *> (data), num*sizeof(T));
      return *this;
      }
    /*! Writes a binary representation of \a data to the attached stream.
        Endianness conversion is performed if requested in the constructor.
        \note \a T must be a primitive data type! */
    template<typename T> bostream &operator<< (const T &data)
      {
      put(&data,1);
      return *this;
      }

    bool getSwap() const
      { return doswap; }
    void setSwap(bool newswap)
      { doswap=newswap; }
    void flipSwap()
      { doswap=!doswap; }
  };

/*! Class for reading binary data from a stream. */
class bistream
  {
  private:
    std::istream &s;
    bool doswap;

  public:
    /*! Creates a new object which is attached to \a s_ and performs
        endianness conversion if \a doswap_==true. */
    bistream (std::istream &s_, bool doswap_=false)
      : s(s_), doswap(doswap_) {}

    /*! Reads a binary representation of \a num objects of type \a T
        from the attached stream and stores them in \a data. Endianness
        conversion is performed if requested in the constructor.
        \note \a T must be a primitive data type! */
    template<typename T> bistream &get (T *data, size_t num)
      {
      s.read (reinterpret_cast<char *> (data), num*sizeof(T));
      if ((sizeof(T)>1) && doswap)
        for (size_t m=0; m<num; ++m)
          byteswap (data[m]);
      return *this;
      }
    /*! Reads a binary representation of \a data from the attached stream.
        Endianness conversion is performed if requested in the constructor.
        \note \a T must be a primitive data type! */
    template<typename T> bistream &operator>> (T &data)
      {
      get (&data,1);
      return *this;
      }

    bool getSwap() const
      { return doswap; }
    void setSwap(bool newswap)
      { doswap=newswap; }
    void flipSwap()
      { doswap=!doswap; }
  };

class bofstream: public std::ofstream
  {
  private:
    bool doswap;

  public:
    /*! */
    bofstream (const char *fname, bool doswap_)
      : std::ofstream(fname,std::ios::binary), doswap(doswap_) {}

    template<typename T> bofstream &operator<< (const T &data)
      {
      if (doswap)
        {
        T tmp = data;
        byteswap (tmp);
        write (reinterpret_cast<const char *> (&tmp), sizeof(T));
        }
      else
        write (reinterpret_cast<const char *> (&data), sizeof(T));
      return *this;
      }
    template<typename T> bofstream &put (const T *data, size_t num)
      {
      if (doswap)
        for (size_t m=0; m<num; ++m)
          {
          T tmp=data[m];
          byteswap (tmp);
          write (reinterpret_cast<const char *> (&tmp), sizeof(T));
          }
      else
        write (reinterpret_cast<const char *> (data), num*sizeof(T));
      return *this;
      }

    bool getSwap() const
      { return doswap; }
    void setSwap(bool newswap)
      { doswap=newswap; }
    void flipSwap()
      { doswap=!doswap; }
  };

class bifstream: public std::ifstream
  {
  private:
    bool doswap;

  public:
    /*! */
    bifstream ()
      : doswap(false) {}
    bifstream (const char *fname, bool doswap_)
      : std::ifstream(fname,std::ios::binary), doswap(doswap_) {}

    void open (const char *fname, bool doswap_)
      {
      doswap=doswap_;
      std::ifstream::open(fname,std::ios::binary);
      }

    template<typename T> bifstream &operator>> (T &data)
      {
      read (reinterpret_cast<char *> (&data), sizeof(T));
      if (doswap) byteswap (data);
      return *this;
      }
    template<typename T> bifstream &get (T *data, size_t num)
      {
      read (reinterpret_cast<char *> (data), num*sizeof(T));
      if (doswap)
        for (size_t m=0; m<num; ++m)
          byteswap (data[m]);
      return *this;
      }

    void rewind()
      { seekg(0,std::ios::beg); }
    void skip(std::streamoff nbytes)
      { seekg(nbytes,std::ios::cur); }

    bool getSwap() const
      { return doswap; }
    void setSwap(bool newswap)
      { doswap=newswap; }
    void flipSwap()
      { doswap=!doswap; }
  };

#endif