IT++ Logo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
itfile.h
Go to the documentation of this file.
1 
29 #ifndef ITFILE_H
30 #define ITFILE_H
31 
32 #include <itpp/base/vec.h>
33 #include <itpp/base/array.h>
34 #include <itpp/base/binfile.h>
35 #include <itpp/base/ittypes.h>
36 
37 
38 namespace itpp
39 {
40 
93 {
94 public:
96  struct data_header {
98  uint64_t hdr_bytes;
100  uint64_t data_bytes;
102  uint64_t block_bytes;
104  std::string name;
106  std::string type;
108  std::string desc;
109  };
110 
111 protected:
113  struct file_header {
115  char magic[4];
117  char version;
118  };
120  static char file_magic[4];
122  static char file_version;
123 };
124 
125 
130 class it_ifile : public it_file_base
131 {
132 public:
134  it_ifile();
136  explicit it_ifile(const std::string& filename);
138  virtual ~it_ifile() { }
140  void open(const std::string& filename);
142  virtual void close();
144  bfstream& low_level() { return s; }
145 
147  bool read_check_file_header();
149  void read_data_header(data_header& h);
150 
152  void low_level_read(char& x);
154  void low_level_read(uint64_t& x);
156  void low_level_read(bool &x);
157 
159  void low_level_read(bin& x);
161  void low_level_read(short& x);
163  void low_level_read(int& x);
165  void low_level_read(float& x);
167  void low_level_read(double& x);
169  void low_level_read(std::complex<float>& x);
171  void low_level_read(std::complex<double>& x);
172 
174  void low_level_read(bvec& v);
176  void low_level_read(svec& v);
178  void low_level_read(ivec& v);
180  void low_level_read_lo(vec& v);
182  void low_level_read_hi(vec& v);
184  void low_level_read_lo(cvec& v);
186  void low_level_read_hi(cvec& v);
187 
189  void low_level_read(std::string& str);
190 
192  void low_level_read(bmat& m);
194  void low_level_read(smat& m);
196  void low_level_read(imat& m);
198  void low_level_read_lo(mat& m);
200  void low_level_read_hi(mat& m);
202  void low_level_read_lo(cmat& m);
204  void low_level_read_hi(cmat& m);
205 
207  void low_level_read(Array<bin>& v);
209  void low_level_read(Array<short>& v);
211  void low_level_read(Array<int>& v);
213  void low_level_read(Array<float>& v);
219  void low_level_read(Array<std::complex<float> >& v);
221  void low_level_read_lo(Array<std::complex<double> >& v);
223  void low_level_read_hi(Array<std::complex<double> >& v);
224 
226  bool seek(const std::string& name);
228  bool seek(int n);
230  void info(std::string& name, std::string& type, std::string& desc,
231  uint64_t& bytes);
232 
233 protected:
236 };
237 
238 
243 class it_file : public it_ifile
244 {
245 public:
247  typedef it_file& (*it_manip)(it_file&);
248 
250  it_file();
251 
258  explicit it_file(const std::string& filename, bool trunc = false);
259 
261  virtual ~it_file() { }
262 
269  void open(const std::string& filename, bool trunc = false);
270 
272  void close();
274  void flush();
275 
277  bfstream& low_level() { return s; }
278 
280  void set_low_precision(bool p = true) { low_prec = p; }
282  bool get_low_precision() const { return low_prec; }
283 
285  void set_next_name(const std::string& name,
286  const std::string& description = "")
287  { next_name = name; next_desc = description; }
288 
290  void write_file_header();
292  void write_data_header(const std::string& type, uint64_t size);
294  void write_data_header(const std::string& type, const std::string& name,
295  uint64_t size, const std::string& description = "");
296 
298  void low_level_write(char x);
300  void low_level_write(uint64_t x);
302  void low_level_write(bool x);
303 
305  void low_level_write(bin x);
307  void low_level_write(short x);
309  void low_level_write(int x);
311  void low_level_write(float x);
313  void low_level_write(double x);
315  void low_level_write(const std::complex<float>& x);
317  void low_level_write(const std::complex<double>& x);
318 
320  void low_level_write(const bvec& v);
322  void low_level_write(const svec& v);
324  void low_level_write(const ivec& v);
326  void low_level_write(const vec& v);
328  void low_level_write(const cvec& v);
329 
331  void low_level_write(const std::string& str);
332 
334  void low_level_write(const bmat& m);
336  void low_level_write(const smat& m);
338  void low_level_write(const imat& m);
340  void low_level_write(const mat& m);
342  void low_level_write(const cmat& m);
343 
345  void low_level_write(const Array<bin>& v);
347  void low_level_write(const Array<short>& v);
349  void low_level_write(const Array<int>& v);
351  void low_level_write(const Array<float>& v);
353  void low_level_write(const Array<double>& v);
355  void low_level_write(const Array<std::complex<float> >& v);
357  void low_level_write(const Array<std::complex<double> >& v);
358 
360  it_file& operator<<(it_manip func) { return (*func)(*this); }
361 
363  void remove(const std::string& name);
365  bool exists(const std::string& name);
367  void pack();
368 
369 protected:
371  void remove();
373  void write_data_header_here(const data_header& h);
374 
376  bool low_prec;
378  std::string next_name;
380  std::string next_desc;
381 
382 private:
383  // Name of the opened file. Needed by the pack() method.
384  std::string fname;
385 };
386 
387 
399 inline it_file& flush(it_file& f)
400 {
401  f.flush();
402  return f;
403 }
404 
418 class Name
419 {
420 public:
422  Name(const std::string& n, const std::string& d = ""): name(n), desc(d) {}
424  Name &operator=(const Name&) { return *this; }
426  const std::string& name;
428  const std::string& desc;
429 };
430 
431 
433 
434 
436 inline it_ifile& operator>>(it_ifile& f, const Name& s)
437 {
438  f.seek(s.name);
439  return f;
440 }
441 
443 inline it_file& operator<<(it_file& f, const Name& s)
444 {
445  f.set_next_name(s.name, s.desc);
446  return f;
447 }
448 
450 it_ifile& operator>>(it_ifile& f, char& v);
452 it_ifile& operator>>(it_ifile &f, bool &v);
453 
455 it_ifile& operator>>(it_ifile& f, bin& v);
457 it_ifile& operator>>(it_ifile& f, short& v);
459 it_ifile& operator>>(it_ifile& f, int& v);
461 it_ifile& operator>>(it_ifile& f, float& v);
463 it_ifile& operator>>(it_ifile& f, double& v);
465 it_ifile& operator>>(it_ifile& f, std::complex<float>& v);
467 it_ifile& operator>>(it_ifile& f, std::complex<double>& v);
468 
470 it_ifile& operator>>(it_ifile& f, bvec& v);
472 it_ifile& operator>>(it_ifile& f, svec& v);
474 it_ifile& operator>>(it_ifile& f, ivec& v);
476 it_ifile& operator>>(it_ifile& f, vec& v);
478 it_ifile& operator>>(it_ifile& f, cvec& v);
479 
481 it_ifile& operator>>(it_ifile& f, std::string& str);
482 
484 it_ifile& operator>>(it_ifile& f, bmat& m);
486 it_ifile& operator>>(it_ifile& f, smat& m);
488 it_ifile& operator>>(it_ifile& f, imat& m);
490 it_ifile& operator>>(it_ifile& f, mat& m);
492 it_ifile& operator>>(it_ifile& f, cmat& m);
493 
495 it_ifile& operator>>(it_ifile& f, Array<bin>& v);
497 it_ifile& operator>>(it_ifile& f, Array<short>& v);
499 it_ifile& operator>>(it_ifile& f, Array<int>& v);
501 it_ifile& operator>>(it_ifile& f, Array<float>& v);
503 it_ifile& operator>>(it_ifile& f, Array<double>& v);
505 it_ifile& operator>>(it_ifile& f, Array<std::complex<float> >& v);
507 it_ifile& operator>>(it_ifile& f, Array<std::complex<double> >& v);
508 
510 it_ifile& operator>>(it_ifile& f, Array<bvec>& v);
512 it_ifile& operator>>(it_ifile& f, Array<svec>& v);
514 it_ifile& operator>>(it_ifile& f, Array<ivec>& v);
516 it_ifile& operator>>(it_ifile& f, Array<vec>& v);
518 it_ifile& operator>>(it_ifile& f, Array<cvec>& v);
519 
521 it_ifile& operator>>(it_ifile& f, Array<std::string>& v);
522 
524 it_ifile& operator>>(it_ifile& f, Array<bmat>& v);
526 it_ifile& operator>>(it_ifile& f, Array<smat>& v);
528 it_ifile& operator>>(it_ifile& f, Array<imat>& v);
530 it_ifile& operator>>(it_ifile& f, Array<mat>& v);
532 it_ifile& operator>>(it_ifile& f, Array<cmat>& v);
533 
534 
536 it_file& operator<<(it_file& f, char x);
538 it_file& operator<<(it_file &f, bool x);
539 
541 it_file& operator<<(it_file& f, bin x);
543 it_file& operator<<(it_file& f, short x);
545 it_file& operator<<(it_file& f, int x);
547 it_file& operator<<(it_file& f, float x);
549 it_file& operator<<(it_file& f, double x);
551 it_file& operator<<(it_file& f, std::complex<float> x);
553 it_file& operator<<(it_file& f, std::complex<double> x);
554 
556 it_file& operator<<(it_file& f, const bvec& v);
558 it_file& operator<<(it_file& f, const svec& v);
560 it_file& operator<<(it_file& f, const ivec& v);
562 it_file& operator<<(it_file& f, const vec& v);
564 it_file& operator<<(it_file& f, const cvec& v);
565 
567 it_file& operator<<(it_file& f, const std::string& str);
568 
570 it_file& operator<<(it_file& f, const bmat& m);
572 it_file& operator<<(it_file& f, const smat& m);
574 it_file& operator<<(it_file& f, const imat& m);
576 it_file& operator<<(it_file& f, const mat& m);
578 it_file& operator<<(it_file& f, const cmat& m);
579 
581 it_file& operator<<(it_file& f, const Array<bin>& v);
583 it_file& operator<<(it_file& f, const Array<short>& v);
585 it_file& operator<<(it_file& f, const Array<int>& v);
587 it_file& operator<<(it_file& f, const Array<float>& v);
589 it_file& operator<<(it_file& f, const Array<double>& v);
591 it_file& operator<<(it_file& f, const Array<std::complex<float> >& v);
593 it_file& operator<<(it_file& f, const Array<std::complex<double> >& v);
594 
596 it_file& operator<<(it_file& f, const Array<bvec>& v);
598 it_file& operator<<(it_file& f, const Array<svec>& v);
600 it_file& operator<<(it_file& f, const Array<ivec>& v);
602 it_file& operator<<(it_file& f, const Array<vec>& v);
604 it_file& operator<<(it_file& f, const Array<cvec>& v);
605 
607 it_file& operator<<(it_file& f, const Array<std::string>& v);
608 
610 it_file& operator<<(it_file& f, const Array<bmat>& v);
612 it_file& operator<<(it_file& f, const Array<smat>& v);
614 it_file& operator<<(it_file& f, const Array<imat>& v);
616 it_file& operator<<(it_file& f, const Array<mat>& v);
618 it_file& operator<<(it_file& f, const Array<cmat>& v);
619 
621 template <class T>
622 void it_save_var_as(const T& v, const std::string& name)
623 {
624  it_file f(name + ".it");
625  f << Name(name) << v;
626  f.close();
627 }
628 
630 template <class T>
631 void it_load_var_as(T& v, const std::string& name)
632 {
633  it_ifile f(name + ".it");
634  f.seek(name);
635  f >> v;
636  f.close();
637 }
638 
640 #define it_save_var(v) it_save_var_as(v,#v)
641 
642 #define it_load_var(v) it_load_var_as(v,#v)
643 
645 
646 
647 // ----------------------------------------------------------------------
648 // Deprecated implementation of IT++ file format version 2
649 // Will be removed in future versions
650 // ----------------------------------------------------------------------
651 
659 {
660 public:
661 
663  struct data_header {
665  char endianity;
670 
671  std::string name;
673  std::string type;
674  };
675 
676 protected:
677 
679  struct file_header {
681  char magic[4];
683  char version;
684  };
686  static char file_magic[4];
688  static char file_version;
689 };
690 
698 {
699 public:
701  it_ifile_old();
703  explicit it_ifile_old(const std::string& name);
705  virtual ~it_ifile_old() { }
707  void open(const std::string& name);
709  virtual void close();
711  bfstream& low_level() { return s; }
712 
714  bool read_check_file_header();
716  void read_data_header(data_header& h);
718  void low_level_read(char& x);
720  void low_level_read(bin& x);
722  void low_level_read(short& x);
724  void low_level_read(int& x);
726  void low_level_read(float& x);
728  void low_level_read(double& x);
730  void low_level_read(std::complex<float>& x);
732  void low_level_read(std::complex<double>& x);
734  void low_level_read_lo(vec& v);
736  void low_level_read_hi(vec& v);
738  void low_level_read(ivec& v);
740  void low_level_read(bvec& v);
742  void low_level_read_lo(cvec& v);
744  void low_level_read_hi(cvec& v);
746  void low_level_read(std::string& str);
748  void low_level_read_lo(mat& m);
750  void low_level_read_hi(mat& m);
752  void low_level_read(imat& m);
754  void low_level_read(bmat& m);
756  void low_level_read_lo(cmat& m);
758  void low_level_read_hi(cmat& m);
759 
767  void low_level_read(Array<int>& v);
769  void low_level_read(Array<bin>& v);
771  void low_level_read_lo(Array<std::complex<float> >& v);
773  void low_level_read_lo(Array<std::complex<double> >& v);
775  void low_level_read_hi(Array<std::complex<double> >& v);
776 
778  bool seek(const std::string& name);
779 
781  bool seek(int n);
783  void info(std::string& name, std::string& type, int& bytes);
784 
785 protected:
788 };
789 
796 class it_file_old : public it_ifile_old
797 {
798 public:
800  typedef it_file_old& (*it_manip)(it_file_old&);
801 
803  it_file_old();
804 
811  explicit it_file_old(const std::string& name, bool trunc = false);
812 
814  virtual ~it_file_old() { }
815 
822  void open(const std::string& name, bool trunc = false);
823 
825  void close();
826 
828  void flush();
829 
831  bfstream& low_level() { return s; }
832 
834  void set_low_precision(bool p = true) { low_prec = p; }
835 
837  bool get_low_precision() { return low_prec; }
838 
840  void set_next_name(const std::string& n) { next_name = n; }
841 
843  void write_file_header();
845  void write_data_header(const std::string& type, uint32_t size);
847  void write_data_header(const std::string& type, const std::string& name,
848  uint32_t size);
850  void low_level_write(char x);
852  void low_level_write(bin x);
854  void low_level_write(short x);
856  void low_level_write(int x);
858  void low_level_write(float x);
860  void low_level_write(double x);
862  void low_level_write(const std::complex<float>& x);
864  void low_level_write(const std::complex<double>& x);
866  void low_level_write(const vec& v);
868  void low_level_write(const ivec& v);
870  void low_level_write(const bvec& v);
872  void low_level_write(const cvec& v);
874  void low_level_write(const std::string& str);
876  void low_level_write(const mat& m);
878  void low_level_write(const imat& m);
880  void low_level_write(const bmat& m);
882  void low_level_write(const cmat& m);
884  void low_level_write(const Array<float>& v);
886  void low_level_write(const Array<double>& v);
888  void low_level_write(const Array<int>& v);
890  void low_level_write(const Array<bin>& v);
892  void low_level_write(const Array<std::complex<float> >& v);
894  void low_level_write(const Array<std::complex<double> >& v);
895 
897  it_file_old& operator<<(it_manip func) { return (*func)(*this); }
898 
900  void remove(const std::string& name);
902  bool exists(const std::string& name);
904  void pack();
905 
906 protected:
908  void remove();
910  void write_data_header_here(const data_header& h);
911 
913  bool low_prec;
915  std::string next_name;
916 };
917 
931 {
932  f.flush();
933  return f;
934 }
935 
936 
938 
939 
942 {
943  f.seek(s.name);
944  return f;
945 }
946 
948 inline it_file_old& operator<<(it_file_old& f, const Name& s)
949 {
950  f.set_next_name(s.name);
951  return f;
952 }
953 
955 it_ifile_old& operator>>(it_ifile_old& f, char& v);
956 
958 it_ifile_old& operator>>(it_ifile_old& f, bin& v);
959 
961 it_ifile_old& operator>>(it_ifile_old& f, short& v);
962 
964 it_ifile_old& operator>>(it_ifile_old& f, int& v);
965 
967 it_ifile_old& operator>>(it_ifile_old& f, float& v);
968 
970 it_ifile_old& operator>>(it_ifile_old& f, double& v);
971 
973 it_ifile_old& operator>>(it_ifile_old& f, std::complex<float>& v);
974 
976 it_ifile_old& operator>>(it_ifile_old& f, std::complex<double>& v);
977 
979 it_ifile_old& operator>>(it_ifile_old& f, vec& v);
980 
982 it_ifile_old& operator>>(it_ifile_old& f, ivec& v);
983 
985 it_ifile_old& operator>>(it_ifile_old& f, bvec& v);
986 
988 it_ifile_old& operator>>(it_ifile_old& f, cvec& v);
989 
991 it_ifile_old& operator>>(it_ifile_old& f, std::string& str);
992 
994 it_ifile_old& operator>>(it_ifile_old& f, mat& m);
995 
997 it_ifile_old& operator>>(it_ifile_old& f, imat& m);
998 
1000 it_ifile_old& operator>>(it_ifile_old& f, bmat& m);
1001 
1003 it_ifile_old& operator>>(it_ifile_old& f, cmat& m);
1004 
1006 it_ifile_old& operator>>(it_ifile_old& f, Array<float>& v);
1007 
1009 it_ifile_old& operator>>(it_ifile_old& f, Array<double>& v);
1010 
1012 it_ifile_old& operator>>(it_ifile_old& f, Array<int>& v);
1013 
1015 it_ifile_old& operator>>(it_ifile_old& f, Array<bin>& v);
1016 
1018 it_ifile_old& operator>>(it_ifile_old& f, Array<std::complex<float> >& v);
1019 
1021 it_ifile_old& operator>>(it_ifile_old& f, Array<std::complex<double> >& v);
1022 
1024 it_ifile_old& operator>>(it_ifile_old& f, Array<vec>& v);
1025 
1027 it_ifile_old& operator>>(it_ifile_old& f, Array<ivec>& v);
1028 
1030 it_ifile_old& operator>>(it_ifile_old& f, Array<bvec>& v);
1031 
1033 it_ifile_old& operator>>(it_ifile_old& f, Array<cvec>& v);
1034 
1036 it_ifile_old& operator>>(it_ifile_old& f, Array<std::string>& v);
1037 
1039 it_ifile_old& operator>>(it_ifile_old& f, Array<mat>& v);
1040 
1042 it_ifile_old& operator>>(it_ifile_old& f, Array<imat>& v);
1043 
1045 it_ifile_old& operator>>(it_ifile_old& f, Array<bmat>& v);
1046 
1048 it_ifile_old& operator>>(it_ifile_old& f, Array<cmat>& v);
1049 
1050 
1052 it_file_old& operator<<(it_file_old& f, char x);
1053 
1055 it_file_old& operator<<(it_file_old& f, bin x);
1056 
1058 it_file_old& operator<<(it_file_old& f, short x);
1059 
1061 it_file_old& operator<<(it_file_old& f, int x);
1062 
1064 it_file_old& operator<<(it_file_old& f, float x);
1065 
1067 it_file_old& operator<<(it_file_old& f, double x);
1068 
1070 it_file_old& operator<<(it_file_old& f, std::complex<float> x);
1071 
1073 it_file_old& operator<<(it_file_old& f, std::complex<double> x);
1074 
1076 it_file_old& operator<<(it_file_old& f, const vec& v);
1077 
1079 it_file_old& operator<<(it_file_old& f, const ivec& v);
1080 
1082 it_file_old& operator<<(it_file_old& f, const bvec& v);
1083 
1085 it_file_old& operator<<(it_file_old& f, const cvec& v);
1086 
1088 it_file_old& operator<<(it_file_old& f, const std::string& str);
1089 
1091 it_file_old& operator<<(it_file_old& f, const mat& m);
1092 
1094 it_file_old& operator<<(it_file_old& f, const imat& m);
1095 
1097 it_file_old& operator<<(it_file_old& f, const bmat& m);
1098 
1100 it_file_old& operator<<(it_file_old& f, const cmat& m);
1101 
1103 it_file_old& operator<<(it_file_old& f, const Array<float>& v);
1104 
1106 it_file_old& operator<<(it_file_old& f, const Array<double>& v);
1107 
1109 it_file_old& operator<<(it_file_old& f, const Array<int>& v);
1110 
1112 it_file_old& operator<<(it_file_old& f, const Array<bin>& v);
1113 
1115 it_file_old& operator<<(it_file_old& f, const Array<std::complex<float> >& v);
1116 
1118 it_file_old& operator<<(it_file_old& f, const Array<std::complex<double> >& v);
1119 
1121 it_file_old& operator<<(it_file_old& f, const Array<vec>& v);
1122 
1124 it_file_old& operator<<(it_file_old& f, const Array<ivec>& v);
1125 
1127 it_file_old& operator<<(it_file_old& f, const Array<bvec>& v);
1128 
1130 it_file_old& operator<<(it_file_old& f, const Array<cvec>& v);
1131 
1133 it_file_old& operator<<(it_file_old& f, const Array<std::string>& v);
1134 
1136 it_file_old& operator<<(it_file_old& f, const Array<mat>& v);
1137 
1139 it_file_old& operator<<(it_file_old& f, const Array<imat>& v);
1140 
1142 it_file_old& operator<<(it_file_old& f, const Array<bmat>& v);
1143 
1145 it_file_old& operator<<(it_file_old& f, const Array<cmat>& v);
1146 
1148 
1149 // ----------------------------------------------------------------------
1150 // End of the deprecated implementation of IT++ file format version 2
1151 // Will be removed in future versions
1152 // ----------------------------------------------------------------------
1153 
1154 } // namespace itpp
1155 
1156 #endif // #ifndef IT_FILE_H
1157 
SourceForge Logo

Generated on Fri Mar 21 2014 17:14:12 for IT++ by Doxygen 1.8.1.2