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

FileIO.cpp

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.cpp,v $
00010  * Date modified: $Date: 2003/03/05 16:32:39 $
00011  * Version:       $Revision: 1.5 $
00012  * -----------------------------------------------------------------
00013  *
00014  ****************** <SNX heading END do not edit this line> ******************/
00015 
00016 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
00017  *
00018  * VR Juggler is (C) Copyright 1998-2003 by Iowa State University
00019  *
00020  * Original Authors:
00021  *   Allen Bierbaum, Christopher Just,
00022  *   Patrick Hartling, Kevin Meinert,
00023  *   Carolina Cruz-Neira, Albert Baker
00024  *
00025  * This library is free software; you can redistribute it and/or
00026  * modify it under the terms of the GNU Library General Public
00027  * License as published by the Free Software Foundation; either
00028  * version 2 of the License, or (at your option) any later version.
00029  *
00030  * This library is distributed in the hope that it will be useful,
00031  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00032  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00033  * Library General Public License for more details.
00034  *
00035  * You should have received a copy of the GNU Library General Public
00036  * License along with this library; if not, write to the
00037  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00038  * Boston, MA 02111-1307, USA.
00039  *
00040  *************** <auto-copyright.pl END do not edit this line> ***************/
00041 
00042 #include <stdio.h>
00043 #include <stdlib.h>
00044 #include <assert.h>
00045 #include <fstream>
00046 
00047 #include <snx/FileIO.h>
00048 
00049 namespace snxFileIO
00050 {
00051 
00052 bool fileExists( const char* const name )
00053 {
00054    if (name == NULL) return false;
00055       
00056    FILE* file = ::fopen( name, "r" );
00057    if (file == NULL)
00058    {
00059       return false;
00060    }
00061    else
00062    {
00063       ::fclose( file );
00064       return true;
00065    }
00066 }
00067    
00068 int fileSize( const char* const filename )
00069 {
00070    if (filename == NULL) return 0;
00071    
00072    if (!fileExists( filename ))
00073       return 0;
00074 
00075    FILE* fh = fopen( filename, "rb" );
00076    assert( fh != NULL ); 
00077 
00078    if ( fseek(fh, 0, SEEK_END) == 0 )
00079    {
00080       return ftell(fh);
00081    }
00082    else
00083    {
00084       return 0;
00085    }
00086 }
00087 
00088 void fileLoad( const char* const filename, std::vector<unsigned char>& data )
00089 {
00090    if (filename == NULL) return;
00091    
00092    if (!fileExists( filename ))
00093       return;
00094 
00095    int size = fileSize( filename );
00096    data.resize( size );
00097    
00098    FILE* fh = fopen( filename, "rb" );
00099    unsigned int file_length = fread( &data[0], 1, data.size(), fh );
00100    assert( file_length == data.size() );
00101    fclose( fh );
00102 }
00103 
00104 // Read one byte of data from the file stream
00105 int ReadByte( FILE *fp, unsigned char& value )
00106 {
00107    return ::fread( &value, 1, sizeof(unsigned char), fp );
00108 }
00109 
00110 // Read one short of data from the file stream
00111 // NOTE: these enhance readability and type checking, try to use them over the generic ReadData function
00112 int ReadShort( Endianness fileByteOrdering, FILE* fp, unsigned short& value )
00113 {
00114    return ReadData( fileByteOrdering, fp, value );
00115 }
00116 
00117 // Read one long of data from the file stream
00118 // NOTE: these enhance readability and type checking, try to use them over the generic ReadData function
00119 int ReadLong( Endianness fileByteOrdering, FILE *fp, unsigned long& value )
00120 {
00121    return ReadData( fileByteOrdering, fp, value );
00122 }
00123 
00124 // Write one byte of data to the file stream
00125 int WriteByte( FILE *fp, const unsigned char& value )
00126 {
00127    return ::fwrite( &value, 1, sizeof(unsigned char), fp );
00128 }
00129 
00130 // Write one short of data to the file stream
00131 // NOTE: these enhance readability and type checking, try to use them over the generic WriteData function
00132 int WriteShort( Endianness fileByteOrdering, FILE* fp, const unsigned short& value )
00133 {
00134    return WriteData( fileByteOrdering, fp, value );
00135 }
00136 
00137 // Write one long of data to the file stream
00138 // NOTE: these enhance readability and type checking, try to use them over the generic WriteData function
00139 int WriteLong( Endianness fileByteOrdering, FILE *fp, const unsigned long& value )
00140 {
00141    return WriteData( fileByteOrdering, fp, value );
00142 }
00143 
00144 void getLine( std::ifstream& f, std::string& text  )
00145 {
00146    char buffer[2049];
00147    f.getline( buffer, 2048, '\n' );
00148    buffer[2048] = '\0';
00149    if (f.gcount() < 2048)
00150    {
00151       buffer[f.gcount()] = '\0';
00152    }
00153    text = buffer;
00154 }
00155 
00156 void getAll( std::ifstream& f, std::string& buffer )
00157 {
00158    //cout<<"Reading:["<<flush;
00159    while ((f.eof() == false) && (f.fail() == 0) )
00160    //while (f.gcount() >= 2048)
00161    {
00162       //cout<<"."<<flush;
00163       char buf[2049];
00164       f.read( buf, 2048 );
00165       buf[2048] = '\0';
00166       if (f.gcount() < 2048)
00167       {
00168          buf[f.gcount()] = '\0';
00169       }
00170       buffer += buf;
00171    }
00172    //cout<<"]\n"<<flush;
00173    //cout << "Gcount == " << f.gcount() << "\n"<<flush;
00174 }
00175 
00176 } // End of snx namespace

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