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

Revision 956, 42.5 kB (checked in by sarka, 14 years ago)

to_setting

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
370  std::string get_fname(){
371                return fname;
372  }
373
374protected:
375  //! Remove the current variable, denoted by \c next_name
376  void remove();
377  //! Write data header \c h at the current file position
378  void write_data_header_here(const data_header& h);
379
380  //! Low precision flag. If true, use float type, otherwise double
381  bool low_prec;
382  //! Name to be used for saving the next variable
383  std::string next_name;
384  //! Description to be used for saving the next variable
385  std::string next_desc;
386
387private:
388  // Name of the opened file. Needed by the pack() method.
389  std::string fname;
390};
391
392
393/*!
394  \brief Flush operator
395  \ingroup itfile
396
397  Flushes the data. Usage:
398  \code
399  vec v1("1 2 3"), v2;
400  it_file f("file.it");
401  f << Name("v") << v1 << flush;
402  \endcode
403*/
404inline it_file& flush(it_file& f)
405{
406  f.flush();
407  return f;
408}
409
410/*!
411  \brief Automatic naming when saving
412  \ingroup itfile
413
414  An easy way to give a variable a name and optionally description when
415  saving. Usage:
416  \code
417  vec v1("1 2 3"), v2;
418  it_file f("file.it");
419  f << Name("v", "A vector of consecutive double values") << v1;
420  f >> Name("v") >> v2;
421  \endcode
422*/
423class Name
424{
425public:
426  //! Constructor
427  Name(const std::string& n, const std::string& d = ""): name(n), desc(d) {}
428  //! Dummy assignment operator - MSVC++ warning C4512
429  Name &operator=(const Name&) { return *this; }
430  //! The name string
431  const std::string& name;
432  //! The description
433  const std::string& desc;
434};
435
436
437/*! \addtogroup itfile */
438//!@{
439
440//! Finds the variable \c Name in the \c it_ifile. Returns file pointer for reading.
441inline it_ifile& operator>>(it_ifile& f, const Name& s)
442{
443  f.seek(s.name);
444  return f;
445}
446
447//! Finds the variable \c Name in the \c it_file. Returns file pointer for writing.
448inline it_file& operator<<(it_file& f, const Name& s)
449{
450  f.set_next_name(s.name, s.desc);
451  return f;
452}
453
454//! Read the char variable \c v from the \c it_ifile pointer
455it_ifile& operator>>(it_ifile& f, char& v);
456//! Read the bool variable \c v from the \c it_ifile pointer
457it_ifile& operator>>(it_ifile &f, bool &v);
458
459//! Read the binary variable \c v from the \c it_ifile pointer
460it_ifile& operator>>(it_ifile& f, bin& v);
461//! Read the short variable \c v from the \c it_ifile pointer
462it_ifile& operator>>(it_ifile& f, short& v);
463//! Read the integer variable \c v from the \c it_ifile pointer
464it_ifile& operator>>(it_ifile& f, int& v);
465//! Read the float variable \c v from the \c it_ifile pointer
466it_ifile& operator>>(it_ifile& f, float& v);
467//! Read the double variable \c v from the \c it_ifile pointer
468it_ifile& operator>>(it_ifile& f, double& v);
469//! Read the float complex variable \c v from the \c it_ifile pointer
470it_ifile& operator>>(it_ifile& f, std::complex<float>& v);
471//! Read the double complex variable \c v from the \c it_ifile pointer
472it_ifile& operator>>(it_ifile& f, std::complex<double>& v);
473
474//! Read the bvec \c v from the \c it_ifile pointer
475it_ifile& operator>>(it_ifile& f, bvec& v);
476//! Read the svec \c v from the \c it_ifile pointer
477it_ifile& operator>>(it_ifile& f, svec& v);
478//! Read the ivec \c v from the \c it_ifile pointer
479it_ifile& operator>>(it_ifile& f, ivec& v);
480//! Read the vec \c v from the \c it_ifile pointer
481it_ifile& operator>>(it_ifile& f, vec& v);
482//! Read the cvec \c v from the \c it_ifile pointer
483it_ifile& operator>>(it_ifile& f, cvec& v);
484
485//! Read the string \c str from the \c it_ifile pointer
486it_ifile& operator>>(it_ifile& f, std::string& str);
487
488//! Read the bmat \c m from the \c it_ifile pointer
489it_ifile& operator>>(it_ifile& f, bmat& m);
490//! Read the smat \c m from the \c it_ifile pointer
491it_ifile& operator>>(it_ifile& f, smat& m);
492//! Read the imat \c m from the \c it_ifile pointer
493it_ifile& operator>>(it_ifile& f, imat& m);
494//! Read the mat \c m from the \c it_ifile pointer
495it_ifile& operator>>(it_ifile& f, mat& m);
496//! Read the cmat \c m from the \c it_ifile pointer
497it_ifile& operator>>(it_ifile& f, cmat& m);
498
499//! Read the binary Array \c v from the \c it_ifile pointer
500it_ifile& operator>>(it_ifile& f, Array<bin>& v);
501//! Read the short integer Array \c v from the \c it_ifile pointer
502it_ifile& operator>>(it_ifile& f, Array<short>& v);
503//! Read the integer Array \c v from the \c it_ifile pointer
504it_ifile& operator>>(it_ifile& f, Array<int>& v);
505//! Read the float Array \c v from the \c it_ifile pointer
506it_ifile& operator>>(it_ifile& f, Array<float>& v);
507//! Read the double Array \c v from the \c it_ifile pointer
508it_ifile& operator>>(it_ifile& f, Array<double>& v);
509//! Read the float complex Array \c v from the \c it_ifile pointer
510it_ifile& operator>>(it_ifile& f, Array<std::complex<float> >& v);
511//! Read the double complex Array \c v from the \c it_ifile pointer
512it_ifile& operator>>(it_ifile& f, Array<std::complex<double> >& v);
513
514//! Read the bvec Array \c v from the \c it_ifile pointer
515it_ifile& operator>>(it_ifile& f, Array<bvec>& v);
516//! Read the svec Array \c v from the \c it_ifile pointer
517it_ifile& operator>>(it_ifile& f, Array<svec>& v);
518//! Read the ivec Array \c v from the \c it_ifile pointer
519it_ifile& operator>>(it_ifile& f, Array<ivec>& v);
520//! Read the vec Array \c v from the \c it_ifile pointer
521it_ifile& operator>>(it_ifile& f, Array<vec>& v);
522//! Read the cvec Array \c v from the \c it_ifile pointer
523it_ifile& operator>>(it_ifile& f, Array<cvec>& v);
524
525//! Read the string Array \c v from the \c it_ifile pointer
526it_ifile& operator>>(it_ifile& f, Array<std::string>& v);
527
528//! Read the bmat Array \c v from the \c it_ifile pointer
529it_ifile& operator>>(it_ifile& f, Array<bmat>& v);
530//! Read the bmat Array \c v from the \c it_ifile pointer
531it_ifile& operator>>(it_ifile& f, Array<smat>& v);
532//! Read the imat Array \c v from the \c it_ifile pointer
533it_ifile& operator>>(it_ifile& f, Array<imat>& v);
534//! Read the mat Array \c v from the \c it_ifile pointer
535it_ifile& operator>>(it_ifile& f, Array<mat>& v);
536//! Read the cmat Array \c v from the \c it_ifile pointer
537it_ifile& operator>>(it_ifile& f, Array<cmat>& v);
538
539
540//! Write the char variable \c x to the \c it_file pointer
541it_file& operator<<(it_file& f, char x);
542//! Write the bool variable \c x to the \c it_file pointer
543it_file& operator<<(it_file &f, bool x);
544
545//! Write the binary variable \c x to the \c it_file pointer
546it_file& operator<<(it_file& f, bin x);
547//! Write the short variable \c x to the \c it_file pointer
548it_file& operator<<(it_file& f, short x);
549//! Write the integer variable \c x to the \c it_file pointer
550it_file& operator<<(it_file& f, int x);
551//! Write the float variable \c x to the \c it_file pointer
552it_file& operator<<(it_file& f, float x);
553//! Write the double variable \c x to the \c it_file pointer
554it_file& operator<<(it_file& f, double x);
555//! Write the float complex variable \c x to the \c it_file pointer
556it_file& operator<<(it_file& f, std::complex<float> x);
557//! Write the double complex variable \c x to the \c it_file pointer
558it_file& operator<<(it_file& f, std::complex<double> x);
559
560//! Write the bvec \c v to the \c it_file pointer
561it_file& operator<<(it_file& f, const bvec& v);
562//! Write the svec \c v to the \c it_file pointer
563it_file& operator<<(it_file& f, const svec& v);
564//! Write the ivec \c v to the \c it_file pointer
565it_file& operator<<(it_file& f, const ivec& v);
566//! Write the vec \c v to the \c it_file pointer
567it_file& operator<<(it_file& f, const vec& v);
568//! Write the cvec \c v to the \c it_file pointer
569it_file& operator<<(it_file& f, const cvec& v);
570
571//! Write the string \c str to the \c it_file pointer
572it_file& operator<<(it_file& f, const std::string& str);
573
574//! Write the bmat \c m to the \c it_file pointer
575it_file& operator<<(it_file& f, const bmat& m);
576//! Write the smat \c m to the \c it_file pointer
577it_file& operator<<(it_file& f, const smat& m);
578//! Write the imat \c m to the \c it_file pointer
579it_file& operator<<(it_file& f, const imat& m);
580//! Write the mat \c m to the \c it_file pointer
581it_file& operator<<(it_file& f, const mat& m);
582//! Write the cmat \c m to the \c it_file pointer
583it_file& operator<<(it_file& f, const cmat& m);
584
585//! Write the bin Array \c v to the \c it_file pointer
586it_file& operator<<(it_file& f, const Array<bin>& v);
587//! Write the short int Array \c v to the \c it_file pointer
588it_file& operator<<(it_file& f, const Array<short>& v);
589//! Write the int Array \c v to the \c it_file pointer
590it_file& operator<<(it_file& f, const Array<int>& v);
591//! Write the float Array \c v to the \c it_file pointer
592it_file& operator<<(it_file& f, const Array<float>& v);
593//! Write the double Array \c v to the \c it_file pointer
594it_file& operator<<(it_file& f, const Array<double>& v);
595//! Write the float complex Array \c v to the \c it_file pointer
596it_file& operator<<(it_file& f, const Array<std::complex<float> >& v);
597//! Write the double complex Array \c v to the \c it_file pointer
598it_file& operator<<(it_file& f, const Array<std::complex<double> >& v);
599
600//! Write the bvec Array \c v to the \c it_file pointer
601it_file& operator<<(it_file& f, const Array<bvec>& v);
602//! Write the svec Array \c v to the \c it_file pointer
603it_file& operator<<(it_file& f, const Array<svec>& v);
604//! Write the ivec Array \c v to the \c it_file pointer
605it_file& operator<<(it_file& f, const Array<ivec>& v);
606//! Write the vec Array \c v to the \c it_file pointer
607it_file& operator<<(it_file& f, const Array<vec>& v);
608//! Write the cvec Array \c v to the \c it_file pointer
609it_file& operator<<(it_file& f, const Array<cvec>& v);
610
611//! Write the string Array \c v to the \c it_file pointer
612it_file& operator<<(it_file& f, const Array<std::string>& v);
613
614//! Write the bmat Array \c v to the \c it_file pointer
615it_file& operator<<(it_file& f, const Array<bmat>& v);
616//! Write the smat Array \c v to the \c it_file pointer
617it_file& operator<<(it_file& f, const Array<smat>& v);
618//! Write the imat Array \c v to the \c it_file pointer
619it_file& operator<<(it_file& f, const Array<imat>& v);
620//! Write the mat Array \c v to the \c it_file pointer
621it_file& operator<<(it_file& f, const Array<mat>& v);
622//! Write the cmat Array \c v to the \c it_file pointer
623it_file& operator<<(it_file& f, const Array<cmat>& v);
624
625//! Save the variable v in the file name.it as the name name.
626template <class T>
627void it_save_var_as(const T& v, const std::string& name)
628{
629  it_file f(name + ".it");
630  f << Name(name) << v;
631  f.close();
632}
633
634//! Load the variable v from the file name.it as the name name.
635template <class T>
636void it_load_var_as(T& v, const std::string& name)
637{
638  it_ifile f(name + ".it");
639  f.seek(name);
640  f >> v;
641  f.close();
642}
643
644//! A convenient macro. Calling it_save_var(M) saves M as 'M' in the file 'M.it'.
645#define it_save_var(v) it_save_var_as(v,#v)
646//! A convenient macro. Calling it_load_var(M) loads M as 'M' in the file 'M.it'.
647#define it_load_var(v) it_load_var_as(v,#v)
648
649//!@}
650
651
652// ----------------------------------------------------------------------
653// Deprecated implementation of IT++ file format version 2
654// Will be removed in future versions
655// ----------------------------------------------------------------------
656
657/*!
658  \brief Base class for it_ifile_old and it_file_old.
659
660  \warning This class is deprecated and will be removed in future.
661  \ingroup itfile
662*/
663class it_file_base_old
664{
665public:
666
667  //! Data header structure
668  struct data_header {
669    //! 0=little, 1=big
670    char endianity;
671    /*! size variables
672     * @{ */
673    uint32_t hdr_bytes, data_bytes, block_bytes;
674    /*! @} */
675    //! data name
676    std::string name;
677    //! data type, e.g. int32, float32, etc. type = "" means deleted
678    std::string type;
679  };
680
681protected:
682
683  //! File header structure
684  struct file_header {
685    //! ACTION: Add documentation
686    char magic[4];
687    //! ACTION: Add documentation
688    char version;
689  };
690  //! ACTION: Add documentation
691  static char file_magic[4];
692  //! ACTION: Add documentation
693  static char file_version;
694};
695
696/*!
697  \brief The old (version 2) IT++ file format reading class.
698
699  \warning This class is deprecated and will be removed in future.
700  \ingroup itfile
701*/
702class it_ifile_old : public it_file_base_old
703{
704public:
705  //!Constructor.
706  it_ifile_old();
707  //!Constructor. Calls open().
708  explicit it_ifile_old(const std::string& name);
709  //!Destructor.
710  virtual ~it_ifile_old() { }
711  //!Open a file.  The file must exist.
712  void open(const std::string& name);
713  //!Close a file.
714  virtual void close();
715  //!Returns pointer to the underlying \c bfstream used
716  bfstream& low_level() { return s; }
717
718  //!Reads and checks the file data header. Returns true if the header is valid and false otherwise.
719  bool read_check_file_header();
720  //!Read the data header and return the result in the variable \c h
721  void read_data_header(data_header& h);
722  //!Read a char value at the current file pointer position
723  void low_level_read(char& x);
724  //!Read a binary value at the current file pointer position
725  void low_level_read(bin& x);
726  //!Read a short value at the current file pointer position
727  void low_level_read(short& x);
728  //!Read an integer value at the current file pointer position
729  void low_level_read(int& x);
730  //!Read a float value at the current file pointer position
731  void low_level_read(float& x);
732  //!Read a double value at the current file pointer position
733  void low_level_read(double& x);
734  //!Read a float complex value at the current file pointer position
735  void low_level_read(std::complex<float>& x);
736  //!Read a double complex value at the current file pointer position
737  void low_level_read(std::complex<double>& x);
738  //!Read a vector of float values at the current file pointer position
739  void low_level_read_lo(vec& v);
740  //!Read a vector of double values at the current file pointer position
741  void low_level_read_hi(vec& v);
742  //!Read a vector of integer values at the current file pointer position
743  void low_level_read(ivec& v);
744  //!Read a vector of binary values at the current file pointer position
745  void low_level_read(bvec& v);
746  //!Read a vector of float complex values at the current file pointer position
747  void low_level_read_lo(cvec& v);
748  //!Read a vector of double complex values at the current file pointer position
749  void low_level_read_hi(cvec& v);
750  //!Read a string at the current file pointer position
751  void low_level_read(std::string& str);
752  //!Read a matrix of float values at the current file pointer position
753  void low_level_read_lo(mat& m);
754  //!Read a matrix of double values at the current file pointer position
755  void low_level_read_hi(mat& m);
756  //!Read a matrix of integer values at the current file pointer position
757  void low_level_read(imat& m);
758  //!Read a matrix of binary values at the current file pointer position
759  void low_level_read(bmat& m);
760  //!Read a matrix of float complex values at the current file pointer position
761  void low_level_read_lo(cmat& m);
762  //!Read a matrix of double complex values at the current file pointer position
763  void low_level_read_hi(cmat& m);
764
765  //!Read an Array of float values at the current file pointer position
766  void low_level_read_lo(Array<float>& v);
767  //!Read an Array of float values at the current file pointer position
768  void low_level_read_lo(Array<double>& v);
769  //!Read an Array of double values at the current file pointer position
770  void low_level_read_hi(Array<double>& v);
771  //!Read an Array of integer values at the current file pointer position
772  void low_level_read(Array<int>& v);
773  //!Read an Array of binary values at the current file pointer position
774  void low_level_read(Array<bin>& v);
775  //!Read an Array of float complex values at the current file pointer position
776  void low_level_read_lo(Array<std::complex<float> >& v);
777  //!Read an Array of float complex values at the current file pointer position
778  void low_level_read_lo(Array<std::complex<double> >& v);
779  //!Read an Array of double complex values at the current file pointer position
780  void low_level_read_hi(Array<std::complex<double> >& v);
781
782  //! Find the variable \c  name.
783  bool seek(const std::string& name);
784
785  //! Find the variable number \c n.
786  bool seek(int n);
787  //! Get information about the current variable.
788  void info(std::string& name, std::string& type, int& bytes);
789
790protected:
791  //! Protected binary file stream
792  bfstream s;
793};
794
795/*!
796  \brief The old (version 2) IT++ file format reading and writing class.
797
798  \warning This class is deprecated and will be removed in future.
799  \ingroup itfile
800*/
801class it_file_old : public it_ifile_old
802{
803public:
804  //! ACTION: Add documentation for this typedef
805  typedef it_file_old& (*it_manip)(it_file_old&);
806
807  //! Constructor.
808  it_file_old();
809
810  /*!
811    \brief Constructor.
812
813    If the file does not exist it will be created.  If \c trunc is true,
814    the file will be truncated.
815  */
816  explicit it_file_old(const std::string& name, bool trunc = false);
817
818  //! Destructor.
819  virtual ~it_file_old() { }
820
821  /*!
822    \brief Open a file for reading and writing.
823
824    If the file does not exist it will be created.  If \c  trunc is true,
825    the file will be truncated.
826  */
827  void open(const std::string& name, bool trunc = false);
828
829  //! Close the file.
830  void close();
831
832  //! Flush the data to disk.
833  void flush();
834
835  //! Returns pointer to the underlying \c bfstream used
836  bfstream& low_level() { return s; }
837
838  //! Set the precision. Low precision means floats, high means doubles.
839  void set_low_precision(bool p = true)  { low_prec = p; }
840
841  //! Get the precision.
842  bool get_low_precision() { return low_prec; }
843
844  //! Set the name of the next name to be saved. See also the \c Name class.
845  void set_next_name(const std::string& n) { next_name = n; }
846
847  //!Write the header for the \c it_file_old
848  void write_file_header();
849  //!Write the data header for a variable, specifying the type and size of the data to follow.
850  void write_data_header(const std::string& type, uint32_t size);
851  //!Write the data header for a variable, specifying the type, name, and size of the data to follow.
852  void write_data_header(const std::string& type, const std::string& name,
853                         uint32_t size);
854  //!Write a char value at the current file pointer position
855  void low_level_write(char x);
856  //!Write a binary value at the current file pointer position
857  void low_level_write(bin x);
858  //!Write a short value at the current file pointer position
859  void low_level_write(short x);
860  //!Write an integer value at the current file pointer position
861  void low_level_write(int x);
862  //!Write a float value at the current file pointer position
863  void low_level_write(float x);
864  //!Write a double value at the current file pointer position
865  void low_level_write(double x);
866  //!Write a float complex value at the current file pointer position
867  void low_level_write(const std::complex<float>& x);
868  //!Write a double complex value at the current file pointer position
869  void low_level_write(const std::complex<double>& x);
870  //!Write a vec at the current file pointer position
871  void low_level_write(const vec& v);
872  //!Write an ivec at the current file pointer position
873  void low_level_write(const ivec& v);
874  //!Write a bvec at the current file pointer position
875  void low_level_write(const bvec& v);
876  //!Write a cvec at the current file pointer position
877  void low_level_write(const cvec& v);
878  //!Write a string at the current file pointer position
879  void low_level_write(const std::string& str);
880  //!Write a mat at the current file pointer position
881  void low_level_write(const mat& m);
882  //!Write a imat at the current file pointer position
883  void low_level_write(const imat& m);
884  //!Write a bmat at the current file pointer position
885  void low_level_write(const bmat& m);
886  //!Write a cmat at the current file pointer position
887  void low_level_write(const cmat& m);
888  //!Write a float Array at the current file pointer position
889  void low_level_write(const Array<float>& v);
890  //!Write a double Array at the current file pointer position
891  void low_level_write(const Array<double>& v);
892  //!Write a integer Array at the current file pointer position
893  void low_level_write(const Array<int>& v);
894  //!Write a bin Array at the current file pointer position
895  void low_level_write(const Array<bin>& v);
896  //!Write a float complex Array at the current file pointer position
897  void low_level_write(const Array<std::complex<float> >& v);
898  //!Write a double complex Array at the current file pointer position
899  void low_level_write(const Array<std::complex<double> >& v);
900
901  //!ACTION: ADD DOCUMENTATION FOR THIS MEMBER !!!!!!!!
902  it_file_old& operator<<(it_manip func) { return (*func)(*this); }
903
904  //! Removes the variable \c name from the file.
905  void remove(const std::string& name);
906  //! Returns true if the variable \c name exists in the file.
907  bool exists(const std::string& name);
908  //! Remove slack space from the file.
909  void pack();
910
911protected:
912  //! ACTION: Add documenation for this protected member
913  void remove();
914  //! ACTION: Add documenation for this protected member
915  void write_data_header_here(const data_header& h);
916
917  //! ACTION: Add documenation for this protected member
918  bool low_prec;
919  //! ACTION: Add documenation for this protected member
920  std::string next_name;
921};
922
923/*!
924  \brief Flush operator.
925  \ingroup itfile
926
927  Flushes the data. Usage:
928
929  \code
930  vec v1("1 2 3"), v2;
931  it_file_old f("file.it");
932  f << Name("v") << v1 << flush;
933  \endcode
934*/
935inline it_file_old& flush(it_file_old& f)
936{
937  f.flush();
938  return f;
939}
940
941
942/*! \addtogroup itfile */
943//!@{
944
945//!Finds the variable \c Name in the \c it_ifile_old. Returns file pointer for reading.
946inline it_ifile_old& operator>>(it_ifile_old& f, const Name& s)
947{
948  f.seek(s.name);
949  return f;
950}
951
952//!Finds the variable \c Name in the \c it_file_old. Returns file pointer for writing.
953inline it_file_old& operator<<(it_file_old& f, const Name& s)
954{
955  f.set_next_name(s.name);
956  return f;
957}
958
959//!Read the char variable \c v from the \c it_ifile_old pointer
960it_ifile_old& operator>>(it_ifile_old& f, char& v);
961
962//!Read the binary variable \c v from the \c it_ifile_old pointer
963it_ifile_old& operator>>(it_ifile_old& f, bin& v);
964
965//!Read the short variable \c v from the \c it_ifile_old pointer
966it_ifile_old& operator>>(it_ifile_old& f, short& v);
967
968//!Read the integer variable \c v from the \c it_ifile_old pointer
969it_ifile_old& operator>>(it_ifile_old& f, int& v);
970
971//!Read the float variable \c v from the \c it_ifile_old pointer
972it_ifile_old& operator>>(it_ifile_old& f, float& v);
973
974//!Read the double variable \c v from the \c it_ifile_old pointer
975it_ifile_old& operator>>(it_ifile_old& f, double& v);
976
977//!Read the float complex variable \c v from the \c it_ifile_old pointer
978it_ifile_old& operator>>(it_ifile_old& f, std::complex<float>& v);
979
980//!Read the double complex variable \c v from the \c it_ifile_old pointer
981it_ifile_old& operator>>(it_ifile_old& f, std::complex<double>& v);
982
983//!Read the vec \c v from the \c it_ifile_old pointer
984it_ifile_old& operator>>(it_ifile_old& f, vec& v);
985
986//!Read the ivec \c v from the \c it_ifile_old pointer
987it_ifile_old& operator>>(it_ifile_old& f, ivec& v);
988
989//!Read the bvec \c v from the \c it_ifile_old pointer
990it_ifile_old& operator>>(it_ifile_old& f, bvec& v);
991
992//!Read the cvec \c v from the \c it_ifile_old pointer
993it_ifile_old& operator>>(it_ifile_old& f, cvec& v);
994
995//!Read the string \c str from the \c it_ifile_old pointer
996it_ifile_old& operator>>(it_ifile_old& f, std::string& str);
997
998//!Read the mat \c m from the \c it_ifile_old pointer
999it_ifile_old& operator>>(it_ifile_old& f, mat& m);
1000
1001//!Read the imat \c m from the \c it_ifile_old pointer
1002it_ifile_old& operator>>(it_ifile_old& f, imat& m);
1003
1004//!Read the bmat \c m from the \c it_ifile_old pointer
1005it_ifile_old& operator>>(it_ifile_old& f, bmat& m);
1006
1007//!Read the cmat \c m from the \c it_ifile_old pointer
1008it_ifile_old& operator>>(it_ifile_old& f, cmat& m);
1009
1010//!Read the float Array \c v from the \c it_ifile_old pointer
1011it_ifile_old& operator>>(it_ifile_old& f, Array<float>& v);
1012
1013//!Read the double Array \c v from the \c it_ifile_old pointer
1014it_ifile_old& operator>>(it_ifile_old& f, Array<double>& v);
1015
1016//!Read the integer Array \c v from the \c it_ifile_old pointer
1017it_ifile_old& operator>>(it_ifile_old& f, Array<int>& v);
1018
1019//!Read the binary Array \c v from the \c it_ifile_old pointer
1020it_ifile_old& operator>>(it_ifile_old& f, Array<bin>& v);
1021
1022//!Read the float complex Array \c v from the \c it_ifile_old pointer
1023it_ifile_old& operator>>(it_ifile_old& f, Array<std::complex<float> >& v);
1024
1025//!Read the double complex Array \c v from the \c it_ifile_old pointer
1026it_ifile_old& operator>>(it_ifile_old& f, Array<std::complex<double> >& v);
1027
1028//!Read the vec Array \c v from the \c it_ifile_old pointer
1029it_ifile_old& operator>>(it_ifile_old& f, Array<vec>& v);
1030
1031//!Read the ivec Array \c v from the \c it_ifile_old pointer
1032it_ifile_old& operator>>(it_ifile_old& f, Array<ivec>& v);
1033
1034//!Read the bvec Array \c v from the \c it_ifile_old pointer
1035it_ifile_old& operator>>(it_ifile_old& f, Array<bvec>& v);
1036
1037//!Read the cvec Array \c v from the \c it_ifile_old pointer
1038it_ifile_old& operator>>(it_ifile_old& f, Array<cvec>& v);
1039
1040//!Read the string Array \c v from the \c it_ifile_old pointer
1041it_ifile_old& operator>>(it_ifile_old& f, Array<std::string>& v);
1042
1043//!Read the mat Array \c v from the \c it_ifile_old pointer
1044it_ifile_old& operator>>(it_ifile_old& f, Array<mat>& v);
1045
1046//!Read the imat Array \c v from the \c it_ifile_old pointer
1047it_ifile_old& operator>>(it_ifile_old& f, Array<imat>& v);
1048
1049//!Read the bmat Array \c v from the \c it_ifile_old pointer
1050it_ifile_old& operator>>(it_ifile_old& f, Array<bmat>& v);
1051
1052//!Read the cmat Array \c v from the \c it_ifile_old pointer
1053it_ifile_old& operator>>(it_ifile_old& f, Array<cmat>& v);
1054
1055
1056//!Write the char variable \c x to the \c it_file_old pointer
1057it_file_old& operator<<(it_file_old& f, char x);
1058
1059//!Write the binary variable \c x to the \c it_file_old pointer
1060it_file_old& operator<<(it_file_old& f, bin x);
1061
1062//!Write the short variable \c x to the \c it_file_old pointer
1063it_file_old& operator<<(it_file_old& f, short x);
1064
1065//!Write the integer variable \c x to the \c it_file_old pointer
1066it_file_old& operator<<(it_file_old& f, int x);
1067
1068//!Write the float variable \c x to the \c it_file_old pointer
1069it_file_old& operator<<(it_file_old& f, float x);
1070
1071//!Write the double variable \c x to the \c it_file_old pointer
1072it_file_old& operator<<(it_file_old& f, double x);
1073
1074//!Write the float complex variable \c x to the \c it_file_old pointer
1075it_file_old& operator<<(it_file_old& f, std::complex<float> x);
1076
1077//!Write the double complex variable \c x to the \c it_file_old pointer
1078it_file_old& operator<<(it_file_old& f, std::complex<double> x);
1079
1080//!Write the vec \c v to the \c it_file_old pointer
1081it_file_old& operator<<(it_file_old& f, const vec& v);
1082
1083//!Write the ivec \c v to the \c it_file_old pointer
1084it_file_old& operator<<(it_file_old& f, const ivec& v);
1085
1086//!Write the bvec \c v to the \c it_file_old pointer
1087it_file_old& operator<<(it_file_old& f, const bvec& v);
1088
1089//!Write the cvec \c v to the \c it_file_old pointer
1090it_file_old& operator<<(it_file_old& f, const cvec& v);
1091
1092//!Write the string \c str to the \c it_file_old pointer
1093it_file_old& operator<<(it_file_old& f, const std::string& str);
1094
1095//!Write the mat \c m to the \c it_file_old pointer
1096it_file_old& operator<<(it_file_old& f, const mat& m);
1097
1098//!Write the imat \c m to the \c it_file_old pointer
1099it_file_old& operator<<(it_file_old& f, const imat& m);
1100
1101//!Write the bmat \c m to the \c it_file_old pointer
1102it_file_old& operator<<(it_file_old& f, const bmat& m);
1103
1104//!Write the cmat \c m to the \c it_file_old pointer
1105it_file_old& operator<<(it_file_old& f, const cmat& m);
1106
1107//!Write the float Array \c v to the \c it_file_old pointer
1108it_file_old& operator<<(it_file_old& f, const Array<float>& v);
1109
1110//!Write the double Array \c v to the \c it_file_old pointer
1111it_file_old& operator<<(it_file_old& f, const Array<double>& v);
1112
1113//!Write the int Array \c v to the \c it_file_old pointer
1114it_file_old& operator<<(it_file_old& f, const Array<int>& v);
1115
1116//!Write the bin Array \c v to the \c it_file_old pointer
1117it_file_old& operator<<(it_file_old& f, const Array<bin>& v);
1118
1119//!Write the float complex Array \c v to the \c it_file_old pointer
1120it_file_old& operator<<(it_file_old& f, const Array<std::complex<float> >& v);
1121
1122//!Write the double complex Array \c v to the \c it_file_old pointer
1123it_file_old& operator<<(it_file_old& f, const Array<std::complex<double> >& v);
1124
1125//!Write the vec Array \c v to the \c it_file_old pointer
1126it_file_old& operator<<(it_file_old& f, const Array<vec>& v);
1127
1128//!Write the ivec Array \c v to the \c it_file_old pointer
1129it_file_old& operator<<(it_file_old& f, const Array<ivec>& v);
1130
1131//!Write the bvec Array \c v to the \c it_file_old pointer
1132it_file_old& operator<<(it_file_old& f, const Array<bvec>& v);
1133
1134//!Write the cvec Array \c v to the \c it_file_old pointer
1135it_file_old& operator<<(it_file_old& f, const Array<cvec>& v);
1136
1137//!Write the string Array \c v to the \c it_file_old pointer
1138it_file_old& operator<<(it_file_old& f, const Array<std::string>& v);
1139
1140//!Write the mat Array \c v to the \c it_file_old pointer
1141it_file_old& operator<<(it_file_old& f, const Array<mat>& v);
1142
1143//!Write the imat Array \c v to the \c it_file_old pointer
1144it_file_old& operator<<(it_file_old& f, const Array<imat>& v);
1145
1146//!Write the bmat Array \c v to the \c it_file_old pointer
1147it_file_old& operator<<(it_file_old& f, const Array<bmat>& v);
1148
1149//!Write the cmat Array \c v to the \c it_file_old pointer
1150it_file_old& operator<<(it_file_old& f, const Array<cmat>& v);
1151
1152//!@}
1153
1154// ----------------------------------------------------------------------
1155// End of the deprecated implementation of IT++ file format version 2
1156// Will be removed in future versions
1157// ----------------------------------------------------------------------
1158
1159} // namespace itpp
1160
1161#endif // #ifndef IT_FILE_H
Note: See TracBrowser for help on using the browser.