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
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #if !defined(ALIZE_FileReader_cpp)
00056 #define ALIZE_FileReader_cpp
00057
00058 #if defined(_WIN32)
00059 #define _CRT_SECURE_NO_WARNINGS
00060 #endif
00061
00062 #include <new>
00063 #include "FileReader.h"
00064 #include "Exception.h"
00065 #include "RealVector.h"
00066
00067
00068 using namespace alize;
00069 typedef FileReader R;
00070
00071
00072 R::FileReader(const FileName& f, const String& path,
00073 const String& extension, bool swap)
00074 :Object(), _fullFileName(path + f + extension), _pFileStruct(NULL),
00075 _fileName(f), _path(path), _extension(extension),
00076 _fileLengthDefined(false), _swap(swap) {}
00077
00078 R& R::create(const FileName& f, const String& path, const String& ext,
00079 bool swap)
00080 {
00081 R* p = new (std::nothrow) R(f, path, ext, swap);
00082 assertMemoryIsAllocated(p, __FILE__, __LINE__);
00083 return *p;
00084 }
00085
00086 bool R::isClosed() const { return _pFileStruct == NULL; }
00087
00088 bool R::isOpen() const { return _pFileStruct != NULL; }
00089
00090 void R::reset()
00091 {
00092 if (isOpen())
00093 seek(0);
00094 }
00095
00096 void R::open()
00097 {
00098 close();
00099 _pFileStruct = ::fopen(_fullFileName.c_str(),"rb");
00100 if (_pFileStruct == NULL)
00101 throw FileNotFoundException("", __FILE__, __LINE__, _fullFileName);
00102 }
00103
00104 void R::close()
00105 {
00106 if (isOpen())
00107 if (::fclose(_pFileStruct) == EOF)
00108 throw IOException("Cannot close file", __FILE__, __LINE__,
00109 _fullFileName);
00110 _pFileStruct = NULL;
00111 }
00112
00113 const FileName& R::getFullFileName() const { return _fullFileName; }
00114
00115 const FileName& R::getFileName() const { return _fileName; }
00116
00117 unsigned long R::getFileLength()
00118 {
00119 if (!_fileLengthDefined)
00120 {
00121 bool wasOpened = isOpen();
00122 unsigned long pos;
00123 long l;
00124
00125 if (wasOpened)
00126 {
00127 if ( (pos = ::ftell(_pFileStruct)) < 0)
00128 throw IOException("ftell", __FILE__, __LINE__, _fullFileName);
00129 }
00130 else
00131 {
00132 open();
00133 pos = 0;
00134 }
00135 if (::fseek(_pFileStruct, 0, SEEK_END) != 0)
00136 throw IOException("fseek", __FILE__, __LINE__, _fullFileName);
00137 if ( (l = ::ftell(_pFileStruct)) < 0)
00138 throw IOException("ftell", __FILE__, __LINE__, _fullFileName);
00139 _fileLengthDefined = true;
00140 _fileLength = l;
00141 if (::fseek(_pFileStruct, pos, SEEK_SET) != 0)
00142 throw IOException("fseek", __FILE__, __LINE__, _fullFileName);
00143 }
00144 return _fileLength;
00145 }
00146
00147 void R::seek(unsigned long pos)
00148 {
00149 if (!isOpen())
00150 open();
00151 if (::fseek(_pFileStruct, pos, SEEK_SET) != 0 )
00152 throw IOException("seek out of bounds",
00153 __FILE__, __LINE__, _fullFileName);
00154 }
00155
00156 void R::read(void* buffer, unsigned long length)
00157 {
00158 assert(buffer != NULL);
00159 if (isClosed())
00160 open();
00161 if (::fread(buffer, 1, length, _pFileStruct) == length)
00162 return;
00163
00164
00165 if (feof(_pFileStruct))
00166 throw EOFException("", __FILE__, __LINE__, _fullFileName);
00167
00168
00169 throw IOException("Cannot read file", __FILE__, __LINE__, _fullFileName);
00170 }
00171
00172 char R::readChar()
00173 {
00174 char byte;
00175 read(&byte, 1);
00176 return byte;
00177 }
00178
00179 int R::readInt2()
00180 {
00181 if (sizeof(short) == 2)
00182 {
00183 short s;
00184 read(&s, 2);
00185 if (_swap)
00186 swap2Bytes(&s, &s);
00187 return s;
00188 }
00189 else
00190 return 0;
00191 }
00192
00193 long R::readInt4()
00194 {
00195
00196
00197 if (sizeof(long) == 4)
00198 {
00199 long s;
00200 read(&s, 4);
00201 if (_swap)
00202 swap4Bytes(&s);
00203 return s;
00204 }
00205 else if (sizeof(int) == 4)
00206 {
00207 int s;
00208 read(&s, 4);
00209 if (_swap)
00210 swap4Bytes(&s);
00211 return (long)s;
00212 }
00213 else
00214 return 0;
00215 }
00216
00217 unsigned long R::readUInt4()
00218 {
00219
00220
00221 if (sizeof(unsigned long) == 4)
00222 {
00223 unsigned long s;
00224 read(&s, 4);
00225 if (_swap)
00226 swap4Bytes(&s);
00227 return s;
00228 }
00229 if (sizeof(unsigned int) == 4)
00230 {
00231 unsigned int s;
00232 read(&s, 4);
00233 if (_swap)
00234 swap4Bytes(&s);
00235 return (unsigned long)s;
00236 }
00237 return 0;
00238 }
00239
00240 double R::readDouble()
00241 {
00242 double s;
00243 read(&s, 8);
00244 if (_swap)
00245 swap8Bytes(&s, &s);
00246 return s;
00247 }
00248
00249 FloatVector& R::readFloats(FloatVector& v)
00250 {
00251 if (_swap)
00252 {
00253 float* array = v.getArray();
00254 unsigned long n = v.size();
00255 read(array, n*4);
00256 for (unsigned long i=0; i<n; i++)
00257 swap4Bytes(&array[i]);
00258 }
00259 else
00260 read(v.getArray(), v.size()*4);
00261 return v;
00262 }
00263
00264 unsigned long R::readSomeFloats(FloatVector& v)
00265 {
00266
00267
00268
00269
00270 if (isClosed())
00271 open();
00272 float* array = v.getArray();
00273 unsigned long n = (unsigned long)(::fread(array, 4, v.size(), _pFileStruct));
00274 if (_swap)
00275 {
00276 char* p = (char*)array;
00277 char t;
00278 for (unsigned long i=0; i<n; i++)
00279 {
00280 t = p[3]; p[3] = p[0]; p[0] = t;
00281 t = p[2]; p[2] = p[1]; p[1] = t;
00282 p += 4;
00283 }
00284 }
00285 return n;
00286 }
00287
00288 float R::readFloat()
00289 {
00290 float s;
00291 read(&s, 4);
00292 if (_swap)
00293 swap4Bytes(&s);
00294 return s;
00295 }
00296
00297 const String& R::readString(unsigned long length)
00298 {
00299 _string.reset();
00300 if (length != 0)
00301 {
00302 char* str = new (std::nothrow) char[length+1];
00303 assertMemoryIsAllocated(str, __FILE__, __LINE__);
00304 try { read(str, length); }
00305 catch (Exception&)
00306 {
00307 delete [] str;
00308 throw;
00309 }
00310 str[length] = 0;
00311 _string = str;
00312 delete [] str;
00313 }
00314 return _string;
00315 }
00316
00317 const String& R::readLine()
00318 {
00319
00320 char t[2];
00321 t[1] = 0;
00322 _string.reset();
00323
00324 while (true)
00325 {
00326 try { read(&t, 1); }
00327 catch (EOFException&)
00328 {
00329 if (_string.length() != 0)
00330 return _string;
00331 throw;
00332 }
00333
00334 if (t[0] == '\n')
00335 return _string;
00336 if (t[0] == '\r')
00337 {
00338 try { read(&t, 1); }
00339 catch (EOFException&)
00340 {
00341 if (_string.length() != 0)
00342 return _string;
00343 throw;
00344 }
00345 if (t[0] == '\n')
00346 return _string;
00347 }
00348 _string += t;
00349 }
00350 }
00351
00352 void R::rewind() {seek(0);}
00353
00354 long R::tell()
00355 {
00356 if (isClosed())
00357 open();
00358 return ::ftell(_pFileStruct);
00359 }
00360
00361 bool& R::swap() { return _swap; }
00362
00363 void R::swap2Bytes(void *src, void *dest)
00364 {
00365 char *p1,*p2;
00366 char tmp;
00367
00368 p1 = (char*)src;
00369 p2 = (char*)dest;
00370 tmp = p1[1];
00371 p2[1] = p1[0];
00372 p2[0] = tmp;
00373 }
00374
00375 void R::swap4Bytes(void *src, void *dest)
00376 {
00377 char *p1,*p2;
00378 char tmp;
00379
00380 p1 = (char*)src;
00381 p2 = (char*)dest;
00382 tmp = p1[3];
00383 p2[3] = p1[0];
00384 p2[0] = tmp;
00385 tmp = p1[2];
00386 p2[2] = p1[1];
00387 p2[1] = tmp;
00388 }
00389
00390 void R::swap4Bytes(void *src)
00391 {
00392 char* p = (char*)src;
00393 char t;
00394 t = p[3]; p[3] = p[0]; p[0] = t;
00395 t = p[2]; p[2] = p[1]; p[1] = t;
00396 }
00397
00398 void R::swap8Bytes(void *src, void *dest)
00399 {
00400 char *p1,*p2;
00401 char tmp;
00402 int i;
00403
00404 p1 = (char*)src;
00405 p2 = (char*)dest;
00406 for (i=0; i<4; i++)
00407 {
00408 tmp = p1[i];
00409 p1[i] = p2[7-i];
00410 p2[7-i] = tmp;
00411 }
00412 }
00413
00414 String R::toString() const
00415 {
00416 return Object::toString()
00417 + "\n full file name = '" + getFullFileName();
00418 }
00419
00420 String R::getClassName() const { return "FileReader"; }
00421
00422 R::~FileReader() { close(); }
00423
00424
00425 #endif // !defined(ALIZE_FileReader_cpp)
00426