Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-06-06 07:38:45

0001 #include "MagneticField/Interpolation/interface/binary_ifstream.h"
0002 
0003 #include <cstdio>
0004 #include <iostream>
0005 
0006 namespace magneticfield::interpolation {
0007   struct binary_ifstream_error {};
0008 
0009   binary_ifstream::binary_ifstream(const char* name) : file_(nullptr) { init(name); }
0010 
0011   binary_ifstream::binary_ifstream(const std::string& name) : file_(nullptr) { init(name.c_str()); }
0012 
0013   void binary_ifstream::init(const char* name) {
0014     file_ = fopen(name, "rb");
0015     if (file_ == nullptr) {
0016       std::cout << "file " << name << " cannot be opened for reading" << std::endl;
0017       throw binary_ifstream_error();
0018     }
0019   }
0020 
0021   binary_ifstream::binary_ifstream(binary_ifstream&& iOther) : file_(iOther.file_) { iOther.file_ = nullptr; }
0022 
0023   binary_ifstream& binary_ifstream::operator=(binary_ifstream&& iOther) {
0024     binary_ifstream temp{std::move(iOther)};
0025     std::swap(file_, temp.file_);
0026     return *this;
0027   }
0028 
0029   binary_ifstream::~binary_ifstream() { close(); }
0030   void binary_ifstream::close() {
0031     if (file_ != nullptr)
0032       fclose(file_);
0033     file_ = nullptr;
0034   }
0035 
0036   binary_ifstream& binary_ifstream::operator>>(char& n) {
0037     n = static_cast<char>(fgetc(file_));
0038     return *this;
0039   }
0040 
0041   binary_ifstream& binary_ifstream::operator>>(unsigned char& n) {
0042     n = static_cast<unsigned char>(fgetc(file_));
0043     return *this;
0044   }
0045 
0046   binary_ifstream& binary_ifstream::operator>>(short& n) {
0047     fread(&n, sizeof(n), 1, file_);
0048     return *this;
0049   }
0050   binary_ifstream& binary_ifstream::operator>>(unsigned short& n) {
0051     fread(&n, sizeof(n), 1, file_);
0052     return *this;
0053   }
0054   binary_ifstream& binary_ifstream::operator>>(int& n) {
0055     fread(&n, sizeof(n), 1, file_);
0056     return *this;
0057   }
0058   binary_ifstream& binary_ifstream::operator>>(unsigned int& n) {
0059     fread(&n, sizeof(n), 1, file_);
0060     return *this;
0061   }
0062 
0063   binary_ifstream& binary_ifstream::operator>>(long& n) {
0064     fread(&n, sizeof(n), 1, file_);
0065     return *this;
0066   }
0067   binary_ifstream& binary_ifstream::operator>>(unsigned long& n) {
0068     fread(&n, sizeof(n), 1, file_);
0069     return *this;
0070   }
0071 
0072   binary_ifstream& binary_ifstream::operator>>(float& n) {
0073     fread(&n, sizeof(n), 1, file_);
0074     return *this;
0075   }
0076   binary_ifstream& binary_ifstream::operator>>(double& n) {
0077     fread(&n, sizeof(n), 1, file_);
0078     return *this;
0079   }
0080 
0081   binary_ifstream& binary_ifstream::operator>>(bool& n) {
0082     n = static_cast<bool>(fgetc(file_));
0083     return *this;
0084   }
0085 
0086   binary_ifstream& binary_ifstream::operator>>(std::string& n) {
0087     unsigned int nchar;
0088     (*this) >> nchar;
0089     char* tmp = new char[nchar + 1];
0090     unsigned int nread = fread(tmp, 1, nchar, file_);
0091     if (nread != nchar)
0092       std::cout << "binary_ifstream error: read less then expected " << std::endl;
0093     n.assign(tmp, nread);
0094     delete[] tmp;
0095     return *this;
0096   }
0097 
0098   bool binary_ifstream::good() const { return !bad() && !eof(); }
0099 
0100   bool binary_ifstream::eof() const { return feof(file_); }
0101 
0102   bool binary_ifstream::fail() const { return file_ == nullptr || ferror(file_) != 0; }
0103 
0104   // don't know the difference between fail() and bad() (yet)
0105   bool binary_ifstream::bad() const { return fail(); }
0106 
0107   bool binary_ifstream::operator!() const { return fail() || bad() || eof(); }
0108 
0109   //binary_ifstream::operator bool() const {return !fail() && !bad();}
0110 
0111   binary_ifstream::operator bool() const { return good(); }
0112 
0113   long binary_ifstream::tellg() { return ftell(file_); }
0114 
0115   binary_ifstream& binary_ifstream::seekg(long to) {
0116     //if there is a problem, calling fail() will return true;
0117     fseek(file_, to, SEEK_SET);
0118     return *this;
0119   }
0120 
0121 }  // namespace magneticfield::interpolation