CMS 3D CMS Logo

binary_ifstream.cc
Go to the documentation of this file.
1 #include "binary_ifstream.h"
2 
3 #include <cstdio>
4 #include <iostream>
5 
7 
8 binary_ifstream::binary_ifstream(const char* name) : file_(nullptr) { init(name); }
9 
10 binary_ifstream::binary_ifstream(const std::string& name) : file_(nullptr) { init(name.c_str()); }
11 
12 void binary_ifstream::init(const char* name) {
13  file_ = fopen(name, "rb");
14  if (file_ == nullptr) {
15  std::cout << "file " << name << " cannot be opened for reading" << std::endl;
16  throw binary_ifstream_error();
17  }
18 }
19 
22  if (file_ != nullptr)
23  fclose(file_);
24  file_ = nullptr;
25 }
26 
28  n = static_cast<char>(fgetc(file_));
29  return *this;
30 }
31 
33  n = static_cast<unsigned char>(fgetc(file_));
34  return *this;
35 }
36 
38  fread(&n, sizeof(n), 1, file_);
39  return *this;
40 }
42  fread(&n, sizeof(n), 1, file_);
43  return *this;
44 }
46  fread(&n, sizeof(n), 1, file_);
47  return *this;
48 }
50  fread(&n, sizeof(n), 1, file_);
51  return *this;
52 }
53 
55  fread(&n, sizeof(n), 1, file_);
56  return *this;
57 }
59  fread(&n, sizeof(n), 1, file_);
60  return *this;
61 }
62 
64  fread(&n, sizeof(n), 1, file_);
65  return *this;
66 }
68  fread(&n, sizeof(n), 1, file_);
69  return *this;
70 }
71 
73  n = static_cast<bool>(fgetc(file_));
74  return *this;
75 }
76 
78  unsigned int nchar;
79  (*this) >> nchar;
80  char* tmp = new char[nchar + 1];
81  unsigned int nread = fread(tmp, 1, nchar, file_);
82  if (nread != nchar)
83  std::cout << "binary_ifstream error: read less then expected " << std::endl;
84  n.assign(tmp, nread);
85  delete[] tmp;
86  return *this;
87 }
88 
89 bool binary_ifstream::good() const { return !bad() && !eof(); }
90 
91 bool binary_ifstream::eof() const { return feof(file_); }
92 
93 bool binary_ifstream::fail() const { return file_ == nullptr || ferror(file_) != 0; }
94 
95 // don't know the difference between fail() and bad() (yet)
96 bool binary_ifstream::bad() const { return fail(); }
97 
98 bool binary_ifstream::operator!() const { return fail() || bad() || eof(); }
99 
100 //binary_ifstream::operator bool() const {return !fail() && !bad();}
101 
102 binary_ifstream::operator bool() const { return good(); }
bool operator!() const
bool fail() const
bool bad() const
bool eof() const
bool good() const
stream state checking
auto const good
min quality of good
binary_ifstream(const char *name)
binary_ifstream & operator>>(char &n)
void init(const char *name)
tmp
align.sh
Definition: createJobs.py:716