00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef OVR_File_h
00023 #define OVR_File_h
00024
00025 #include "OVR_RefCount.h"
00026 #include "OVR_Std.h"
00027 #include "OVR_Alg.h"
00028
00029 #include <stdio.h>
00030 #include "OVR_String.h"
00031
00032 namespace OVR {
00033
00034
00035 class FileConstants;
00036 class File;
00037 class DelegatedFile;
00038 class BufferedFile;
00039
00040
00041
00042
00043 class FileConstants
00044 {
00045 public:
00046
00047
00048 enum OpenFlags
00049 {
00050 Open_Read = 1,
00051 Open_Write = 2,
00052 Open_ReadWrite = 3,
00053
00054
00055
00056
00057
00058 Open_Truncate = 4,
00059
00060
00061
00062
00063 Open_Create = 8,
00064
00065
00066 Open_CreateOnly = 24,
00067
00068
00069 Open_Buffered = 32
00070 };
00071
00072
00073 enum Modes
00074 {
00075 Mode_Read = 0444,
00076 Mode_Write = 0222,
00077 Mode_Execute = 0111,
00078
00079 Mode_ReadWrite = 0666
00080 };
00081
00082
00083 enum SeekOps
00084 {
00085 Seek_Set = 0,
00086 Seek_Cur = 1,
00087 Seek_End = 2
00088 };
00089
00090
00091 enum Errors
00092 {
00093 Error_FileNotFound = 0x1001,
00094 Error_Access = 0x1002,
00095 Error_IOError = 0x1003,
00096 Error_DiskFull = 0x1004
00097 };
00098 };
00099
00100
00101
00102
00103
00104
00105
00106
00107 class File : public RefCountBase<File>, public FileConstants
00108 {
00109 public:
00110 File() { }
00111
00112
00113
00114
00115
00116 virtual const char* GetFilePath() = 0;
00117
00118
00119
00120
00121
00122 virtual bool IsValid() = 0;
00123
00124 virtual bool IsWritable() = 0;
00125
00126
00127 virtual int Tell() = 0;
00128 virtual SInt64 LTell() = 0;
00129
00130
00131 virtual int GetLength() = 0;
00132 virtual SInt64 LGetLength() = 0;
00133
00134
00135
00136
00137
00138
00139
00140 virtual int GetErrorCode() = 0;
00141
00142
00143
00144
00145
00146
00147
00148 virtual int Write(const UByte *pbufer, int numBytes) = 0;
00149
00150
00151
00152
00153 virtual int Read(UByte *pbufer, int numBytes) = 0;
00154
00155
00156
00157 virtual int SkipBytes(int numBytes) = 0;
00158
00159
00160
00161 virtual int BytesAvailable() = 0;
00162
00163
00164
00165 virtual bool Flush() = 0;
00166
00167
00168
00169 inline bool IsEOF() { return !BytesAvailable(); }
00170
00171
00172
00173
00174 virtual int Seek(int offset, int origin=Seek_Set) = 0;
00175 virtual SInt64 LSeek(SInt64 offset, int origin=Seek_Set) = 0;
00176
00177 int SeekToBegin() {return Seek(0); }
00178 int SeekToEnd() {return Seek(0,Seek_End); }
00179 int Skip(int numBytes) {return Seek(numBytes,Seek_Cur); }
00180
00181
00182
00183
00184 virtual int CopyFromStream(File *pstream, int byteSize) = 0;
00185
00186
00187
00188 virtual bool Close() = 0;
00189
00190
00191
00192
00193
00194 private:
00195 UInt64 PRead64() { UInt64 v = 0; Read((UByte*)&v, 8); return v; }
00196 UInt32 PRead32() { UInt32 v = 0; Read((UByte*)&v, 4); return v; }
00197 UInt16 PRead16() { UInt16 v = 0; Read((UByte*)&v, 2); return v; }
00198 UByte PRead8() { UByte v = 0; Read((UByte*)&v, 1); return v; }
00199 void PWrite64(UInt64 v) { Write((UByte*)&v, 8); }
00200 void PWrite32(UInt32 v) { Write((UByte*)&v, 4); }
00201 void PWrite16(UInt16 v) { Write((UByte*)&v, 2); }
00202 void PWrite8(UByte v) { Write((UByte*)&v, 1); }
00203
00204 public:
00205
00206
00207 inline void WriteUByte(UByte v) { PWrite8((UByte)Alg::ByteUtil::SystemToLE(v)); }
00208 inline void WriteSByte(SByte v) { PWrite8((UByte)Alg::ByteUtil::SystemToLE(v)); }
00209 inline void WriteUInt8(UByte v) { PWrite8((UByte)Alg::ByteUtil::SystemToLE(v)); }
00210 inline void WriteSInt8(SByte v) { PWrite8((UByte)Alg::ByteUtil::SystemToLE(v)); }
00211 inline void WriteUInt16(UInt16 v) { PWrite16((UInt16)Alg::ByteUtil::SystemToLE(v)); }
00212 inline void WriteSInt16(SInt16 v) { PWrite16((UInt16)Alg::ByteUtil::SystemToLE(v)); }
00213 inline void WriteUInt32(UInt32 v) { PWrite32((UInt32)Alg::ByteUtil::SystemToLE(v)); }
00214 inline void WriteSInt32(SInt32 v) { PWrite32((UInt32)Alg::ByteUtil::SystemToLE(v)); }
00215 inline void WriteUInt64(UInt64 v) { PWrite64((UInt64)Alg::ByteUtil::SystemToLE(v)); }
00216 inline void WriteSInt64(SInt64 v) { PWrite64((UInt64)Alg::ByteUtil::SystemToLE(v)); }
00217 inline void WriteFloat(float v) { v = Alg::ByteUtil::SystemToLE(v); Write((UByte*)&v, 4); }
00218 inline void WriteDouble(double v) { v = Alg::ByteUtil::SystemToLE(v); Write((UByte*)&v, 8); }
00219
00220 inline void WriteUByteBE(UByte v) { PWrite8((UByte)Alg::ByteUtil::SystemToBE(v)); }
00221 inline void WriteSByteBE(SByte v) { PWrite8((UByte)Alg::ByteUtil::SystemToBE(v)); }
00222 inline void WriteUInt8BE(UInt16 v) { PWrite8((UByte)Alg::ByteUtil::SystemToBE(v)); }
00223 inline void WriteSInt8BE(SInt16 v) { PWrite8((UByte)Alg::ByteUtil::SystemToBE(v)); }
00224 inline void WriteUInt16BE(UInt16 v) { PWrite16((UInt16)Alg::ByteUtil::SystemToBE(v)); }
00225 inline void WriteSInt16BE(UInt16 v) { PWrite16((UInt16)Alg::ByteUtil::SystemToBE(v)); }
00226 inline void WriteUInt32BE(UInt32 v) { PWrite32((UInt32)Alg::ByteUtil::SystemToBE(v)); }
00227 inline void WriteSInt32BE(UInt32 v) { PWrite32((UInt32)Alg::ByteUtil::SystemToBE(v)); }
00228 inline void WriteUInt64BE(UInt64 v) { PWrite64((UInt64)Alg::ByteUtil::SystemToBE(v)); }
00229 inline void WriteSInt64BE(UInt64 v) { PWrite64((UInt64)Alg::ByteUtil::SystemToBE(v)); }
00230 inline void WriteFloatBE(float v) { v = Alg::ByteUtil::SystemToBE(v); Write((UByte*)&v, 4); }
00231 inline void WriteDoubleBE(double v) { v = Alg::ByteUtil::SystemToBE(v); Write((UByte*)&v, 8); }
00232
00233
00234 inline UByte ReadUByte() { return (UByte)Alg::ByteUtil::LEToSystem(PRead8()); }
00235 inline SByte ReadSByte() { return (SByte)Alg::ByteUtil::LEToSystem(PRead8()); }
00236 inline UByte ReadUInt8() { return (UByte)Alg::ByteUtil::LEToSystem(PRead8()); }
00237 inline SByte ReadSInt8() { return (SByte)Alg::ByteUtil::LEToSystem(PRead8()); }
00238 inline UInt16 ReadUInt16() { return (UInt16)Alg::ByteUtil::LEToSystem(PRead16()); }
00239 inline SInt16 ReadSInt16() { return (SInt16)Alg::ByteUtil::LEToSystem(PRead16()); }
00240 inline UInt32 ReadUInt32() { return (UInt32)Alg::ByteUtil::LEToSystem(PRead32()); }
00241 inline SInt32 ReadSInt32() { return (SInt32)Alg::ByteUtil::LEToSystem(PRead32()); }
00242 inline UInt64 ReadUInt64() { return (UInt64)Alg::ByteUtil::LEToSystem(PRead64()); }
00243 inline SInt64 ReadSInt64() { return (SInt64)Alg::ByteUtil::LEToSystem(PRead64()); }
00244 inline float ReadFloat() { float v = 0.0f; Read((UByte*)&v, 4); return Alg::ByteUtil::LEToSystem(v); }
00245 inline double ReadDouble() { double v = 0.0; Read((UByte*)&v, 8); return Alg::ByteUtil::LEToSystem(v); }
00246
00247 inline UByte ReadUByteBE() { return (UByte)Alg::ByteUtil::BEToSystem(PRead8()); }
00248 inline SByte ReadSByteBE() { return (SByte)Alg::ByteUtil::BEToSystem(PRead8()); }
00249 inline UByte ReadUInt8BE() { return (UByte)Alg::ByteUtil::BEToSystem(PRead8()); }
00250 inline SByte ReadSInt8BE() { return (SByte)Alg::ByteUtil::BEToSystem(PRead8()); }
00251 inline UInt16 ReadUInt16BE() { return (UInt16)Alg::ByteUtil::BEToSystem(PRead16()); }
00252 inline SInt16 ReadSInt16BE() { return (SInt16)Alg::ByteUtil::BEToSystem(PRead16()); }
00253 inline UInt32 ReadUInt32BE() { return (UInt32)Alg::ByteUtil::BEToSystem(PRead32()); }
00254 inline SInt32 ReadSInt32BE() { return (SInt32)Alg::ByteUtil::BEToSystem(PRead32()); }
00255 inline UInt64 ReadUInt64BE() { return (UInt64)Alg::ByteUtil::BEToSystem(PRead64()); }
00256 inline SInt64 ReadSInt64BE() { return (SInt64)Alg::ByteUtil::BEToSystem(PRead64()); }
00257 inline float ReadFloatBE() { float v = 0.0f; Read((UByte*)&v, 4); return Alg::ByteUtil::BEToSystem(v); }
00258 inline double ReadDoubleBE() { double v = 0.0; Read((UByte*)&v, 8); return Alg::ByteUtil::BEToSystem(v); }
00259 };
00260
00261
00262
00263
00264 class DelegatedFile : public File
00265 {
00266 protected:
00267
00268 Ptr<File> pFile;
00269
00270
00271 DelegatedFile() : pFile(0) { }
00272 DelegatedFile(const DelegatedFile &source) : File() { OVR_UNUSED(source); }
00273 public:
00274
00275 DelegatedFile(File *pfile) : pFile(pfile) { }
00276
00277
00278 virtual const char* GetFilePath() { return pFile->GetFilePath(); }
00279
00280
00281 virtual bool IsValid() { return pFile && pFile->IsValid(); }
00282 virtual bool IsWritable() { return pFile->IsWritable(); }
00283
00284
00285 virtual int Tell() { return pFile->Tell(); }
00286 virtual SInt64 LTell() { return pFile->LTell(); }
00287
00288 virtual int GetLength() { return pFile->GetLength(); }
00289 virtual SInt64 LGetLength() { return pFile->LGetLength(); }
00290
00291
00292
00293 virtual int GetErrorCode() { return pFile->GetErrorCode(); }
00294
00295
00296 virtual int Write(const UByte *pbuffer, int numBytes) { return pFile->Write(pbuffer,numBytes); }
00297 virtual int Read(UByte *pbuffer, int numBytes) { return pFile->Read(pbuffer,numBytes); }
00298
00299 virtual int SkipBytes(int numBytes) { return pFile->SkipBytes(numBytes); }
00300
00301 virtual int BytesAvailable() { return pFile->BytesAvailable(); }
00302
00303 virtual bool Flush() { return pFile->Flush(); }
00304
00305
00306 virtual int Seek(int offset, int origin=Seek_Set) { return pFile->Seek(offset,origin); }
00307 virtual SInt64 LSeek(SInt64 offset, int origin=Seek_Set) { return pFile->LSeek(offset,origin); }
00308
00309 virtual int CopyFromStream(File *pstream, int byteSize) { return pFile->CopyFromStream(pstream,byteSize); }
00310
00311
00312 virtual bool Close() { return pFile->Close(); }
00313 };
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323 class BufferedFile : public DelegatedFile
00324 {
00325 protected:
00326 enum BufferModeType
00327 {
00328 NoBuffer,
00329 ReadBuffer,
00330 WriteBuffer
00331 };
00332
00333
00334 UByte* pBuffer;
00335 BufferModeType BufferMode;
00336
00337 unsigned Pos;
00338
00339 unsigned DataSize;
00340
00341 UInt64 FilePos;
00342
00343
00344 bool SetBufferMode(BufferModeType mode);
00345
00346
00347 void FlushBuffer();
00348
00349
00350 void LoadBuffer();
00351
00352
00353 BufferedFile();
00354 inline BufferedFile(const BufferedFile &source) : DelegatedFile() { OVR_UNUSED(source); }
00355 public:
00356
00357
00358
00359 BufferedFile(File *pfile);
00360 ~BufferedFile();
00361
00362
00363
00364
00365
00366
00367 virtual int Tell();
00368 virtual SInt64 LTell();
00369
00370 virtual int GetLength();
00371 virtual SInt64 LGetLength();
00372
00373
00374
00375 virtual int Write(const UByte *pbufer, int numBytes);
00376 virtual int Read(UByte *pbufer, int numBytes);
00377
00378 virtual int SkipBytes(int numBytes);
00379
00380 virtual int BytesAvailable();
00381
00382 virtual bool Flush();
00383
00384 virtual int Seek(int offset, int origin=Seek_Set);
00385 virtual SInt64 LSeek(SInt64 offset, int origin=Seek_Set);
00386
00387 virtual int CopyFromStream(File *pstream, int byteSize);
00388
00389 virtual bool Close();
00390 };
00391
00392
00393
00394
00395
00396 class MemoryFile : public File
00397 {
00398 public:
00399
00400 const char* GetFilePath() { return FilePath.ToCStr(); }
00401
00402 bool IsValid() { return Valid; }
00403 bool IsWritable() { return false; }
00404
00405 bool Flush() { return true; }
00406 int GetErrorCode() { return 0; }
00407
00408 int Tell() { return FileIndex; }
00409 SInt64 LTell() { return (SInt64) FileIndex; }
00410
00411 int GetLength() { return FileSize; }
00412 SInt64 LGetLength() { return (SInt64) FileSize; }
00413
00414 bool Close()
00415 {
00416 Valid = false;
00417 return false;
00418 }
00419
00420 int CopyFromStream(File *pstream, int byteSize)
00421 { OVR_UNUSED2(pstream, byteSize);
00422 return 0;
00423 }
00424
00425 int Write(const UByte *pbuffer, int numBytes)
00426 { OVR_UNUSED2(pbuffer, numBytes);
00427 return 0;
00428 }
00429
00430 int Read(UByte *pbufer, int numBytes)
00431 {
00432 if (FileIndex + numBytes > FileSize)
00433 {
00434 numBytes = FileSize - FileIndex;
00435 }
00436
00437 if (numBytes > 0)
00438 {
00439 ::memcpy (pbufer, &FileData [FileIndex], numBytes);
00440
00441 FileIndex += numBytes;
00442 }
00443
00444 return numBytes;
00445 }
00446
00447 int SkipBytes(int numBytes)
00448 {
00449 if (FileIndex + numBytes > FileSize)
00450 {
00451 numBytes = FileSize - FileIndex;
00452 }
00453
00454 FileIndex += numBytes;
00455
00456 return numBytes;
00457 }
00458
00459 int BytesAvailable()
00460 {
00461 return (FileSize - FileIndex);
00462 }
00463
00464 int Seek(int offset, int origin = Seek_Set)
00465 {
00466 switch (origin)
00467 {
00468 case Seek_Set : FileIndex = offset; break;
00469 case Seek_Cur : FileIndex += offset; break;
00470 case Seek_End : FileIndex = FileSize - offset; break;
00471 }
00472
00473 return FileIndex;
00474 }
00475
00476 SInt64 LSeek(SInt64 offset, int origin = Seek_Set)
00477 {
00478 return (SInt64) Seek((int) offset, origin);
00479 }
00480
00481 public:
00482
00483 MemoryFile (const String& fileName, const UByte *pBuffer, int buffSize)
00484 : FilePath(fileName)
00485 {
00486 FileData = pBuffer;
00487 FileSize = buffSize;
00488 FileIndex = 0;
00489 Valid = (!fileName.IsEmpty() && pBuffer && buffSize > 0) ? true : false;
00490 }
00491
00492
00493 MemoryFile (const char* pfileName, const UByte *pBuffer, int buffSize)
00494 : FilePath(pfileName)
00495 {
00496 FileData = pBuffer;
00497 FileSize = buffSize;
00498 FileIndex = 0;
00499 Valid = (pfileName && pBuffer && buffSize > 0) ? true : false;
00500 }
00501 private:
00502
00503 String FilePath;
00504 const UByte *FileData;
00505 int FileSize;
00506 int FileIndex;
00507 bool Valid;
00508 };
00509
00510
00511
00512
00513
00514 const char* OVR_CDECL GetShortFilename(const char* purl);
00515
00516 }
00517
00518 #endif