00001
00029 #ifndef ITFILE_H
00030 #define ITFILE_H
00031
00032 #include <itpp/base/vec.h>
00033 #include <itpp/base/array.h>
00034 #include <itpp/base/binfile.h>
00035 #include <itpp/base/itcompat.h>
00036
00037
00038 namespace itpp
00039 {
00040
00092 class it_file_base
00093 {
00094 public:
00096 struct data_header {
00098 uint64_t hdr_bytes;
00100 uint64_t data_bytes;
00102 uint64_t block_bytes;
00104 std::string name;
00106 std::string type;
00108 std::string desc;
00109 };
00110
00111 protected:
00113 struct file_header {
00115 char magic[4];
00117 char version;
00118 };
00120 static char file_magic[4];
00122 static char file_version;
00123 };
00124
00125
00130 class it_ifile : public it_file_base
00131 {
00132 public:
00134 it_ifile();
00136 explicit it_ifile(const std::string& filename);
00138 virtual ~it_ifile() { }
00140 void open(const std::string& filename);
00142 virtual void close();
00144 bfstream& low_level() { return s; }
00145
00147 bool read_check_file_header();
00149 void read_data_header(data_header& h);
00150
00152 void low_level_read(char& x);
00154 void low_level_read(uint64_t& x);
00156 void low_level_read(bool &x);
00157
00159 void low_level_read(bin& x);
00161 void low_level_read(short& x);
00163 void low_level_read(int& x);
00165 void low_level_read(float& x);
00167 void low_level_read(double& x);
00169 void low_level_read(std::complex<float>& x);
00171 void low_level_read(std::complex<double>& x);
00172
00174 void low_level_read(bvec& v);
00176 void low_level_read(svec& v);
00178 void low_level_read(ivec& v);
00180 void low_level_read_lo(vec& v);
00182 void low_level_read_hi(vec& v);
00184 void low_level_read_lo(cvec& v);
00186 void low_level_read_hi(cvec& v);
00187
00189 void low_level_read(std::string& str);
00190
00192 void low_level_read(bmat& m);
00194 void low_level_read(smat& m);
00196 void low_level_read(imat& m);
00198 void low_level_read_lo(mat& m);
00200 void low_level_read_hi(mat& m);
00202 void low_level_read_lo(cmat& m);
00204 void low_level_read_hi(cmat& m);
00205
00207 void low_level_read(Array<bin>& v);
00209 void low_level_read(Array<short>& v);
00211 void low_level_read(Array<int>& v);
00213 void low_level_read(Array<float>& v);
00215 void low_level_read_lo(Array<double>& v);
00217 void low_level_read_hi(Array<double>& v);
00219 void low_level_read(Array<std::complex<float> >& v);
00221 void low_level_read_lo(Array<std::complex<double> >& v);
00223 void low_level_read_hi(Array<std::complex<double> >& v);
00224
00226 bool seek(const std::string& name);
00228 bool seek(int n);
00230 void info(std::string& name, std::string& type, std::string& desc,
00231 uint64_t& bytes);
00232
00233 protected:
00235 bfstream s;
00236 };
00237
00238
00243 class it_file : public it_ifile
00244 {
00245 public:
00247 typedef it_file& (*it_manip)(it_file&);
00248
00250 it_file();
00251
00258 explicit it_file(const std::string& filename, bool trunc = false);
00259
00261 virtual ~it_file() { }
00262
00269 void open(const std::string& filename, bool trunc = false);
00270
00272 void close();
00274 void flush();
00275
00277 bfstream& low_level() { return s; }
00278
00280 void set_low_precision(bool p = true) { low_prec = p; }
00282 bool get_low_precision() const { return low_prec; }
00283
00285 void set_next_name(const std::string& name,
00286 const std::string& description = "")
00287 { next_name = name; next_desc = description; }
00288
00290 void write_file_header();
00292 void write_data_header(const std::string& type, uint64_t size);
00294 void write_data_header(const std::string& type, const std::string& name,
00295 uint64_t size, const std::string& description = "");
00296
00298 void low_level_write(char x);
00300 void low_level_write(uint64_t x);
00302 void low_level_write(bool x);
00303
00305 void low_level_write(bin x);
00307 void low_level_write(short x);
00309 void low_level_write(int x);
00311 void low_level_write(float x);
00313 void low_level_write(double x);
00315 void low_level_write(const std::complex<float>& x);
00317 void low_level_write(const std::complex<double>& x);
00318
00320 void low_level_write(const bvec& v);
00322 void low_level_write(const svec& v);
00324 void low_level_write(const ivec& v);
00326 void low_level_write(const vec& v);
00328 void low_level_write(const cvec& v);
00329
00331 void low_level_write(const std::string& str);
00332
00334 void low_level_write(const bmat& m);
00336 void low_level_write(const smat& m);
00338 void low_level_write(const imat& m);
00340 void low_level_write(const mat& m);
00342 void low_level_write(const cmat& m);
00343
00345 void low_level_write(const Array<bin>& v);
00347 void low_level_write(const Array<short>& v);
00349 void low_level_write(const Array<int>& v);
00351 void low_level_write(const Array<float>& v);
00353 void low_level_write(const Array<double>& v);
00355 void low_level_write(const Array<std::complex<float> >& v);
00357 void low_level_write(const Array<std::complex<double> >& v);
00358
00360 it_file& operator<<(it_manip func) { return (*func)(*this); }
00361
00363 void remove(const std::string& name);
00365 bool exists(const std::string& name);
00367 void pack();
00368
00369 protected:
00371 void remove();
00373 void write_data_header_here(const data_header& h);
00374
00376 bool low_prec;
00378 std::string next_name;
00380 std::string next_desc;
00381
00382 private:
00383
00384 std::string fname;
00385 };
00386
00387
00399 inline it_file& flush(it_file& f)
00400 {
00401 f.flush();
00402 return f;
00403 }
00404
00418 class Name
00419 {
00420 public:
00422 Name(const std::string& n, const std::string& d = ""): name(n), desc(d) {}
00424 Name &operator=(const Name&) { return *this; }
00426 const std::string& name;
00428 const std::string& desc;
00429 };
00430
00431
00433
00434
00436 inline it_ifile& operator>>(it_ifile& f, const Name& s)
00437 {
00438 f.seek(s.name);
00439 return f;
00440 }
00441
00443 inline it_file& operator<<(it_file& f, const Name& s)
00444 {
00445 f.set_next_name(s.name, s.desc);
00446 return f;
00447 }
00448
00450 it_ifile& operator>>(it_ifile& f, char& v);
00452 it_ifile& operator>>(it_ifile &f, bool &v);
00453
00455 it_ifile& operator>>(it_ifile& f, bin& v);
00457 it_ifile& operator>>(it_ifile& f, short& v);
00459 it_ifile& operator>>(it_ifile& f, int& v);
00461 it_ifile& operator>>(it_ifile& f, float& v);
00463 it_ifile& operator>>(it_ifile& f, double& v);
00465 it_ifile& operator>>(it_ifile& f, std::complex<float>& v);
00467 it_ifile& operator>>(it_ifile& f, std::complex<double>& v);
00468
00470 it_ifile& operator>>(it_ifile& f, bvec& v);
00472 it_ifile& operator>>(it_ifile& f, svec& v);
00474 it_ifile& operator>>(it_ifile& f, ivec& v);
00476 it_ifile& operator>>(it_ifile& f, vec& v);
00478 it_ifile& operator>>(it_ifile& f, cvec& v);
00479
00481 it_ifile& operator>>(it_ifile& f, std::string& str);
00482
00484 it_ifile& operator>>(it_ifile& f, bmat& m);
00486 it_ifile& operator>>(it_ifile& f, smat& m);
00488 it_ifile& operator>>(it_ifile& f, imat& m);
00490 it_ifile& operator>>(it_ifile& f, mat& m);
00492 it_ifile& operator>>(it_ifile& f, cmat& m);
00493
00495 it_ifile& operator>>(it_ifile& f, Array<bin>& v);
00497 it_ifile& operator>>(it_ifile& f, Array<short>& v);
00499 it_ifile& operator>>(it_ifile& f, Array<int>& v);
00501 it_ifile& operator>>(it_ifile& f, Array<float>& v);
00503 it_ifile& operator>>(it_ifile& f, Array<double>& v);
00505 it_ifile& operator>>(it_ifile& f, Array<std::complex<float> >& v);
00507 it_ifile& operator>>(it_ifile& f, Array<std::complex<double> >& v);
00508
00510 it_ifile& operator>>(it_ifile& f, Array<bvec>& v);
00512 it_ifile& operator>>(it_ifile& f, Array<svec>& v);
00514 it_ifile& operator>>(it_ifile& f, Array<ivec>& v);
00516 it_ifile& operator>>(it_ifile& f, Array<vec>& v);
00518 it_ifile& operator>>(it_ifile& f, Array<cvec>& v);
00519
00521 it_ifile& operator>>(it_ifile& f, Array<std::string>& v);
00522
00524 it_ifile& operator>>(it_ifile& f, Array<bmat>& v);
00526 it_ifile& operator>>(it_ifile& f, Array<smat>& v);
00528 it_ifile& operator>>(it_ifile& f, Array<imat>& v);
00530 it_ifile& operator>>(it_ifile& f, Array<mat>& v);
00532 it_ifile& operator>>(it_ifile& f, Array<cmat>& v);
00533
00534
00536 it_file& operator<<(it_file& f, char x);
00538 it_file& operator<<(it_file &f, bool x);
00539
00541 it_file& operator<<(it_file& f, bin x);
00543 it_file& operator<<(it_file& f, short x);
00545 it_file& operator<<(it_file& f, int x);
00547 it_file& operator<<(it_file& f, float x);
00549 it_file& operator<<(it_file& f, double x);
00551 it_file& operator<<(it_file& f, std::complex<float> x);
00553 it_file& operator<<(it_file& f, std::complex<double> x);
00554
00556 it_file& operator<<(it_file& f, const bvec& v);
00558 it_file& operator<<(it_file& f, const svec& v);
00560 it_file& operator<<(it_file& f, const ivec& v);
00562 it_file& operator<<(it_file& f, const vec& v);
00564 it_file& operator<<(it_file& f, const cvec& v);
00565
00567 it_file& operator<<(it_file& f, const std::string& str);
00568
00570 it_file& operator<<(it_file& f, const bmat& m);
00572 it_file& operator<<(it_file& f, const smat& m);
00574 it_file& operator<<(it_file& f, const imat& m);
00576 it_file& operator<<(it_file& f, const mat& m);
00578 it_file& operator<<(it_file& f, const cmat& m);
00579
00581 it_file& operator<<(it_file& f, const Array<bin>& v);
00583 it_file& operator<<(it_file& f, const Array<short>& v);
00585 it_file& operator<<(it_file& f, const Array<int>& v);
00587 it_file& operator<<(it_file& f, const Array<float>& v);
00589 it_file& operator<<(it_file& f, const Array<double>& v);
00591 it_file& operator<<(it_file& f, const Array<std::complex<float> >& v);
00593 it_file& operator<<(it_file& f, const Array<std::complex<double> >& v);
00594
00596 it_file& operator<<(it_file& f, const Array<bvec>& v);
00598 it_file& operator<<(it_file& f, const Array<svec>& v);
00600 it_file& operator<<(it_file& f, const Array<ivec>& v);
00602 it_file& operator<<(it_file& f, const Array<vec>& v);
00604 it_file& operator<<(it_file& f, const Array<cvec>& v);
00605
00607 it_file& operator<<(it_file& f, const Array<std::string>& v);
00608
00610 it_file& operator<<(it_file& f, const Array<bmat>& v);
00612 it_file& operator<<(it_file& f, const Array<smat>& v);
00614 it_file& operator<<(it_file& f, const Array<imat>& v);
00616 it_file& operator<<(it_file& f, const Array<mat>& v);
00618 it_file& operator<<(it_file& f, const Array<cmat>& v);
00619
00621 template <class T>
00622 void it_save_var_as(const T& v, const std::string& name)
00623 {
00624 it_file f(name + ".it");
00625 f << Name(name) << v;
00626 f.close();
00627 }
00628
00630 template <class T>
00631 void it_load_var_as(T& v, const std::string& name)
00632 {
00633 it_ifile f(name + ".it");
00634 f.seek(name);
00635 f >> v;
00636 f.close();
00637 }
00638
00640 #define it_save_var(v) it_save_var_as(v,#v)
00642 #define it_load_var(v) it_load_var_as(v,#v)
00643
00645
00646
00647
00648
00649
00650
00651
00658 class it_file_base_old
00659 {
00660 public:
00661
00663 struct data_header {
00665 char endianity;
00668 uint32_t hdr_bytes, data_bytes, block_bytes;
00670
00671 std::string name;
00673 std::string type;
00674 };
00675
00676 protected:
00677
00679 struct file_header {
00681 char magic[4];
00683 char version;
00684 };
00686 static char file_magic[4];
00688 static char file_version;
00689 };
00690
00697 class it_ifile_old : public it_file_base_old
00698 {
00699 public:
00701 it_ifile_old();
00703 explicit it_ifile_old(const std::string& name);
00705 virtual ~it_ifile_old() { }
00707 void open(const std::string& name);
00709 virtual void close();
00711 bfstream& low_level() { return s; }
00712
00714 bool read_check_file_header();
00716 void read_data_header(data_header& h);
00718 void low_level_read(char& x);
00720 void low_level_read(bin& x);
00722 void low_level_read(short& x);
00724 void low_level_read(int& x);
00726 void low_level_read(float& x);
00728 void low_level_read(double& x);
00730 void low_level_read(std::complex<float>& x);
00732 void low_level_read(std::complex<double>& x);
00734 void low_level_read_lo(vec& v);
00736 void low_level_read_hi(vec& v);
00738 void low_level_read(ivec& v);
00740 void low_level_read(bvec& v);
00742 void low_level_read_lo(cvec& v);
00744 void low_level_read_hi(cvec& v);
00746 void low_level_read(std::string& str);
00748 void low_level_read_lo(mat& m);
00750 void low_level_read_hi(mat& m);
00752 void low_level_read(imat& m);
00754 void low_level_read(bmat& m);
00756 void low_level_read_lo(cmat& m);
00758 void low_level_read_hi(cmat& m);
00759
00761 void low_level_read_lo(Array<float>& v);
00763 void low_level_read_lo(Array<double>& v);
00765 void low_level_read_hi(Array<double>& v);
00767 void low_level_read(Array<int>& v);
00769 void low_level_read(Array<bin>& v);
00771 void low_level_read_lo(Array<std::complex<float> >& v);
00773 void low_level_read_lo(Array<std::complex<double> >& v);
00775 void low_level_read_hi(Array<std::complex<double> >& v);
00776
00778 bool seek(const std::string& name);
00779
00781 bool seek(int n);
00783 void info(std::string& name, std::string& type, int& bytes);
00784
00785 protected:
00787 bfstream s;
00788 };
00789
00796 class it_file_old : public it_ifile_old
00797 {
00798 public:
00800 typedef it_file_old& (*it_manip)(it_file_old&);
00801
00803 it_file_old();
00804
00811 explicit it_file_old(const std::string& name, bool trunc = false);
00812
00814 virtual ~it_file_old() { }
00815
00822 void open(const std::string& name, bool trunc = false);
00823
00825 void close();
00826
00828 void flush();
00829
00831 bfstream& low_level() { return s; }
00832
00834 void set_low_precision(bool p = true) { low_prec = p; }
00835
00837 bool get_low_precision() { return low_prec; }
00838
00840 void set_next_name(const std::string& n) { next_name = n; }
00841
00843 void write_file_header();
00845 void write_data_header(const std::string& type, uint32_t size);
00847 void write_data_header(const std::string& type, const std::string& name,
00848 uint32_t size);
00850 void low_level_write(char x);
00852 void low_level_write(bin x);
00854 void low_level_write(short x);
00856 void low_level_write(int x);
00858 void low_level_write(float x);
00860 void low_level_write(double x);
00862 void low_level_write(const std::complex<float>& x);
00864 void low_level_write(const std::complex<double>& x);
00866 void low_level_write(const vec& v);
00868 void low_level_write(const ivec& v);
00870 void low_level_write(const bvec& v);
00872 void low_level_write(const cvec& v);
00874 void low_level_write(const std::string& str);
00876 void low_level_write(const mat& m);
00878 void low_level_write(const imat& m);
00880 void low_level_write(const bmat& m);
00882 void low_level_write(const cmat& m);
00884 void low_level_write(const Array<float>& v);
00886 void low_level_write(const Array<double>& v);
00888 void low_level_write(const Array<int>& v);
00890 void low_level_write(const Array<bin>& v);
00892 void low_level_write(const Array<std::complex<float> >& v);
00894 void low_level_write(const Array<std::complex<double> >& v);
00895
00897 it_file_old& operator<<(it_manip func) { return (*func)(*this); }
00898
00900 void remove(const std::string& name);
00902 bool exists(const std::string& name);
00904 void pack();
00905
00906 protected:
00908 void remove();
00910 void write_data_header_here(const data_header& h);
00911
00913 bool low_prec;
00915 std::string next_name;
00916 };
00917
00930 inline it_file_old& flush(it_file_old& f)
00931 {
00932 f.flush();
00933 return f;
00934 }
00935
00936
00938
00939
00941 inline it_ifile_old& operator>>(it_ifile_old& f, const Name& s)
00942 {
00943 f.seek(s.name);
00944 return f;
00945 }
00946
00948 inline it_file_old& operator<<(it_file_old& f, const Name& s)
00949 {
00950 f.set_next_name(s.name);
00951 return f;
00952 }
00953
00955 it_ifile_old& operator>>(it_ifile_old& f, char& v);
00956
00958 it_ifile_old& operator>>(it_ifile_old& f, bin& v);
00959
00961 it_ifile_old& operator>>(it_ifile_old& f, short& v);
00962
00964 it_ifile_old& operator>>(it_ifile_old& f, int& v);
00965
00967 it_ifile_old& operator>>(it_ifile_old& f, float& v);
00968
00970 it_ifile_old& operator>>(it_ifile_old& f, double& v);
00971
00973 it_ifile_old& operator>>(it_ifile_old& f, std::complex<float>& v);
00974
00976 it_ifile_old& operator>>(it_ifile_old& f, std::complex<double>& v);
00977
00979 it_ifile_old& operator>>(it_ifile_old& f, vec& v);
00980
00982 it_ifile_old& operator>>(it_ifile_old& f, ivec& v);
00983
00985 it_ifile_old& operator>>(it_ifile_old& f, bvec& v);
00986
00988 it_ifile_old& operator>>(it_ifile_old& f, cvec& v);
00989
00991 it_ifile_old& operator>>(it_ifile_old& f, std::string& str);
00992
00994 it_ifile_old& operator>>(it_ifile_old& f, mat& m);
00995
00997 it_ifile_old& operator>>(it_ifile_old& f, imat& m);
00998
01000 it_ifile_old& operator>>(it_ifile_old& f, bmat& m);
01001
01003 it_ifile_old& operator>>(it_ifile_old& f, cmat& m);
01004
01006 it_ifile_old& operator>>(it_ifile_old& f, Array<float>& v);
01007
01009 it_ifile_old& operator>>(it_ifile_old& f, Array<double>& v);
01010
01012 it_ifile_old& operator>>(it_ifile_old& f, Array<int>& v);
01013
01015 it_ifile_old& operator>>(it_ifile_old& f, Array<bin>& v);
01016
01018 it_ifile_old& operator>>(it_ifile_old& f, Array<std::complex<float> >& v);
01019
01021 it_ifile_old& operator>>(it_ifile_old& f, Array<std::complex<double> >& v);
01022
01024 it_ifile_old& operator>>(it_ifile_old& f, Array<vec>& v);
01025
01027 it_ifile_old& operator>>(it_ifile_old& f, Array<ivec>& v);
01028
01030 it_ifile_old& operator>>(it_ifile_old& f, Array<bvec>& v);
01031
01033 it_ifile_old& operator>>(it_ifile_old& f, Array<cvec>& v);
01034
01036 it_ifile_old& operator>>(it_ifile_old& f, Array<std::string>& v);
01037
01039 it_ifile_old& operator>>(it_ifile_old& f, Array<mat>& v);
01040
01042 it_ifile_old& operator>>(it_ifile_old& f, Array<imat>& v);
01043
01045 it_ifile_old& operator>>(it_ifile_old& f, Array<bmat>& v);
01046
01048 it_ifile_old& operator>>(it_ifile_old& f, Array<cmat>& v);
01049
01050
01052 it_file_old& operator<<(it_file_old& f, char x);
01053
01055 it_file_old& operator<<(it_file_old& f, bin x);
01056
01058 it_file_old& operator<<(it_file_old& f, short x);
01059
01061 it_file_old& operator<<(it_file_old& f, int x);
01062
01064 it_file_old& operator<<(it_file_old& f, float x);
01065
01067 it_file_old& operator<<(it_file_old& f, double x);
01068
01070 it_file_old& operator<<(it_file_old& f, std::complex<float> x);
01071
01073 it_file_old& operator<<(it_file_old& f, std::complex<double> x);
01074
01076 it_file_old& operator<<(it_file_old& f, const vec& v);
01077
01079 it_file_old& operator<<(it_file_old& f, const ivec& v);
01080
01082 it_file_old& operator<<(it_file_old& f, const bvec& v);
01083
01085 it_file_old& operator<<(it_file_old& f, const cvec& v);
01086
01088 it_file_old& operator<<(it_file_old& f, const std::string& str);
01089
01091 it_file_old& operator<<(it_file_old& f, const mat& m);
01092
01094 it_file_old& operator<<(it_file_old& f, const imat& m);
01095
01097 it_file_old& operator<<(it_file_old& f, const bmat& m);
01098
01100 it_file_old& operator<<(it_file_old& f, const cmat& m);
01101
01103 it_file_old& operator<<(it_file_old& f, const Array<float>& v);
01104
01106 it_file_old& operator<<(it_file_old& f, const Array<double>& v);
01107
01109 it_file_old& operator<<(it_file_old& f, const Array<int>& v);
01110
01112 it_file_old& operator<<(it_file_old& f, const Array<bin>& v);
01113
01115 it_file_old& operator<<(it_file_old& f, const Array<std::complex<float> >& v);
01116
01118 it_file_old& operator<<(it_file_old& f, const Array<std::complex<double> >& v);
01119
01121 it_file_old& operator<<(it_file_old& f, const Array<vec>& v);
01122
01124 it_file_old& operator<<(it_file_old& f, const Array<ivec>& v);
01125
01127 it_file_old& operator<<(it_file_old& f, const Array<bvec>& v);
01128
01130 it_file_old& operator<<(it_file_old& f, const Array<cvec>& v);
01131
01133 it_file_old& operator<<(it_file_old& f, const Array<std::string>& v);
01134
01136 it_file_old& operator<<(it_file_old& f, const Array<mat>& v);
01137
01139 it_file_old& operator<<(it_file_old& f, const Array<imat>& v);
01140
01142 it_file_old& operator<<(it_file_old& f, const Array<bmat>& v);
01143
01145 it_file_old& operator<<(it_file_old& f, const Array<cmat>& v);
01146
01148
01149
01150
01151
01152
01153
01154 }
01155
01156 #endif // #ifndef IT_FILE_H
01157