root/library/bdm/base/itpp/base/itfile.h @ 813

Revision 813, 42.5 kB (checked in by smidl, 14 years ago)
Line 
1/*!
2 * \file
3 * \brief Definition of classes for the IT++ file format
4 * \author Tony Ottosson, Tobias Ringstrom 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-2009  (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 ITFILE_H
31#define ITFILE_H
32
33#include <itpp/base/vec.h>
34#include <itpp/base/array.h>
35#include <itpp/base/binfile.h>
36#include <itpp/base/ittypes.h>
37
38
39namespace itpp
40{
41
42/*!
43  \addtogroup itfile
44  \brief The IT++ file format
45  \author Tony Ottosson, Tobias Ringstrom and Adam Piatyszek
46
47  The IT++ file format is a file format that can be used to save (load)
48  variables to (from) files. These files can also be read an written by
49  Matlab or Octave using the m-files \c itload.m and \c itsave.m.
50
51  The class it_ifile is used for reading only, whereas the class it_file
52  can be used for both reading and writing.
53
54  Saving of a variable is done in two steps. The first step is to supply
55  the name and optionally description of the variable to be saved. This
56  can be done either by calling the function it_file::set_next_name() or
57  by using the helper class Name:
58
59  \code
60  vec v("1 2 3");
61  bvec b = "0 1 0 1";
62  it_file f("file.it");
63  f << Name("v", "A double vector of tree values") << v;
64  f.set_next_name("b");
65  f << b;
66  \endcode
67
68  The reading is done in a similar way:
69
70  \code
71  vec v;
72  bvec b;
73  it_ifile f("file.it");
74  f >> Name("v") >> v;
75  f.seek("b");
76  f >> b;
77  \endcode
78
79  \note Since version 3, IT++ file format uses the IEEE little endian byte
80  ordering ("ieee-le" in Matlab/Octave). This version is not backward
81  compatible with previous versions. If you need to read/write data in
82  IT++ file format version 2, you can use the it_ifile_old and it_file_old
83  classes. Please have in mind that these "old" classes are deprecated and
84  will be removed from the IT++ library in future.
85
86  \warning Do not use the names that begin with an existing type.
87*/
88
89/*!
90  \brief Base class for it_ifile and it_file.
91  \ingroup itfile
92*/
93class it_file_base
94{
95public:
96  //! Data header structure
97  struct data_header {
98    //! Number of bytes of the header
99    uint64_t hdr_bytes;
100    //! Number of bytes of the data
101    uint64_t data_bytes;
102    //! Number of bytes of the header + data
103    uint64_t block_bytes;
104    //! Data name
105    std::string name;
106    //! Data type, e.g. int32, float32, etc. type = "" means deleted
107    std::string type;
108    //! Data description
109    std::string desc;
110  };
111
112protected:
113  //! File header structure
114  struct file_header {
115    //! IT++ file marker: "IT++"
116    char magic[4];
117    //! IT++ file format version
118    char version;
119  };
120  //! IT++ file marker: "IT++"
121  static char file_magic[4];
122  //! IT++ file version
123  static char file_version;
124};
125
126
127/*!
128  \brief The IT++ file format reading class.
129  \ingroup itfile
130*/
131class it_ifile : public it_file_base
132{
133public:
134  //! Default constructor
135  it_ifile();
136  //! Constructor that calls open(filename)
137  explicit it_ifile(const std::string& filename);
138  //! Destructor
139  virtual ~it_ifile() { }
140  //! Open an existing file in read-only mode
141  void open(const std::string& filename);
142  //! Close the file
143  virtual void close();
144  //! Returns pointer to the underlying \c bfstream used
145  bfstream& low_level() { return s; }
146
147  //! Read and check the file header. Return true if the header is valid and false otherwise.
148  bool read_check_file_header();
149  //! Read data header and return the result in the variable \c h
150  void read_data_header(data_header& h);
151
152  //! Read a char value at the current file pointer position
153  void low_level_read(char& x);
154  //! Read a 64-bit unsigned integer value at the current file pointer position
155  void low_level_read(uint64_t& x);
156  //! Read a bool value at the current file pointer position
157  void low_level_read(bool &x);
158
159  //! Read a binary value at the current file pointer position
160  void low_level_read(bin& x);
161  //! Read a short value at the current file pointer position
162  void low_level_read(short& x);
163  //! Read an integer value at the current file pointer position
164  void low_level_read(int& x);
165  //! Read a float value at the current file pointer position
166  void low_level_read(float& x);
167  //! Read a double value at the current file pointer position
168  void low_level_read(double& x);
169  //! Read a float complex value at the current file pointer position
170  void low_level_read(std::complex<float>& x);
171  //! Read a double complex value at the current file pointer position
172  void low_level_read(std::complex<double>& x);
173
174  //! Read a vector of binary values at the current file pointer position
175  void low_level_read(bvec& v);
176  //! Read a vector of short integer values at the current file pointer position
177  void low_level_read(svec& v);
178  //! Read a vector of integer values at the current file pointer position
179  void low_level_read(ivec& v);
180  //! Read a vector of float values at the current file pointer position
181  void low_level_read_lo(vec& v);
182  //! Read a vector of double values at the current file pointer position
183  void low_level_read_hi(vec& v);
184  //! Read a vector of float complex values at the current file pointer position
185  void low_level_read_lo(cvec& v);
186  //! Read a vector of double complex values at the current file pointer position
187  void low_level_read_hi(cvec& v);
188
189  //! Read a string at the current file pointer position
190  void low_level_read(std::string& str);
191
192  //! Read a matrix of binary values at the current file pointer position
193  void low_level_read(bmat& m);
194  //! Read a matrix of short integer values at the current file pointer position
195  void low_level_read(smat& m);
196  //! Read a matrix of integer values at the current file pointer position
197  void low_level_read(imat& m);
198  //! Read a matrix of float values at the current file pointer position
199  void low_level_read_lo(mat& m);
200  //! Read a matrix of double values at the current file pointer position
201  void low_level_read_hi(mat& m);
202  //! Read a matrix of float complex values at the current file pointer position
203  void low_level_read_lo(cmat& m);
204  //! Read a matrix of double complex values at the current file pointer position
205  void low_level_read_hi(cmat& m);
206
207  //! Read an Array of binary values at the current file pointer position
208  void low_level_read(Array<bin>& v);
209  //! Read an Array of short integer values at the current file pointer position
210  void low_level_read(Array<short>& v);
211  //! Read an Array of integer values at the current file pointer position
212  void low_level_read(Array<int>& v);
213  //! Read an Array of float values at the current file pointer position
214  void low_level_read(Array<float>& v);
215  //! Read an Array of float values at the current file pointer position
216  void low_level_read_lo(Array<double>& v);
217  //! Read an Array of double values at the current file pointer position
218  void low_level_read_hi(Array<double>& v);
219  //! Read an Array of float complex values at the current file pointer position
220  void low_level_read(Array<std::complex<float> >& v);
221  //! Read an Array of float complex values at the current file pointer position
222  void low_level_read_lo(Array<std::complex<double> >& v);
223  //! Read an Array of double complex values at the current file pointer position
224  void low_level_read_hi(Array<std::complex<double> >& v);
225
226  //! Find the variable \c name
227  bool seek(const std::string& name);
228  //! Find the variable number \c n
229  bool seek(int n);
230  //! Get information about the current variable
231  void info(std::string& name, std::string& type, std::string& desc,
232            uint64_t& bytes);
233
234protected:
235  //! Protected binary file stream
236  bfstream s;
237};
238
239
240/*!
241  \brief The IT++ file format reading and writing class
242  \ingroup itfile
243*/
244class it_file : public it_ifile
245{
246public:
247  //! ACTION: Add documentation for this typedef
248  typedef it_file& (*it_manip)(it_file&);
249
250  //! Default constructor
251  it_file();
252
253  /*!
254    \brief Constructor that calls open()
255
256    If the file does not exist it will be created. If \c trunc is true,
257    the file will be truncated.
258  */
259  explicit it_file(const std::string& filename, bool trunc = false);
260
261  //! Destructor
262  virtual ~it_file() { }
263
264  /*!
265    \brief Open a file for reading and writing
266
267    If the file does not exist it will be created. If \c trunc is true,
268    the file will be truncated.
269  */
270  void open(const std::string& filename, bool trunc = false);
271
272  //! Close the file
273  void close();
274  //! Flush the data to disk
275  void flush();
276
277  //! Returns pointer to the underlying \c bfstream used
278  bfstream& low_level() { return s; }
279
280  //! Set the precision. Low precision means floats, high means doubles.
281  void set_low_precision(bool p = true)  { low_prec = p; }
282  //! Get the precision
283  bool get_low_precision() const { return low_prec; }
284
285  //! Set the name and optionally description of the next variable to be saved
286  void set_next_name(const std::string& name,
287                     const std::string& description = "")
288  { next_name = name; next_desc = description; }
289
290  //! Write the header for the \c it_file
291  void write_file_header();
292  //! Write the data header for a variable, specifying the type and size of the data to follow.
293  void write_data_header(const std::string& type, uint64_t size);
294  //! Write the data header for a variable, specifying the type, name, size and optionally description of the data to follow.
295  void write_data_header(const std::string& type, const std::string& name,
296                         uint64_t size, const std::string& description = "");
297
298  //! Write a char value at the current file pointer position
299  void low_level_write(char x);
300  //! Write an unsigned integer 64-bit value at the current file pointer position
301  void low_level_write(uint64_t x);
302  //! Write a bool value at the current file pointer position
303  void low_level_write(bool x);
304
305  //! Write a binary value at the current file pointer position
306  void low_level_write(bin x);
307  //! Write a short value at the current file pointer position
308  void low_level_write(short x);
309  //! Write an integer value at the current file pointer position
310  void low_level_write(int x);
311  //! Write a float value at the current file pointer position
312  void low_level_write(float x);
313  //! Write a double value at the current file pointer position
314  void low_level_write(double x);
315  //! Write a float complex value at the current file pointer position
316  void low_level_write(const std::complex<float>& x);
317  //! Write a double complex value at the current file pointer position
318  void low_level_write(const std::complex<double>& x);
319
320  //! Write a bvec at the current file pointer position
321  void low_level_write(const bvec& v);
322  //! Write an svec at the current file pointer position
323  void low_level_write(const svec& v);
324  //! Write an ivec at the current file pointer position
325  void low_level_write(const ivec& v);
326  //! Write a vec at the current file pointer position
327  void low_level_write(const vec& v);
328  //! Write a cvec at the current file pointer position
329  void low_level_write(const cvec& v);
330
331  //! Write a string at the current file pointer position
332  void low_level_write(const std::string& str);
333
334  //! Write a bmat at the current file pointer position
335  void low_level_write(const bmat& m);
336  //! Write an smat at the current file pointer position
337  void low_level_write(const smat& m);
338  //! Write an imat at the current file pointer position
339  void low_level_write(const imat& m);
340  //! Write a mat at the current file pointer position
341  void low_level_write(const mat& m);
342  //! Write a cmat at the current file pointer position
343  void low_level_write(const cmat& m);
344
345  //! Write a bin Array at the current file pointer position
346  void low_level_write(const Array<bin>& v);
347  //! Write a short Array at the current file pointer position
348  void low_level_write(const Array<short>& v);
349  //! Write an integer Array at the current file pointer position
350  void low_level_write(const Array<int>& v);
351  //! Write a float Array at the current file pointer position
352  void low_level_write(const Array<float>& v);
353  //! Write a double Array at the current file pointer position
354  void low_level_write(const Array<double>& v);
355  //! Write a float complex Array at the current file pointer position
356  void low_level_write(const Array<std::complex<float> >& v);
357  //! Write a double complex Array at the current file pointer position
358  void low_level_write(const Array<std::complex<double> >& v);
359
360  //!ACTION: ADD DOCUMENTATION FOR THIS MEMBER !!!!!!!!
361  it_file& operator<<(it_manip func) { return (*func)(*this); }
362
363  //! Removes the variable \c name from the file
364  void remove(const std::string& name);
365  //! Returns true if the variable \c name exists in the file
366  bool exists(const std::string& name);
367  //! Remove slack space from the file
368  void pack();
369
370protected:
371  //! Remove the current variable, denoted by \c next_name
372  void remove();
373  //! Write data header \c h at the current file position
374  void write_data_header_here(const data_header& h);
375
376  //! Low precision flag. If true, use float type, otherwise double
377  bool low_prec;
378  //! Name to be used for saving the next variable
379  std::string next_name;
380  //! Description to be used for saving the next variable
381  std::string next_desc;
382
383private:
384  // Name of the opened file. Needed by the pack() method.
385  std::string fname;
386};
387
388
389/*!
390  \brief Flush operator
391  \ingroup itfile
392
393  Flushes the data. Usage:
394  \code
395  vec v1("1 2 3"), v2;
396  it_file f("file.it");
397  f << Name("v") << v1 << flush;
398  \endcode
399*/
400inline it_file& flush(it_file& f)
401{
402  f.flush();
403  return f;
404}
405
406/*!
407  \brief Automatic naming when saving
408  \ingroup itfile
409
410  An easy way to give a variable a name and optionally description when
411  saving. Usage:
412  \code
413  vec v1("1 2 3"), v2;
414  it_file f("file.it");
415  f << Name("v", "A vector of consecutive double values") << v1;
416  f >> Name("v") >> v2;
417  \endcode
418*/
419class Name
420{
421public:
422  //! Constructor
423  Name(const std::string& n, const std::string& d = ""): name(n), desc(d) {}
424  //! Dummy assignment operator - MSVC++ warning C4512
425  Name &operator=(const Name&) { return *this; }
426  //! The name string
427  const std::string& name;
428  //! The description
429  const std::string& desc;
430};
431
432
433/*! \addtogroup itfile */
434//!@{
435
436//! Finds the variable \c Name in the \c it_ifile. Returns file pointer for reading.
437inline it_ifile& operator>>(it_ifile& f, const Name& s)
438{
439  f.seek(s.name);
440  return f;
441}
442
443//! Finds the variable \c Name in the \c it_file. Returns file pointer for writing.
444inline it_file& operator<<(it_file& f, const Name& s)
445{
446  f.set_next_name(s.name, s.desc);
447  return f;
448}
449
450//! Read the char variable \c v from the \c it_ifile pointer
451it_ifile& operator>>(it_ifile& f, char& v);
452//! Read the bool variable \c v from the \c it_ifile pointer
453it_ifile& operator>>(it_ifile &f, bool &v);
454
455//! Read the binary variable \c v from the \c it_ifile pointer
456it_ifile& operator>>(it_ifile& f, bin& v);
457//! Read the short variable \c v from the \c it_ifile pointer
458it_ifile& operator>>(it_ifile& f, short& v);
459//! Read the integer variable \c v from the \c it_ifile pointer
460it_ifile& operator>>(it_ifile& f, int& v);
461//! Read the float variable \c v from the \c it_ifile pointer
462it_ifile& operator>>(it_ifile& f, float& v);
463//! Read the double variable \c v from the \c it_ifile pointer
464it_ifile& operator>>(it_ifile& f, double& v);
465//! Read the float complex variable \c v from the \c it_ifile pointer
466it_ifile& operator>>(it_ifile& f, std::complex<float>& v);
467//! Read the double complex variable \c v from the \c it_ifile pointer
468it_ifile& operator>>(it_ifile& f, std::complex<double>& v);
469
470//! Read the bvec \c v from the \c it_ifile pointer
471it_ifile& operator>>(it_ifile& f, bvec& v);
472//! Read the svec \c v from the \c it_ifile pointer
473it_ifile& operator>>(it_ifile& f, svec& v);
474//! Read the ivec \c v from the \c it_ifile pointer
475it_ifile& operator>>(it_ifile& f, ivec& v);
476//! Read the vec \c v from the \c it_ifile pointer
477it_ifile& operator>>(it_ifile& f, vec& v);
478//! Read the cvec \c v from the \c it_ifile pointer
479it_ifile& operator>>(it_ifile& f, cvec& v);
480
481//! Read the string \c str from the \c it_ifile pointer
482it_ifile& operator>>(it_ifile& f, std::string& str);
483
484//! Read the bmat \c m from the \c it_ifile pointer
485it_ifile& operator>>(it_ifile& f, bmat& m);
486//! Read the smat \c m from the \c it_ifile pointer
487it_ifile& operator>>(it_ifile& f, smat& m);
488//! Read the imat \c m from the \c it_ifile pointer
489it_ifile& operator>>(it_ifile& f, imat& m);
490//! Read the mat \c m from the \c it_ifile pointer
491it_ifile& operator>>(it_ifile& f, mat& m);
492//! Read the cmat \c m from the \c it_ifile pointer
493it_ifile& operator>>(it_ifile& f, cmat& m);
494
495//! Read the binary Array \c v from the \c it_ifile pointer
496it_ifile& operator>>(it_ifile& f, Array<bin>& v);
497//! Read the short integer Array \c v from the \c it_ifile pointer
498it_ifile& operator>>(it_ifile& f, Array<short>& v);
499//! Read the integer Array \c v from the \c it_ifile pointer
500it_ifile& operator>>(it_ifile& f, Array<int>& v);
501//! Read the float Array \c v from the \c it_ifile pointer
502it_ifile& operator>>(it_ifile& f, Array<float>& v);
503//! Read the double Array \c v from the \c it_ifile pointer
504it_ifile& operator>>(it_ifile& f, Array<double>& v);
505//! Read the float complex Array \c v from the \c it_ifile pointer
506it_ifile& operator>>(it_ifile& f, Array<std::complex<float> >& v);
507//! Read the double complex Array \c v from the \c it_ifile pointer
508it_ifile& operator>>(it_ifile& f, Array<std::complex<double> >& v);
509
510//! Read the bvec Array \c v from the \c it_ifile pointer
511it_ifile& operator>>(it_ifile& f, Array<bvec>& v);
512//! Read the svec Array \c v from the \c it_ifile pointer
513it_ifile& operator>>(it_ifile& f, Array<svec>& v);
514//! Read the ivec Array \c v from the \c it_ifile pointer
515it_ifile& operator>>(it_ifile& f, Array<ivec>& v);
516//! Read the vec Array \c v from the \c it_ifile pointer
517it_ifile& operator>>(it_ifile& f, Array<vec>& v);
518//! Read the cvec Array \c v from the \c it_ifile pointer
519it_ifile& operator>>(it_ifile& f, Array<cvec>& v);
520
521//! Read the string Array \c v from the \c it_ifile pointer
522it_ifile& operator>>(it_ifile& f, Array<std::string>& v);
523
524//! Read the bmat Array \c v from the \c it_ifile pointer
525it_ifile& operator>>(it_ifile& f, Array<bmat>& v);
526//! Read the bmat Array \c v from the \c it_ifile pointer
527it_ifile& operator>>(it_ifile& f, Array<smat>& v);
528//! Read the imat Array \c v from the \c it_ifile pointer
529it_ifile& operator>>(it_ifile& f, Array<imat>& v);
530//! Read the mat Array \c v from the \c it_ifile pointer
531it_ifile& operator>>(it_ifile& f, Array<mat>& v);
532//! Read the cmat Array \c v from the \c it_ifile pointer
533it_ifile& operator>>(it_ifile& f, Array<cmat>& v);
534
535
536//! Write the char variable \c x to the \c it_file pointer
537it_file& operator<<(it_file& f, char x);
538//! Write the bool variable \c x to the \c it_file pointer
539it_file& operator<<(it_file &f, bool x);
540
541//! Write the binary variable \c x to the \c it_file pointer
542it_file& operator<<(it_file& f, bin x);
543//! Write the short variable \c x to the \c it_file pointer
544it_file& operator<<(it_file& f, short x);
545//! Write the integer variable \c x to the \c it_file pointer
546it_file& operator<<(it_file& f, int x);
547//! Write the float variable \c x to the \c it_file pointer
548it_file& operator<<(it_file& f, float x);
549//! Write the double variable \c x to the \c it_file pointer
550it_file& operator<<(it_file& f, double x);
551//! Write the float complex variable \c x to the \c it_file pointer
552it_file& operator<<(it_file& f, std::complex<float> x);
553//! Write the double complex variable \c x to the \c it_file pointer
554it_file& operator<<(it_file& f, std::complex<double> x);
555
556//! Write the bvec \c v to the \c it_file pointer
557it_file& operator<<(it_file& f, const bvec& v);
558//! Write the svec \c v to the \c it_file pointer
559it_file& operator<<(it_file& f, const svec& v);
560//! Write the ivec \c v to the \c it_file pointer
561it_file& operator<<(it_file& f, const ivec& v);
562//! Write the vec \c v to the \c it_file pointer
563it_file& operator<<(it_file& f, const vec& v);
564//! Write the cvec \c v to the \c it_file pointer
565it_file& operator<<(it_file& f, const cvec& v);
566
567//! Write the string \c str to the \c it_file pointer
568it_file& operator<<(it_file& f, const std::string& str);
569
570//! Write the bmat \c m to the \c it_file pointer
571it_file& operator<<(it_file& f, const bmat& m);
572//! Write the smat \c m to the \c it_file pointer
573it_file& operator<<(it_file& f, const smat& m);
574//! Write the imat \c m to the \c it_file pointer
575it_file& operator<<(it_file& f, const imat& m);
576//! Write the mat \c m to the \c it_file pointer
577it_file& operator<<(it_file& f, const mat& m);
578//! Write the cmat \c m to the \c it_file pointer
579it_file& operator<<(it_file& f, const cmat& m);
580
581//! Write the bin Array \c v to the \c it_file pointer
582it_file& operator<<(it_file& f, const Array<bin>& v);
583//! Write the short int Array \c v to the \c it_file pointer
584it_file& operator<<(it_file& f, const Array<short>& v);
585//! Write the int Array \c v to the \c it_file pointer
586it_file& operator<<(it_file& f, const Array<int>& v);
587//! Write the float Array \c v to the \c it_file pointer
588it_file& operator<<(it_file& f, const Array<float>& v);
589//! Write the double Array \c v to the \c it_file pointer
590it_file& operator<<(it_file& f, const Array<double>& v);
591//! Write the float complex Array \c v to the \c it_file pointer
592it_file& operator<<(it_file& f, const Array<std::complex<float> >& v);
593//! Write the double complex Array \c v to the \c it_file pointer
594it_file& operator<<(it_file& f, const Array<std::complex<double> >& v);
595
596//! Write the bvec Array \c v to the \c it_file pointer
597it_file& operator<<(it_file& f, const Array<bvec>& v);
598//! Write the svec Array \c v to the \c it_file pointer
599it_file& operator<<(it_file& f, const Array<svec>& v);
600//! Write the ivec Array \c v to the \c it_file pointer
601it_file& operator<<(it_file& f, const Array<ivec>& v);
602//! Write the vec Array \c v to the \c it_file pointer
603it_file& operator<<(it_file& f, const Array<vec>& v);
604//! Write the cvec Array \c v to the \c it_file pointer
605it_file& operator<<(it_file& f, const Array<cvec>& v);
606
607//! Write the string Array \c v to the \c it_file pointer
608it_file& operator<<(it_file& f, const Array<std::string>& v);
609
610//! Write the bmat Array \c v to the \c it_file pointer
611it_file& operator<<(it_file& f, const Array<bmat>& v);
612//! Write the smat Array \c v to the \c it_file pointer
613it_file& operator<<(it_file& f, const Array<smat>& v);
614//! Write the imat Array \c v to the \c it_file pointer
615it_file& operator<<(it_file& f, const Array<imat>& v);
616//! Write the mat Array \c v to the \c it_file pointer
617it_file& operator<<(it_file& f, const Array<mat>& v);
618//! Write the cmat Array \c v to the \c it_file pointer
619it_file& operator<<(it_file& f, const Array<cmat>& v);
620
621//! Save the variable v in the file name.it as the name name.
622template <class T>
623void it_save_var_as(const T& v, const std::string& name)
624{
625  it_file f(name + ".it");
626  f << Name(name) << v;
627  f.close();
628}
629
630//! Load the variable v from the file name.it as the name name.
631template <class T>
632void it_load_var_as(T& v, const std::string& name)
633{
634  it_ifile f(name + ".it");
635  f.seek(name);
636  f >> v;
637  f.close();
638}
639
640//! A convenient macro. Calling it_save_var(M) saves M as 'M' in the file 'M.it'.
641#define it_save_var(v) it_save_var_as(v,#v)
642//! A convenient macro. Calling it_load_var(M) loads M as 'M' in the file 'M.it'.
643#define it_load_var(v) it_load_var_as(v,#v)
644
645//!@}
646
647
648// ----------------------------------------------------------------------
649// Deprecated implementation of IT++ file format version 2
650// Will be removed in future versions
651// ----------------------------------------------------------------------
652
653/*!
654  \brief Base class for it_ifile_old and it_file_old.
655
656  \warning This class is deprecated and will be removed in future.
657  \ingroup itfile
658*/
659class it_file_base_old
660{
661public:
662
663  //! Data header structure
664  struct data_header {
665    //! 0=little, 1=big
666    char endianity;
667    /*! size variables
668     * @{ */
669    uint32_t hdr_bytes, data_bytes, block_bytes;
670    /*! @} */
671    //! data name
672    std::string name;
673    //! data type, e.g. int32, float32, etc. type = "" means deleted
674    std::string type;
675  };
676
677protected:
678
679  //! File header structure
680  struct file_header {
681    //! ACTION: Add documentation
682    char magic[4];
683    //! ACTION: Add documentation
684    char version;
685  };
686  //! ACTION: Add documentation
687  static char file_magic[4];
688  //! ACTION: Add documentation
689  static char file_version;
690};
691
692/*!
693  \brief The old (version 2) IT++ file format reading class.
694
695  \warning This class is deprecated and will be removed in future.
696  \ingroup itfile
697*/
698class it_ifile_old : public it_file_base_old
699{
700public:
701  //!Constructor.
702  it_ifile_old();
703  //!Constructor. Calls open().
704  explicit it_ifile_old(const std::string& name);
705  //!Destructor.
706  virtual ~it_ifile_old() { }
707  //!Open a file.  The file must exist.
708  void open(const std::string& name);
709  //!Close a file.
710  virtual void close();
711  //!Returns pointer to the underlying \c bfstream used
712  bfstream& low_level() { return s; }
713
714  //!Reads and checks the file data header. Returns true if the header is valid and false otherwise.
715  bool read_check_file_header();
716  //!Read the data header and return the result in the variable \c h
717  void read_data_header(data_header& h);
718  //!Read a char value at the current file pointer position
719  void low_level_read(char& x);
720  //!Read a binary value at the current file pointer position
721  void low_level_read(bin& x);
722  //!Read a short value at the current file pointer position
723  void low_level_read(short& x);
724  //!Read an integer value at the current file pointer position
725  void low_level_read(int& x);
726  //!Read a float value at the current file pointer position
727  void low_level_read(float& x);
728  //!Read a double value at the current file pointer position
729  void low_level_read(double& x);
730  //!Read a float complex value at the current file pointer position
731  void low_level_read(std::complex<float>& x);
732  //!Read a double complex value at the current file pointer position
733  void low_level_read(std::complex<double>& x);
734  //!Read a vector of float values at the current file pointer position
735  void low_level_read_lo(vec& v);
736  //!Read a vector of double values at the current file pointer position
737  void low_level_read_hi(vec& v);
738  //!Read a vector of integer values at the current file pointer position
739  void low_level_read(ivec& v);
740  //!Read a vector of binary values at the current file pointer position
741  void low_level_read(bvec& v);
742  //!Read a vector of float complex values at the current file pointer position
743  void low_level_read_lo(cvec& v);
744  //!Read a vector of double complex values at the current file pointer position
745  void low_level_read_hi(cvec& v);
746  //!Read a string at the current file pointer position
747  void low_level_read(std::string& str);
748  //!Read a matrix of float values at the current file pointer position
749  void low_level_read_lo(mat& m);
750  //!Read a matrix of double values at the current file pointer position
751  void low_level_read_hi(mat& m);
752  //!Read a matrix of integer values at the current file pointer position
753  void low_level_read(imat& m);
754  //!Read a matrix of binary values at the current file pointer position
755  void low_level_read(bmat& m);
756  //!Read a matrix of float complex values at the current file pointer position
757  void low_level_read_lo(cmat& m);
758  //!Read a matrix of double complex values at the current file pointer position
759  void low_level_read_hi(cmat& m);
760
761  //!Read an Array of float values at the current file pointer position
762  void low_level_read_lo(Array<float>& v);
763  //!Read an Array of float values at the current file pointer position
764  void low_level_read_lo(Array<double>& v);
765  //!Read an Array of double values at the current file pointer position
766  void low_level_read_hi(Array<double>& v);
767  //!Read an Array of integer values at the current file pointer position
768  void low_level_read(Array<int>& v);
769  //!Read an Array of binary values at the current file pointer position
770  void low_level_read(Array<bin>& v);
771  //!Read an Array of float complex values at the current file pointer position
772  void low_level_read_lo(Array<std::complex<float> >& v);
773  //!Read an Array of float complex values at the current file pointer position
774  void low_level_read_lo(Array<std::complex<double> >& v);
775  //!Read an Array of double complex values at the current file pointer position
776  void low_level_read_hi(Array<std::complex<double> >& v);
777
778  //! Find the variable \c  name.
779  bool seek(const std::string& name);
780
781  //! Find the variable number \c n.
782  bool seek(int n);
783  //! Get information about the current variable.
784  void info(std::string& name, std::string& type, int& bytes);
785
786protected:
787  //! Protected binary file stream
788  bfstream s;
789};
790
791/*!
792  \brief The old (version 2) IT++ file format reading and writing class.
793
794  \warning This class is deprecated and will be removed in future.
795  \ingroup itfile
796*/
797class it_file_old : public it_ifile_old
798{
799public:
800  //! ACTION: Add documentation for this typedef
801  typedef it_file_old& (*it_manip)(it_file_old&);
802
803  //! Constructor.
804  it_file_old();
805
806  /*!
807    \brief Constructor.
808
809    If the file does not exist it will be created.  If \c trunc is true,
810    the file will be truncated.
811  */
812  explicit it_file_old(const std::string& name, bool trunc = false);
813
814  //! Destructor.
815  virtual ~it_file_old() { }
816
817  /*!
818    \brief Open a file for reading and writing.
819
820    If the file does not exist it will be created.  If \c  trunc is true,
821    the file will be truncated.
822  */
823  void open(const std::string& name, bool trunc = false);
824
825  //! Close the file.
826  void close();
827
828  //! Flush the data to disk.
829  void flush();
830
831  //! Returns pointer to the underlying \c bfstream used
832  bfstream& low_level() { return s; }
833
834  //! Set the precision. Low precision means floats, high means doubles.
835  void set_low_precision(bool p = true)  { low_prec = p; }
836
837  //! Get the precision.
838  bool get_low_precision() { return low_prec; }
839
840  //! Set the name of the next name to be saved. See also the \c Name class.
841  void set_next_name(const std::string& n) { next_name = n; }
842
843  //!Write the header for the \c it_file_old
844  void write_file_header();
845  //!Write the data header for a variable, specifying the type and size of the data to follow.
846  void write_data_header(const std::string& type, uint32_t size);
847  //!Write the data header for a variable, specifying the type, name, and size of the data to follow.
848  void write_data_header(const std::string& type, const std::string& name,
849                         uint32_t size);
850  //!Write a char value at the current file pointer position
851  void low_level_write(char x);
852  //!Write a binary value at the current file pointer position
853  void low_level_write(bin x);
854  //!Write a short value at the current file pointer position
855  void low_level_write(short x);
856  //!Write an integer value at the current file pointer position
857  void low_level_write(int x);
858  //!Write a float value at the current file pointer position
859  void low_level_write(float x);
860  //!Write a double value at the current file pointer position
861  void low_level_write(double x);
862  //!Write a float complex value at the current file pointer position
863  void low_level_write(const std::complex<float>& x);
864  //!Write a double complex value at the current file pointer position
865  void low_level_write(const std::complex<double>& x);
866  //!Write a vec at the current file pointer position
867  void low_level_write(const vec& v);
868  //!Write an ivec at the current file pointer position
869  void low_level_write(const ivec& v);
870  //!Write a bvec at the current file pointer position
871  void low_level_write(const bvec& v);
872  //!Write a cvec at the current file pointer position
873  void low_level_write(const cvec& v);
874  //!Write a string at the current file pointer position
875  void low_level_write(const std::string& str);
876  //!Write a mat at the current file pointer position
877  void low_level_write(const mat& m);
878  //!Write a imat at the current file pointer position
879  void low_level_write(const imat& m);
880  //!Write a bmat at the current file pointer position
881  void low_level_write(const bmat& m);
882  //!Write a cmat at the current file pointer position
883  void low_level_write(const cmat& m);
884  //!Write a float Array at the current file pointer position
885  void low_level_write(const Array<float>& v);
886  //!Write a double Array at the current file pointer position
887  void low_level_write(const Array<double>& v);
888  //!Write a integer Array at the current file pointer position
889  void low_level_write(const Array<int>& v);
890  //!Write a bin Array at the current file pointer position
891  void low_level_write(const Array<bin>& v);
892  //!Write a float complex Array at the current file pointer position
893  void low_level_write(const Array<std::complex<float> >& v);
894  //!Write a double complex Array at the current file pointer position
895  void low_level_write(const Array<std::complex<double> >& v);
896
897  //!ACTION: ADD DOCUMENTATION FOR THIS MEMBER !!!!!!!!
898  it_file_old& operator<<(it_manip func) { return (*func)(*this); }
899
900  //! Removes the variable \c name from the file.
901  void remove(const std::string& name);
902  //! Returns true if the variable \c name exists in the file.
903  bool exists(const std::string& name);
904  //! Remove slack space from the file.
905  void pack();
906
907protected:
908  //! ACTION: Add documenation for this protected member
909  void remove();
910  //! ACTION: Add documenation for this protected member
911  void write_data_header_here(const data_header& h);
912
913  //! ACTION: Add documenation for this protected member
914  bool low_prec;
915  //! ACTION: Add documenation for this protected member
916  std::string next_name;
917};
918
919/*!
920  \brief Flush operator.
921  \ingroup itfile
922
923  Flushes the data. Usage:
924
925  \code
926  vec v1("1 2 3"), v2;
927  it_file_old f("file.it");
928  f << Name("v") << v1 << flush;
929  \endcode
930*/
931inline it_file_old& flush(it_file_old& f)
932{
933  f.flush();
934  return f;
935}
936
937
938/*! \addtogroup itfile */
939//!@{
940
941//!Finds the variable \c Name in the \c it_ifile_old. Returns file pointer for reading.
942inline it_ifile_old& operator>>(it_ifile_old& f, const Name& s)
943{
944  f.seek(s.name);
945  return f;
946}
947
948//!Finds the variable \c Name in the \c it_file_old. Returns file pointer for writing.
949inline it_file_old& operator<<(it_file_old& f, const Name& s)
950{
951  f.set_next_name(s.name);
952  return f;
953}
954
955//!Read the char variable \c v from the \c it_ifile_old pointer
956it_ifile_old& operator>>(it_ifile_old& f, char& v);
957
958//!Read the binary variable \c v from the \c it_ifile_old pointer
959it_ifile_old& operator>>(it_ifile_old& f, bin& v);
960
961//!Read the short variable \c v from the \c it_ifile_old pointer
962it_ifile_old& operator>>(it_ifile_old& f, short& v);
963
964//!Read the integer variable \c v from the \c it_ifile_old pointer
965it_ifile_old& operator>>(it_ifile_old& f, int& v);
966
967//!Read the float variable \c v from the \c it_ifile_old pointer
968it_ifile_old& operator>>(it_ifile_old& f, float& v);
969
970//!Read the double variable \c v from the \c it_ifile_old pointer
971it_ifile_old& operator>>(it_ifile_old& f, double& v);
972
973//!Read the float complex variable \c v from the \c it_ifile_old pointer
974it_ifile_old& operator>>(it_ifile_old& f, std::complex<float>& v);
975
976//!Read the double complex variable \c v from the \c it_ifile_old pointer
977it_ifile_old& operator>>(it_ifile_old& f, std::complex<double>& v);
978
979//!Read the vec \c v from the \c it_ifile_old pointer
980it_ifile_old& operator>>(it_ifile_old& f, vec& v);
981
982//!Read the ivec \c v from the \c it_ifile_old pointer
983it_ifile_old& operator>>(it_ifile_old& f, ivec& v);
984
985//!Read the bvec \c v from the \c it_ifile_old pointer
986it_ifile_old& operator>>(it_ifile_old& f, bvec& v);
987
988//!Read the cvec \c v from the \c it_ifile_old pointer
989it_ifile_old& operator>>(it_ifile_old& f, cvec& v);
990
991//!Read the string \c str from the \c it_ifile_old pointer
992it_ifile_old& operator>>(it_ifile_old& f, std::string& str);
993
994//!Read the mat \c m from the \c it_ifile_old pointer
995it_ifile_old& operator>>(it_ifile_old& f, mat& m);
996
997//!Read the imat \c m from the \c it_ifile_old pointer
998it_ifile_old& operator>>(it_ifile_old& f, imat& m);
999
1000//!Read the bmat \c m from the \c it_ifile_old pointer
1001it_ifile_old& operator>>(it_ifile_old& f, bmat& m);
1002
1003//!Read the cmat \c m from the \c it_ifile_old pointer
1004it_ifile_old& operator>>(it_ifile_old& f, cmat& m);
1005
1006//!Read the float Array \c v from the \c it_ifile_old pointer
1007it_ifile_old& operator>>(it_ifile_old& f, Array<float>& v);
1008
1009//!Read the double Array \c v from the \c it_ifile_old pointer
1010it_ifile_old& operator>>(it_ifile_old& f, Array<double>& v);
1011
1012//!Read the integer Array \c v from the \c it_ifile_old pointer
1013it_ifile_old& operator>>(it_ifile_old& f, Array<int>& v);
1014
1015//!Read the binary Array \c v from the \c it_ifile_old pointer
1016it_ifile_old& operator>>(it_ifile_old& f, Array<bin>& v);
1017
1018//!Read the float complex Array \c v from the \c it_ifile_old pointer
1019it_ifile_old& operator>>(it_ifile_old& f, Array<std::complex<float> >& v);
1020
1021//!Read the double complex Array \c v from the \c it_ifile_old pointer
1022it_ifile_old& operator>>(it_ifile_old& f, Array<std::complex<double> >& v);
1023
1024//!Read the vec Array \c v from the \c it_ifile_old pointer
1025it_ifile_old& operator>>(it_ifile_old& f, Array<vec>& v);
1026
1027//!Read the ivec Array \c v from the \c it_ifile_old pointer
1028it_ifile_old& operator>>(it_ifile_old& f, Array<ivec>& v);
1029
1030//!Read the bvec Array \c v from the \c it_ifile_old pointer
1031it_ifile_old& operator>>(it_ifile_old& f, Array<bvec>& v);
1032
1033//!Read the cvec Array \c v from the \c it_ifile_old pointer
1034it_ifile_old& operator>>(it_ifile_old& f, Array<cvec>& v);
1035
1036//!Read the string Array \c v from the \c it_ifile_old pointer
1037it_ifile_old& operator>>(it_ifile_old& f, Array<std::string>& v);
1038
1039//!Read the mat Array \c v from the \c it_ifile_old pointer
1040it_ifile_old& operator>>(it_ifile_old& f, Array<mat>& v);
1041
1042//!Read the imat Array \c v from the \c it_ifile_old pointer
1043it_ifile_old& operator>>(it_ifile_old& f, Array<imat>& v);
1044
1045//!Read the bmat Array \c v from the \c it_ifile_old pointer
1046it_ifile_old& operator>>(it_ifile_old& f, Array<bmat>& v);
1047
1048//!Read the cmat Array \c v from the \c it_ifile_old pointer
1049it_ifile_old& operator>>(it_ifile_old& f, Array<cmat>& v);
1050
1051
1052//!Write the char variable \c x to the \c it_file_old pointer
1053it_file_old& operator<<(it_file_old& f, char x);
1054
1055//!Write the binary variable \c x to the \c it_file_old pointer
1056it_file_old& operator<<(it_file_old& f, bin x);
1057
1058//!Write the short variable \c x to the \c it_file_old pointer
1059it_file_old& operator<<(it_file_old& f, short x);
1060
1061//!Write the integer variable \c x to the \c it_file_old pointer
1062it_file_old& operator<<(it_file_old& f, int x);
1063
1064//!Write the float variable \c x to the \c it_file_old pointer
1065it_file_old& operator<<(it_file_old& f, float x);
1066
1067//!Write the double variable \c x to the \c it_file_old pointer
1068it_file_old& operator<<(it_file_old& f, double x);
1069
1070//!Write the float complex variable \c x to the \c it_file_old pointer
1071it_file_old& operator<<(it_file_old& f, std::complex<float> x);
1072
1073//!Write the double complex variable \c x to the \c it_file_old pointer
1074it_file_old& operator<<(it_file_old& f, std::complex<double> x);
1075
1076//!Write the vec \c v to the \c it_file_old pointer
1077it_file_old& operator<<(it_file_old& f, const vec& v);
1078
1079//!Write the ivec \c v to the \c it_file_old pointer
1080it_file_old& operator<<(it_file_old& f, const ivec& v);
1081
1082//!Write the bvec \c v to the \c it_file_old pointer
1083it_file_old& operator<<(it_file_old& f, const bvec& v);
1084
1085//!Write the cvec \c v to the \c it_file_old pointer
1086it_file_old& operator<<(it_file_old& f, const cvec& v);
1087
1088//!Write the string \c str to the \c it_file_old pointer
1089it_file_old& operator<<(it_file_old& f, const std::string& str);
1090
1091//!Write the mat \c m to the \c it_file_old pointer
1092it_file_old& operator<<(it_file_old& f, const mat& m);
1093
1094//!Write the imat \c m to the \c it_file_old pointer
1095it_file_old& operator<<(it_file_old& f, const imat& m);
1096
1097//!Write the bmat \c m to the \c it_file_old pointer
1098it_file_old& operator<<(it_file_old& f, const bmat& m);
1099
1100//!Write the cmat \c m to the \c it_file_old pointer
1101it_file_old& operator<<(it_file_old& f, const cmat& m);
1102
1103//!Write the float Array \c v to the \c it_file_old pointer
1104it_file_old& operator<<(it_file_old& f, const Array<float>& v);
1105
1106//!Write the double Array \c v to the \c it_file_old pointer
1107it_file_old& operator<<(it_file_old& f, const Array<double>& v);
1108
1109//!Write the int Array \c v to the \c it_file_old pointer
1110it_file_old& operator<<(it_file_old& f, const Array<int>& v);
1111
1112//!Write the bin Array \c v to the \c it_file_old pointer
1113it_file_old& operator<<(it_file_old& f, const Array<bin>& v);
1114
1115//!Write the float complex Array \c v to the \c it_file_old pointer
1116it_file_old& operator<<(it_file_old& f, const Array<std::complex<float> >& v);
1117
1118//!Write the double complex Array \c v to the \c it_file_old pointer
1119it_file_old& operator<<(it_file_old& f, const Array<std::complex<double> >& v);
1120
1121//!Write the vec Array \c v to the \c it_file_old pointer
1122it_file_old& operator<<(it_file_old& f, const Array<vec>& v);
1123
1124//!Write the ivec Array \c v to the \c it_file_old pointer
1125it_file_old& operator<<(it_file_old& f, const Array<ivec>& v);
1126
1127//!Write the bvec Array \c v to the \c it_file_old pointer
1128it_file_old& operator<<(it_file_old& f, const Array<bvec>& v);
1129
1130//!Write the cvec Array \c v to the \c it_file_old pointer
1131it_file_old& operator<<(it_file_old& f, const Array<cvec>& v);
1132
1133//!Write the string Array \c v to the \c it_file_old pointer
1134it_file_old& operator<<(it_file_old& f, const Array<std::string>& v);
1135
1136//!Write the mat Array \c v to the \c it_file_old pointer
1137it_file_old& operator<<(it_file_old& f, const Array<mat>& v);
1138
1139//!Write the imat Array \c v to the \c it_file_old pointer
1140it_file_old& operator<<(it_file_old& f, const Array<imat>& v);
1141
1142//!Write the bmat Array \c v to the \c it_file_old pointer
1143it_file_old& operator<<(it_file_old& f, const Array<bmat>& v);
1144
1145//!Write the cmat Array \c v to the \c it_file_old pointer
1146it_file_old& operator<<(it_file_old& f, const Array<cmat>& v);
1147
1148//!@}
1149
1150// ----------------------------------------------------------------------
1151// End of the deprecated implementation of IT++ file format version 2
1152// Will be removed in future versions
1153// ----------------------------------------------------------------------
1154
1155} // namespace itpp
1156
1157#endif // #ifndef IT_FILE_H
Note: See TracBrowser for help on using the browser.