CMS 3D CMS Logo

binary_ifstream.cc
Go to the documentation of this file.
2 
3 #include <cstdio>
4 #include <iostream>
5 
8 
9  binary_ifstream::binary_ifstream(const char* name) : file_(nullptr) { init(name); }
10 
11  binary_ifstream::binary_ifstream(const std::string& name) : file_(nullptr) { init(name.c_str()); }
12 
13  void binary_ifstream::init(const char* name) {
14  file_ = fopen(name, "rb");
15  if (file_ == nullptr) {
16  std::cout << "file " << name << " cannot be opened for reading" << std::endl;
17  throw binary_ifstream_error();
18  }
19  }
20 
21  binary_ifstream::binary_ifstream(binary_ifstream&& iOther) : file_(iOther.file_) { iOther.file_ = nullptr; }
22 
25  std::swap(file_, temp.file_);
26  return *this;
27  }
28 
31  if (file_ != nullptr)
32  fclose(file_);
33  file_ = nullptr;
34  }
35 
37  n = static_cast<char>(fgetc(file_));
38  return *this;
39  }
40 
42  n = static_cast<unsigned char>(fgetc(file_));
43  return *this;
44  }
45 
47  fread(&n, sizeof(n), 1, file_);
48  return *this;
49  }
51  fread(&n, sizeof(n), 1, file_);
52  return *this;
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  fread(&n, sizeof(n), 1, file_);
74  return *this;
75  }
77  fread(&n, sizeof(n), 1, file_);
78  return *this;
79  }
80 
82  n = static_cast<bool>(fgetc(file_));
83  return *this;
84  }
85 
87  unsigned int nchar;
88  (*this) >> nchar;
89  char* tmp = new char[nchar + 1];
90  unsigned int nread = fread(tmp, 1, nchar, file_);
91  if (nread != nchar)
92  std::cout << "binary_ifstream error: read less then expected " << std::endl;
93  n.assign(tmp, nread);
94  delete[] tmp;
95  return *this;
96  }
97 
98  bool binary_ifstream::good() const { return !bad() && !eof(); }
99 
100  bool binary_ifstream::eof() const { return feof(file_); }
101 
102  bool binary_ifstream::fail() const { return file_ == nullptr || ferror(file_) != 0; }
103 
104  // don't know the difference between fail() and bad() (yet)
105  bool binary_ifstream::bad() const { return fail(); }
106 
107  bool binary_ifstream::operator!() const { return fail() || bad() || eof(); }
108 
109  //binary_ifstream::operator bool() const {return !fail() && !bad();}
110 
111  binary_ifstream::operator bool() const { return good(); }
112 
113  long binary_ifstream::tellg() { return ftell(file_); }
114 
116  //if there is a problem, calling fail() will return true;
117  fseek(file_, to, SEEK_SET);
118  return *this;
119  }
120 
121 } // namespace magneticfield::interpolation
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:112
bool good() const
stream state checking
binary_ifstream & operator=(const binary_ifstream &)=delete
tmp
align.sh
Definition: createJobs.py:716
def move(src, dest)
Definition: eostools.py:511