Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

FileIO.h

Go to the documentation of this file.
00001 /****************** <SNX heading BEGIN do not edit this line> *****************
00002  *
00003  * sonix
00004  *
00005  * Original Authors:
00006  *   Kevin Meinert, Carolina Cruz-Neira
00007  *
00008  * -----------------------------------------------------------------
00009  * File:          $RCSfile: FileIO.h,v $
00010  * Date modified: $Date: 2003/12/01 22:25:22 $
00011  * Version:       $Revision: 1.13 $
00012  * -----------------------------------------------------------------
00013  *
00014  ****************** <SNX heading END do not edit this line> ******************/
00015 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
00016  *
00017  * VR Juggler is (C) Copyright 1998-2003 by Iowa State University
00018  *
00019  * Original Authors:
00020  *   Allen Bierbaum, Christopher Just,
00021  *   Patrick Hartling, Kevin Meinert,
00022  *   Carolina Cruz-Neira, Albert Baker
00023  *
00024  * This library is free software; you can redistribute it and/or
00025  * modify it under the terms of the GNU Library General Public
00026  * License as published by the Free Software Foundation; either
00027  * version 2 of the License, or (at your option) any later version.
00028  *
00029  * This library is distributed in the hope that it will be useful,
00030  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00031  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00032  * Library General Public License for more details.
00033  *
00034  * You should have received a copy of the GNU Library General Public
00035  * License along with this library; if not, write to the
00036  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00037  * Boston, MA 02111-1307, USA.
00038  *
00039  *************** <auto-copyright.pl END do not edit this line> ***************/
00040 
00041 
00042 
00043 
00044 
00046 //
00047 //                         -=     CFileIO class     =-
00048 //
00049 // Description: "stuff useful for file i/o most handle endian-ness issues.
00050 //
00052 //
00053 //    $Date: 2003/12/01 22:25:22 $
00054 //    $Revision: 1.13 $
00055 //    Copyright (C) 1998, 1999, 2000  Kevin Meinert, KevinMeinert@bigfoot.com
00056 //
00057 //    This library is free software; you can redistribute it and/or
00058 //    modify it under the terms of the GNU Library General Public
00059 //    License as published by the Free Software Foundation; either
00060 //    version 2 of the License, or (at your option) any later version.
00061 //
00062 //    This library is distributed in the hope that it will be useful,
00063 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00064 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00065 //    Library General Public License for more details.
00066 //
00067 //    You should have received a copy of the GNU Library General Public
00068 //    License along with this library; if not, write to the Free
00069 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00070 //
00072 
00074 #ifndef CFILEIO_INCLUDED
00075 #define CFILEIO_INCLUDED
00076 
00077 #include <string>
00078 #include <vector>
00079 #include <fstream>
00080 #include <stdio.h> // for FILE
00081 
00082 #include <snx/Endian.h>
00083 
00084 namespace snxFileIO
00085 {
00086    //: true - 
00087    bool fileExists( const char* const name );
00088 
00089    int fileSize( const char* const filename );
00090 
00091    void fileLoad( const char* const filename, std::vector<unsigned char>& data );
00092 
00093    enum Endianness
00094    {
00095       LITTLE, BIG
00096    };
00097 
00098    //: Read
00099    // this function is designed to work with little endian files, such as TGA and BMP.
00100    // this will return data that is correctly translated for your system architecture.
00101    // i.e. reading a little endian BMP on a big endian mips system, 
00102    //      returns (translated) bigendian data
00103    // i.e. reading a little endian TGA on a little endian intel system, 
00104    //      returns (untranslated) littleendian data
00105    template<class typeT>
00106    inline int ReadData( Endianness fileByteOrdering, FILE* fp, typeT& data )
00107    {
00108       int size = ::fread( &data, sizeof(typeT), 1, fp );
00109 
00110       // if we're not on a little endian machine (intel is little endian) then reverse the bytes.
00111       if (fileByteOrdering == snxFileIO::LITTLE && vpr::System::isBigEndian() ||
00112          fileByteOrdering == snxFileIO::BIG && vpr::System::isLittleEndian())
00113       {
00114          snxEndian::byteReverse( data );
00115       }
00116 
00117       return size;
00118    }
00119 
00120    //: Write
00121    // this function is designed to work with little endian files, such as TGA and BMP.
00122    template<class typeT>
00123    inline int WriteData( Endianness fileByteOrdering, FILE* fp, const typeT& data )
00124    {
00125       typeT tempData = data;
00126 
00127       // if we're not on a little endian machine (i.e. intel is little endian, mips is big) 
00128       // then reverse the bytes.
00129       if (fileByteOrdering == LITTLE && vpr::System::isBigEndian() ||
00130          fileByteOrdering == BIG && vpr::System::isLittleEndian())
00131       {
00132          snxEndian::byteReverse( tempData );
00133       }
00134          
00135       int size = ::fwrite( &tempData, 1, sizeof(typeT), fp );
00136 
00137       return size;
00138    }
00139 
00140    // Read one byte of data from the file stream
00141    int ReadByte( FILE *fp, unsigned char& value );
00142 
00143    // Read one short of data from the file stream
00144    // NOTE: these enhance readability and type checking, try to use them over the generic ReadData function
00145    int ReadShort( Endianness fileByteOrdering, FILE* fp, unsigned short& value );
00146 
00147    // Read one long of data from the file stream
00148    // NOTE: these enhance readability and type checking, try to use them over the generic ReadData function
00149    int ReadLong( Endianness fileByteOrdering, FILE *fp, unsigned long& value );
00150 
00151    // Write one byte of data to the file stream
00152    int WriteByte( FILE *fp, const unsigned char& value );
00153 
00154    // Write one short of data to the file stream
00155    // NOTE: these enhance readability and type checking, try to use them over the generic WriteData function
00156    int WriteShort( Endianness fileByteOrdering, FILE* fp, const unsigned short& value );
00157 
00158    // Write one long of data to the file stream
00159    // NOTE: these enhance readability and type checking, try to use them over the generic WriteData function
00160    int WriteLong( Endianness fileByteOrdering, FILE *fp, const unsigned long& value );
00161 
00162    void getLine( std::ifstream& f, std::string& text  );
00163 
00164    void getAll( std::ifstream& f, std::string& buffer );
00165 
00166 } //end namespace.
00167 
00168 #endif

Generated on Sun May 2 14:39:26 2004 for Juggler Sonix by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002