File: binfile.h

package info (click to toggle)
libitpp 4.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 7,520 kB
  • ctags: 6,341
  • sloc: cpp: 51,608; sh: 9,248; makefile: 636; fortran: 8
file content (339 lines) | stat: -rw-r--r-- 12,194 bytes parent folder | download
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*!
 * \file
 * \brief Binary file formats definitions
 * \author Tony Ottosson, Thomas Eriksson and Adam Piatyszek
 *
 * -------------------------------------------------------------------------
 *
 * IT++ - C++ library of mathematical, signal processing, speech processing,
 *        and communications classes and functions
 *
 * Copyright (C) 1995-2008  (see AUTHORS file for a list of contributors)
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * -------------------------------------------------------------------------
 */

#ifndef BINFILE_H
#define BINFILE_H

#include <itpp/base/ittypes.h>
#include <fstream>


namespace itpp {

  /*!
    \brief Checks if a file named \c name already exists on the disk
    \ingroup itfile
  */
  bool exist(const std::string& name);

  /*!
    \brief Base class for binary file classes
    \ingroup itfile

    This class serves as a base class for the classes \c bofstream,
    \c bifstream, and \c bfstream. It controls the endianity (i.e. the
    byte order of multibyte numbers on the disk) of the inhereted classes.
  */
  class bfstream_base {
  public:
    /*!
      \brief Definition of the endian data type

      The Endianness defines the order in which multibyte numbers are stored
      in the file. The two orders are called "Little Endian" (\c l_endian )
      and "Big Endian" (\c b_endian ).

      "Little Endian" means that the low-order byte of the number is stored
      at the lowest address (i.e. the little end comes first). "Big Endian"
      means that the high-order byte of the number is stored in memory at
      the lowest address (i.e. the big end comes first)
    */
    enum endian { l_endian, b_endian };

    /*!
      \brief Class Constructor

      \param e Defines the endianity of the class. Possible values are \c
      l_endian for little endian or \c b_endian for big endian. The default
      value is \c b_endian.
    */
    bfstream_base(endian e = b_endian);

    /*!
      \brief Returns the endianity of the class
    */
    endian get_endianity() const
    {
      if (switch_endianity) {
        if (native_endianity == l_endian)
          return b_endian;
        else
          return l_endian;
      }
      else
        return native_endianity;
    }

    /*!
      \brief Returns the native endianity for this computer architecture

      Intel processors use "Little Endian" byte ordering while e.g. Motorola
      processors use "Big Endian" byte ordering.
    */
    endian get_native_endianity() const { return native_endianity; }

    /*!
      \brief Set the endianity for this class
    */
    void set_endianity(endian e)
    {
      if (native_endianity == e)
        switch_endianity = false;
      else
        switch_endianity = true;
    }

    /*!
      \brief Set the endianity of this class to the native endianity for
      this computer architecture
    */
    void set_native_endianity() { switch_endianity = false; }

  protected:
    //! Indicates if the endianity of the processed data needs to be changed
    bool switch_endianity;
    //! The native endianity for this computer architecture
    endian native_endianity;
  };

  /*!
    \brief Binary Outfile Class
    \ingroup itfile
  */
  class bofstream : public bfstream_base, public std::ofstream {
  public:
    /*!
      \brief Class constructor that opens a file and sets the endianity

      \param name The name of the file to open
      \param e Defines the endianity of the class. Possible values are
      \c l_endian for "Little Endian" or \c b_endian for "Big Endian". The
      default value is \c b_endian.
    */
    bofstream(const std::string& name, endian e = b_endian);

    //! Class Constructor
    bofstream();

    //! Class Destructor
    ~bofstream() { }

    /*!
      \brief Open a file for writing and set the endianity

      \param name The name of the file to open
      \param e Defines the endianity of the class (default value is
      \c b_endian )
    */
    void open(const std::string& name, endian e = b_endian);

    //! Writes a signed char variable to the binary output file
    bofstream& operator<<(char a);
    //! Writes an unsigned char variable to the binary output file
    bofstream& operator<<(unsigned char a);
    //! Writes a 16-bit signed integer variable to the binary output file
    bofstream& operator<<(int16_t a);
    //! Writes a 16-bit unsigned integer variable to the binary output file
    bofstream& operator<<(uint16_t a);
    //! Writes a 32-bit signed integer variable to the binary output file
    bofstream& operator<<(int32_t a);
    //! Writes a 32-bit unsigned integer variable to the binary output file
    bofstream& operator<<(uint32_t a);
    //! Writes a 64-bit signed integer variable to the binary output file
    bofstream& operator<<(int64_t a);
    //! Writes a 64-bit unsigned ingeger variable to the binary output file
    bofstream& operator<<(uint64_t a);
    //! Writes a float variable to the binary output file
    bofstream& operator<<(float a);
    //! Writes a double variable to the binary output file
    bofstream& operator<<(double a);
    //! Writes a char* string to the binary output file
    bofstream& operator<<(const char* a);
    //! Writes a string variable to the binary output file
    bofstream& operator<<(const std::string& a);
  };

  /*!
    \brief Binary Infile Class
    \ingroup itfile
  */
  class bifstream : public bfstream_base, public std::ifstream {
  public:
    /*!
      \brief Class constructor that opens a file and sets the endianity

      \param name The name of the file to open
      \param e Defines the endianity of the class. Possible values are
      \c l_endian for "Little Endian" or \c b_endian for "Big Endian". The
      default value is \c b_endian.
    */
    bifstream(const std::string& name, endian e = b_endian);

