23 #ifndef INC_KY_Kernel_File_H
24 #define INC_KY_Kernel_File_H
94 Error_FileNotFound = 0x1001,
95 Error_Access = 0x1002,
96 Error_IOError = 0x1003,
97 Error_DiskFull = 0x1004
108 class File :
public RefCountBase<File, Stat_Default_Mem>,
public FileConstants
117 virtual const char* GetFilePath() = 0;
123 virtual bool IsValid() = 0;
125 virtual bool IsWritable() = 0;
131 virtual int Tell () = 0;
132 virtual SInt64 LTell () = 0;
135 virtual int GetLength () = 0;
136 virtual SInt64 LGetLength () = 0;
144 virtual int GetErrorCode() = 0;
152 virtual int Write(
const UByte *pbufer,
int numBytes) = 0;
157 virtual int Read(UByte *pbufer,
int numBytes) = 0;
161 virtual int SkipBytes(
int numBytes) = 0;
165 virtual int BytesAvailable() = 0;
169 virtual bool Flush() = 0;
173 KY_INLINE
bool IsEOF()
174 {
return !BytesAvailable(); }
179 virtual int Seek(
int offset,
int origin=Seek_Set) = 0;
180 virtual SInt64 LSeek(SInt64 offset,
int origin=Seek_Set) = 0;
182 virtual int SeekToBegin() {
return Seek(0); }
183 int SeekToEnd() {
return Seek(0,Seek_End); }
184 int Skip(
int numBytes) {
return Seek(numBytes,Seek_Cur); }
188 virtual bool ChangeSize(
int newSize) = 0;
192 virtual int CopyFromStream(File *pstream,
int byteSize) = 0;
196 virtual bool Close() = 0;
205 UInt64 PRead64() { UInt64 v = 0; Read((UByte*)&v, 8);
return v; }
206 UInt32 PRead32() { UInt32 v = 0; Read((UByte*)&v, 4);
return v; }
207 UInt16 PRead16() { UInt16 v = 0; Read((UByte*)&v, 2);
return v; }
208 UInt8 PRead8() { UInt8 v = 0; Read((UByte*)&v, 1);
return v; }
209 void PWrite64(UInt64 v) { Write((UByte*)&v, 8); }
210 void PWrite32(UInt32 v) { Write((UByte*)&v, 4); }
211 void PWrite16(UInt16 v) { Write((UByte*)&v, 2); }
212 void PWrite8(UInt8 v) { Write((UByte*)&v, 1); }
217 inline void WriteUByte(UByte v) { PWrite8((UInt8)Alg::ByteUtil::SystemToLE(v)); }
218 inline void WriteSByte(SByte v) { PWrite8((UInt8)Alg::ByteUtil::SystemToLE(v)); }
219 inline void WriteUInt8(UInt8 v) { PWrite8((UInt8)Alg::ByteUtil::SystemToLE(v)); }
220 inline void WriteSInt8(SInt8 v) { PWrite8((UInt8)Alg::ByteUtil::SystemToLE(v)); }
221 inline void WriteUInt16(UInt16 v) { PWrite16((UInt16)Alg::ByteUtil::SystemToLE(v)); }
222 inline void WriteSInt16(SInt16 v) { PWrite16((UInt16)Alg::ByteUtil::SystemToLE(v)); }
223 inline void WriteUInt32(UInt32 v) { PWrite32((UInt32)Alg::ByteUtil::SystemToLE(v)); }
224 inline void WriteSInt32(SInt32 v) { PWrite32((UInt32)Alg::ByteUtil::SystemToLE(v)); }
225 inline void WriteUInt64(UInt64 v) { PWrite64((UInt64)Alg::ByteUtil::SystemToLE(v)); }
226 inline void WriteSInt64(SInt64 v) { PWrite64((UInt64)Alg::ByteUtil::SystemToLE(v)); }
227 inline void WriteFloat(
float v) { v = Alg::ByteUtil::SystemToLE(v); Write((UByte*)&v, 4); }
229 inline void WriteDouble(Double v) { v = Alg::ByteUtil::SystemToLE(v); Write((UByte*)&v, 8); }
232 inline void WriteUByteBE(UByte v) { PWrite8((UInt8)Alg::ByteUtil::SystemToBE(v)); }
233 inline void WriteSByteBE(SByte v) { PWrite8((UInt8)Alg::ByteUtil::SystemToBE(v)); }
234 inline void WriteUInt8BE(UInt16 v) { PWrite8((UInt8)Alg::ByteUtil::SystemToBE(v)); }
235 inline void WriteSInt8BE(SInt16 v) { PWrite8((UInt8)Alg::ByteUtil::SystemToBE(v)); }
236 inline void WriteUInt16BE(UInt16 v) { PWrite16((UInt16)Alg::ByteUtil::SystemToBE(v)); }
237 inline void WriteSInt16BE(UInt16 v) { PWrite16((UInt16)Alg::ByteUtil::SystemToBE(v)); }
238 inline void WriteUInt32BE(UInt32 v) { PWrite32((UInt32)Alg::ByteUtil::SystemToBE(v)); }
239 inline void WriteSInt32BE(UInt32 v) { PWrite32((UInt32)Alg::ByteUtil::SystemToBE(v)); }
240 inline void WriteUInt64BE(UInt64 v) { PWrite64((UInt64)Alg::ByteUtil::SystemToBE(v)); }
241 inline void WriteSInt64BE(UInt64 v) { PWrite64((UInt64)Alg::ByteUtil::SystemToBE(v)); }
242 inline void WriteFloatBE(
float v) { v = Alg::ByteUtil::SystemToBE(v); Write((UByte*)&v, 4); }
244 inline void WriteDoubleBE(Double v) { v = Alg::ByteUtil::SystemToBE(v); Write((UByte*)&v, 8); }
248 inline UByte ReadUByte() {
return (UByte)Alg::ByteUtil::LEToSystem(PRead8()); }
249 inline SByte ReadSByte() {
return (SByte)Alg::ByteUtil::LEToSystem(PRead8()); }
250 inline UInt8 ReadUInt8() {
return (UInt8)Alg::ByteUtil::LEToSystem(PRead8()); }
251 inline SInt8 ReadSInt8() {
return (SInt8)Alg::ByteUtil::LEToSystem(PRead8()); }
252 inline UInt16 ReadUInt16() {
return (UInt16)Alg::ByteUtil::LEToSystem(PRead16()); }
253 inline SInt16 ReadSInt16() {
return (SInt16)Alg::ByteUtil::LEToSystem(PRead16()); }
254 inline UInt32 ReadUInt32() {
return (UInt32)Alg::ByteUtil::LEToSystem(PRead32()); }
255 inline SInt32 ReadSInt32() {
return (SInt32)Alg::ByteUtil::LEToSystem(PRead32()); }
256 inline UInt64 ReadUInt64() {
return (UInt64)Alg::ByteUtil::LEToSystem(PRead64()); }
257 inline SInt64 ReadSInt64() {
return (SInt64)Alg::ByteUtil::LEToSystem(PRead64()); }
258 inline float ReadFloat() {
float v = 0.0f; Read((UByte*)&v, 4);
return Alg::ByteUtil::LEToSystem(v); }
260 inline Double ReadDouble() { Double v = 0.0; Read((UByte*)&v, 8);
return Alg::ByteUtil::LEToSystem(v); }
263 inline UByte ReadUByteBE() {
return (UByte)Alg::ByteUtil::BEToSystem(PRead8()); }
264 inline SByte ReadSByteBE() {
return (SByte)Alg::ByteUtil::BEToSystem(PRead8()); }
265 inline UInt8 ReadUInt8BE() {
return (UInt8)Alg::ByteUtil::BEToSystem(PRead8()); }
266 inline SInt8 ReadSInt8BE() {
return (SInt8)Alg::ByteUtil::BEToSystem(PRead8()); }
267 inline UInt16 ReadUInt16BE() {
return (UInt16)Alg::ByteUtil::BEToSystem(PRead16()); }
268 inline SInt16 ReadSInt16BE() {
return (SInt16)Alg::ByteUtil::BEToSystem(PRead16()); }
269 inline UInt32 ReadUInt32BE() {
return (UInt32)Alg::ByteUtil::BEToSystem(PRead32()); }
270 inline SInt32 ReadSInt32BE() {
return (SInt32)Alg::ByteUtil::BEToSystem(PRead32()); }
271 inline UInt64 ReadUInt64BE() {
return (UInt64)Alg::ByteUtil::BEToSystem(PRead64()); }
272 inline SInt64 ReadSInt64BE() {
return (SInt64)Alg::ByteUtil::BEToSystem(PRead64()); }
273 inline float ReadFloatBE() {
float v = 0.0f; Read((UByte*)&v, 4);
return Alg::ByteUtil::BEToSystem(v); }
275 inline Double ReadDoubleBE() { Double v = 0.0; Read((UByte*)&v, 8);
return Alg::ByteUtil::BEToSystem(v); }
282 class DelegatedFile :
public File
289 DelegatedFile() : pFile(0) { }
290 DelegatedFile(
const DelegatedFile &source) : File() { KY_UNUSED(source); }
293 DelegatedFile(File *pfile) : pFile(pfile) { }
296 virtual const char* GetFilePath() {
return pFile->GetFilePath(); }
299 virtual bool IsValid() {
return pFile && pFile->IsValid(); }
300 virtual bool IsWritable() {
return pFile->IsWritable(); }
303 virtual int Tell() {
return pFile->Tell(); }
304 virtual SInt64 LTell() {
return pFile->LTell(); }
306 virtual int GetLength() {
return pFile->GetLength(); }
307 virtual SInt64 LGetLength() {
return pFile->LGetLength(); }
311 virtual int GetErrorCode() {
return pFile->GetErrorCode(); }
314 virtual int Write(
const UByte *pbuffer,
int numBytes) {
return pFile->Write(pbuffer,numBytes); }
315 virtual int Read(UByte *pbuffer,
int numBytes) {
return pFile->Read(pbuffer,numBytes); }
317 virtual int SkipBytes(
int numBytes) {
return pFile->SkipBytes(numBytes); }
319 virtual int BytesAvailable() {
return pFile->BytesAvailable(); }
321 virtual bool Flush() {
return pFile->Flush(); }
324 virtual int Seek(
int offset,
int origin=Seek_Set) {
return pFile->Seek(offset,origin); }
325 virtual SInt64 LSeek(SInt64 offset,
int origin=Seek_Set) {
return pFile->LSeek(offset,origin); }
328 virtual bool ChangeSize(
int newSize) {
return pFile->ChangeSize(newSize); }
329 virtual int CopyFromStream(File *pstream,
int byteSize) {
return pFile->CopyFromStream(pstream,byteSize); }
332 virtual bool Close() {
return pFile->Close(); }
343 class BufferedFile :
public DelegatedFile
355 BufferModeType BufferMode;
364 KY_EXPORT
bool SetBufferMode(BufferModeType mode);
367 KY_EXPORT
void FlushBuffer();
370 KY_EXPORT
void LoadBuffer();
373 KY_EXPORT BufferedFile();
374 inline BufferedFile(
const BufferedFile &source) : DelegatedFile() { KY_UNUSED(source); }
376 static int FILEBUFFER_SIZE;
377 static int FILEBUFFER_TOLERANCE;
381 KY_EXPORT BufferedFile(File *pfile);
382 KY_EXPORT ~BufferedFile();
389 KY_EXPORT
virtual int Tell();
390 KY_EXPORT
virtual SInt64 LTell();
392 KY_EXPORT
virtual int GetLength();
393 KY_EXPORT
virtual SInt64 LGetLength();
397 KY_EXPORT
virtual int Write(
const UByte *pbufer,
int numBytes);
398 KY_EXPORT
virtual int Read(UByte *pbufer,
int numBytes);
400 KY_EXPORT
virtual int SkipBytes(
int numBytes);
402 KY_EXPORT
virtual int BytesAvailable();
404 KY_EXPORT
virtual bool Flush();
406 KY_EXPORT
virtual int Seek(
int offset,
int origin=Seek_Set);
407 KY_EXPORT
virtual SInt64 LSeek(SInt64 offset,
int origin=Seek_Set);
409 KY_EXPORT
virtual bool ChangeSize(
int newSize);
410 KY_EXPORT
virtual int CopyFromStream(File *pstream,
int byteSize);
412 KY_EXPORT
virtual bool Close();
419 class MemoryFile :
public File
423 const char* GetFilePath() {
return FilePath.ToCStr(); }
425 bool IsValid() {
return Valid; }
426 bool IsWritable() {
return false; }
428 bool Flush() {
return true; }
429 int GetErrorCode() {
return 0; }
431 int Tell() {
return FileIndex; }
432 SInt64 LTell() {
return (SInt64) FileIndex; }
434 int GetLength() {
return FileSize; }
435 SInt64 LGetLength() {
return (SInt64) FileSize; }
443 int CopyFromStream(File *pstream,
int byteSize)
444 { KY_UNUSED2(pstream, byteSize);
448 int Write(
const UByte *pbuffer,
int numBytes)
449 { KY_UNUSED2(pbuffer, numBytes);
453 int Read(UByte *pbufer,
int numBytes)
455 if (FileIndex + numBytes > FileSize)
457 numBytes = FileSize - FileIndex;
462 ::memcpy (pbufer, &FileData [FileIndex], numBytes);
464 FileIndex += numBytes;
470 int SkipBytes(
int numBytes)
472 if (FileIndex + numBytes > FileSize)
474 numBytes = FileSize - FileIndex;
477 FileIndex += numBytes;
484 return (FileSize - FileIndex);
487 int Seek(
int offset,
int origin = Seek_Set)
491 case Seek_Set : FileIndex = offset;
break;
492 case Seek_Cur : FileIndex += offset;
break;
493 case Seek_End : FileIndex = FileSize - offset;
break;
499 SInt64 LSeek(SInt64 offset,
int origin = Seek_Set)
501 return (SInt64) Seek((
int) offset, origin);
504 bool ChangeSize(
int newSize)
512 MemoryFile (
const String& fileName,
const UByte *pBuffer,
int buffSize)
518 Valid = (!fileName.IsEmpty() && pBuffer && buffSize > 0) ?
true :
false;
522 MemoryFile (
const char* pfileName,
const UByte *pBuffer,
int buffSize)
523 : FilePath(pfileName)
528 Valid = (pfileName && pBuffer && buffSize > 0) ?
true :
false;
533 const UByte *FileData;
543 const char* KY_CDECL GetShortFilename(
const char* purl);
Definition: gamekitcrowddispersion.h:20