IT++ Logo

itfile.h

Go to the documentation of this file.
00001 
00030 #ifndef ITFILE_H
00031 #define ITFILE_H
00032 
00033 #include <itpp/base/vec.h>
00034 #include <itpp/base/array.h>
00035 #include <itpp/base/binfile.h>
00036 #include <itpp/base/ittypes.h>
00037 
00038 
00039 namespace itpp
00040 {
00041 
00093 class it_file_base
00094 {
00095 public:
00097   struct data_header {
00099     uint64_t hdr_bytes;
00101     uint64_t data_bytes;
00103     uint64_t block_bytes;
00105     std::string name;
00107     std::string type;
00109     std::string desc;
00110   };
00111 
00112 protected:
00114   struct file_header {
00116     char magic[4];
00118     char version;
00119   };
00121   static char file_magic[4];
00123   static char file_version;
00124 };
00125 
00126 
00131 class it_ifile : public it_file_base
00132 {
00133 public:
00135   it_ifile();
00137   explicit it_ifile(const std::string& filename);
00139   virtual ~it_ifile() { }
00141   void open(const std::string& filename);
00143   virtual void close();
00145   bfstream& low_level() { return s; }
00146 
00148   bool read_check_file_header();
00150   void read_data_header(data_header& h);
00151 
00153   void low_level_read(char& x);
00155   void low_level_read(uint64_t& x);
00157   void low_level_read(bool &x);
00158 
00160   void low_level_read(bin& x);
00162   void low_level_read(short& x);
00164   void low_level_read(int& x);
00166   void low_level_read(float& x);
00168   void low_level_read(double& x);
00170   void low_level_read(std::complex<float>& x);
00172   void low_level_read(std::complex<double>& x);
00173 
00175   void low_level_read(bvec& v);
00177   void low_level_read(svec& v);
00179   void low_level_read(ivec& v);
00181   void low_level_read_lo(vec& v);
00183   void low_level_read_hi(vec& v);
00185   void low_level_read_lo(cvec& v);
00187   void low_level_read_hi(cvec& v);
00188 
00190   void low_level_read(std::string& str);
00191 
00193   void low_level_read(bmat& m);
00195   void low_level_read(smat& m);
00197   void low_level_read(imat& m);
00199   void low_level_read_lo(mat& m);
00201   void low_level_read_hi(mat& m);
00203   void low_level_read_lo(cmat& m);
00205   void low_level_read_hi(cmat& m);
00206 
00208   void low_level_read(Array<bin>& v);
00210   void low_level_read(Array<short>& v);
00212   void low_level_read(Array<int>& v);
00214   void low_level_read(Array<float>& v);
00216   void low_level_read_lo(Array<double>& v);
00218   void low_level_read_hi(Array<double>& v);
00220   void low_level_read(Array<std::complex<float> >& v);
00222   void low_level_read_lo(Array<std::complex<double> >& v);
00224   void low_level_read_hi(Array<std::complex<double> >& v);
00225 
00227   bool seek(const std::string& name);
00229   bool seek(int n);
00231   void info(std::string& name, std::string& type, std::string& desc,
00232             uint64_t& bytes);
00233 
00234 protected:
00236   bfstream s;
00237 };
00238 
00239 
00244 class it_file : public it_ifile
00245 {
00246 public:
00248   typedef it_file& (*it_manip)(it_file&);
00249 
00251   it_file();
00252 
00259   explicit it_file(const std::string& filename, bool trunc = false);
00260 
00262   virtual ~it_file() { }
00263 
00270   void open(const std::string& filename, bool trunc = false);
00271 
00273   void close();
00275   void flush();
00276 
00278   bfstream& low_level() { return s; }
00279 
00281   void set_low_precision(bool p = true)  { low_prec = p; }
00283   bool get_low_precision() const { return low_prec; }
00284 
00286   void set_next_name(const std::string& name,
00287                      const std::string& description = "")
00288   { next_name = name; next_desc = description; }
00289 
00291   void write_file_header();
00293   void write_data_header(const std::string& type, uint64_t size);
00295   void write_data_header(const std::string& type, const std::string& name,
00296                          uint64_t size, const std::string& description = "");
00297 
00299   void low_level_write(char x);
00301   void low_level_write(uint64_t x);
00303   void low_level_write(bool x);
00304 
00306   void low_level_write(bin x);
00308   void low_level_write(short x);
00310   void low_level_write(int x);
00312   void low_level_write(float x);
00314   void low_level_write(double x);
00316   void low_level_write(const std::complex<float>& x);
00318   void low_level_write(const std::complex<double>& x);
00319 
00321   void low_level_write(const bvec& v);
00323   void low_level_write(const svec& v);
00325   void low_level_write(const ivec& v);
00327   void low_level_write(const vec& v);
00329   void low_level_write(const cvec& v);
00330 
00332   void low_level_write(const std::string& str);
00333 
00335   void low_level_write(const bmat& m);
00337   void low_level_write(const smat& m);
00339   void low_level_write(const imat& m);
00341   void low_level_write(const mat& m);
00343   void low_level_write(const cmat& m);
00344 
00346   void low_level_write(const Array<bin>& v);
00348   void low_level_write(const Array<short>& v);
00350   void low_level_write(const Array<int>& v);
00352   void low_level_write(const Array<float>& v);
00354   void low_level_write(const Array<double>& v);
00356   void low_level_write(const Array<std::complex<float> >& v);
00358   void low_level_write(const Array<std::complex<double> >& v);
00359 
00361   it_file& operator<<(it_manip func) { return (*func)(*this); }
00362 
00364   void remove(const std::string& name);
00366   bool exists(const std::string& name);
00368   void pack();
00369 
00370 protected:
00372   void remove();
00374   void write_data_header_here(const data_header& h);
00375 
00377   bool low_prec;
00379   std::string next_name;
00381   std::string next_desc;
00382 
00383 private:
00384   // Name of the opened file. Needed by the pack() method.
00385   std::string fname;
00386 };
00387 
00388 
00400 inline it_file& flush(it_file& f)
00401 {
00402   f.flush();
00403   return f;
00404 }
00405 
00419 class Name
00420 {
00421 public:
00423   Name(const std::string& n, const std::string& d = ""): name(n), desc(d) {}
00425   Name &operator=(const Name&) { return *this; }
00427   const std::string& name;
00429   const std::string& desc;
00430 };
00431 
00432 
00434 
00435 
00437 inline it_ifile& operator>>(it_ifile& f, const Name& s)
00438 {
00439   f.seek(s.name);
00440   return f;
00441 }
00442 
00444 inline it_file& operator<<(it_file& f, const Name& s)
00445 {
00446   f.set_next_name(s.name, s.desc);
00447   return f;
00448 }
00449 
00451 it_ifile& operator>>(it_ifile& f, char& v);
00453 it_ifile& operator>>(it_ifile &f, bool &v);
00454 
00456 it_ifile& operator>>(it_ifile& f, bin& v);
00458 it_ifile& operator>>(it_ifile& f, short& v);
00460 it_ifile& operator>>(it_ifile& f, int& v);
00462 it_ifile& operator>>(it_ifile& f, float& v);
00464 it_ifile& operator>>(it_ifile& f, double& v);
00466 it_ifile& operator>>(it_ifile& f, std::complex<float>& v);
00468 it_ifile& operator>>(it_ifile& f, std::complex<double>& v);
00469 
00471 it_ifile& operator>>(it_ifile& f, bvec& v);
00473 it_ifile& operator>>(it_ifile& f, svec& v);
00475 it_ifile& operator>>(it_ifile& f, ivec& v);
00477 it_ifile& operator>>(it_ifile& f, vec& v);
00479 it_ifile& operator>>(it_ifile& f, cvec& v);
00480 
00482 it_ifile& operator>>(it_ifile& f, std::string& str);
00483 
00485 it_ifile& operator>>(it_ifile& f, bmat& m);
00487 it_ifile& operator>>(it_ifile& f, smat& m);
00489 it_ifile& operator>>(it_ifile& f, imat& m);
00491 it_ifile& operator>>(it_ifile& f, mat& m);
00493 it_ifile& operator>>(it_ifile& f, cmat& m);
00494 
00496 it_ifile& operator>>(it_ifile& f, Array<bin>& v);
00498 it_ifile& operator>>(it_ifile& f, Array<short>& v);
00500 it_ifile& operator>>(it_ifile& f, Array<int>& v);
00502 it_ifile& operator>>(it_ifile& f, Array<float>& v);
00504 it_ifile& operator>>(it_ifile& f, Array<double>& v);
00506 it_ifile& operator>>(it_ifile& f, Array<std::complex<float> >& v);
00508 it_ifile& operator>>(it_ifile& f, Array<std::complex<double> >& v);
00509 
00511 it_ifile& operator>>(it_ifile& f, Array<bvec>& v);
00513 it_ifile& operator>>(it_ifile& f, Array<svec>& v);
00515 it_ifile& operator>>(it_ifile& f, Array<ivec>& v);
00517 it_ifile& operator>>(it_ifile& f, Array<vec>& v);
00519 it_ifile& operator>>(it_ifile& f, Array<cvec>& v);
00520 
00522 it_ifile& operator>>(it_ifile& f, Array<std::string>& v);
00523 
00525 it_ifile& operator>>(it_ifile& f, Array<bmat>& v);
00527 it_ifile& operator>>(it_ifile& f, Array<smat>& v);
00529 it_ifile& operator>>(it_ifile& f, Array<imat>& v);
00531 it_ifile& operator>>(it_ifile& f, Array<mat>& v);
00533 it_ifile& operator>>(it_ifile& f, Array<cmat>& v);
00534 
00535 
00537 it_file& operator<<(it_file& f, char x);
00539 it_file& operator<<(it_file &f, bool x);
00540 
00542 it_file& operator<<(it_file& f, bin x);
00544 it_file& operator<<(it_file& f, short x);
00546 it_file& operator<<(it_file& f, int x);
00548 it_file& operator<<(it_file& f, float x);
00550 it_file& operator<<(it_file& f, double x);
00552 it_file& operator<<(it_file& f, std::complex<float> x);
00554 it_file& operator<<(it_file& f, std::complex<double> x);
00555 
00557 it_file& operator<<(it_file& f, const bvec& v);
00559 it_file& operator<<(it_file& f, const svec& v);
00561 it_file& operator<<(it_file& f, const ivec& v);
00563 it_file& operator<<(it_file& f, const vec& v);
00565 it_file& operator<<(it_file& f, const cvec& v);
00566 
00568 it_file& operator<<(it_file& f, const std::string& str);
00569 
00571 it_file& operator<<(it_file& f, const bmat& m);
00573 it_file& operator<<(it_file& f, const smat& m);
00575 it_file& operator<<(it_file& f, const imat& m);
00577 it_file& operator<<(it_file& f, const mat& m);
00579 it_file& operator<<(it_file& f, const cmat& m);
00580 
00582 it_file& operator<<(it_file& f, const Array<bin>& v);
00584 it_file& operator<<(it_file& f, const Array<short>& v);
00586 it_file& operator<<(it_file& f, const Array<int>& v);
00588 it_file& operator<<(it_file& f, const Array<float>& v);
00590 it_file& operator<<(it_file& f, const Array<double>& v);
00592 it_file& operator<<(it_file& f, const Array<std::complex<float> >& v);
00594 it_file& operator<<(it_file& f, const Array<std::complex<double> >& v);
00595 
00597 it_file& operator<<(it_file& f, const Array<bvec>& v);
00599 it_file& operator<<(it_file& f, const Array<svec>& v);
00601 it_file& operator<<(it_file& f, const Array<ivec>& v);
00603 it_file& operator<<(it_file& f, const Array<vec>& v);
00605 it_file& operator<<(it_file& f, const Array<cvec>& v);
00606 
00608 it_file& operator<<(it_file& f, const Array<std::string>& v);
00609 
00611 it_file& operator<<(it_file& f, const Array<bmat>& v);
00613 it_file& operator<<(it_file& f, const Array<smat>& v);
00615 it_file& operator<<(it_file& f, const Array<imat>& v);
00617 it_file& operator<<(it_file& f, const Array<mat>& v);
00619 it_file& operator<<(it_file& f, const Array<cmat>& v);
00620 
00622 template <class T>
00623 void it_save_var_as(const T& v, const std::string& name)
00624 {
00625   it_file f(name + ".it");
00626   f << Name(name) << v;
00627   f.close();
00628 }
00629 
00631 template <class T>
00632 void it_load_var_as(T& v, const std::string& name)
00633 {
00634   it_ifile f(name + ".it");
00635   f.seek(name);
00636   f >> v;
00637   f.close();
00638 }
00639 
00641 #define it_save_var(v) it_save_var_as(v,#v)
00643 #define it_load_var(v) it_load_var_as(v,#v)
00644 
00646 
00647 
00648 // ----------------------------------------------------------------------
00649 // Deprecated implementation of IT++ file format version 2
00650 // Will be removed in future versions
00651 // ----------------------------------------------------------------------
00652 
00659 class it_file_base_old
00660 {
00661 public:
00662 
00664   struct data_header {
00666     char endianity;
00669     uint32_t hdr_bytes, data_bytes, block_bytes;
00671 
00672     std::string name;
00674     std::string type;
00675   };
00676 
00677 protected:
00678 
00680   struct file_header {
00682     char magic[4];
00684     char version;
00685   };
00687   static char file_magic[4];
00689   static char file_version;
00690 };
00691 
00698 class it_ifile_old : public it_file_base_old
00699 {
00700 public:
00702   it_ifile_old();
00704   explicit it_ifile_old(const std::string& name);
00706   virtual ~it_ifile_old() { }
00708   void open(const std::string& name);
00710   virtual void close();
00712   bfstream& low_level() { return s; }
00713 
00715   bool read_check_file_header();
00717   void read_data_header(data_header& h);
00719   void low_level_read(char& x);
00721   void low_level_read(bin& x);
00723   void low_level_read(short& x);
00725   void low_level_read(int& x);
00727   void low_level_read(float& x);
00729   void low_level_read(double& x);
00731   void low_level_read(std::complex<float>& x);
00733   void low_level_read(std::complex<double>& x);
00735   void low_level_read_lo(vec& v);
00737   void low_level_read_hi(vec& v);
00739   void low_level_read(ivec& v);
00741   void low_level_read(bvec& v);
00743   void low_level_read_lo(cvec& v);
00745   void low_level_read_hi(cvec& v);
00747   void low_level_read(std::string& str);
00749   void low_level_read_lo(mat& m);
00751   void low_level_read_hi(mat& m);
00753   void low_level_read(imat& m);
00755   void low_level_read(bmat& m);
00757   void low_level_read_lo(cmat& m);
00759   void low_level_read_hi(cmat& m);
00760 
00762   void low_level_read_lo(Array<float>& v);
00764   void low_level_read_lo(Array<double>& v);
00766   void low_level_read_hi(Array<double>& v);
00768   void low_level_read(Array<int>& v);
00770   void low_level_read(Array<bin>& v);
00772   void low_level_read_lo(Array<std::complex<float> >& v);
00774   void low_level_read_lo(Array<std::complex<double> >& v);
00776   void low_level_read_hi(Array<std::complex<double> >& v);
00777 
00779   bool seek(const std::string& name);
00780 
00782   bool seek(int n);
00784   void info(std::string& name, std::string& type, int& bytes);
00785 
00786 protected:
00788   bfstream s;
00789 };
00790 
00797 class it_file_old : public it_ifile_old
00798 {
00799 public:
00801   typedef it_file_old& (*it_manip)(it_file_old&);
00802 
00804   it_file_old();
00805 
00812   explicit it_file_old(const std::string& name, bool trunc = false);
00813 
00815   virtual ~it_file_old() { }
00816 
00823   void open(const std::string& name, bool trunc = false);
00824 
00826   void close();
00827 
00829   void flush();
00830 
00832   bfstream& low_level() { return s; }
00833 
00835   void set_low_precision(bool p = true)  { low_prec = p; }
00836 
00838   bool get_low_precision() { return low_prec; }
00839 
00841   void set_next_name(const std::string& n) { next_name = n; }
00842 
00844   void write_file_header();
00846   void write_data_header(const std::string& type, uint32_t size);
00848   void write_data_header(const std::string& type, const std::string& name,
00849                          uint32_t size);
00851   void low_level_write(char x);
00853   void low_level_write(bin x);
00855   void low_level_write(short x);
00857   void low_level_write(int x);
00859   void low_level_write(float x);
00861   void low_level_write(double x);
00863   void low_level_write(const std::complex<float>& x);
00865   void low_level_write(const std::complex<double>& x);
00867   void low_level_write(const vec& v);
00869   void low_level_write(const ivec& v);
00871   void low_level_write(const bvec& v);
00873   void low_level_write(const cvec& v);
00875   void low_level_write(const std::string& str);
00877   void low_level_write(const mat& m);
00879   void low_level_write(const imat& m);
00881   void low_level_write(const bmat& m);
00883   void low_level_write(const cmat& m);
00885   void low_level_write(const Array<float>& v);
00887   void low_level_write(const Array<double>& v);
00889   void low_level_write(const Array<int>& v);
00891   void low_level_write(const Array<bin>& v);
00893   void low_level_write(const Array<std::complex<float> >& v);
00895   void low_level_write(const Array<std::complex<double> >& v);
00896 
00898   it_file_old& operator<<(it_manip func) { return (*func)(*this); }
00899 
00901   void remove(const std::string& name);
00903   bool exists(const std::string& name);
00905   void pack();
00906 
00907 protected:
00909   void remove();
00911   void write_data_header_here(const data_header& h);
00912 
00914   bool low_prec;
00916   std::string next_name;
00917 };
00918 
00931 inline it_file_old& flush(it_file_old& f)
00932 {
00933   f.flush();
00934   return f;
00935 }
00936 
00937 
00939 
00940 
00942 inline it_ifile_old& operator>>(it_ifile_old& f, const Name& s)
00943 {
00944   f.seek(s.name);
00945   return f;
00946 }
00947 
00949 inline it_file_old& operator<<(it_file_old& f, const Name& s)
00950 {
00951   f.set_next_name(s.name);
00952   return f;
00953 }
00954 
00956 it_ifile_old& operator>>(it_ifile_old& f, char& v);
00957 
00959 it_ifile_old& operator>>(it_ifile_old& f, bin& v);
00960 
00962 it_ifile_old& operator>>(it_ifile_old& f, short& v);
00963 
00965 it_ifile_old& operator>>(it_ifile_old& f, int& v);
00966 
00968 it_ifile_old& operator>>(it_ifile_old& f, float& v);
00969 
00971 it_ifile_old& operator>>(it_ifile_old& f, double& v);
00972 
00974 it_ifile_old& operator>>(it_ifile_old& f, std::complex<float>& v);
00975 
00977 it_ifile_old& operator>>(it_ifile_old& f, std::complex<double>& v);
00978 
00980 it_ifile_old& operator>>(it_ifile_old& f, vec& v);
00981 
00983 it_ifile_old& operator>>(it_ifile_old& f, ivec& v);
00984 
00986 it_ifile_old& operator>>(it_ifile_old& f, bvec& v);
00987 
00989 it_ifile_old& operator>>(it_ifile_old& f, cvec& v);
00990 
00992 it_ifile_old& operator>>(it_ifile_old& f, std::string& str);
00993 
00995 it_ifile_old& operator>>(it_ifile_old& f, mat& m);
00996 
00998 it_ifile_old& operator>>(it_ifile_old& f, imat& m);
00999 
01001 it_ifile_old& operator>>(it_ifile_old& f, bmat& m);
01002 
01004 it_ifile_old& operator>>(it_ifile_old& f, cmat& m);
01005 
01007 it_ifile_old& operator>>(it_ifile_old& f, Array<float>& v);
01008 
01010 it_ifile_old& operator>>(it_ifile_old& f, Array<double>& v);
01011 
01013 it_ifile_old& operator>>(it_ifile_old& f, Array<int>& v);
01014 
01016 it_ifile_old& operator>>(it_ifile_old& f, Array<bin>& v);
01017 
01019 it_ifile_old& operator>>(it_ifile_old& f, Array<std::complex<float> >& v);
01020 
01022 it_ifile_old& operator>>(it_ifile_old& f, Array<std::complex<double> >& v);
01023 
01025 it_ifile_old& operator>>(it_ifile_old& f, Array<vec>& v);
01026 
01028 it_ifile_old& operator>>(it_ifile_old& f, Array<ivec>& v);
01029 
01031 it_ifile_old& operator>>(it_ifile_old& f, Array<bvec>& v);
01032 
01034 it_ifile_old& operator>>(it_ifile_old& f, Array<cvec>& v);
01035 
01037 it_ifile_old& operator>>(it_ifile_old& f, Array<std::string>& v);
01038 
01040 it_ifile_old& operator>>(it_ifile_old& f, Array<mat>& v);
01041 
01043 it_ifile_old& operator>>(it_ifile_old& f, Array<imat>& v);
01044 
01046 it_ifile_old& operator>>(it_ifile_old& f, Array<bmat>& v);
01047 
01049 it_ifile_old& operator>>(it_ifile_old& f, Array<cmat>& v);
01050 
01051 
01053 it_file_old& operator<<(it_file_old& f, char x);
01054 
01056 it_file_old& operator<<(it_file_old& f, bin x);
01057 
01059 it_file_old& operator<<(it_file_old& f, short x);
01060 
01062 it_file_old& operator<<(it_file_old& f, int x);
01063 
01065 it_file_old& operator<<(it_file_old& f, float x);
01066 
01068 it_file_old& operator<<(it_file_old& f, double x);
01069 
01071 it_file_old& operator<<(it_file_old& f, std::complex<float> x);
01072 
01074 it_file_old& operator<<(it_file_old& f, std::complex<double> x);
01075 
01077 it_file_old& operator<<(it_file_old& f, const vec& v);
01078 
01080 it_file_old& operator<<(it_file_old& f, const ivec& v);
01081 
01083 it_file_old& operator<<(it_file_old& f, const bvec& v);
01084 
01086 it_file_old& operator<<(it_file_old& f, const cvec& v);
01087 
01089 it_file_old& operator<<(it_file_old& f, const std::string& str);
01090 
01092 it_file_old& operator<<(it_file_old& f, const mat& m);
01093 
01095 it_file_old& operator<<(it_file_old& f, const imat& m);
01096 
01098 it_file_old& operator<<(it_file_old& f, const bmat& m);
01099 
01101 it_file_old& operator<<(it_file_old& f, const cmat& m);
01102 
01104 it_file_old& operator<<(it_file_old& f, const Array<float>& v);
01105 
01107 it_file_old& operator<<(it_file_old& f, const Array<double>& v);
01108 
01110 it_file_old& operator<<(it_file_old& f, const Array<int>& v);
01111 
01113 it_file_old& operator<<(it_file_old& f, const Array<bin>& v);
01114 
01116 it_file_old& operator<<(it_file_old& f, const Array<std::complex<float> >& v);
01117 
01119 it_file_old& operator<<(it_file_old& f, const Array<std::complex<double> >& v);
01120 
01122 it_file_old& operator<<(it_file_old& f, const Array<vec>& v);
01123 
01125 it_file_old& operator<<(it_file_old& f, const Array<ivec>& v);
01126 
01128 it_file_old& operator<<(it_file_old& f, const Array<bvec>& v);
01129 
01131 it_file_old& operator<<(it_file_old& f, const Array<cvec>& v);
01132 
01134 it_file_old& operator<<(it_file_old& f, const Array<std::string>& v);
01135 
01137 it_file_old& operator<<(it_file_old& f, const Array<mat>& v);
01138 
01140 it_file_old& operator<<(it_file_old& f, const Array<imat>& v);
01141 
01143 it_file_old& operator<<(it_file_old& f, const Array<bmat>& v);
01144 
01146 it_file_old& operator<<(it_file_old& f, const Array<cmat>& v);
01147 
01149 
01150 // ----------------------------------------------------------------------
01151 // End of the deprecated implementation of IT++ file format version 2
01152 // Will be removed in future versions
01153 // ----------------------------------------------------------------------
01154 
01155 } // namespace itpp
01156 
01157 #endif // #ifndef IT_FILE_H
01158 
SourceForge Logo

Generated on Sun Jul 26 08:36:48 2009 for IT++ by Doxygen 1.5.9