Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

EndianIn.cpp

Go to the documentation of this file.
00001 
00002 /*
00003     TEDDY - General graphics application library
00004     Copyright (C) 1999-2002  Timo Suoranta
00005     tksuoran@cc.helsinki.fi
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2.1 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     $Id: EndianIn.cpp,v 1.4 2002/01/11 14:35:03 tksuoran Exp $
00022 */
00023 
00024 
00025 #include "Teddy/SysSupport/EndianIn.h"
00026 #include "Teddy/SysSupport/Exception.h"
00027 
00028 
00029 namespace Teddy      {
00030 namespace SysSupport {
00031 
00032 
00034     EndianIn::EndianIn( const std::string &file_name ){
00035     open( file_name );
00036 }
00037 
00038 
00040 EndianIn::~EndianIn(){
00041     close();
00042 }
00043 
00044 
00046 void EndianIn::open( const std::string &file_name ){
00047     ifs = new ifstream( file_name.c_str(), ios::in|ios::binary );
00048     /*
00049     cout << "Opened file " << file_name;
00050     cout << " for reading. Handle " << (long)ifs;
00051     cout << " Good status " << ifs->good();
00052     cout << " Is Open " << ifs->is_open();
00053     cout << " tellg " << ifs->tellg();
00054     cout << endl;
00055     if( ifs->tellg()!=0 ){
00056         cout << "Hmm, seems like we are not at the beginning of the stream. Rewinding." << endl;
00057         ifs->seekg(0,ios::beg);
00058         cout << "Final tellg " << ifs->tellg() << endl;
00059     }
00060     */
00061 }
00062 
00063 
00065 void EndianIn::close(){
00066     //cout << "Closing file at pos " << ifs->tellg() << endl;
00067     if( ifs != NULL ){
00068         ifs->close(), ifs = NULL;
00069         delete ifs;
00070         ifs = NULL;
00071     }
00072 }
00073 
00074 
00076 unsigned long EndianIn::len(){
00077     streampos original = ifs->tellg();
00078     ifs->seekg( 0, ios::end );
00079     unsigned long l = ifs->tellg();
00080     ifs->seekg( original, ios::beg );
00081     return l;
00082 }
00083 
00084 
00085 void EndianIn::read_all( char *buf ){
00086     ifs->read( buf, len() );
00087 }
00088 
00089 
00091 unsigned char EndianIn::read_byte(){
00092     int            C;
00093     unsigned char  c = 0;
00094 
00095     C = ifs->get();
00096     if( C == EOF ){
00097         throw( Exception("EOF read error") );
00098     }else{
00099         c = (unsigned char)C;
00100     }
00101     return c;
00102 }
00103 
00104 
00106 unsigned short int EndianIn::read_short(){
00107     unsigned char c1, c2;
00108 
00109     c1 = read_byte();
00110     c2 = read_byte();
00111 
00112     if( q_MSBfirst() ){
00113         return (c1 << 8) | c2;
00114     }else{
00115         return (c2 << 8) | c1;
00116     }
00117 }
00118 
00119 
00121 unsigned long int EndianIn::read_long(){
00122     unsigned char c1, c2, c3, c4;
00123 
00124     c1 = read_byte();
00125     c2 = read_byte();
00126     c3 = read_byte();
00127     c4 = read_byte();   
00128 
00129     if ( q_MSBfirst() ){
00130         return (c1 << 24) | (c2 << 16) | (c3 << 8) | c4;
00131     }else{
00132         return (c4 << 24) | (c3 << 16) | (c2 << 8) | c1;
00133     }
00134 }
00135 
00136 
00138 float EndianIn::read_float(){
00139     float          ret_val;
00140     long          *shadow = (long*)(&ret_val);
00141     unsigned char  c1, c2, c3, c4;
00142 
00143     c1 = read_byte();
00144     c2 = read_byte();
00145     c3 = read_byte();
00146     c4 = read_byte();   
00147 
00148     if ( q_MSBfirst() ){
00149         *shadow = (c1 << 24) | (c2 << 16) | (c3 << 8) | c4;
00150     }else{
00151         *shadow = (c4 << 24) | (c3 << 16) | (c2 << 8) | c1;
00152     }
00153 
00154     return ret_val;
00155 }
00156 
00157 
00158 };  //  namespace SysSupport
00159 };  //  namespace Teddy
00160 
00161 
00162