root/win32/itpp-4.0.1/itpp/base/binfile.h @ 109

Revision 35, 11.9 kB (checked in by mido, 16 years ago)

zasadni zmeny ve /win32

Line 
1/*!
2 * \file
3 * \brief Binary file formats definitions
4 * \author Tony Ottosson, Thomas Eriksson and Adam Piatyszek
5 *
6 * -------------------------------------------------------------------------
7 *
8 * IT++ - C++ library of mathematical, signal processing, speech processing,
9 *        and communications classes and functions
10 *
11 * Copyright (C) 1995-2007  (see AUTHORS file for a list of contributors)
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 *
27 * -------------------------------------------------------------------------
28 */
29
30#ifndef BINFILE_H
31#define BINFILE_H
32
33#include <itpp/base/ittypes.h>
34#include <fstream>
35
36
37namespace itpp {
38
39  /*!
40    \brief Checks if a filename already exists on the disk
41    \ingroup itfile
42  */
43  bool exist(const std::string& name);
44
45  /*!
46    \brief Base class for binary file classes
47    \ingroup itfile
48
49    This class serves as a base class for the classes \c bofstream,
50    \c bifstream, and \c bfstream. It controls the endianity (i.e. the
51    byte order of multibyte numbers on the disk) of the inhereted classes.
52  */
53  class bfstream_base {
54  public:
55    /*!
56      \brief Definition of the endian data type
57
58      The Endianness defines the order in which multibyte numbers are stored
59      in the file. The two orders are called "Little Endian" (\c l_endian )
60      and "Big Endian" (\c b_endian ).
61
62      "Little Endian" means that the low-order byte of the number is stored
63      at the lowest adress (i.e. the little end comes first). "Big Endian"
64      means that the high-order byte of the number is stored in memory at
65      the lowest address (i.e. the big end comes first)
66    */
67    enum endian { l_endian, b_endian };
68
69    /*!
70      \brief Class Constructor
71
72      \param e Defines the endianity of the class. Possible values are \c
73      l_endian for little endian or \c b_endian for big endian. The default
74      value is \c b_endian.
75    */
76    bfstream_base(endian e = b_endian);
77
78    /*!
79      \brief Returns the endianity of the class
80    */
81    endian get_endianity() const
82    {
83      if (switch_endianity) {
84        if (native_endianity == l_endian)
85          return b_endian;
86        else
87          return l_endian;
88      }
89      else
90        return native_endianity;
91    }
92
93    /*!
94      \brief Returns the native endianity for this computer architecture
95
96      Intel processors use "Little Endian" byte ordering while e.g. Motorola
97      processors use "Big Endian" byte ordering.
98    */
99    endian get_native_endianity() const { return native_endianity; }
100
101    /*!
102      \brief Set the endianity for this class
103    */
104    void set_endianity(endian e)
105    {
106      if (native_endianity == e)
107        switch_endianity = false;
108      else
109        switch_endianity = true;
110    }
111
112    /*!
113      \brief Set the endianity of this class to the native endianity for
114      this computer architecture
115    */
116    void set_native_endianity() { switch_endianity = false; }
117
118  protected:
119    //! Indicates if the endianity of the processed data needs to be changed
120    bool switch_endianity;
121    //! The native endianity for this computer architecture
122    endian native_endianity;
123  };
124
125  /*!
126    \brief Binary Outfile Class
127    \ingroup itfile
128  */
129  class bofstream : public bfstream_base, public std::ofstream {
130  public:
131    /*!
132      \brief Class constructor that opens a file and sets the endianity
133
134      \param name The name of the file to open
135      \param e Defines the endianity of the class. Possible values are
136      \c l_endian for "Little Endian" or \c b_endian for "Big Endian". The
137      default value is \c b_endian.
138    */
139    bofstream(const std::string& name, endian e = b_endian);
140
141    //! Class Constructor
142    bofstream();
143
144    //! Class Destructor
145    ~bofstream() { }
146
147    /*!
148      \brief Open a file for writing and set the endianity
149
150      \param name The name of the file to open
151      \param e Defines the endianity of the class (default value is
152      \c b_endian )
153    */
154    void open(const std::string& name, endian e = b_endian);
155
156    //! Writes a signed char variable to the binary output file
157    bofstream& operator<<(char a);
158    //! Writes an unsigned char variable to the binary output file
159    bofstream& operator<<(unsigned char a);
160    //! Writes a 16-bit signed integer variable to the binary output file
161    bofstream& operator<<(int16_t a);
162    //! Writes a 16-bit unsigned integer variable to the binary output file
163    bofstream& operator<<(uint16_t a);
164    //! Writes a 32-bit signed integer variable to the binary output file
165    bofstream& operator<<(int32_t a);
166    //! Writes a 32-bit unsigned integer variable to the binary output file
167    bofstream& operator<<(uint32_t a);
168    //! Writes a 64-bit signed integer variable to the binary output file
169    bofstream& operator<<(int64_t a);
170    //! Writes a 64-bit unsigned ingeger variable to the binary output file
171    bofstream& operator<<(uint64_t a);
172    //! Writes a float variable to the binary output file
173    bofstream& operator<<(float a);
174    //! Writes a double variable to the binary output file
175    bofstream& operator<<(double a);
176    //! Writes a char* string to the binary output file
177    bofstream& operator<<(const char* a);
178    //! Writes a string variable to the binary output file
179    bofstream& operator<<(const std::string& a);
180  };
181
182  /*!
183    \brief Binary Infile Class
184    \ingroup itfile
185  */
186  class bifstream : public bfstream_base, public std::ifstream {
187  public:
188    /*!
189      \brief Class constructor that opens a file and sets the endianity
190
191      \param name The name of the file to open
192      \param e Defines the endianity of the class. Possible values are
193      \c l_endian for "Little Endian" or \c b_endian for "Big Endian". The
194      default value is \c b_endian.
195    */
196    bifstream(const std::string& name, endian e = b_endian);
197
198    //! Class Constructor
199    bifstream();
200
201    //! Class Destructor
202    ~bifstream() { }
203
204    /*!
205      \brief Open a file for reading and set the endianity
206
207      \param name The name of the file to open
208      \param e Defines the endianity of the class (default value is
209      \c b_endian )
210    */
211    void open(const std::string& name, endian e = b_endian);
212
213    //! Returns the length in bytes of the file
214    int length();
215
216    //! Reads a signed char variable from the binary output file
217    bifstream& operator>>(char& a);
218    //! Reads an unsigned char variable from the binary output file
219    bifstream& operator>>(unsigned char& a);
220    //! Reads a 16-bit signed integer variable from the binary output file
221    bifstream& operator>>(int16_t& a);
222    //! Reads a 16-bit unsigned integer variable from the binary output file
223    bifstream& operator>>(uint16_t& a);
224    //! Reads a 32-bit signed integer variable from the binary output file
225    bifstream& operator>>(int32_t& a);
226    //! Reads a 32-bit unsigned integer variable from the binary output file
227    bifstream& operator>>(uint32_t& a);
228    //! Reads a 64-bit signed integer variable from the binary output file
229    bifstream& operator>>(int64_t& a);
230    //! Reads a 64-bit unsigned ingeger variable from the binary output file
231    bifstream& operator>>(uint64_t& a);
232    //! Reads a float variable from the binary output file
233    bifstream& operator>>(float& a);
234    //! Reads a double variable from the binary output file
235    bifstream& operator>>(double& a);
236    //! Reads a char* string from the binary output file
237    bifstream& operator>>(char* a);
238    //! Reads a string variable from the binary output file
239    bifstream& operator>>(std::string& a);
240  };
241
242  /*!
243    \brief Binary in/out-file Class
244    \ingroup itfile
245  */
246  class bfstream : public bfstream_base, public std::fstream {
247  public:
248    /*!
249      \brief Class constructor that opens a file and sets the endianity
250
251      \param name The name of the file to open
252      \param e Defines the endianity of the class. Possible values are
253      \c l_endian for "Little Endian" or \c b_endian for "Big Endian".
254      The default value is \c b_endian.
255    */
256    bfstream(const std::string& name, endian e = b_endian);
257
258    //! Class Constructor
259    bfstream();
260
261    //! Class Destructor
262    ~bfstream() { }
263
264    /*!
265      \brief Open a file for reading and writing and set the endianity
266
267      \param name The name of the file to open
268      \param trunc Rewrite the file if it exists (default value is \c false)
269      \param e Defines the endianity of the class (default value is
270      \c b_endian )
271    */
272    void open(const std::string& name, bool trunc = false, endian e = b_endian);
273
274    /*!
275      \brief Open a file for reading only and set the endianity
276
277      \param name The name of the file to open
278      \param e Defines the endianity of the class (default value is
279      \c b_endian )
280    */
281    void open_readonly(const std::string& name, endian e = b_endian);
282
283    //! Returns the length in bytes of the file
284    int length();
285
286    //! Writes an signed char variable to the binary output file
287    bfstream& operator<<(char a);
288    //! Writes an unsigned char variable to the binary output file
289    bfstream& operator<<(unsigned char a);
290    //! Writes a 16-bit signed integer variable to the binary output file
291    bfstream& operator<<(int16_t a);
292    //! Writes a 16-bit unsigned integer variable to the binary output file
293    bfstream& operator<<(uint16_t a);
294    //! Writes a 32-bit signed integer variable to the binary output file
295    bfstream& operator<<(int32_t a);
296    //! Writes a 32-bit unsigned integer variable to the binary output file
297    bfstream& operator<<(uint32_t a);
298    //! Writes a 64-bit signed integer variable to the binary output file
299    bfstream& operator<<(int64_t a);
300    //! Writes a 64-bit unsigned ingeger variable to the binary output file
301    bfstream& operator<<(uint64_t a);
302    //! Writes a float variable to the binary output file
303    bfstream& operator<<(float a);
304    //! Writes a double variable to the binary output file
305    bfstream& operator<<(double a);
306    //! Writes a char* string to the binary output file
307    bfstream& operator<<(const char* a);
308    //! Writes a string variable to the binary output file
309    bfstream& operator<<(const std::string& a);
310
311    //! Reads a char variable from the binary output file
312    bfstream& operator>>(char& a);
313    //! Reads an unsigned char variable from the binary output file
314    bfstream& operator>>(unsigned char& a);
315    //! Reads a 16-bit signed integer variable from the binary output file
316    bfstream& operator>>(int16_t& a);
317    //! Reads a 16-bit unsigned integer variable from the binary output file
318    bfstream& operator>>(uint16_t& a);
319    //! Reads a 32-bit signed integer variable from the binary output file
320    bfstream& operator>>(int32_t& a);
321    //! Reads a 32-bit unsigned integer variable from the binary output file
322    bfstream& operator>>(uint32_t& a);
323    //! Reads a 64-bit signed integer variable from the binary output file
324    bfstream& operator>>(int64_t& a);
325    //! Reads a 64-bit unsigned ingeger variable from the binary output file
326    bfstream& operator>>(uint64_t& a);
327    //! Reads a float variable from the binary output file
328    bfstream& operator>>(float& a);
329    //! Reads a double variable from the binary output file
330    bfstream& operator>>(double& a);
331    //! Reads a char* string from the binary output file
332    bfstream& operator>>(char* a);
333    //! Reads a string variable from the binary output file
334    bfstream& operator>>(std::string& a);
335  };
336
337} //namespace itpp
338
339#endif // #ifndef BINFILE_H
Note: See TracBrowser for help on using the browser.