IT++ Logo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
44 template<typename T1, typename T2> inline
45 void read_endian(T1& st, T2& data, bool switch_endian = false)
46 {
47  int bytes = sizeof(T2);
48  char *c = reinterpret_cast<char *>(&data);
49  if (!switch_endian)
50  st.read(c, bytes);
51  else
52  for (int i = bytes - 1; i >= 0; i--)
53  st.get(c[i]);
54 }
55 
57 template<typename T1, typename T2> inline
58 void write_endian(T1& st, T2 data, bool switch_endian = false)
59 {
60  int bytes = sizeof(T2);
61  char *c = reinterpret_cast<char *>(&data);
62  if (!switch_endian)
63  st.write(c, bytes);
64  else
65  for (int i = bytes - 1; i >= 0; i--)
66  st.put(c[i]);
67 }
68 
69 // ----------------------------------------------------------------------
70 
71 bool exist(const std::string& name)
72 {
73  bool file_exists = false;
74  ifstream file(name.c_str(), ios::in);
75  if (file.is_open()) {
76  file_exists = true;
77  }
78  file.close();
79  return file_exists;
80 }
81 
82 // ----------------------------------------------------------------------
83 // bfstream_base
84 // ----------------------------------------------------------------------
85 
87  switch_endianity(false),
88  native_endianity(is_bigendian() ? b_endian : l_endian)
89 {
90  if (native_endianity != e)
91  switch_endianity = true;
92 }
93 
94 // ----------------------------------------------------------------------
95 // bofstream
96 // ----------------------------------------------------------------------
97 
98 bofstream::bofstream(const std::string& name, endian e) :
99  bfstream_base(e), ofstream(name.c_str(), ios::out | ios::binary) {}
100 
102 
103 void bofstream::open(const std::string& name, endian e)
104 {
105  if (native_endianity != e)
106  switch_endianity = true;
107  else
108  switch_endianity = false;
109  ofstream::open(name.c_str(), ios::out | ios::binary);
110 }
111 
113 {
114  put(a);
115  return *this;
116 }
117 
119 {
120  put(static_cast<char>(a));
121  return *this;
122 }
123 
125 {
126  write_endian<bofstream, int16_t>(*this, a, switch_endianity);
127  return *this;
128 }
129 
131 {
132  write_endian<bofstream, uint16_t>(*this, a, switch_endianity);
133  return *this;
134 }
135 
137 {
138  write_endian<bofstream, int32_t>(*this, a, switch_endianity);
139  return *this;
140 }
141 
143 {
144  write_endian<bofstream, uint32_t>(*this, a, switch_endianity);
145  return *this;
146 }
147 
149 {
150  write_endian<bofstream, int64_t>(*this, a, switch_endianity);
151  return *this;
152 }
153 
155 {
156  write_endian<bofstream, uint64_t>(*this, a, switch_endianity);
157  return *this;
158 }
159 
161 {
162  write_endian<bofstream, float>(*this, a, switch_endianity);
163  return *this;
164 }
165 
167 {
168  write_endian<bofstream, double>(*this, a, switch_endianity);
169  return *this;
170 }
171 
173 {
174  write(a, strlen(a) + 1);
175  return *this;
176 }
177 
178 bofstream& bofstream::operator<<(const std::string& a)
179 {
180  write(a.c_str(), a.size() + 1);
181  return *this;
182 }
183 
184 // ----------------------------------------------------------------------
185 // bifstream
186 // ----------------------------------------------------------------------
187 
188 bifstream::bifstream(const std::string& name, endian e) :
189  bfstream_base(e), ifstream(name.c_str(), ios::in | ios::binary) {}
190 
192 
193 void bifstream::open(const std::string& name, endian e)
194 {
195  if (native_endianity != e)
196  switch_endianity = true;
197  else
198  switch_endianity = false;
199  ifstream::open(name.c_str(), ios::in | ios::binary);
200 }
201 
202 int bifstream::length() // in bytes
203 {
204  std::streampos pos1, len;
205  pos1 = tellg();
206  seekg(0, ios::end);
207  len = tellg();
208  seekg(pos1);
209  return int(len);
210 }
211 
213 {
214  get(a);
215  return *this;
216 }
217 
219 {
220  char tmp;
221  get(tmp);
222  a = tmp;
223  return *this;
224 }
225 
227 {
228  read_endian<bifstream, int16_t>(*this, a, switch_endianity);
229  return *this;
230 }
231 
233 {
234  read_endian<bifstream, uint16_t>(*this, a, switch_endianity);
235  return *this;
236 }
237 
239 {
240  read_endian<bifstream, int32_t>(*this, a, switch_endianity);
241  return *this;
242 }
243 
245 {
246  read_endian<bifstream, uint32_t>(*this, a, switch_endianity);
247  return *this;
248 }
249 
251 {
252  read_endian<bifstream, int64_t>(*this, a, switch_endianity);
253  return *this;
254 }
255 
257 {
258  read_endian<bifstream, uint64_t>(*this, a, switch_endianity);
259  return *this;
260 }
261 
263 {
264  read_endian<bifstream, float>(*this, a, switch_endianity);
265  return *this;
266 }
267 
269 {
270  read_endian<bifstream, double>(*this, a, switch_endianity);
271  return *this;
272 }
273 
275 {
276  getline(a, '\0');
277  return *this;
278 }
279 
281 {
282  std::getline(*this, a, '\0');
283  return *this;
284 }
285 
286 // ----------------------------------------------------------------------
287 // bfstream
288 // ----------------------------------------------------------------------
289 
290 bfstream::bfstream(const std::string& name, endian e) :
291  bfstream_base(e), fstream(name.c_str(), ios::in | ios::out | ios::binary)
292 {}
293 
295 
296 void bfstream::open(const std::string& name, bool trnc, endian e)
297 {
298  if (native_endianity != e)
299  switch_endianity = true;
300  else
301  switch_endianity = false;
302 
303  if (trnc)
304  fstream::open(name.c_str(), ios::in | ios::out | ios::binary
305  | ios::trunc);
306  else
307  fstream::open(name.c_str(), ios::in | ios::out | ios::binary);
308 }
309 
310 void bfstream::open_readonly(const std::string& name, endian e)
311 {
312  if (native_endianity != e)
313  switch_endianity = true;
314  else
315  switch_endianity = false;
316  fstream::open(name.c_str(), ios::in | ios::binary);
317 }
318 
319 int bfstream::length() // in bytes
320 {
321  std::streampos pos1, len;
322  pos1 = tellg();
323  seekg(0, ios::end);
324  len = tellg();
325  seekg(pos1);
326  return int(len);
327 }
328 
330 {
331  put(a);
332  return *this;
333 }
334 
336 {
337  put(static_cast<char>(a));
338  return *this;
339 }
340 
342 {
343  write_endian<bfstream, int16_t>(*this, a, switch_endianity);
344  return *this;
345 }
346 
348 {
349  write_endian<bfstream, uint16_t>(*this, a, switch_endianity);
350  return *this;
351 }
352 
354 {
355  write_endian<bfstream, int32_t>(*this, a, switch_endianity);
356  return *this;
357 }
358 
360 {
361  write_endian<bfstream, uint32_t>(*this, a, switch_endianity);
362  return *this;
363 }
364 
366 {
367  write_endian<bfstream, int64_t>(*this, a, switch_endianity);
368  return *this;
369 }
370 
372 {
373  write_endian<bfstream, uint64_t>(*this, a, switch_endianity);
374  return *this;
375 }
376 
378 {
379  write_endian<bfstream, float>(*this, a, switch_endianity);
380  return *this;
381 }
382 
384 {
385  write_endian<bfstream, double>(*this, a, switch_endianity);
386  return *this;
387 }
388 
390 {
391  write(a, strlen(a) + 1);
392  return *this;
393 }
394 
395 bfstream& bfstream::operator<<(const std::string& a)
396 {
397  write(a.c_str(), a.size() + 1);
398  return *this;
399 }
400 
401 
403 {
404  get(a);
405  return *this;
406 }
407 
408 bfstream& bfstream::operator>>(unsigned char& a)
409 {
410  char tmp;
411  get(tmp);
412  a = tmp;
413  return *this;
414 }
415 
417 {
418  read_endian<bfstream, int16_t>(*this, a, switch_endianity);
419  return *this;
420 }
421 
423 {
424  read_endian<bfstream, uint16_t>(*this, a, switch_endianity);
425  return *this;
426 }
427 
429 {
430  read_endian<bfstream, int32_t>(*this, a, switch_endianity);
431  return *this;
432 }
433 
435 {
436  read_endian<bfstream, uint32_t>(*this, a, switch_endianity);
437  return *this;
438 }
439 
441 {
442  read_endian<bfstream, int64_t>(*this, a, switch_endianity);
443  return *this;
444 }
445 
447 {
448  read_endian<bfstream, uint64_t>(*this, a, switch_endianity);
449  return *this;
450 }
451 
453 {
454  read_endian<bfstream, float>(*this, a, switch_endianity);
455  return *this;
456 }
457 
459 {
460  read_endian<bfstream, double>(*this, a, switch_endianity);
461  return *this;
462 }
463 
465 {
466  getline(a, '\0');
467  return *this;
468 }
469 
471 {
472  std::getline(*this, a, '\0');
473  return *this;
474 }
475 
476 } // namespace itpp
SourceForge Logo

Generated on Fri Mar 21 2014 17:14:12 for IT++ by Doxygen 1.8.1.2