root/win32/itpp-4.0.1/itpp/base/itfile.h @ 35

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

zasadni zmeny ve /win32

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