00001 /* Copyright (C) 2003-2008 Oliver Lemke <olemke@core-dump.info> 00002 00003 This program is free software; you can redistribute it and/or modify it 00004 under the terms of the GNU General Public License as published by the 00005 Free Software Foundation; either version 2, or (at your option) any 00006 later version. 00007 00008 This program is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 GNU General Public License for more details. 00012 00013 You should have received a copy of the GNU General Public License 00014 along with this program; if not, write to the Free Software 00015 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 00016 USA. */ 00017 00018 00020 // File description 00022 00031 #include <fstream> 00032 #include <stdexcept> 00033 #include "bifstream.h" 00034 00035 void bifstream::seek(long spos, Offset offs) 00036 { 00037 if(!in) { err = NotOpen; return; } 00038 00039 switch(offs) { 00040 case Set: this->seekg(spos, ios::beg); break; 00041 case Add: this->seekg(spos, ios::cur); break; 00042 case End: this->seekg(spos, ios::end); break; 00043 } 00044 } 00045 00046 long bifstream::pos() 00047 { 00048 if(!in) { err = NotOpen; return 0; } 00049 return streamoff (this->tellg()); 00050 } 00051 00052 bifstream::Byte bifstream::getByte() 00053 { 00054 int iread; 00055 00056 if(this->good ()) { 00057 iread = this->get (); 00058 if(iread == EOF) err |= Eof; 00059 return (Byte)iread; 00060 } else { 00061 err |= NotOpen; 00062 throw runtime_error ("Reading from binary file failed"); 00063 return 0; 00064 } 00065 } 00066 00067 00068 /* Overloaded input operators */ 00069 bifstream& operator>> (bifstream& bif, double& n) 00070 { n = (double)bif.readFloat (binio::Double); return (bif); } 00071 00072 bifstream& operator>> (bifstream& bif, float& n) 00073 { n = (float)bif.readFloat (binio::Double); return (bif); } 00074 00075 bifstream& operator>> (bifstream& bif, long& n) 00076 { n = (long)bif.readInt (4); return (bif); } 00077 00078 bifstream& operator>> (bifstream& bif, int& n) 00079 { n = (int)bif.readInt (4); return (bif); } 00080