IT++ Logo
binfile.h
Go to the documentation of this file.
1 
29 #ifndef BINFILE_H
30 #define BINFILE_H
31 
32 #include <itpp/itexports.h>
33 
34 #include <itpp/base/ittypes.h>
35 #include <itpp/base/binary.h>
36 #include <fstream>
37 
38 #ifdef _MSC_VER
39 #pragma warning( disable : 4250 )
40 #endif
41 
42 namespace itpp
43 {
44 
49 bool exist(const std::string& name);
50 
59 class ITPP_EXPORT bfstream_base
60 {
61 public:
74  enum endian { l_endian, b_endian };
75 
83  bfstream_base(endian e = b_endian);
84 
89  if (switch_endianity) {
90  if (native_endianity == l_endian)
91  return b_endian;
92  else
93  return l_endian;
94  }
95  else
96  return native_endianity;
97  }
98 
105  endian get_native_endianity() const { return native_endianity; }
106 
111  if (native_endianity == e)
112  switch_endianity = false;
113  else
114  switch_endianity = true;
115  }
116 
121  void set_native_endianity() { switch_endianity = false; }
122 
123 protected:
128 };
129 
130 namespace binfile_details
131 {
150  class ITPP_EXPORT Ofstream_Binfile_Facade
151  {
152  std::ofstream* _str;
153  //following makes class non-copiable
155  void operator= (const Ofstream_Binfile_Facade& o);
156  public:
160  explicit Ofstream_Binfile_Facade ( const char * filename,
161  std::ios_base::openmode mode = std::ios_base::out | std::ios_base::binary);
163  bool is_open() {return _str->is_open();}
165  void open ( const char * filename,
166  std::ios_base::openmode mode = std::ios_base::out | std::ios_base::binary )
167  {_str->open(filename,mode);}
169  void close()
170  {_str->close();}
172  Ofstream_Binfile_Facade& write (const char* c, std::streamsize n)
173  {_str->write(c,n); return *this;}
175  Ofstream_Binfile_Facade& put (const char c)
176  {_str->put(c); return *this;};
178  std::streampos tellp()
179  {return _str->tellp();}
181  Ofstream_Binfile_Facade& seekp (std::streampos pos)
182  {_str->seekp(pos); return *this;}
184  Ofstream_Binfile_Facade& seekp (std::streamoff pos, std::ios_base::seekdir way)
185  {_str->seekp(pos,way); return *this;}
188  {_str->flush(); return *this;}
189 
191  bool good() const {return _str->good();}
193  bool eof() const {return _str->eof();}
195  bool fail() const {return _str->fail();}
197  bool bad() const {return _str->bad();}
198 
200  bool operator!() const {return _str->fail();}
202  operator bool() const {return _str->good();}
203 
205  std::ios_base::iostate rdstate() const {return _str->rdstate();}
207  void setstate (std::ios_base::iostate state) {_str->setstate(state);}
209  void clear (std::ios_base::iostate state = std::ios_base::goodbit) {_str->clear(state);}
211  std::ios_base::iostate exceptions() const {return _str->exceptions();}
213  void exceptions (std::ios_base::iostate except) {_str->exceptions(except);}
214 
216  virtual ~Ofstream_Binfile_Facade();
217  protected:
219  std::ofstream* stream() {return _str;}
220  };
221 
240  class ITPP_EXPORT Ifstream_Binfile_Facade
241  {
242  std::ifstream* _str;
243  //following makes class non-copiable
245  void operator= (const Ifstream_Binfile_Facade& o);
246  public:
250  explicit Ifstream_Binfile_Facade ( const char * filename,
251  std::ios_base::openmode mode = std::ios_base::in | std::ios_base::binary);
253  bool is_open()
254  {return _str->is_open();}
256  void open ( const char * filename,
257  std::ios_base::openmode mode = std::ios_base::in | std::ios_base::binary)
258  {_str->open(filename,mode);}
260  void close() {_str->close();}
262  std::streamsize gcount() const {return _str->gcount();}
264  int get() {return _str->get();}
266  Ifstream_Binfile_Facade& get(char& c) {_str->get(c); return *this;}
268  Ifstream_Binfile_Facade& get (char* s, std::streamsize n)
269  {_str->get(s,n); return *this;}
271  Ifstream_Binfile_Facade& get (char* s, std::streamsize n, char delim)
272  {_str->get(s,n,delim); return *this;}
274  Ifstream_Binfile_Facade& getline (char* s, std::streamsize n )
275  {_str->getline(s,n); return *this;}
276  Ifstream_Binfile_Facade& getline (char* s, std::streamsize n, char delim )
277  {_str->getline(s,n,delim); return *this;}
279  Ifstream_Binfile_Facade& ignore (std::streamsize n = 1, int delim = EOF)
280  {_str->ignore(n,delim); return *this;}
282  int peek() {return _str->peek();}
284  Ifstream_Binfile_Facade& read (char* s, std::streamsize n)
285  {_str->read(s,n); return *this;}
287  std::streamsize readsome (char* s, std::streamsize n)
288  {return _str->readsome(s,n);}
290  Ifstream_Binfile_Facade& putback (char c)
291  {_str->putback(c); return *this;}
293  Ifstream_Binfile_Facade& unget() {_str->unget(); return *this;}
295  std::streampos tellg() {return _str->tellg();}
297  Ifstream_Binfile_Facade& seekg (std::streampos pos)
298  {_str->seekg(pos); return *this;}
300  Ifstream_Binfile_Facade& seekg (std::streamoff pos, std::ios_base::seekdir way)
301  {_str->seekg(pos,way); return *this;}
302 
304  bool good() const {return _str->good();}
306  bool eof() const {return _str->eof();}
308  bool fail() const {return _str->fail();}
310  bool bad() const {return _str->bad();}
311 
313  bool operator!() const {return _str->fail();}
315  operator bool() const {return _str->good();}
316 
318  std::ios_base::iostate rdstate() const {return _str->rdstate();}
320  void setstate (std::ios_base::iostate state) {_str->setstate(state);}
322  void clear (std::ios_base::iostate state = std::ios_base::goodbit) {_str->clear(state);}
324  std::ios_base::iostate exceptions() const {return _str->exceptions();}
326  void exceptions (std::ios_base::iostate except) {_str->exceptions(except);}
327 
329  virtual ~Ifstream_Binfile_Facade();
330  protected:
332  std::ifstream* stream() {return _str;}
333  };
334 
354  class ITPP_EXPORT Fstream_Binfile_Facade
355  {
356  std::fstream* _str;
357  //following makes class non-copiable
359  void operator= (const Fstream_Binfile_Facade& o);
360  public:
364  explicit Fstream_Binfile_Facade ( const char * filename,
365  std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out | std::ios_base::binary);
367  bool is_open() {return _str->is_open();}
369  void open ( const char * filename,
370  std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out | std::ios_base::binary)
371  {_str->open(filename,mode);}
373  void close() {_str->close();}
374 
376  Fstream_Binfile_Facade& write (const char* c, std::streamsize n)
377  {_str->write(c,n); return *this;};
379  Fstream_Binfile_Facade& put (const char c)
380  {_str->put(c); return *this;};
382  std::streampos tellp() {return _str->tellp();}
384  Fstream_Binfile_Facade& seekp (std::streampos pos)
385  {_str->seekp(pos); return *this;}
387  Fstream_Binfile_Facade& seekp (std::streamoff pos, std::ios_base::seekdir way)
388  {_str->seekp(pos,way); return *this;}
390  Fstream_Binfile_Facade& flush() {_str->flush(); return *this;}
392  std::streamsize gcount() const {return _str->gcount();}
394  int get() {return _str->get();}
396  Fstream_Binfile_Facade& get(char& c){_str->get(c); return *this;}
398  Fstream_Binfile_Facade& get(char* s, std::streamsize n)
399  {_str->get(s,n); return *this;}
401  Fstream_Binfile_Facade& get(char* s, std::streamsize n, char delim)
402  {_str->get(s,n,delim); return *this;}
404  Fstream_Binfile_Facade& getline(char* s, std::streamsize n)
405  {_str->getline(s,n); return *this;}
406  Fstream_Binfile_Facade& getline(char* s, std::streamsize n, char delim)
407  {_str->getline(s,n,delim); return *this;}
409  Fstream_Binfile_Facade& ignore (std::streamsize n = 1, int delim = EOF)
410  {_str->ignore(n,delim); return *this;}
412  int peek() {return _str->peek();}
414  Fstream_Binfile_Facade& read (char* s, std::streamsize n)
415  {_str->read(s,n); return *this;}
417  std::streamsize readsome (char* s, std::streamsize n)
418  {return _str->readsome(s,n);}
420  Fstream_Binfile_Facade& putback (char c)
421  {_str->putback(c); return *this;}
424  {_str->unget(); return *this;}
426  std::streampos tellg() {return _str->tellg();}
428  Fstream_Binfile_Facade& seekg (std::streampos pos)
429  {_str->seekg(pos); return *this;}
431  Fstream_Binfile_Facade& seekg (std::streamoff pos, std::ios_base::seekdir way)
432  {_str->seekg(pos,way); return *this;}
433 
435  bool good() const {return _str->good();}
437  bool eof() const {return _str->eof();}
439  bool fail() const {return _str->fail();}
441  bool bad() const {return _str->bad();}
442 
444  bool operator!() const {return _str->fail();}
446  operator bool() const {return _str->good();}
447 
449  std::ios_base::iostate rdstate() const {return _str->rdstate();}
451  void setstate (std::ios_base::iostate state) {_str->setstate(state);}
453  void clear (std::ios_base::iostate state = std::ios_base::goodbit)
454  {_str->clear(state);}
456  std::ios_base::iostate exceptions() const {return _str->exceptions();}
458  void exceptions (std::ios_base::iostate except) {_str->exceptions(except);}
459 
461  virtual ~Fstream_Binfile_Facade();
462  protected:
464  std::fstream* stream() {return _str;}
465  };
466 
467 
468 }
469 
475 {
476 public:
486  bofstream(const std::string& name, endian e = b_endian);
487 
489  bofstream();
490 
493 
502  void open(const std::string& name, bool trunc = false, endian e = b_endian);
503 
505  bofstream& operator<<(char a);
507  bofstream& operator<<(int8_t a);
509  bofstream& operator<<(uint8_t a);
511  bofstream& operator<<(int16_t a);
513  bofstream& operator<<(uint16_t a);
515  bofstream& operator<<(int32_t a);
517  bofstream& operator<<(uint32_t a);
519  bofstream& operator<<(int64_t a);
521  bofstream& operator<<(uint64_t a);
523  bofstream& operator<<(float a);
525  bofstream& operator<<(double a);
529  bofstream& operator<<(const char* a);
531  bofstream& operator<<(const std::string& a);
532 };
533 
539 {
540 public:
549  bifstream(const std::string& name, endian e = b_endian);
550 
552  bifstream();
553 
556 
564  void open(const std::string& name, endian e = b_endian);
565 
567  int length();
568 
570  bifstream& operator>>(char& a);
572  bifstream& operator>>(int8_t& a);
574  bifstream& operator>>(uint8_t& a);
576  bifstream& operator>>(int16_t& a);
578  bifstream& operator>>(uint16_t& a);
580  bifstream& operator>>(int32_t& a);
582  bifstream& operator>>(uint32_t& a);
584  bifstream& operator>>(int64_t& a);
586  bifstream& operator>>(uint64_t& a);
588  bifstream& operator>>(float& a);
590  bifstream& operator>>(double& a);
592  bifstream& operator>>(bin& a);
594  bifstream& operator>>(char* a);
596  bifstream& operator>>(std::string& a);
597 };
598 
604 {
605 public:
614  bfstream(const std::string& name, endian e = b_endian);
615 
617  bfstream();
618 
620  ~bfstream() { }
621 
630  void open(const std::string& name, bool trunc = false, endian e = b_endian);
631 
639  void open_readonly(const std::string& name, endian e = b_endian);
640 
642  int length();
643 
645  bfstream& operator<<(char a);
647  bfstream& operator<<(int8_t a);
649  bfstream& operator<<(uint8_t a);
651  bfstream& operator<<(int16_t a);
653  bfstream& operator<<(uint16_t a);
655  bfstream& operator<<(int32_t a);
657  bfstream& operator<<(uint32_t a);
659  bfstream& operator<<(int64_t a);
661  bfstream& operator<<(uint64_t a);
663  bfstream& operator<<(float a);
665  bfstream& operator<<(double a);
667  bfstream& operator<<(bin a);
669  bfstream& operator<<(const char* a);
671  bfstream& operator<<(const std::string& a);
672 
674  bfstream& operator>>(char& a);
676  bfstream& operator>>(int8_t& a);
678  bfstream& operator>>(uint8_t & a);
680  bfstream& operator>>(int16_t& a);
682  bfstream& operator>>(uint16_t& a);
684  bfstream& operator>>(int32_t& a);
686  bfstream& operator>>(uint32_t& a);
688  bfstream& operator>>(int64_t& a);
690  bfstream& operator>>(uint64_t& a);
692  bfstream& operator>>(float& a);
694  bfstream& operator>>(double& a);
696  bfstream& operator>>(bin& a);
698  bfstream& operator>>(char* a);
700  bfstream& operator>>(std::string& a);
701 };
702 
703 } //namespace itpp
704 
705 
706 #endif // #ifndef BINFILE_H
SourceForge Logo

Generated on Sat Jul 6 2013 10:54:19 for IT++ by Doxygen 1.8.2