    //! Class Constructor
    bifstream();

    //! Class Destructor
    ~bifstream() { }

    /*!
      \brief Open a file for reading and set the endianity

      \param name The name of the file to open
      \param e Defines the endianity of the class (default value is
      \c b_endian )
    */
    void open(const std::string& name, endian e = b_endian);

    //! Returns the length in bytes of the file
    int length();

    //! Reads a signed char variable from the binary output file
    bifstream& operator>>(char& a);
    //! Reads an unsigned char variable from the binary output file
    bifstream& operator>>(unsigned char& a);
    //! Reads a 16-bit signed integer variable from the binary output file
    bifstream& operator>>(int16_t& a);
    //! Reads a 16-bit unsigned integer variable from the binary output file
    bifstream& operator>>(uint16_t& a);
    //! Reads a 32-bit signed integer variable from the binary output file
    bifstream& operator>>(int32_t& a);
    //! Reads a 32-bit unsigned integer variable from the binary output file
    bifstream& operator>>(uint32_t& a);
    //! Reads a 64-bit signed integer variable from the binary output file
    bifstream& operator>>(int64_t& a);
    //! Reads a 64-bit unsigned ingeger variable from the binary output file
    bifstream& operator>>(uint64_t& a);
    //! Reads a float variable from the binary output file
    bifstream& operator>>(float& a);
    //! Reads a double variable from the binary output file
    bifstream& operator>>(double& a);
    //! Reads a char* string from the binary output file
    bifstream& operator>>(char* a);
    //! Reads a string variable from the binary output file
    bifstream& operator>>(std::string& a);
  };

  /*!
    \brief Binary in/out-file Class
    \ingroup itfile
  */
  class bfstream : public bfstream_base, public std::fstream {
  public:
    /*!
      \brief Class constructor that opens a file and sets the endianity

      \param name The name of the file to open
      \param e Defines the endianity of the class. Possible values are
      \c l_endian for "Little Endian" or \c b_endian for "Big Endian".
      The default value is \c b_endian.
    */
    bfstream(const std::string& name, endian e = b_endian);

    //! Class Constructor
    bfstream();

    //! Class Destructor
    ~bfstream() { }

    /*!
      \brief Open a file for reading and writing and set the endianity

      \param name The name of the file to open
      \param trunc Rewrite the file if it exists (default value is \c false)
      \param e Defines the endianity of the class (default value is
      \c b_endian )
    */
    void open(const std::string& name, bool trunc = false, endian e = b_endian);

    /*!
      \brief Open a file for reading only and set the endianity

      \param name The name of the file to open
      \param e Defines the endianity of the class (default value is
      \c b_endian )
    */
    void open_readonly(const std::string& name, endian e = b_endian);

    //! Returns the length in bytes of the file
    int length();

    //! Writes an signed char variable to the binary output file
    bfstream& operator<<(char a);
    //! Writes an unsigned char variable to the binary output file
    bfstream& operator<<(unsigned char a);
    //! Writes a 16-bit signed integer variable to the binary output file
    bfstream& operator<<(int16_t a);
    //! Writes a 16-bit unsigned integer variable to the binary output file
    bfstream& operator<<(uint16_t a);
    //! Writes a 32-bit signed integer variable to the binary output file
    bfstream& operator<<(int32_t a);
    //! Writes a 32-bit unsigned integer variable to the binary output file
    bfstream& operator<<(uint32_t a);
    //! Writes a 64-bit signed integer variable to the binary output file
    bfstream& operator<<(int64_t a);
    //! Writes a 64-bit unsigned ingeger variable to the binary output file
    bfstream& operator<<(uint64_t a);
    //! Writes a float variable to the binary output file
    bfstream& operator<<(float a);
    //! Writes a double variable to the binary output file
    bfstream& operator<<(double a);
    //! Writes a char* string to the binary output file
    bfstream& operator<<(const char* a);
    //! Writes a string variable to the binary output file
    bfstream& operator<<(const std::string& a);

    //! Reads a char variable from the binary output file
    bfstream& operator>>(char& a);
    //! Reads an unsigned char variable from the binary output file
    bfstream& operator>>(unsigned char& a);
    //! Reads a 16-bit signed integer variable from the binary output file
    bfstream& operator>>(int16_t& a);
    //! Reads a 16-bit unsigned integer variable from the binary output file
    bfstream& operator>>(uint16_t& a);
    //! Reads a 32-bit signed integer variable from the binary output file
    bfstream& operator>>(int32_t& a);
    //! Reads a 32-bit unsigned integer variable from the binary output file
    bfstream& operator>>(uint32_t& a);
    //! Reads a 64-bit signed integer variable from the binary output file
    bfstream& operator>>(int64_t& a);
    //! Reads a 64-bit unsigned ingeger variable from the binary output file
    bfstream& operator>>(uint64_t& a);
    //! Reads a float variable from the binary output file
    bfstream& operator>>(float& a);
    //! Reads a double variable from the binary output file
    bfstream& operator>>(double& a);
    //! Reads a char* string from the binary output file
    bfstream& operator>>(char* a);
    //! Reads a string variable from the binary output file
    bfstream& operator>>(std::string& a);
  };

} //namespace itpp

#endif // #ifndef BINFILE_H