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

EndianOut.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: EndianOut.cpp,v 1.4 2002/01/11 14:35:03 tksuoran Exp $
00022 */
00023 
00024 
00025 #include "Teddy/SysSupport/EndianOut.h"
00026 #include "Teddy/SysSupport/Exception.h"
00027 
00028 
00029 namespace Teddy      {
00030 namespace SysSupport {
00031 
00032 
00034 EndianOut::EndianOut( const char *file_name ){
00035     open( file_name );
00036 }
00037 
00038 
00040 EndianOut::~EndianOut(){
00041     close();
00042 }
00043 
00044 
00046 void EndianOut::open( const char *name ){
00047     ofs = new ofstream( name,ios::out|ios::trunc|ios::binary );
00048 }
00049 
00050 
00052 void EndianOut::close(){
00053     ofs->close(), ofs = NULL;
00054 }
00055 
00056 
00058 void EndianOut::write_byte( const int item ){
00059     if( !ofs->put((unsigned char)item) ){
00060         throw( Exception("EOF write error") );
00061     }
00062 }
00063 
00064 
00066 void EndianOut::write_short( const unsigned short item ){
00067     if( q_MSBfirst() ){
00068         write_byte((item>>8) & 0xff ),
00069         write_byte(item & 0xff );
00070     }else{
00071         write_byte(item & 0xff ),
00072         write_byte((item>>8) & 0xff );
00073     }
00074 }
00075 
00076 
00078 void EndianOut::write_long( const unsigned long item ){
00079     unsigned int t = item;
00080     int i;
00081 
00082     if( q_MSBfirst() ){
00083         for(i=24; i>=0; i-=8){
00084             write_byte((t >> i) & 0xff );
00085         }
00086     }else{
00087         for(i=0; i<4; i++){
00088             write_byte(t & 0xff ), t >>= 8;
00089         }
00090     }
00091 }
00092 
00093 
00095 void EndianOut::write_float( const float item ){
00096     unsigned int t = *(unsigned int *)(&item);
00097     int i;
00098 
00099     if( q_MSBfirst() ){
00100         for(i=24; i>=0; i-=8){
00101             write_byte((t >> i) & 0xff );
00102         }
00103     }else{
00104         for(i=0; i<4; i++){
00105             write_byte(t & 0xff ), t >>= 8;
00106         }
00107     }
00108 }
00109 
00110 
00111 };  //  namespace SysSupport
00112 };  //  namespace Teddy
00113