00001 #include "MagneticField/Interpolation/src/binary_ifstream.h"
00002
00003 #include <cstdio>
00004 #include <iostream>
00005
00006 struct binary_ifstream_error {};
00007
00008 binary_ifstream::binary_ifstream( const char* name) : file_(0)
00009 {
00010 init (name);
00011 }
00012
00013 binary_ifstream::binary_ifstream( const std::string& name) : file_(0)
00014 {
00015 init (name.c_str());
00016 }
00017
00018 void binary_ifstream::init( const char* name)
00019 {
00020 file_ = fopen( name, "rb");
00021 if (file_ == 0) {
00022 std::cout << "file " << name << " cannot be opened for reading"
00023 << std::endl;
00024 throw binary_ifstream_error();
00025 }
00026 }
00027
00028 binary_ifstream::~binary_ifstream()
00029 {
00030 close();
00031 }
00032 void binary_ifstream::close()
00033 {
00034 if (file_ != 0) fclose( file_);
00035 file_ = 0;
00036 }
00037
00038 binary_ifstream& binary_ifstream::operator>>( char& n) {
00039 n = static_cast<char>(fgetc(file_));
00040 return *this;
00041 }
00042
00043 binary_ifstream& binary_ifstream::operator>>( unsigned char& n) {
00044 n = static_cast<unsigned char>(fgetc(file_));
00045 return *this;
00046 }
00047
00048 binary_ifstream& binary_ifstream::operator>>( short& n) {
00049 fread( &n, sizeof(n), 1, file_); return *this;}
00050 binary_ifstream& binary_ifstream::operator>>( unsigned short& n) {
00051 fread( &n, sizeof(n), 1, file_); return *this;}
00052 binary_ifstream& binary_ifstream::operator>>( int& n) {
00053 fread( &n, sizeof(n), 1, file_); return *this;}
00054 binary_ifstream& binary_ifstream::operator>>( unsigned int& n) {
00055 fread( &n, sizeof(n), 1, file_); return *this;}
00056
00057 binary_ifstream& binary_ifstream::operator>>( long& n) {
00058 fread( &n, sizeof(n), 1, file_); return *this;}
00059 binary_ifstream& binary_ifstream::operator>>( unsigned long& n) {
00060 fread( &n, sizeof(n), 1, file_); return *this;}
00061
00062 binary_ifstream& binary_ifstream::operator>>( float& n) {
00063 fread( &n, sizeof(n), 1, file_); return *this;}
00064 binary_ifstream& binary_ifstream::operator>>( double& n) {
00065 fread( &n, sizeof(n), 1, file_); return *this;}
00066
00067 binary_ifstream& binary_ifstream::operator>>( bool& n) {
00068 n = static_cast<bool>(fgetc(file_));
00069 return *this;
00070 }
00071
00072 binary_ifstream& binary_ifstream::operator>>( std::string& n) {
00073 unsigned int nchar;
00074 (*this) >> nchar;
00075 char* tmp = new char[nchar+1];
00076 unsigned int nread = fread( tmp, 1, nchar, file_);
00077 if (nread != nchar) std::cout << "binary_ifstream error: read less then expected " << std::endl;
00078 n.assign( tmp, nread);
00079 delete[] tmp;
00080 return *this;
00081 }
00082
00083 bool binary_ifstream::good() const
00084 {
00085 return !bad() && !eof();
00086 }
00087
00088 bool binary_ifstream::eof() const
00089 {
00090 return feof( file_);
00091 }
00092
00093 bool binary_ifstream::fail() const
00094 {
00095 return file_ == 0 || ferror( file_) != 0;
00096 }
00097
00098
00099 bool binary_ifstream::bad() const {return fail();}
00100
00101 bool binary_ifstream::operator!() const {return fail() || bad() || eof();}
00102
00103
00104
00105 binary_ifstream::operator bool() const {
00106 return good();
00107 }