IT++ Logo
binfile.cpp
Go to the documentation of this file.
1
29#include <itpp/base/binfile.h>
30#include <itpp/base/math/misc.h>
31#include <cstring>
32
33
34using std::ofstream;
35using std::ifstream;
36using std::fstream;
37using std::ios;
38
39
40namespace itpp
41{
42
43namespace binfile_details
44{
48 Ofstream_Binfile_Facade::Ofstream_Binfile_Facade ( const char * filename, std::ios_base::openmode mode) :
49 _str(new std::ofstream(filename,mode)){};
52
56 Ifstream_Binfile_Facade::Ifstream_Binfile_Facade ( const char * filename, std::ios_base::openmode mode) :
57 _str(new std::ifstream(filename,mode)){};
60
64 Fstream_Binfile_Facade::Fstream_Binfile_Facade ( const char * filename, std::ios_base::openmode mode) :
65 _str(new std::fstream(filename,mode)){};
68}
69
71template<typename T1, typename T2> inline
72void read_endian(T1& st, T2& data, bool switch_endian = false)
73{
74 int bytes = sizeof(T2);
75 char *c = reinterpret_cast<char *>(&data);
76 if (!switch_endian)
77 st.read(c, bytes);
78 else
79 for (int i = bytes - 1; i >= 0; i--)
80 st.get(c[i]);
81}
82
84template<typename T1, typename T2> inline
85void write_endian(T1& st, T2 data, bool switch_endian = false)
86{
87 int bytes = sizeof(T2);
88 char *c = reinterpret_cast<char *>(&data);
89 if (!switch_endian)
90 st.write(c, bytes);
91 else
92 for (int i = bytes - 1; i >= 0; i--)
93 st.put(c[i]);
94}
95
96// ----------------------------------------------------------------------
97
98bool exist(const std::string& name)
99{
100 bool file_exists = false;
101 ifstream file(name.c_str(), ios::in);
102 if (file.is_open()) {
103 file_exists = true;
104 }
105 file.close();
106 return file_exists;
107}
108
109// ----------------------------------------------------------------------
110// bfstream_base
111// ----------------------------------------------------------------------
112
114 switch_endianity(false),
115 native_endianity(is_bigendian() ? b_endian : l_endian)
116{
117 if (native_endianity != e)
118 switch_endianity = true;
119}
120
121// ----------------------------------------------------------------------
122// bofstream
123// ----------------------------------------------------------------------
124
125bofstream::bofstream(const std::string& name, endian e) :
126 bfstream_base(e), binfile_details::Ofstream_Binfile_Facade(name.c_str()) {}
127
128bofstream::bofstream() : bfstream_base(), binfile_details::Ofstream_Binfile_Facade() {}
129
130void bofstream::open(const std::string& name, bool truncate, endian e)
131{
132 if (native_endianity != e)
133 switch_endianity = true;
134 else
135 switch_endianity = false;
136 Ofstream_Binfile_Facade::open(name.c_str(),
137 truncate? ios::out | ios::binary | ios::trunc : ios::out | ios::binary);
138}
139
141{
142 put(a);
143 return *this;
144}
145
147{
148 it_assert(sizeof(char) == sizeof(int8_t),"Unexpected int8_t size.");
149 put(static_cast<char>(a));
150 return *this;
151}
152
154{
155 it_assert(sizeof(char) == sizeof(uint8_t),"Unexpected uint8_t size.");
156 put(static_cast<char>(a));
157 return *this;
158}
159
161{
162 write_endian<bofstream, int16_t>(*this, a, switch_endianity);
163 return *this;
164}
165
167{
168 write_endian<bofstream, uint16_t>(*this, a, switch_endianity);
169 return *this;
170}
171
173{
174 write_endian<bofstream, int32_t>(*this, a, switch_endianity);
175 return *this;
176}
177
179{
180 write_endian<bofstream, uint32_t>(*this, a, switch_endianity);
181 return *this;
182}
183
185{
186 write_endian<bofstream, int64_t>(*this, a, switch_endianity);
187 return *this;
188}
189
191{
192 write_endian<bofstream, uint64_t>(*this, a, switch_endianity);
193 return *this;
194}
195
197{
198 write_endian<bofstream, float>(*this, a, switch_endianity);
199 return *this;
200}
201
203{
204 write_endian<bofstream, double>(*this, a, switch_endianity);
205 return *this;
206}
207
209{
210 write_endian<bofstream, int32_t>(*this, static_cast<int32_t>(a), switch_endianity);
211 return *this;
212}
213
214
216{
217 write(a, strlen(a) + 1);
218 return *this;
219}
220
221bofstream& bofstream::operator<<(const std::string& a)
222{
223 write(a.c_str(), a.size() + 1);
224 return *this;
225}
226
227// ----------------------------------------------------------------------
228// bifstream
229// ----------------------------------------------------------------------
230
231bifstream::bifstream(const std::string& name, endian e) :
232 bfstream_base(e), binfile_details::Ifstream_Binfile_Facade(name.c_str(), ios::in | ios::binary) {}
233
234bifstream::bifstream() : bfstream_base(), binfile_details::Ifstream_Binfile_Facade() {}
235
236void bifstream::open(const std::string& name, endian e)
237{
238 if (native_endianity != e)
239 switch_endianity = true;
240 else
241 switch_endianity = false;
242 binfile_details::Ifstream_Binfile_Facade::open(name.c_str(), ios::in | ios::binary);
243}
244
245int bifstream::length() // in bytes
246{
247 std::streampos pos1, len;
248 pos1 = tellg();
249 seekg(0, ios::end);
250 len = tellg();
251 seekg(pos1);
252 return int(len);
253}
254
256{
257 get(a);
258 return *this;
259}
260
262{
263 it_assert(sizeof(char) == sizeof(int8_t),"Unexpected int8_t size.");
264 char tmp;
265 get(tmp);
266 a = tmp;
267 return *this;
268}
269
271{
272 it_assert(sizeof(char) == sizeof(uint8_t),"Unexpected uint8_t size.");
273 char tmp;
274 get(tmp);
275 a = tmp;
276 return *this;
277}
278
280{
281 read_endian<bifstream, int16_t>(*this, a, switch_endianity);
282 return *this;
283}
284
286{
287 read_endian<bifstream, uint16_t>(*this, a, switch_endianity);
288 return *this;
289}
290
292{
293 read_endian<bifstream, int32_t>(*this, a, switch_endianity);
294 return *this;
295}
296
298{
299 read_endian<bifstream, uint32_t>(*this, a, switch_endianity);
300 return *this;
301}
302
304{
305 read_endian<bifstream, int64_t>(*this, a, switch_endianity);
306 return *this;
307}
308
310{
311 read_endian<bifstream, uint64_t>(*this, a, switch_endianity);
312 return *this;
313}
314
316{
317 read_endian<bifstream, float>(*this, a, switch_endianity);
318 return *this;
319}
320
322{
323 read_endian<bifstream, double>(*this, a, switch_endianity);
324 return *this;
325}
326
328{
329 uint32_t tmp;
330 read_endian<bifstream, uint32_t>(*this, tmp, switch_endianity);
331 it_assert((tmp == 0) || (tmp == 1),
332 "bifstream::operator>>(): binary input value must be 0 or 1");
333 a = tmp;
334 return *this;
335}
336
338{
339 getline(a, '\0');
340 return *this;
341}
342
344{
345 std::getline(*stream(), a, '\0');
346 return *this;
347}
348
349// ----------------------------------------------------------------------
350// bfstream
351// ----------------------------------------------------------------------
352
353bfstream::bfstream(const std::string& name, endian e) :
354 bfstream_base(e), binfile_details::Fstream_Binfile_Facade(name.c_str(), ios::in | ios::out | ios::binary)
355{}
356
357bfstream::bfstream() : bfstream_base(), binfile_details::Fstream_Binfile_Facade() {}
358
359void bfstream::open(const std::string& name, bool trnc, endian e)
360{
361 if (native_endianity != e)
362 switch_endianity = true;
363 else
364 switch_endianity = false;
365
366 if (trnc)
367 binfile_details::Fstream_Binfile_Facade::open(name.c_str(), ios::in | ios::out | ios::binary
368 | ios::trunc);
369 else
370 binfile_details::Fstream_Binfile_Facade::open(name.c_str(), ios::in | ios::out | ios::binary);
371}
372
373void bfstream::open_readonly(const std::string& name, endian e)
374{
375 if (native_endianity != e)
376 switch_endianity = true;
377 else
378 switch_endianity = false;
379 binfile_details::Fstream_Binfile_Facade::open(name.c_str(), ios::in | ios::binary);
380}
381
382int bfstream::length() // in bytes
383{
384 std::streampos pos1, len;
385 pos1 = tellg();
386 seekg(0, ios::end);
387 len = tellg();
388 seekg(pos1);
389 return int(len);
390}
391
393{
394 put(a);
395 return *this;
396}
397
399{
400 it_assert(sizeof(char) == sizeof(int8_t),"Unexpected int8_t size.");
401 put(static_cast<char>(a));
402 return *this;
403}
404
406{
407 it_assert(sizeof(char) == sizeof(uint8_t),"Unexpected uint8_t size.");
408 put(static_cast<char>(a));
409 return *this;
410}
411
413{
414 write_endian<bfstream, int16_t>(*this, a, switch_endianity);
415 return *this;
416}
417
419{
420 write_endian<bfstream, uint16_t>(*this, a, switch_endianity);
421 return *this;
422}
423
425{
426 write_endian<bfstream, int32_t>(*this, a, switch_endianity);
427 return *this;
428}
429
431{
432 write_endian<bfstream, uint32_t>(*this, a, switch_endianity);
433 return *this;
434}
435
437{
438 write_endian<bfstream, int64_t>(*this, a, switch_endianity);
439 return *this;
440}
441
443{
444 write_endian<bfstream, uint64_t>(*this, a, switch_endianity);
445 return *this;
446}
447
449{
450 write_endian<bfstream, float>(*this, a, switch_endianity);
451 return *this;
452}
453
455{
456 write_endian<bfstream, double>(*this, a, switch_endianity);
457 return *this;
458}
459
461{
462 write_endian<bfstream, int32_t>(*this, static_cast<int32_t>(a), switch_endianity);
463 return *this;
464}
465
467{
468 write(a, strlen(a) + 1);
469 return *this;
470}
471
472bfstream& bfstream::operator<<(const std::string& a)
473{
474 write(a.c_str(), a.size() + 1);
475 return *this;
476}
477
478
480{
481 get(a);
482 return *this;
483}
484
486{
487 it_assert(sizeof(char) == sizeof(int8_t),"Unexpected int8_t size.");
488 char tmp;
489 get(tmp);
490 a = tmp;
491 return *this;
492}
493
495{
496 it_assert(sizeof(char) == sizeof(uint8_t),"Unexpected uint8_t size.");
497 char tmp;
498 get(tmp);
499 a = tmp;
500 return *this;
501}
502
504{
505 read_endian<bfstream, int16_t>(*this, a, switch_endianity);
506 return *this;
507}
508
510{
511 read_endian<bfstream, uint16_t>(*this, a, switch_endianity);
512 return *this;
513}
514
516{
517 read_endian<bfstream, int32_t>(*this, a, switch_endianity);
518 return *this;
519}
520
522{
523 read_endian<bfstream, uint32_t>(*this, a, switch_endianity);
524 return *this;
525}
526
528{
529 read_endian<bfstream, int64_t>(*this, a, switch_endianity);
530 return *this;
531}
532
534{
535 read_endian<bfstream, uint64_t>(*this, a, switch_endianity);
536 return *this;
537}
538
540{
541 read_endian<bfstream, float>(*this, a, switch_endianity);
542 return *this;
543}
544
546{
547 read_endian<bfstream, double>(*this, a, switch_endianity);
548 return *this;
549}
550
552{
553 uint32_t tmp;
554 read_endian<bfstream, uint32_t>(*this, tmp, switch_endianity);
555 it_assert((tmp == 0) || (tmp == 1),
556 "bfstream::operator>>(): binary input value must be 0 or 1");
557 a = tmp;
558 return *this;
559}
560
562{
563 getline(a, '\0');
564 return *this;
565}
566
568{
569 std::getline(*stream(), a, '\0');
570 return *this;
571}
572
573} // namespace itpp
Binary file formats definitions.
Base class for binary file classes.
Definition: binfile.h:60
endian
Definition of the endian data type.
Definition: binfile.h:74
bfstream_base(endian e=b_endian)
Class Constructor.
Definition: binfile.cpp:113
bool switch_endianity
Indicates if the endianity of the processed data needs to be changed.
Definition: binfile.h:125
endian native_endianity
The native endianity for this computer architecture.
Definition: binfile.h:127
Binary in/out-file Class.
Definition: binfile.h:604
void open_readonly(const std::string &name, endian e=b_endian)
Open a file for reading only and set the endianity.
Definition: binfile.cpp:373
bfstream & operator>>(char &a)
Reads a char variable from the binary file.
Definition: binfile.cpp:479
bfstream & operator<<(char a)
Writes an char variable to the binary file.
Definition: binfile.cpp:392
int length()
Returns the length in bytes of the file.
Definition: binfile.cpp:382
bfstream()
Class Constructor.
Definition: binfile.cpp:357
void open(const std::string &name, bool trunc=false, endian e=b_endian)
Open a file for reading and writing and set the endianity.
Definition: binfile.cpp:359
Binary Infile Class.
Definition: binfile.h:539
void open(const std::string &name, endian e=b_endian)
Open a file for reading and set the endianity.
Definition: binfile.cpp:236
bifstream()
Class Constructor.
Definition: binfile.cpp:234
int length()
Returns the length in bytes of the file.
Definition: binfile.cpp:245
bifstream & operator>>(char &a)
Reads a signed char variable from the binary input file.
Definition: binfile.cpp:255
Binary arithmetic (boolean) class.
Definition: binary.h:57
Fstream_Binfile_Facade & seekg(std::streampos pos)
Set position.
Definition: binfile.h:428
Fstream_Binfile_Facade & put(const char c)
Output single char.
Definition: binfile.h:379
virtual ~Fstream_Binfile_Facade()
Destructor.
Definition: binfile.cpp:67
Fstream_Binfile_Facade & getline(char *s, std::streamsize n)
Get multiple chars to c-string without trailing 0.
Definition: binfile.h:404
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
Fstream_Binfile_Facade()
Default Constructor.
Definition: binfile.cpp:62
std::fstream * stream()
Access to internal stream for derived classes.
Definition: binfile.h:464
std::streampos tellg()
Get position.
Definition: binfile.h:426
Fstream_Binfile_Facade & write(const char *c, std::streamsize n)
Output multiple characters.
Definition: binfile.h:376
std::ifstream * stream()
Access to internal stream for derived classes.
Definition: binfile.h:332
Ifstream_Binfile_Facade()
Default Constructor.
Definition: binfile.cpp:54
Ifstream_Binfile_Facade & seekg(std::streampos pos)
Set position.
Definition: binfile.h:297
Ifstream_Binfile_Facade & getline(char *s, std::streamsize n)
Get multiple chars to c-string without trailing 0.
Definition: binfile.h:274
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::streampos tellg()
Get position.
Definition: binfile.h:295
Ofstream_Binfile_Facade()
Default Constructor.
Definition: binfile.cpp:46
Ofstream_Binfile_Facade & put(const char c)
Output single char.
Definition: binfile.h:175
Ofstream_Binfile_Facade & write(const char *c, std::streamsize n)
Output multiple characters.
Definition: binfile.h:172
Binary Outfile Class.
Definition: binfile.h:475
bofstream & operator<<(char a)
Writes a signed char variable to the binary output file.
Definition: binfile.cpp:140
void open(const std::string &name, bool trunc=false, endian e=b_endian)
Open a file for writing and set the endianity.
Definition: binfile.cpp:130
bofstream()
Class Constructor.
Definition: binfile.cpp:128
#define it_assert(t, s)
Abort if t is not true.
Definition: itassert.h:94
bool exist(const std::string &name)
Checks if a file named name already exists on the disk.
Definition: binfile.cpp:98
bool is_bigendian()
Returns true if machine endianness is BIG_ENDIAN.
Definition: misc.cpp:50
Miscellaneous functions - header file.
itpp namespace
Definition: itmex.h:37
void read_endian(T1 &st, T2 &data, bool switch_endian=false)
Read binary data and optionally switch endianness.
Definition: binfile.cpp:72
void write_endian(T1 &st, T2 data, bool switch_endian=false)
Write binary data and optionally switch endianness.
Definition: binfile.cpp:85
STL namespace.

Generated on Tue Aug 17 2021 10:59:15 for IT++ by Doxygen 1.9.4