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);}
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);}
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
Ofstream_Binfile_Facade & seekp(std::streampos pos)
Set position.
Definition: binfile.h:181
bool operator!() const
Unary not operator to check the stream state.
Definition: binfile.h:313
void open(const char *filename, std::ios_base::openmode mode=std::ios_base::out|std::ios_base::binary)
Method to open corresponding file.
Definition: binfile.h:165
void clear(std::ios_base::iostate state=std::ios_base::goodbit)
Method to set stream state (overwrites stream state flags)
Definition: binfile.h:322
std::ios_base::iostate exceptions() const
Method to get the exceptions mask.
Definition: binfile.h:456
bool good() const
This method returns true is stream state is good.
Definition: binfile.h:191
void clear(std::ios_base::iostate state=std::ios_base::goodbit)
Method to set stream state (overwrites stream state flags)
Definition: binfile.h:209
int peek()
Peak single char from the top of the buffer.
Definition: binfile.h:412
void open(const char *filename, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::binary)
Method to open corresponding file.
Definition: binfile.h:256
std::streamsize gcount() const
Last extracted chars count.
Definition: binfile.h:392
bool operator!() const
Unary not operator to check the stream state.
Definition: binfile.h:444
~bofstream()
Class Destructor.
Definition: binfile.h:492
Ifstream_Binfile_Facade & seekg(std::streamoff pos, std::ios_base::seekdir way)
Set relative position.
Definition: binfile.h:300
Fstream_Binfile_Facade & seekp(std::streamoff pos, std::ios_base::seekdir way)
Set relative position.
Definition: binfile.h:387
Ifstream_Binfile_Facade & putback(char c)
This method attempts to put back single char.
Definition: binfile.h:290
void close()
Method to close corresponding file.
Definition: binfile.h:260
Ofstream Interface Facade for Binary Streams.This class implements std::ofstream facade to make ITPP ...
Definition: binfile.h:150
void setstate(std::ios_base::iostate state)
Method to set the stream state (combines already set flags with flags provide by user) ...
Definition: binfile.h:320
bool fail() const
This method returns true if either failbit or badbit is set.
Definition: binfile.h:308
Fstream_Binfile_Facade & write(const char *c, std::streamsize n)
Output multiple characters.
Definition: binfile.h:376
bool eof() const
This method returns true if eof is reached.
Definition: binfile.h:193
Fstream_Binfile_Facade & seekp(std::streampos pos)
Set position.
Definition: binfile.h:384
void set_native_endianity()
Set the endianity of this class to the native endianity for this computer architecture.
Definition: binfile.h:121
int peek()
Peak single char from the top of the buffer.
Definition: binfile.h:282
Binary class definition.
std::ostream & operator<<(std::ostream &output, const bin &inbin)
Output stream of bin.
Definition: binary.cpp:36
Fstream_Binfile_Facade & seekg(std::streampos pos)
Set position.
Definition: binfile.h:428
std::ios_base::iostate rdstate() const
Method to read stream state flags.
Definition: binfile.h:449
Ifstream_Binfile_Facade & read(char *s, std::streamsize n)
Read n chars from stream.
Definition: binfile.h:284
std::streampos tellp()
Get position.
Definition: binfile.h:178
bool fail() const
This method returns true if either failbit or badbit is set.
Definition: binfile.h:439
Ifstream_Binfile_Facade & unget()
Unget last extracted char.
Definition: binfile.h:293
void close()
Method to close corresponding file.
Definition: binfile.h:373
std::fstream * stream()
Access to internal stream for derived classes.
Definition: binfile.h:464
Ifstream_Binfile_Facade & ignore(std::streamsize n=1, int delim=EOF)
Extract and ignore chars.
Definition: binfile.h:279
Binary in/out-file Class.
Definition: binfile.h:603
int length(const Vec< T > &v)
Length of vector.
Definition: matfunc.h:51
std::ios_base::iostate rdstate() const
Method to read stream state flags.
Definition: binfile.h:318
Binary Infile Class.
Definition: binfile.h:538
std::ios_base::iostate exceptions() const
Method to get the exceptions mask.
Definition: binfile.h:211
void exceptions(std::ios_base::iostate except)
Method to set the exceptions mask.
Definition: binfile.h:326
bool good() const
This method returns true is stream state is good.
Definition: binfile.h:304
bool switch_endianity
Indicates if the endianity of the processed data needs to be changed.
Definition: binfile.h:125
void set_endianity(endian e)
Set the endianity for this class.
Definition: binfile.h:110
Fstream_Binfile_Facade & unget()
Unget last extracted char.
Definition: binfile.h:423
~bifstream()
Class Destructor.
Definition: binfile.h:555
std::ios_base::iostate rdstate() const
Method to read stream state flags.
Definition: binfile.h:205
Fstream_Binfile_Facade & seekg(std::streamoff pos, std::ios_base::seekdir way)
Set relative position.
Definition: binfile.h:431
endian native_endianity
The native endianity for this computer architecture.
Definition: binfile.h:127
Ifstream Interface Facade for Binary Streams.This class implements std::ofstream facade to make ITPP ...
Definition: binfile.h:240
bool eof() const
This method returns true if eof is reached.
Definition: binfile.h:437
std::ofstream * stream()
Access to internal stream for derived classes.
Definition: binfile.h:219
void close()
Method to close corresponding file.
Definition: binfile.h:169
IT++ type definitions.
Ofstream_Binfile_Facade & seekp(std::streamoff pos, std::ios_base::seekdir way)
Set relative position.
Definition: binfile.h:184
bool bad() const
This method returns true if badbit is set.
Definition: binfile.h:310
Ofstream_Binfile_Facade & write(const char *c, std::streamsize n)
Output multiple characters.
Definition: binfile.h:172
Ofstream_Binfile_Facade & flush()
Flushes stream buffer.
Definition: binfile.h:187
Fstream_Binfile_Facade & put(const char c)
Output single char.
Definition: binfile.h:379
Fstream_Binfile_Facade & putback(char c)
This method attempts to put back single char.
Definition: binfile.h:420
Base class for binary file classesThis class serves as a base class for the classes bofstream...
Definition: binfile.h:59
bool fail() const
This method returns true if either failbit or badbit is set.
Definition: binfile.h:195
Fstream_Binfile_Facade & ignore(std::streamsize n=1, int delim=EOF)
Extract and ignore chars.
Definition: binfile.h:409
Fstream Interface Facade for Binary Streams.This class implements std::fstream facade to make ITPP bi...
Definition: binfile.h:354
void exceptions(std::ios_base::iostate except)
Method to set the exceptions mask.
Definition: binfile.h:458
std::streampos tellp()
Get position.
Definition: binfile.h:382
void setstate(std::ios_base::iostate state)
Method to set the stream state (combines already set flags with flags provide by user) ...
Definition: binfile.h:207
std::streamsize gcount() const
Last extracted chars count.
Definition: binfile.h:262
bool good() const
This method returns true is stream state is good.
Definition: binfile.h:435
void open(const char *filename, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out|std::ios_base::binary)
Method to open corresponding file.
Definition: binfile.h:369
itpp namespace
Definition: itmex.h:36
Binary Outfile Class.
Definition: binfile.h:474
endian get_endianity() const
Returns the endianity of the class.
Definition: binfile.h:88
Ifstream_Binfile_Facade & seekg(std::streampos pos)
Set position.
Definition: binfile.h:297
endian
Definition of the endian data type.
Definition: binfile.h:74
Fstream_Binfile_Facade & getline(char *s, std::streamsize n)
Get multiple chars to c-string without trailing 0.
Definition: binfile.h:404
bool bad() const
This method returns true if badbit is set.
Definition: binfile.h:441
~bfstream()
Class Destructor.
Definition: binfile.h:620
Binary arithmetic (boolean) class.
Definition: binary.h:56
Ifstream_Binfile_Facade & getline(char *s, std::streamsize n)
Get multiple chars to c-string without trailing 0.
Definition: binfile.h:274
std::ios_base::iostate exceptions() const
Method to get the exceptions mask.
Definition: binfile.h:324
void setstate(std::ios_base::iostate state)
Method to set the stream state (combines already set flags with flags provide by user) ...
Definition: binfile.h:451
std::streampos tellg()
Get position.
Definition: binfile.h:426
std::streampos tellg()
Get position.
Definition: binfile.h:295
Fstream_Binfile_Facade & flush()
Flushes stream buffer.
Definition: binfile.h:390
endian get_native_endianity() const
Returns the native endianity for this computer architecture.
Definition: binfile.h:105
void clear(std::ios_base::iostate state=std::ios_base::goodbit)
Method to set stream state (overwrites stream state flags)
Definition: binfile.h:453
bool bad() const
This method returns true if badbit is set.
Definition: binfile.h:197
std::istream & operator>>(std::istream &input, bin &outbin)
Input stream of bin.
Definition: binary.cpp:42
Fstream_Binfile_Facade & read(char *s, std::streamsize n)
Read n chars from stream.
Definition: binfile.h:414
bool operator!() const
Unary not operator to check the stream state.
Definition: binfile.h:200
bool eof() const
This method returns true if eof is reached.
Definition: binfile.h:306
bool exist(const std::string &name)
Checks if a file named name already exists on the disk.
Definition: binfile.cpp:98
std::streamsize readsome(char *s, std::streamsize n)
Read up to n available chars from stream.
Definition: binfile.h:287
std::streamsize readsome(char *s, std::streamsize n)
Read up to n available chars from stream.
Definition: binfile.h:417
Ofstream_Binfile_Facade & put(const char c)
Output single char.
Definition: binfile.h:175
std::ifstream * stream()
Access to internal stream for derived classes.
Definition: binfile.h:332
void exceptions(std::ios_base::iostate except)
Method to set the exceptions mask.
Definition: binfile.h:213

Generated on Thu Jun 21 2018 16:06:18 for IT++ by Doxygen 1.8.13