00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
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
00105 int ReadByte( FILE *fp, unsigned char& value )
00106 {
00107 return ::fread( &value, 1, sizeof(unsigned char), fp );
00108 }
00109
00110
00111
00112 int ReadShort( Endianness fileByteOrdering, FILE* fp, unsigned short& value )
00113 {
00114 return ReadData( fileByteOrdering, fp, value );
00115 }
00116
00117
00118
00119 int ReadLong( Endianness fileByteOrdering, FILE *fp, unsigned long& value )
00120 {
00121 return ReadData( fileByteOrdering, fp, value );
00122 }
00123
00124
00125 int WriteByte( FILE *fp, const unsigned char& value )
00126 {
00127 return ::fwrite( &value, 1, sizeof(unsigned char), fp );
00128 }
00129
00130
00131
00132 int WriteShort( Endianness fileByteOrdering, FILE* fp, const unsigned short& value )
00133 {
00134 return WriteData( fileByteOrdering, fp, value );
00135 }
00136
00137
00138
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
00159 while ((f.eof() == false) && (f.fail() == 0) )
00160
00161 {
00162
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
00173
00174 }
00175
00176 }