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 
34 using std::ofstream;
35 using std::ifstream;
36 using std::fstream;
37 using std::ios;
38 
39 
40 namespace itpp
41 {
42 
43 namespace binfile_details
44 {
46  Ofstream_Binfile_Facade::Ofstream_Binfile_Facade ( ) : _str(new std::ofstream()) {};
48  Ofstream_Binfile_Facade::Ofstream_Binfile_Facade ( const char * filename, std::ios_base::openmode mode) :
49  _str(new std::ofstream(filename,mode)){};
52 
54  Ifstream_Binfile_Facade::Ifstream_Binfile_Facade ( ) : _str(new std::ifstream()) {};
56  Ifstream_Binfile_Facade::Ifstream_Binfile_Facade ( const char * filename, std::ios_base::openmode mode) :
57  _str(new std::ifstream(filename,mode)){};
60 
62  Fstream_Binfile_Facade::Fstream_Binfile_Facade ( ) : _str(new std::fstream()) {};
64  Fstream_Binfile_Facade::Fstream_Binfile_Facade ( const char * filename, std::ios_base::openmode mode) :
65  _str(new std::fstream(filename,mode)){};
68 }
69 
71 template<typename T1, typename T2> inline
72 void 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 
84 template<typename T1, typename T2> inline
85 void 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 
98 bool 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 
125 bofstream::bofstream(const std::string& name, endian e) :
126  bfstream_base(e), binfile_details::Ofstream_Binfile_Facade(name.c_str()) {}
127 
128 bofstream::bofstream() : bfstream_base(), binfile_details::Ofstream_Binfile_Facade() {}
129 
130 void 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 
221 bofstream& 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 
231 bifstream::bifstream(const std::string& name, endian e) :
232  bfstream_base(e), binfile_details::Ifstream_Binfile_Facade(name.c_str(), ios::in | ios::binary) {}
233 
234 bifstream::bifstream() : bfstream_base(), binfile_details::Ifstream_Binfile_Facade() {}
235 
236 void 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 
245 int 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 
353 bfstream::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 
357 bfstream::bfstream() : bfstream_base(), binfile_details::Fstream_Binfile_Facade() {}
358 
359 void 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 
373 void 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 
382 int 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 
472 bfstream& 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
SourceForge Logo

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