00001 00033 #ifndef ITFILE_H 00034 #define ITFILE_H 00035 00036 #include <itpp/base/vec.h> 00037 #include <itpp/base/array.h> 00038 #include <itpp/base/binfile.h> 00039 #include <itpp/base/machdep.h> 00040 00041 00042 namespace itpp { 00043 00082 class it_file_base { 00083 public: 00084 00086 struct data_header { 00088 char endianity; 00090 it_u32 hdr_bytes, data_bytes, block_bytes; 00092 std::string name, type; 00093 }; 00094 00095 protected: 00096 00098 struct file_header { 00100 char magic[4]; 00102 char version; 00103 }; 00105 static char file_magic[4]; 00107 static char file_version; 00108 }; 00109 00115 class it_ifile : public it_file_base { 00116 public: 00118 it_ifile(); 00120 explicit it_ifile(const std::string &name); 00122 virtual ~it_ifile() { } 00124 void open(const std::string &name); 00126 virtual void close(); 00128 bfstream &low_level() { return s; } 00129 00131 bool read_check_file_header(); 00133 void read_data_header(data_header &h); 00135 void low_level_read(bin &x); 00137 void low_level_read(short &x); 00139 void low_level_read(int &x); 00140 //Read a long long value at the current file pointer position 00141 //void low_level_read(long_long &x); 00143 void low_level_read(float &x); 00145 void low_level_read(double &x); 00147 void low_level_read(std::complex<float> &x); 00149 void low_level_read(std::complex<double> &x); 00151 void low_level_read_lo(vec &v); 00153 void low_level_read_hi(vec &v); 00154 //Read a vector of long long values at the current file pointer position 00155 //void low_level_read(llvec &v); 00157 void low_level_read(ivec &v); 00159 void low_level_read(bvec &v); 00161 void low_level_read_lo(cvec &v); 00163 void low_level_read_hi(cvec &v); 00165 void low_level_read(std::string &str); 00167 void low_level_read_lo(mat &m); 00169 void low_level_read_hi(mat &m); 00170 //Read a matrix of long long values at the current file pointer position 00171 //void low_level_read(llmat &m); 00173 void low_level_read(imat &m); 00175 void low_level_read(bmat &m); 00177 void low_level_read_lo(cmat &m); 00179 void low_level_read_hi(cmat &m); 00180 00182 void low_level_read_lo(Array<float> &v); 00184 void low_level_read_lo(Array<double> &v); 00186 void low_level_read_hi(Array<double> &v); 00187 //Read an Array of long long values at the current file pointer position 00188 //void low_level_read(Array<long_long> &v); 00190 void low_level_read(Array<int> &v); 00192 void low_level_read(Array<bin> &v); 00194 void low_level_read_lo(Array<std::complex<float> > &v); 00196 void low_level_read_lo(Array<std::complex<double> > &v); 00198 void low_level_read_hi(Array<std::complex<double> > &v); 00199 00201 bool seek(const std::string &name); 00202 00204 bool seek(int n); 00206 void info(std::string &name, std::string &type, int &bytes); 00207 00208 protected: 00210 bfstream s; 00211 }; 00212 00218 class it_file : public it_ifile { 00219 public: 00221 typedef it_file& (*it_manip)(it_file&); 00222 00224 it_file(); 00225 00232 explicit it_file(const std::string &name, bool trunc=false); 00233 00235 virtual ~it_file() { } 00236 00243 void open(const std::string &name, bool trunc=false); 00244 00246 void close(); 00247 00249 void flush(); 00250 00252 bfstream &low_level() { return s; } 00253 00255 void set_low_precision(bool p=true) { low_prec = p; } 00256 00258 bool get_low_precision() { return low_prec; } 00259 00261 void set_next_name(const std::string &n) { next_name=n; } 00262 00264 void write_file_header(); 00266 void write_data_header(const std::string &type, it_u32 size); 00268 void write_data_header(const std::string &type, const std::string &name, it_u32 size); 00270 void low_level_write(bin x); 00272 void low_level_write(short x); 00274 void low_level_write(int x); 00275 //Write a long long value at the current file pointer position 00276 //void low_level_write(long_long x); 00278 void low_level_write(float x); 00280 void low_level_write(double x); 00282 void low_level_write(const std::complex<float> &x); 00284 void low_level_write(const std::complex<double> &x); 00286 void low_level_write(const vec &v); 00288 void low_level_write(const ivec &v); 00290 void low_level_write(const bvec &v); 00291 //Write a llvec at the current file pointer position 00292 //void low_level_write(const llvec &v); 00294 void low_level_write(const cvec &v); 00296 void low_level_write(const std::string &str); 00298 void low_level_write(const mat &m); 00300 void low_level_write(const imat &m); 00302 void low_level_write(const bmat &m); 00303 //Write a llmat at the current file pointer position 00304 //void low_level_write(const llmat &m); 00306 void low_level_write(const cmat &m); 00308 void low_level_write(const Array<float> &v); 00310 void low_level_write(const Array<double> &v); 00312 void low_level_write(const Array<int> &v); 00314 void low_level_write(const Array<bin> &v); 00315 //Write a long long Array at the current file pointer position 00316 //void low_level_write(const Array<long_long> &v); 00318 void low_level_write(const Array<std::complex<float> > &v); 00320 void low_level_write(const Array<std::complex<double> > &v); 00321 00323 it_file& operator<<(it_manip func) { return (*func)(*this); } 00324 00326 void remove(const std::string &name); 00328 bool exists(const std::string &name); 00330 void pack(); 00331 00332 protected: 00334 void remove(); 00336 void write_data_header_here(const data_header &h); 00337 00339 bool low_prec; 00341 std::string next_name; 00342 }; 00343 00356 inline it_file& flush(it_file& f) 00357 { 00358 f.flush(); 00359 return f; 00360 } 00361 00375 class Name { 00376 public: 00378 Name(const std::string &n) : name(n) { } 00380 const std::string &name; 00381 }; 00382 00384 00385 00387 inline it_ifile &operator>>(it_ifile &f, const Name &s) 00388 { 00389 f.seek(s.name); 00390 return f; 00391 } 00392 00394 inline it_file &operator<<(it_file &f, const Name &s) 00395 { 00396 f.set_next_name(s.name); 00397 return f; 00398 } 00399 00401 it_ifile &operator>>(it_ifile &f, bin &v); 00402 00404 it_ifile &operator>>(it_ifile &f, short &v); 00405 00407 it_ifile &operator>>(it_ifile &f, int &v); 00408 00410 it_ifile &operator>>(it_ifile &f, float &v); 00411 00413 it_ifile &operator>>(it_ifile &f, double &v); 00414 00416 it_ifile &operator>>(it_ifile &f, std::complex<float> &v); 00417 00419 it_ifile &operator>>(it_ifile &f, std::complex<double> &v); 00420 00422 it_ifile &operator>>(it_ifile &f, vec &v); 00423 00425 it_ifile &operator>>(it_ifile &f, ivec &v); 00426 00428 it_ifile &operator>>(it_ifile &f, bvec &v); 00429 00431 it_ifile &operator>>(it_ifile &f, cvec &v); 00432 00434 it_ifile &operator>>(it_ifile &f, std::string &str); 00435 00437 it_ifile &operator>>(it_ifile &f, mat &m); 00438 00440 it_ifile &operator>>(it_ifile &f, imat &m); 00441 00443 it_ifile &operator>>(it_ifile &f, bmat &m); 00444 00446 it_ifile &operator>>(it_ifile &f, cmat &m); 00447 00449 it_ifile &operator>>(it_ifile &f, Array<float> &v); 00450 00452 it_ifile &operator>>(it_ifile &f, Array<double> &v); 00453 00455 it_ifile &operator>>(it_ifile &f, Array<int> &v); 00456 00458 it_ifile &operator>>(it_ifile &f, Array<bin> &v); 00459 00461 it_ifile &operator>>(it_ifile &f, Array<std::complex<float> > &v); 00462 00464 it_ifile &operator>>(it_ifile &f, Array<std::complex<double> > &v); 00465 00467 it_ifile &operator>>(it_ifile &f, Array<vec> &v); 00468 00470 it_ifile &operator>>(it_ifile &f, Array<ivec> &v); 00471 00473 it_ifile &operator>>(it_ifile &f, Array<bvec> &v); 00474 00476 it_ifile &operator>>(it_ifile &f, Array<cvec> &v); 00477 00479 it_ifile &operator>>(it_ifile &f, Array<std::string> &v); 00480 00482 it_ifile &operator>>(it_ifile &f, Array<mat> &v); 00483 00485 it_ifile &operator>>(it_ifile &f, Array<imat> &v); 00486 00488 it_ifile &operator>>(it_ifile &f, Array<bmat> &v); 00489 00491 it_ifile &operator>>(it_ifile &f, Array<cmat> &v); 00492 00494 it_file &operator<<(it_file &f, bin x); 00495 00497 it_file &operator<<(it_file &f, short x); 00498 00500 it_file &operator<<(it_file &f, int x); 00501 00503 it_file &operator<<(it_file &f, float x); 00504 00506 it_file &operator<<(it_file &f, double x); 00507 00509 it_file &operator<<(it_file &f, std::complex<float> x); 00510 00512 it_file &operator<<(it_file &f, std::complex<double> x); 00513 00515 it_file &operator<<(it_file &f, const vec &v); 00516 00518 it_file &operator<<(it_file &f, const ivec &v); 00519 00521 it_file &operator<<(it_file &f, const bvec &v); 00522 00524 it_file &operator<<(it_file &f, const cvec &v); 00525 00527 it_file &operator<<(it_file &f, const std::string &str); 00528 00530 it_file &operator<<(it_file &f, const mat &m); 00531 00533 it_file &operator<<(it_file &f, const imat &m); 00534 00536 it_file &operator<<(it_file &f, const bmat &m); 00537 00539 it_file &operator<<(it_file &f, const cmat &m); 00540 00542 it_file &operator<<(it_file &f, const Array<float> &v); 00543 00545 it_file &operator<<(it_file &f, const Array<double> &v); 00546 00548 it_file &operator<<(it_file &f, const Array<int> &v); 00549 00551 it_file &operator<<(it_file &f, const Array<bin> &v); 00552 00554 it_file &operator<<(it_file &f, const Array<std::complex<float> > &v); 00555 00557 it_file &operator<<(it_file &f, const Array<std::complex<double> > &v); 00558 00560 it_file &operator<<(it_file &f, const Array<vec> &v); 00561 00563 it_file &operator<<(it_file &f, const Array<ivec> &v); 00564 00566 it_file &operator<<(it_file &f, const Array<bvec> &v); 00567 00569 it_file &operator<<(it_file &f, const Array<cvec> &v); 00570 00572 it_file &operator<<(it_file &f, const Array<std::string> &v); 00573 00575 it_file &operator<<(it_file &f, const Array<mat> &v); 00576 00578 it_file &operator<<(it_file &f, const Array<imat> &v); 00579 00581 it_file &operator<<(it_file &f, const Array<bmat> &v); 00582 00584 it_file &operator<<(it_file &f, const Array<cmat> &v); 00585 00587 template <class T> 00588 void it_save_var_as(const T &v, const std::string &name) 00589 { 00590 it_file f(name + ".it"); 00591 f << Name(name) << v; 00592 f.close(); 00593 } 00594 00596 template <class T> 00597 void it_load_var_as(T &v, const std::string &name) 00598 { 00599 it_ifile f(name + ".it"); 00600 f.seek(name); 00601 f >> v; 00602 f.close(); 00603 } 00604 00606 #define it_save_var(v) it_save_var_as(v,#v) 00608 #define it_load_var(v) it_load_var_as(v,#v) 00609 00611 00612 } // namespace itpp 00613 00614 #endif // #ifndef IT_FILE_H 00615
Generated on Wed Apr 18 11:45:33 2007 for IT++ by Doxygen 1.5.2