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 
00092   class it_file_base {
00093   public:
00095     struct data_header {
00097       uint64_t hdr_bytes;
00099       uint64_t data_bytes;
00101       uint64_t block_bytes;
00103       std::string name;
00105       std::string type;
00107       std::string desc;
00108     };
00109 
00110   protected:
00112     struct file_header {
00114       char magic[4];
00116       char version;
00117     };
00119     static char file_magic[4];
00121     static char file_version;
00122   };
00123 
00124 
00129   class it_ifile : public it_file_base {
00130   public:
00132     it_ifile();
00134     explicit it_ifile(const std::string& filename);
00136     virtual ~it_ifile() { }
00138     void open(const std::string& filename);
00140     virtual void close();
00142     bfstream& low_level() { return s; }
00143 
00145     bool read_check_file_header();
00147     void read_data_header(data_header& h);
00148 
00150     void low_level_read(char& x);
00152     void low_level_read(uint64_t& x);
00154     void low_level_read(bool &x);
00155 
00157     void low_level_read(bin& x);
00159     void low_level_read(short& x);
00161     void low_level_read(int& x);
00163     void low_level_read(float& x);
00165     void low_level_read(double& x);
00167     void low_level_read(std::complex<float>& x);
00169     void low_level_read(std::complex<double>& x);
00170 
00172     void low_level_read(bvec& v);
00174     void low_level_read(svec& v);
00176     void low_level_read(ivec& v);
00178     void low_level_read_lo(vec& v);
00180     void low_level_read_hi(vec& v);
00182     void low_level_read_lo(cvec& v);
00184     void low_level_read_hi(cvec& v);
00185 
00187     void low_level_read(std::string& str);
00188 
00190     void low_level_read(bmat& m);
00192     void low_level_read(smat& m);
00194     void low_level_read(imat& m);
00196     void low_level_read_lo(mat& m);
00198     void low_level_read_hi(mat& m);
00200     void low_level_read_lo(cmat& m);
00202     void low_level_read_hi(cmat& m);
00203 
00205     void low_level_read(Array<bin>& v);
00207     void low_level_read(Array<short>& v);
00209     void low_level_read(Array<int>& v);
00211     void low_level_read(Array<float>& v);
00213     void low_level_read_lo(Array<double>& v);
00215     void low_level_read_hi(Array<double>& v);
00217     void low_level_read(Array<std::complex<float> >& v);
00219     void low_level_read_lo(Array<std::complex<double> >& v);
00221     void low_level_read_hi(Array<std::complex<double> >& v);
00222 
00224     bool seek(const std::string& name);
00226     bool seek(int n);
00228     void info(std::string& name, std::string& type, std::string& desc,
00229               uint64_t& bytes);
00230 
00231   protected:
00233     bfstream s;
00234   };
00235 
00236 
00241   class it_file : public it_ifile {
00242   public:
00244     typedef it_file& (*it_manip)(it_file&);
00245 
00247     it_file();
00248 
00255     explicit it_file(const std::string& filename, bool trunc = false);
00256 
00258     virtual ~it_file() { }
00259 
00266     void open(const std::string& filename, bool trunc = false);
00267 
00269     void close();
00271     void flush();
00272 
00274     bfstream& low_level() { return s; }
00275 
00277     void set_low_precision(bool p = true)  { low_prec = p; }
00279     bool get_low_precision() const { return low_prec; }
00280 
00282     void set_next_name(const std::string& name,
00283                        const std::string& description = "")
00284     { next_name = name; next_desc = description; }
00285 
00287     void write_file_header();
00289     void write_data_header(const std::string& type, uint64_t size);
00291     void write_data_header(const std::string& type, const std::string& name,
00292                            uint64_t size, const std::string& description = "");
00293 
00295     void low_level_write(char x);
00297     void low_level_write(uint64_t x);
00299     void low_level_write(bool x);
00300 
00302     void low_level_write(bin x);
00304     void low_level_write(short x);
00306     void low_level_write(int x);
00308     void low_level_write(float x);
00310     void low_level_write(double x);
00312     void low_level_write(const std::complex<float>& x);
00314     void low_level_write(const std::complex<double>& x);
00315 
00317     void low_level_write(const bvec& v);
00319     void low_level_write(const svec& v);
00321     void low_level_write(const ivec& v);
00323     void low_level_write(const vec& v);
00325     void low_level_write(const cvec& v);
00326 
00328     void low_level_write(const std::string& str);
00329 
00331     void low_level_write(const bmat& m);
00333     void low_level_write(const smat& m);
00335     void low_level_write(const imat& m);
00337     void low_level_write(const mat& m);
00339     void low_level_write(const cmat& m);
00340 
00342     void low_level_write(const Array<bin>& v);
00344     void low_level_write(const Array<short>& v);
00346     void low_level_write(const Array<int>& v);
00348     void low_level_write(const Array<float>& v);
00350     void low_level_write(const Array<double>& v);
00352     void low_level_write(const Array<std::complex<float> >& v);
00354     void low_level_write(const Array<std::complex<double> >& v);
00355 
00357     it_file& operator<<(it_manip func) { return (*func)(*this); }
00358 
00360     void remove(const std::string& name);
00362     bool exists(const std::string& name);
00364     void pack();
00365 
00366   protected:
00368     void remove();
00370     void write_data_header_here(const data_header& h);
00371 
00373     bool low_prec;
00375     std::string next_name;
00377     std::string next_desc;
00378 
00379   private:
00380     // Name of the opened file. Needed by the pack() method.
00381     std::string fname;
00382   };
00383 
00384 
00396   inline it_file& flush(it_file& f)
00397   {
00398     f.flush();
00399     return f;
00400   }
00401 
00415   class Name {
00416   public:
00418     Name(const std::string& n, const std::string& d = ""): name(n), desc(d) {}
00420     const std::string& name;
00422     const std::string& desc;
00423   };
00424 
00425 
00427 
00428 
00430   inline it_ifile& operator>>(it_ifile& f, const Name& s)
00431   {
00432     f.seek(s.name);
00433     return f;
00434   }
00435 
00437   inline it_file& operator<<(it_file& f, const Name& s)
00438   {
00439     f.set_next_name(s.name, s.desc);
00440     return f;
00441   }
00442 
00444   it_ifile& operator>>(it_ifile& f, char& v);
00446   it_ifile& operator>>(it_ifile &f, bool &v);
00447 
00449   it_ifile& operator>>(it_ifile& f, bin& v);
00451   it_ifile& operator>>(it_ifile& f, short& v);
00453   it_ifile& operator>>(it_ifile& f, int& v);
00455   it_ifile& operator>>(it_ifile& f, float& v);
00457   it_ifile& operator>>(it_ifile& f, double& v);
00459   it_ifile& operator>>(it_ifile& f, std::complex<float>& v);
00461   it_ifile& operator>>(it_ifile& f, std::complex<double>& v);
00462 
00464   it_ifile& operator>>(it_ifile& f, bvec& v);
00466   it_ifile& operator>>(it_ifile& f, svec& v);
00468   it_ifile& operator>>(it_ifile& f, ivec& v);
00470   it_ifile& operator>>(it_ifile& f, vec& v);
00472   it_ifile& operator>>(it_ifile& f, cvec& v);
00473 
00475   it_ifile& operator>>(it_ifile& f, std::string& str);
00476 
00478   it_ifile& operator>>(it_ifile& f, bmat& m);
00480   it_ifile& operator>>(it_ifile& f, smat& m);
00482   it_ifile& operator>>(it_ifile& f, imat& m);
00484   it_ifile& operator>>(it_ifile& f, mat& m);
00486   it_ifile& operator>>(it_ifile& f, cmat& m);
00487 
00489   it_ifile& operator>>(it_ifile& f, Array<bin>& v);
00491   it_ifile& operator>>(it_ifile& f, Array<short>& v);
00493   it_ifile& operator>>(it_ifile& f, Array<int>& v);
00495   it_ifile& operator>>(it_ifile& f, Array<float>& v);
00497   it_ifile& operator>>(it_ifile& f, Array<double>& v);
00499   it_ifile& operator>>(it_ifile& f, Array<std::complex<float> >& v);
00501   it_ifile& operator>>(it_ifile& f, Array<std::complex<double> >& v);
00502 
00504   it_ifile& operator>>(it_ifile& f, Array<bvec>& v);
00506   it_ifile& operator>>(it_ifile& f, Array<svec>& v);
00508   it_ifile& operator>>(it_ifile& f, Array<ivec>& v);
00510   it_ifile& operator>>(it_ifile& f, Array<vec>& v);
00512   it_ifile& operator>>(it_ifile& f, Array<cvec>& v);
00513 
00515   it_ifile& operator>>(it_ifile& f, Array<std::string>& v);
00516 
00518   it_ifile& operator>>(it_ifile& f, Array<bmat>& v);
00520   it_ifile& operator>>(it_ifile& f, Array<smat>& v);
00522   it_ifile& operator>>(it_ifile& f, Array<imat>& v);
00524   it_ifile& operator>>(it_ifile& f, Array<mat>& v);
00526   it_ifile& operator>>(it_ifile& f, Array<cmat>& v);
00527 
00528 
00530   it_file& operator<<(it_file& f, char x);
00532   it_file& operator<<(it_file &f, bool x);
00533 
00535   it_file& operator<<(it_file& f, bin x);
00537   it_file& operator<<(it_file& f, short x);
00539   it_file& operator<<(it_file& f, int x);
00541   it_file& operator<<(it_file& f, float x);
00543   it_file& operator<<(it_file& f, double x);
00545   it_file& operator<<(it_file& f, std::complex<float> x);
00547   it_file& operator<<(it_file& f, std::complex<double> x);
00548 
00550   it_file& operator<<(it_file& f, const bvec& v);
00552   it_file& operator<<(it_file& f, const svec& v);
00554   it_file& operator<<(it_file& f, const ivec& v);
00556   it_file& operator<<(it_file& f, const vec& v);
00558   it_file& operator<<(it_file& f, const cvec& v);
00559 
00561   it_file& operator<<(it_file& f, const std::string& str);
00562 
00564   it_file& operator<<(it_file& f, const bmat& m);
00566   it_file& operator<<(it_file& f, const smat& m);
00568   it_file& operator<<(it_file& f, const imat& m);
00570   it_file& operator<<(it_file& f, const mat& m);
00572   it_file& operator<<(it_file& f, const cmat& m);
00573 
00575   it_file& operator<<(it_file& f, const Array<bin>& v);
00577   it_file& operator<<(it_file& f, const Array<short>& v);
00579   it_file& operator<<(it_file& f, const Array<int>& v);
00581   it_file& operator<<(it_file& f, const Array<float>& v);
00583   it_file& operator<<(it_file& f, const Array<double>& v);
00585   it_file& operator<<(it_file& f, const Array<std::complex<float> >& v);
00587   it_file& operator<<(it_file& f, const Array<std::complex<double> >& v);
00588 
00590   it_file& operator<<(it_file& f, const Array<bvec>& v);
00592   it_file& operator<<(it_file& f, const Array<svec>& v);
00594   it_file& operator<<(it_file& f, const Array<ivec>& v);
00596   it_file& operator<<(it_file& f, const Array<vec>& v);
00598   it_file& operator<<(it_file& f, const Array<cvec>& v);
00599 
00601   it_file& operator<<(it_file& f, const Array<std::string>& v);
00602 
00604   it_file& operator<<(it_file& f, const Array<bmat>& v);
00606   it_file& operator<<(it_file& f, const Array<smat>& v);
00608   it_file& operator<<(it_file& f, const Array<imat>& v);
00610   it_file& operator<<(it_file& f, const Array<mat>& v);
00612   it_file& operator<<(it_file& f, const Array<cmat>& v);
00613 
00615   template <class T>
00616   void it_save_var_as(const T& v, const std::string& name)
00617   {
00618     it_file f(name + ".it");
00619     f << Name(name) << v;
00620     f.close();
00621   }
00622 
00624   template <class T>
00625   void it_load_var_as(T& v, const std::string& name)
00626   {
00627     it_ifile f(name + ".it");
00628     f.seek(name);
00629     f >> v;
00630     f.close();
00631   }
00632 
00634 #define it_save_var(v) it_save_var_as(v,#v)
00636 #define it_load_var(v) it_load_var_as(v,#v)
00637 
00639 
00640 
00641   // ----------------------------------------------------------------------
00642   // Deprecated implementation of IT++ file format version 2
00643   // Will be removed in future versions
00644   // ----------------------------------------------------------------------
00645 
00652   class it_file_base_old {
00653   public:
00654 
00656     struct data_header {
00658       char endianity;
00661       uint32_t hdr_bytes, data_bytes, block_bytes;
00663 
00664       std::string name;
00666       std::string type;
00667     };
00668 
00669   protected:
00670 
00672     struct file_header {
00674       char magic[4];
00676       char version;
00677     };
00679     static char file_magic[4];
00681     static char file_version;
00682   };
00683 
00690   class it_ifile_old : public it_file_base_old {
00691   public:
00693     it_ifile_old();
00695     explicit it_ifile_old(const std::string& name);
00697     virtual ~it_ifile_old() { }
00699     void open(const std::string& name);
00701     virtual void close();
00703     bfstream& low_level() { return s; }
00704 
00706     bool read_check_file_header();
00708     void read_data_header(data_header& h);
00710     void low_level_read(char& x);
00712     void low_level_read(bin& x);
00714     void low_level_read(short& x);
00716     void low_level_read(int& x);
00718     void low_level_read(float& x);
00720     void low_level_read(double& x);
00722     void low_level_read(std::complex<float>& x);
00724     void low_level_read(std::complex<double>& x);
00726     void low_level_read_lo(vec& v);
00728     void low_level_read_hi(vec& v);
00730     void low_level_read(ivec& v);
00732     void low_level_read(bvec& v);
00734     void low_level_read_lo(cvec& v);
00736     void low_level_read_hi(cvec& v);
00738     void low_level_read(std::string& str);
00740     void low_level_read_lo(mat& m);
00742     void low_level_read_hi(mat& m);
00744     void low_level_read(imat& m);
00746     void low_level_read(bmat& m);
00748     void low_level_read_lo(cmat& m);
00750     void low_level_read_hi(cmat& m);
00751 
00753     void low_level_read_lo(Array<float>& v);
00755     void low_level_read_lo(Array<double>& v);
00757     void low_level_read_hi(Array<double>& v);
00759     void low_level_read(Array<int>& v);
00761     void low_level_read(Array<bin>& v);
00763     void low_level_read_lo(Array<std::complex<float> >& v);
00765     void low_level_read_lo(Array<std::complex<double> >& v);
00767     void low_level_read_hi(Array<std::complex<double> >& v);
00768 
00770     bool seek(const std::string& name);
00771 
00773     bool seek(int n);
00775     void info(std::string& name, std::string& type, int& bytes);
00776 
00777   protected:
00779     bfstream s;
00780   };
00781 
00788   class it_file_old : public it_ifile_old {
00789   public:
00791     typedef it_file_old& (*it_manip)(it_file_old&);
00792 
00794     it_file_old();
00795 
00802     explicit it_file_old(const std::string& name, bool trunc=false);
00803 
00805     virtual ~it_file_old() { }
00806 
00813     void open(const std::string& name, bool trunc=false);
00814 
00816     void close();
00817 
00819     void flush();
00820 
00822     bfstream& low_level() { return s; }
00823 
00825     void set_low_precision(bool p=true)  { low_prec = p; }
00826 
00828     bool get_low_precision() { return low_prec; }
00829 
00831     void set_next_name(const std::string& n) { next_name=n; }
00832 
00834     void write_file_header();
00836     void write_data_header(const std::string& type, uint32_t size);
00838     void write_data_header(const std::string& type, const std::string& name,
00839                            uint32_t size);
00841     void low_level_write(char x);
00843     void low_level_write(bin x);
00845     void low_level_write(short x);
00847     void low_level_write(int x);
00849     void low_level_write(float x);
00851     void low_level_write(double x);
00853     void low_level_write(const std::complex<float>& x);
00855     void low_level_write(const std::complex<double>& x);
00857     void low_level_write(const vec& v);
00859     void low_level_write(const ivec& v);
00861     void low_level_write(const bvec& v);
00863     void low_level_write(const cvec& v);
00865     void low_level_write(const std::string& str);
00867     void low_level_write(const mat& m);
00869     void low_level_write(const imat& m);
00871     void low_level_write(const bmat& m);
00873     void low_level_write(const cmat& m);
00875     void low_level_write(const Array<float>& v);
00877     void low_level_write(const Array<double>& v);
00879     void low_level_write(const Array<int>& v);
00881     void low_level_write(const Array<bin>& v);
00883     void low_level_write(const Array<std::complex<float> >& v);
00885     void low_level_write(const Array<std::complex<double> >& v);
00886 
00888     it_file_old& operator<<(it_manip func) { return (*func)(*this); }
00889 
00891     void remove(const std::string& name);
00893     bool exists(const std::string& name);
00895     void pack();
00896 
00897   protected:
00899     void remove();
00901     void write_data_header_here(const data_header& h);
00902 
00904     bool low_prec;
00906     std::string next_name;
00907   };
00908 
00921   inline it_file_old& flush(it_file_old& f)
00922     {
00923       f.flush();
00924       return f;
00925     }
00926 
00927 
00929 
00930 
00932   inline it_ifile_old& operator>>(it_ifile_old& f, const Name& s)
00933     {
00934       f.seek(s.name);
00935       return f;
00936     }
00937 
00939   inline it_file_old& operator<<(it_file_old& f, const Name& s)
00940     {
00941       f.set_next_name(s.name);
00942       return f;
00943     }
00944 
00946   it_ifile_old& operator>>(it_ifile_old& f, char& v);
00947 
00949   it_ifile_old& operator>>(it_ifile_old& f, bin& v);
00950 
00952   it_ifile_old& operator>>(it_ifile_old& f, short& v);
00953 
00955   it_ifile_old& operator>>(it_ifile_old& f, int& v);
00956 
00958   it_ifile_old& operator>>(it_ifile_old& f, float& v);
00959 
00961   it_ifile_old& operator>>(it_ifile_old& f, double& v);
00962 
00964   it_ifile_old& operator>>(it_ifile_old& f, std::complex<float>& v);
00965 
00967   it_ifile_old& operator>>(it_ifile_old& f, std::complex<double>& v);
00968 
00970   it_ifile_old& operator>>(it_ifile_old& f, vec& v);
00971 
00973   it_ifile_old& operator>>(it_ifile_old& f, ivec& v);
00974 
00976   it_ifile_old& operator>>(it_ifile_old& f, bvec& v);
00977 
00979   it_ifile_old& operator>>(it_ifile_old& f, cvec& v);
00980 
00982   it_ifile_old& operator>>(it_ifile_old& f, std::string& str);
00983 
00985   it_ifile_old& operator>>(it_ifile_old& f, mat& m);
00986 
00988   it_ifile_old& operator>>(it_ifile_old& f, imat& m);
00989 
00991   it_ifile_old& operator>>(it_ifile_old& f, bmat& m);
00992 
00994   it_ifile_old& operator>>(it_ifile_old& f, cmat& m);
00995 
00997   it_ifile_old& operator>>(it_ifile_old& f, Array<float>& v);
00998 
01000   it_ifile_old& operator>>(it_ifile_old& f, Array<double>& v);
01001 
01003   it_ifile_old& operator>>(it_ifile_old& f, Array<int>& v);
01004 
01006   it_ifile_old& operator>>(it_ifile_old& f, Array<bin>& v);
01007 
01009   it_ifile_old& operator>>(it_ifile_old& f, Array<std::complex<float> >& v);
01010 
01012   it_ifile_old& operator>>(it_ifile_old& f, Array<std::complex<double> >& v);
01013 
01015   it_ifile_old& operator>>(it_ifile_old& f, Array<vec>& v);
01016 
01018   it_ifile_old& operator>>(it_ifile_old& f, Array<ivec>& v);
01019 
01021   it_ifile_old& operator>>(it_ifile_old& f, Array<bvec>& v);
01022 
01024   it_ifile_old& operator>>(it_ifile_old& f, Array<cvec>& v);
01025 
01027   it_ifile_old& operator>>(it_ifile_old& f, Array<std::string>& v);
01028 
01030   it_ifile_old& operator>>(it_ifile_old& f, Array<mat>& v);
01031 
01033   it_ifile_old& operator>>(it_ifile_old& f, Array<imat>& v);
01034 
01036   it_ifile_old& operator>>(it_ifile_old& f, Array<bmat>& v);
01037 
01039   it_ifile_old& operator>>(it_ifile_old& f, Array<cmat>& v);
01040 
01041 
01043   it_file_old& operator<<(it_file_old& f, char x);
01044 
01046   it_file_old& operator<<(it_file_old& f, bin x);
01047 
01049   it_file_old& operator<<(it_file_old& f, short x);
01050 
01052   it_file_old& operator<<(it_file_old& f, int x);
01053 
01055   it_file_old& operator<<(it_file_old& f, float x);
01056 
01058   it_file_old& operator<<(it_file_old& f, double x);
01059 
01061   it_file_old& operator<<(it_file_old& f, std::complex<float> x);
01062 
01064   it_file_old& operator<<(it_file_old& f, std::complex<double> x);
01065 
01067   it_file_old& operator<<(it_file_old& f, const vec& v);
01068 
01070   it_file_old& operator<<(it_file_old& f, const ivec& v);
01071 
01073   it_file_old& operator<<(it_file_old& f, const bvec& v);
01074 
01076   it_file_old& operator<<(it_file_old& f, const cvec& v);
01077 
01079   it_file_old& operator<<(it_file_old& f, const std::string& str);
01080 
01082   it_file_old& operator<<(it_file_old& f, const mat& m);
01083 
01085   it_file_old& operator<<(it_file_old& f, const imat& m);
01086 
01088   it_file_old& operator<<(it_file_old& f, const bmat& m);
01089 
01091   it_file_old& operator<<(it_file_old& f, const cmat& m);
01092 
01094   it_file_old& operator<<(it_file_old& f, const Array<float>& v);
01095 
01097   it_file_old& operator<<(it_file_old& f, const Array<double>& v);
01098 
01100   it_file_old& operator<<(it_file_old& f, const Array<int>& v);
01101 
01103   it_file_old& operator<<(it_file_old& f, const Array<bin>& v);
01104 
01106   it_file_old& operator<<(it_file_old& f, const Array<std::complex<float> >& v);
01107 
01109   it_file_old& operator<<(it_file_old& f, const Array<std::complex<double> >& v);
01110 
01112   it_file_old& operator<<(it_file_old& f, const Array<vec>& v);
01113 
01115   it_file_old& operator<<(it_file_old& f, const Array<ivec>& v);
01116 
01118   it_file_old& operator<<(it_file_old& f, const Array<bvec>& v);
01119 
01121   it_file_old& operator<<(it_file_old& f, const Array<cvec>& v);
01122 
01124   it_file_old& operator<<(it_file_old& f, const Array<std::string>& v);
01125 
01127   it_file_old& operator<<(it_file_old& f, const Array<mat>& v);
01128 
01130   it_file_old& operator<<(it_file_old& f, const Array<imat>& v);
01131 
01133   it_file_old& operator<<(it_file_old& f, const Array<bmat>& v);
01134 
01136   it_file_old& operator<<(it_file_old& f, const Array<cmat>& v);
01137 
01139 
01140   // ----------------------------------------------------------------------
01141   // End of the deprecated implementation of IT++ file format version 2
01142   // Will be removed in future versions
01143   // ----------------------------------------------------------------------
01144 
01145 } // namespace itpp
01146 
01147 #endif // #ifndef IT_FILE_H
01148 
SourceForge Logo

Generated on Sat May 3 16:10:40 2008 for IT++ by Doxygen 1.5.5