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(); }
good
const auto good
min quality of good
Definition: CAHitNtupletGeneratorKernelsImpl.h:760
electrons_cff.bool
bool
Definition: electrons_cff.py:366
binary_ifstream::good
bool good() const
stream state checking
Definition: binary_ifstream.cc:89
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
gather_cfg.cout
cout
Definition: gather_cfg.py:144
binary_ifstream::operator>>
binary_ifstream & operator>>(char &n)
Definition: binary_ifstream.cc:27
createJobs.tmp
tmp
align.sh
Definition: createJobs.py:716
binary_ifstream::init
void init(const char *name)
Definition: binary_ifstream.cc:12
Utilities.operator
operator
Definition: Utilities.py:24
binary_ifstream
Definition: binary_ifstream.h:8
binary_ifstream::~binary_ifstream
~binary_ifstream()
Definition: binary_ifstream.cc:20
binary_ifstream.h
binary_ifstream::binary_ifstream
binary_ifstream(const char *name)
Definition: binary_ifstream.cc:8
binary_ifstream::close
void close()
Definition: binary_ifstream.cc:21
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
binary_ifstream::operator!
bool operator!() const
Definition: binary_ifstream.cc:98
binary_ifstream::eof
bool eof() const
Definition: binary_ifstream.cc:91
binary_ifstream::fail
bool fail() const
Definition: binary_ifstream.cc:93
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
binary_ifstream::file_
FILE * file_
Definition: binary_ifstream.h:44
binary_ifstream_error
Definition: binary_ifstream.cc:6
binary_ifstream::bad
bool bad() const
Definition: binary_ifstream.cc:96