oculus1
diff libovr/Src/Kernel/OVR_File.cpp @ 3:b069a5c27388
added a couple more stuff, fixed all the LibOVR line endings
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 15 Sep 2013 04:10:05 +0300 |
parents | e2f9e4603129 |
children |
line diff
1.1 --- a/libovr/Src/Kernel/OVR_File.cpp Sat Sep 14 17:51:03 2013 +0300 1.2 +++ b/libovr/Src/Kernel/OVR_File.cpp Sun Sep 15 04:10:05 2013 +0300 1.3 @@ -1,1 +1,571 @@ 1.4 -/************************************************************************** 1.5 1.6 Filename : OVR_File.cpp 1.7 Content : File wrapper class implementation (Win32) 1.8 1.9 Created : April 5, 1999 1.10 Authors : Michael Antonov 1.11 1.12 Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved. 1.13 1.14 Use of this software is subject to the terms of the Oculus license 1.15 agreement provided at the time of installation or download, or which 1.16 otherwise accompanies this software in either electronic or hard copy form. 1.17 1.18 **************************************************************************/ 1.19 1.20 #define GFILE_CXX 1.21 1.22 // Standard C library (Captain Obvious guarantees!) 1.23 #include <stdio.h> 1.24 1.25 #include "OVR_File.h" 1.26 1.27 namespace OVR { 1.28 1.29 // Buffered file adds buffering to an existing file 1.30 // FILEBUFFER_SIZE defines the size of internal buffer, while 1.31 // FILEBUFFER_TOLERANCE controls the amount of data we'll effectively try to buffer 1.32 #define FILEBUFFER_SIZE (8192-8) 1.33 #define FILEBUFFER_TOLERANCE 4096 1.34 1.35 // ** Constructor/Destructor 1.36 1.37 // Hidden constructor 1.38 // Not supposed to be used 1.39 BufferedFile::BufferedFile() : DelegatedFile(0) 1.40 { 1.41 pBuffer = (UByte*)OVR_ALLOC(FILEBUFFER_SIZE); 1.42 BufferMode = NoBuffer; 1.43 FilePos = 0; 1.44 Pos = 0; 1.45 DataSize = 0; 1.46 } 1.47 1.48 // Takes another file as source 1.49 BufferedFile::BufferedFile(File *pfile) : DelegatedFile(pfile) 1.50 { 1.51 pBuffer = (UByte*)OVR_ALLOC(FILEBUFFER_SIZE); 1.52 BufferMode = NoBuffer; 1.53 FilePos = pfile->LTell(); 1.54 Pos = 0; 1.55 DataSize = 0; 1.56 } 1.57 1.58 1.59 // Destructor 1.60 BufferedFile::~BufferedFile() 1.61 { 1.62 // Flush in case there's data 1.63 if (pFile) 1.64 FlushBuffer(); 1.65 // Get rid of buffer 1.66 if (pBuffer) 1.67 OVR_FREE(pBuffer); 1.68 } 1.69 1.70 /* 1.71 bool BufferedFile::VCopy(const Object &source) 1.72 { 1.73 if (!DelegatedFile::VCopy(source)) 1.74 return 0; 1.75 1.76 // Data members 1.77 BufferedFile *psource = (BufferedFile*)&source; 1.78 1.79 // Buffer & the mode it's in 1.80 pBuffer = psource->pBuffer; 1.81 BufferMode = psource->BufferMode; 1.82 Pos = psource->Pos; 1.83 DataSize = psource->DataSize; 1.84 return 1; 1.85 } 1.86 */ 1.87 1.88 // Initializes buffering to a certain mode 1.89 bool BufferedFile::SetBufferMode(BufferModeType mode) 1.90 { 1.91 if (!pBuffer) 1.92 return false; 1.93 if (mode == BufferMode) 1.94 return true; 1.95 1.96 FlushBuffer(); 1.97 1.98 // Can't set write mode if we can't write 1.99 if ((mode==WriteBuffer) && (!pFile || !pFile->IsWritable()) ) 1.100 return 0; 1.101 1.102 // And SetMode 1.103 BufferMode = mode; 1.104 Pos = 0; 1.105 DataSize = 0; 1.106 return 1; 1.107 } 1.108 1.109 // Flushes buffer 1.110 void BufferedFile::FlushBuffer() 1.111 { 1.112 switch(BufferMode) 1.113 { 1.114 case WriteBuffer: 1.115 // Write data in buffer 1.116 FilePos += pFile->Write(pBuffer,Pos); 1.117 Pos = 0; 1.118 break; 1.119 1.120 case ReadBuffer: 1.121 // Seek back & reset buffer data 1.122 if ((DataSize-Pos)>0) 1.123 FilePos = pFile->LSeek(-(int)(DataSize-Pos), Seek_Cur); 1.124 DataSize = 0; 1.125 Pos = 0; 1.126 break; 1.127 default: 1.128 // not handled! 1.129 break; 1.130 } 1.131 } 1.132 1.133 // Reloads data for ReadBuffer 1.134 void BufferedFile::LoadBuffer() 1.135 { 1.136 if (BufferMode == ReadBuffer) 1.137 { 1.138 // We should only reload once all of pre-loaded buffer is consumed. 1.139 OVR_ASSERT(Pos == DataSize); 1.140 1.141 // WARNING: Right now LoadBuffer() assumes the buffer's empty 1.142 int sz = pFile->Read(pBuffer,FILEBUFFER_SIZE); 1.143 DataSize = sz<0 ? 0 : (unsigned)sz; 1.144 Pos = 0; 1.145 FilePos += DataSize; 1.146 } 1.147 } 1.148 1.149 1.150 // ** Overridden functions 1.151 1.152 // We override all the functions that can possibly 1.153 // require buffer mode switch, flush, or extra calculations 1.154 1.155 // Tell() requires buffer adjustment 1.156 int BufferedFile::Tell() 1.157 { 1.158 if (BufferMode == ReadBuffer) 1.159 return int (FilePos - DataSize + Pos); 1.160 1.161 int pos = pFile->Tell(); 1.162 // Adjust position based on buffer mode & data 1.163 if (pos!=-1) 1.164 { 1.165 OVR_ASSERT(BufferMode != ReadBuffer); 1.166 if (BufferMode == WriteBuffer) 1.167 pos += Pos; 1.168 } 1.169 return pos; 1.170 } 1.171 1.172 SInt64 BufferedFile::LTell() 1.173 { 1.174 if (BufferMode == ReadBuffer) 1.175 return FilePos - DataSize + Pos; 1.176 1.177 SInt64 pos = pFile->LTell(); 1.178 if (pos!=-1) 1.179 { 1.180 OVR_ASSERT(BufferMode != ReadBuffer); 1.181 if (BufferMode == WriteBuffer) 1.182 pos += Pos; 1.183 } 1.184 return pos; 1.185 } 1.186 1.187 int BufferedFile::GetLength() 1.188 { 1.189 int len = pFile->GetLength(); 1.190 // If writing through buffer, file length may actually be bigger 1.191 if ((len!=-1) && (BufferMode==WriteBuffer)) 1.192 { 1.193 int currPos = pFile->Tell() + Pos; 1.194 if (currPos>len) 1.195 len = currPos; 1.196 } 1.197 return len; 1.198 } 1.199 SInt64 BufferedFile::LGetLength() 1.200 { 1.201 SInt64 len = pFile->LGetLength(); 1.202 // If writing through buffer, file length may actually be bigger 1.203 if ((len!=-1) && (BufferMode==WriteBuffer)) 1.204 { 1.205 SInt64 currPos = pFile->LTell() + Pos; 1.206 if (currPos>len) 1.207 len = currPos; 1.208 } 1.209 return len; 1.210 } 1.211 1.212 /* 1.213 bool BufferedFile::Stat(FileStats *pfs) 1.214 { 1.215 // Have to fix up length is stat 1.216 if (pFile->Stat(pfs)) 1.217 { 1.218 if (BufferMode==WriteBuffer) 1.219 { 1.220 SInt64 currPos = pFile->LTell() + Pos; 1.221 if (currPos > pfs->Size) 1.222 { 1.223 pfs->Size = currPos; 1.224 // ?? 1.225 pfs->Blocks = (pfs->Size+511) >> 9; 1.226 } 1.227 } 1.228 return 1; 1.229 } 1.230 return 0; 1.231 } 1.232 */ 1.233 1.234 int BufferedFile::Write(const UByte *psourceBuffer, int numBytes) 1.235 { 1.236 if ( (BufferMode==WriteBuffer) || SetBufferMode(WriteBuffer)) 1.237 { 1.238 // If not data space in buffer, flush 1.239 if ((FILEBUFFER_SIZE-(int)Pos)<numBytes) 1.240 { 1.241 FlushBuffer(); 1.242 // If bigger then tolerance, just write directly 1.243 if (numBytes>FILEBUFFER_TOLERANCE) 1.244 { 1.245 int sz = pFile->Write(psourceBuffer,numBytes); 1.246 if (sz > 0) 1.247 FilePos += sz; 1.248 return sz; 1.249 } 1.250 } 1.251 1.252 // Enough space in buffer.. so copy to it 1.253 memcpy(pBuffer+Pos, psourceBuffer, numBytes); 1.254 Pos += numBytes; 1.255 return numBytes; 1.256 } 1.257 int sz = pFile->Write(psourceBuffer,numBytes); 1.258 if (sz > 0) 1.259 FilePos += sz; 1.260 return sz; 1.261 } 1.262 1.263 int BufferedFile::Read(UByte *pdestBuffer, int numBytes) 1.264 { 1.265 if ( (BufferMode==ReadBuffer) || SetBufferMode(ReadBuffer)) 1.266 { 1.267 // Data in buffer... copy it 1.268 if ((int)(DataSize-Pos) >= numBytes) 1.269 { 1.270 memcpy(pdestBuffer, pBuffer+Pos, numBytes); 1.271 Pos += numBytes; 1.272 return numBytes; 1.273 } 1.274 1.275 // Not enough data in buffer, copy buffer 1.276 int readBytes = DataSize-Pos; 1.277 memcpy(pdestBuffer, pBuffer+Pos, readBytes); 1.278 numBytes -= readBytes; 1.279 pdestBuffer += readBytes; 1.280 Pos = DataSize; 1.281 1.282 // Don't reload buffer if more then tolerance 1.283 // (No major advantage, and we don't want to write a loop) 1.284 if (numBytes>FILEBUFFER_TOLERANCE) 1.285 { 1.286 numBytes = pFile->Read(pdestBuffer,numBytes); 1.287 if (numBytes > 0) 1.288 { 1.289 FilePos += numBytes; 1.290 Pos = DataSize = 0; 1.291 } 1.292 return readBytes + ((numBytes==-1) ? 0 : numBytes); 1.293 } 1.294 1.295 // Reload the buffer 1.296 // WARNING: Right now LoadBuffer() assumes the buffer's empty 1.297 LoadBuffer(); 1.298 if ((int)(DataSize-Pos) < numBytes) 1.299 numBytes = (int)DataSize-Pos; 1.300 1.301 memcpy(pdestBuffer, pBuffer+Pos, numBytes); 1.302 Pos += numBytes; 1.303 return numBytes + readBytes; 1.304 1.305 /* 1.306 // Alternative Read implementation. The one above is probably better 1.307 // due to FILEBUFFER_TOLERANCE. 1.308 int total = 0; 1.309 1.310 do { 1.311 int bufferBytes = (int)(DataSize-Pos); 1.312 int copyBytes = (bufferBytes > numBytes) ? numBytes : bufferBytes; 1.313 1.314 memcpy(pdestBuffer, pBuffer+Pos, copyBytes); 1.315 numBytes -= copyBytes; 1.316 pdestBuffer += copyBytes; 1.317 Pos += copyBytes; 1.318 total += copyBytes; 1.319 1.320 if (numBytes == 0) 1.321 break; 1.322 LoadBuffer(); 1.323 1.324 } while (DataSize > 0); 1.325 1.326 return total; 1.327 */ 1.328 } 1.329 int sz = pFile->Read(pdestBuffer,numBytes); 1.330 if (sz > 0) 1.331 FilePos += sz; 1.332 return sz; 1.333 } 1.334 1.335 1.336 int BufferedFile::SkipBytes(int numBytes) 1.337 { 1.338 int skippedBytes = 0; 1.339 1.340 // Special case for skipping a little data in read buffer 1.341 if (BufferMode==ReadBuffer) 1.342 { 1.343 skippedBytes = (((int)DataSize-(int)Pos) >= numBytes) ? numBytes : (DataSize-Pos); 1.344 Pos += skippedBytes; 1.345 numBytes -= skippedBytes; 1.346 } 1.347 1.348 if (numBytes) 1.349 { 1.350 numBytes = pFile->SkipBytes(numBytes); 1.351 // Make sure we return the actual number skipped, or error 1.352 if (numBytes!=-1) 1.353 { 1.354 skippedBytes += numBytes; 1.355 FilePos += numBytes; 1.356 Pos = DataSize = 0; 1.357 } 1.358 else if (skippedBytes <= 0) 1.359 skippedBytes = -1; 1.360 } 1.361 return skippedBytes; 1.362 } 1.363 1.364 int BufferedFile::BytesAvailable() 1.365 { 1.366 int available = pFile->BytesAvailable(); 1.367 // Adjust available size based on buffers 1.368 switch(BufferMode) 1.369 { 1.370 case ReadBuffer: 1.371 available += DataSize-Pos; 1.372 break; 1.373 case WriteBuffer: 1.374 available -= Pos; 1.375 if (available<0) 1.376 available= 0; 1.377 break; 1.378 default: 1.379 break; 1.380 } 1.381 return available; 1.382 } 1.383 1.384 bool BufferedFile::Flush() 1.385 { 1.386 FlushBuffer(); 1.387 return pFile->Flush(); 1.388 } 1.389 1.390 // Seeking could be optimized better.. 1.391 int BufferedFile::Seek(int offset, int origin) 1.392 { 1.393 if (BufferMode == ReadBuffer) 1.394 { 1.395 if (origin == Seek_Cur) 1.396 { 1.397 // Seek can fall either before or after Pos in the buffer, 1.398 // but it must be within bounds. 1.399 if (((unsigned(offset) + Pos)) <= DataSize) 1.400 { 1.401 Pos += offset; 1.402 return int (FilePos - DataSize + Pos); 1.403 } 1.404 1.405 // Lightweight buffer "Flush". We do this to avoid an extra seek 1.406 // back operation which would take place if we called FlushBuffer directly. 1.407 origin = Seek_Set; 1.408 OVR_ASSERT(((FilePos - DataSize + Pos) + (UInt64)offset) < ~(UInt64)0); 1.409 offset = (int)(FilePos - DataSize + Pos) + offset; 1.410 Pos = DataSize = 0; 1.411 } 1.412 else if (origin == Seek_Set) 1.413 { 1.414 if (((unsigned)offset - (FilePos-DataSize)) <= DataSize) 1.415 { 1.416 OVR_ASSERT((FilePos-DataSize) < ~(UInt64)0); 1.417 Pos = (unsigned)offset - (unsigned)(FilePos-DataSize); 1.418 return offset; 1.419 } 1.420 Pos = DataSize = 0; 1.421 } 1.422 else 1.423 { 1.424 FlushBuffer(); 1.425 } 1.426 } 1.427 else 1.428 { 1.429 FlushBuffer(); 1.430 } 1.431 1.432 /* 1.433 // Old Seek Logic 1.434 if (origin == Seek_Cur && offset + Pos < DataSize) 1.435 { 1.436 //OVR_ASSERT((FilePos - DataSize) >= (FilePos - DataSize + Pos + offset)); 1.437 Pos += offset; 1.438 OVR_ASSERT(int (Pos) >= 0); 1.439 return int (FilePos - DataSize + Pos); 1.440 } 1.441 else if (origin == Seek_Set && unsigned(offset) >= FilePos - DataSize && unsigned(offset) < FilePos) 1.442 { 1.443 Pos = unsigned(offset - FilePos + DataSize); 1.444 OVR_ASSERT(int (Pos) >= 0); 1.445 return int (FilePos - DataSize + Pos); 1.446 } 1.447 1.448 FlushBuffer(); 1.449 */ 1.450 1.451 1.452 FilePos = pFile->Seek(offset,origin); 1.453 return int (FilePos); 1.454 } 1.455 1.456 SInt64 BufferedFile::LSeek(SInt64 offset, int origin) 1.457 { 1.458 if (BufferMode == ReadBuffer) 1.459 { 1.460 if (origin == Seek_Cur) 1.461 { 1.462 // Seek can fall either before or after Pos in the buffer, 1.463 // but it must be within bounds. 1.464 if (((unsigned(offset) + Pos)) <= DataSize) 1.465 { 1.466 Pos += (unsigned)offset; 1.467 return SInt64(FilePos - DataSize + Pos); 1.468 } 1.469 1.470 // Lightweight buffer "Flush". We do this to avoid an extra seek 1.471 // back operation which would take place if we called FlushBuffer directly. 1.472 origin = Seek_Set; 1.473 offset = (SInt64)(FilePos - DataSize + Pos) + offset; 1.474 Pos = DataSize = 0; 1.475 } 1.476 else if (origin == Seek_Set) 1.477 { 1.478 if (((UInt64)offset - (FilePos-DataSize)) <= DataSize) 1.479 { 1.480 Pos = (unsigned)((UInt64)offset - (FilePos-DataSize)); 1.481 return offset; 1.482 } 1.483 Pos = DataSize = 0; 1.484 } 1.485 else 1.486 { 1.487 FlushBuffer(); 1.488 } 1.489 } 1.490 else 1.491 { 1.492 FlushBuffer(); 1.493 } 1.494 1.495 /* 1.496 OVR_ASSERT(BufferMode != NoBuffer); 1.497 1.498 if (origin == Seek_Cur && offset + Pos < DataSize) 1.499 { 1.500 Pos += int (offset); 1.501 return FilePos - DataSize + Pos; 1.502 } 1.503 else if (origin == Seek_Set && offset >= SInt64(FilePos - DataSize) && offset < SInt64(FilePos)) 1.504 { 1.505 Pos = unsigned(offset - FilePos + DataSize); 1.506 return FilePos - DataSize + Pos; 1.507 } 1.508 1.509 FlushBuffer(); 1.510 */ 1.511 1.512 FilePos = pFile->LSeek(offset,origin); 1.513 return FilePos; 1.514 } 1.515 1.516 int BufferedFile::CopyFromStream(File *pstream, int byteSize) 1.517 { 1.518 // We can't rely on overridden Write() 1.519 // because delegation doesn't override virtual pointers 1.520 // So, just re-implement 1.521 UByte buff[0x4000]; 1.522 int count = 0; 1.523 int szRequest, szRead, szWritten; 1.524 1.525 while(byteSize) 1.526 { 1.527 szRequest = (byteSize > int(sizeof(buff))) ? int(sizeof(buff)) : byteSize; 1.528 1.529 szRead = pstream->Read(buff,szRequest); 1.530 szWritten = 0; 1.531 if (szRead > 0) 1.532 szWritten = Write(buff,szRead); 1.533 1.534 count +=szWritten; 1.535 byteSize-=szWritten; 1.536 if (szWritten < szRequest) 1.537 break; 1.538 } 1.539 return count; 1.540 } 1.541 1.542 // Closing files 1.543 bool BufferedFile::Close() 1.544 { 1.545 switch(BufferMode) 1.546 { 1.547 case WriteBuffer: 1.548 FlushBuffer(); 1.549 break; 1.550 case ReadBuffer: 1.551 // No need to seek back on close 1.552 BufferMode = NoBuffer; 1.553 break; 1.554 default: 1.555 break; 1.556 } 1.557 return pFile->Close(); 1.558 } 1.559 1.560 1.561 // ***** Global path helpers 1.562 1.563 // Find trailing short filename in a path. 1.564 const char* OVR_CDECL GetShortFilename(const char* purl) 1.565 { 1.566 UPInt len = OVR_strlen(purl); 1.567 for (UPInt i=len; i>0; i--) 1.568 if (purl[i]=='\\' || purl[i]=='/') 1.569 return purl+i+1; 1.570 return purl; 1.571 } 1.572 1.573 } // OVR 1.574 1.575 \ No newline at end of file 1.576 +/************************************************************************** 1.577 + 1.578 +Filename : OVR_File.cpp 1.579 +Content : File wrapper class implementation (Win32) 1.580 + 1.581 +Created : April 5, 1999 1.582 +Authors : Michael Antonov 1.583 + 1.584 +Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved. 1.585 + 1.586 +Use of this software is subject to the terms of the Oculus license 1.587 +agreement provided at the time of installation or download, or which 1.588 +otherwise accompanies this software in either electronic or hard copy form. 1.589 + 1.590 +**************************************************************************/ 1.591 + 1.592 +#define GFILE_CXX 1.593 + 1.594 +// Standard C library (Captain Obvious guarantees!) 1.595 +#include <stdio.h> 1.596 + 1.597 +#include "OVR_File.h" 1.598 + 1.599 +namespace OVR { 1.600 + 1.601 +// Buffered file adds buffering to an existing file 1.602 +// FILEBUFFER_SIZE defines the size of internal buffer, while 1.603 +// FILEBUFFER_TOLERANCE controls the amount of data we'll effectively try to buffer 1.604 +#define FILEBUFFER_SIZE (8192-8) 1.605 +#define FILEBUFFER_TOLERANCE 4096 1.606 + 1.607 +// ** Constructor/Destructor 1.608 + 1.609 +// Hidden constructor 1.610 +// Not supposed to be used 1.611 +BufferedFile::BufferedFile() : DelegatedFile(0) 1.612 +{ 1.613 + pBuffer = (UByte*)OVR_ALLOC(FILEBUFFER_SIZE); 1.614 + BufferMode = NoBuffer; 1.615 + FilePos = 0; 1.616 + Pos = 0; 1.617 + DataSize = 0; 1.618 +} 1.619 + 1.620 +// Takes another file as source 1.621 +BufferedFile::BufferedFile(File *pfile) : DelegatedFile(pfile) 1.622 +{ 1.623 + pBuffer = (UByte*)OVR_ALLOC(FILEBUFFER_SIZE); 1.624 + BufferMode = NoBuffer; 1.625 + FilePos = pfile->LTell(); 1.626 + Pos = 0; 1.627 + DataSize = 0; 1.628 +} 1.629 + 1.630 + 1.631 +// Destructor 1.632 +BufferedFile::~BufferedFile() 1.633 +{ 1.634 + // Flush in case there's data 1.635 + if (pFile) 1.636 + FlushBuffer(); 1.637 + // Get rid of buffer 1.638 + if (pBuffer) 1.639 + OVR_FREE(pBuffer); 1.640 +} 1.641 + 1.642 +/* 1.643 +bool BufferedFile::VCopy(const Object &source) 1.644 +{ 1.645 + if (!DelegatedFile::VCopy(source)) 1.646 + return 0; 1.647 + 1.648 + // Data members 1.649 + BufferedFile *psource = (BufferedFile*)&source; 1.650 + 1.651 + // Buffer & the mode it's in 1.652 + pBuffer = psource->pBuffer; 1.653 + BufferMode = psource->BufferMode; 1.654 + Pos = psource->Pos; 1.655 + DataSize = psource->DataSize; 1.656 + return 1; 1.657 +} 1.658 +*/ 1.659 + 1.660 +// Initializes buffering to a certain mode 1.661 +bool BufferedFile::SetBufferMode(BufferModeType mode) 1.662 +{ 1.663 + if (!pBuffer) 1.664 + return false; 1.665 + if (mode == BufferMode) 1.666 + return true; 1.667 + 1.668 + FlushBuffer(); 1.669 + 1.670 + // Can't set write mode if we can't write 1.671 + if ((mode==WriteBuffer) && (!pFile || !pFile->IsWritable()) ) 1.672 + return 0; 1.673 + 1.674 + // And SetMode 1.675 + BufferMode = mode; 1.676 + Pos = 0; 1.677 + DataSize = 0; 1.678 + return 1; 1.679 +} 1.680 + 1.681 +// Flushes buffer 1.682 +void BufferedFile::FlushBuffer() 1.683 +{ 1.684 + switch(BufferMode) 1.685 + { 1.686 + case WriteBuffer: 1.687 + // Write data in buffer 1.688 + FilePos += pFile->Write(pBuffer,Pos); 1.689 + Pos = 0; 1.690 + break; 1.691 + 1.692 + case ReadBuffer: 1.693 + // Seek back & reset buffer data 1.694 + if ((DataSize-Pos)>0) 1.695 + FilePos = pFile->LSeek(-(int)(DataSize-Pos), Seek_Cur); 1.696 + DataSize = 0; 1.697 + Pos = 0; 1.698 + break; 1.699 + default: 1.700 + // not handled! 1.701 + break; 1.702 + } 1.703 +} 1.704 + 1.705 +// Reloads data for ReadBuffer 1.706 +void BufferedFile::LoadBuffer() 1.707 +{ 1.708 + if (BufferMode == ReadBuffer) 1.709 + { 1.710 + // We should only reload once all of pre-loaded buffer is consumed. 1.711 + OVR_ASSERT(Pos == DataSize); 1.712 + 1.713 + // WARNING: Right now LoadBuffer() assumes the buffer's empty 1.714 + int sz = pFile->Read(pBuffer,FILEBUFFER_SIZE); 1.715 + DataSize = sz<0 ? 0 : (unsigned)sz; 1.716 + Pos = 0; 1.717 + FilePos += DataSize; 1.718 + } 1.719 +} 1.720 + 1.721 + 1.722 +// ** Overridden functions 1.723 + 1.724 +// We override all the functions that can possibly 1.725 +// require buffer mode switch, flush, or extra calculations 1.726 + 1.727 +// Tell() requires buffer adjustment 1.728 +int BufferedFile::Tell() 1.729 +{ 1.730 + if (BufferMode == ReadBuffer) 1.731 + return int (FilePos - DataSize + Pos); 1.732 + 1.733 + int pos = pFile->Tell(); 1.734 + // Adjust position based on buffer mode & data 1.735 + if (pos!=-1) 1.736 + { 1.737 + OVR_ASSERT(BufferMode != ReadBuffer); 1.738 + if (BufferMode == WriteBuffer) 1.739 + pos += Pos; 1.740 + } 1.741 + return pos; 1.742 +} 1.743 + 1.744 +SInt64 BufferedFile::LTell() 1.745 +{ 1.746 + if (BufferMode == ReadBuffer) 1.747 + return FilePos - DataSize + Pos; 1.748 + 1.749 + SInt64 pos = pFile->LTell(); 1.750 + if (pos!=-1) 1.751 + { 1.752 + OVR_ASSERT(BufferMode != ReadBuffer); 1.753 + if (BufferMode == WriteBuffer) 1.754 + pos += Pos; 1.755 + } 1.756 + return pos; 1.757 +} 1.758 + 1.759 +int BufferedFile::GetLength() 1.760 +{ 1.761 + int len = pFile->GetLength(); 1.762 + // If writing through buffer, file length may actually be bigger 1.763 + if ((len!=-1) && (BufferMode==WriteBuffer)) 1.764 + { 1.765 + int currPos = pFile->Tell() + Pos; 1.766 + if (currPos>len) 1.767 + len = currPos; 1.768 + } 1.769 + return len; 1.770 +} 1.771 +SInt64 BufferedFile::LGetLength() 1.772 +{ 1.773 + SInt64 len = pFile->LGetLength(); 1.774 + // If writing through buffer, file length may actually be bigger 1.775 + if ((len!=-1) && (BufferMode==WriteBuffer)) 1.776 + { 1.777 + SInt64 currPos = pFile->LTell() + Pos; 1.778 + if (currPos>len) 1.779 + len = currPos; 1.780 + } 1.781 + return len; 1.782 +} 1.783 + 1.784 +/* 1.785 +bool BufferedFile::Stat(FileStats *pfs) 1.786 +{ 1.787 + // Have to fix up length is stat 1.788 + if (pFile->Stat(pfs)) 1.789 + { 1.790 + if (BufferMode==WriteBuffer) 1.791 + { 1.792 + SInt64 currPos = pFile->LTell() + Pos; 1.793 + if (currPos > pfs->Size) 1.794 + { 1.795 + pfs->Size = currPos; 1.796 + // ?? 1.797 + pfs->Blocks = (pfs->Size+511) >> 9; 1.798 + } 1.799 + } 1.800 + return 1; 1.801 + } 1.802 + return 0; 1.803 +} 1.804 +*/ 1.805 + 1.806 +int BufferedFile::Write(const UByte *psourceBuffer, int numBytes) 1.807 +{ 1.808 + if ( (BufferMode==WriteBuffer) || SetBufferMode(WriteBuffer)) 1.809 + { 1.810 + // If not data space in buffer, flush 1.811 + if ((FILEBUFFER_SIZE-(int)Pos)<numBytes) 1.812 + { 1.813 + FlushBuffer(); 1.814 + // If bigger then tolerance, just write directly 1.815 + if (numBytes>FILEBUFFER_TOLERANCE) 1.816 + { 1.817 + int sz = pFile->Write(psourceBuffer,numBytes); 1.818 + if (sz > 0) 1.819 + FilePos += sz; 1.820 + return sz; 1.821 + } 1.822 + } 1.823 + 1.824 + // Enough space in buffer.. so copy to it 1.825 + memcpy(pBuffer+Pos, psourceBuffer, numBytes); 1.826 + Pos += numBytes; 1.827 + return numBytes; 1.828 + } 1.829 + int sz = pFile->Write(psourceBuffer,numBytes); 1.830 + if (sz > 0) 1.831 + FilePos += sz; 1.832 + return sz; 1.833 +} 1.834 + 1.835 +int BufferedFile::Read(UByte *pdestBuffer, int numBytes) 1.836 +{ 1.837 + if ( (BufferMode==ReadBuffer) || SetBufferMode(ReadBuffer)) 1.838 + { 1.839 + // Data in buffer... copy it 1.840 + if ((int)(DataSize-Pos) >= numBytes) 1.841 + { 1.842 + memcpy(pdestBuffer, pBuffer+Pos, numBytes); 1.843 + Pos += numBytes; 1.844 + return numBytes; 1.845 + } 1.846 + 1.847 + // Not enough data in buffer, copy buffer 1.848 + int readBytes = DataSize-Pos; 1.849 + memcpy(pdestBuffer, pBuffer+Pos, readBytes); 1.850 + numBytes -= readBytes; 1.851 + pdestBuffer += readBytes; 1.852 + Pos = DataSize; 1.853 + 1.854 + // Don't reload buffer if more then tolerance 1.855 + // (No major advantage, and we don't want to write a loop) 1.856 + if (numBytes>FILEBUFFER_TOLERANCE) 1.857 + { 1.858 + numBytes = pFile->Read(pdestBuffer,numBytes); 1.859 + if (numBytes > 0) 1.860 + { 1.861 + FilePos += numBytes; 1.862 + Pos = DataSize = 0; 1.863 + } 1.864 + return readBytes + ((numBytes==-1) ? 0 : numBytes); 1.865 + } 1.866 + 1.867 + // Reload the buffer 1.868 + // WARNING: Right now LoadBuffer() assumes the buffer's empty 1.869 + LoadBuffer(); 1.870 + if ((int)(DataSize-Pos) < numBytes) 1.871 + numBytes = (int)DataSize-Pos; 1.872 + 1.873 + memcpy(pdestBuffer, pBuffer+Pos, numBytes); 1.874 + Pos += numBytes; 1.875 + return numBytes + readBytes; 1.876 + 1.877 + /* 1.878 + // Alternative Read implementation. The one above is probably better 1.879 + // due to FILEBUFFER_TOLERANCE. 1.880 + int total = 0; 1.881 + 1.882 + do { 1.883 + int bufferBytes = (int)(DataSize-Pos); 1.884 + int copyBytes = (bufferBytes > numBytes) ? numBytes : bufferBytes; 1.885 + 1.886 + memcpy(pdestBuffer, pBuffer+Pos, copyBytes); 1.887 + numBytes -= copyBytes; 1.888 + pdestBuffer += copyBytes; 1.889 + Pos += copyBytes; 1.890 + total += copyBytes; 1.891 + 1.892 + if (numBytes == 0) 1.893 + break; 1.894 + LoadBuffer(); 1.895 + 1.896 + } while (DataSize > 0); 1.897 + 1.898 + return total; 1.899 + */ 1.900 + } 1.901 + int sz = pFile->Read(pdestBuffer,numBytes); 1.902 + if (sz > 0) 1.903 + FilePos += sz; 1.904 + return sz; 1.905 +} 1.906 + 1.907 + 1.908 +int BufferedFile::SkipBytes(int numBytes) 1.909 +{ 1.910 + int skippedBytes = 0; 1.911 + 1.912 + // Special case for skipping a little data in read buffer 1.913 + if (BufferMode==ReadBuffer) 1.914 + { 1.915 + skippedBytes = (((int)DataSize-(int)Pos) >= numBytes) ? numBytes : (DataSize-Pos); 1.916 + Pos += skippedBytes; 1.917 + numBytes -= skippedBytes; 1.918 + } 1.919 + 1.920 + if (numBytes) 1.921 + { 1.922 + numBytes = pFile->SkipBytes(numBytes); 1.923 + // Make sure we return the actual number skipped, or error 1.924 + if (numBytes!=-1) 1.925 + { 1.926 + skippedBytes += numBytes; 1.927 + FilePos += numBytes; 1.928 + Pos = DataSize = 0; 1.929 + } 1.930 + else if (skippedBytes <= 0) 1.931 + skippedBytes = -1; 1.932 + } 1.933 + return skippedBytes; 1.934 +} 1.935 + 1.936 +int BufferedFile::BytesAvailable() 1.937 +{ 1.938 + int available = pFile->BytesAvailable(); 1.939 + // Adjust available size based on buffers 1.940 + switch(BufferMode) 1.941 + { 1.942 + case ReadBuffer: 1.943 + available += DataSize-Pos; 1.944 + break; 1.945 + case WriteBuffer: 1.946 + available -= Pos; 1.947 + if (available<0) 1.948 + available= 0; 1.949 + break; 1.950 + default: 1.951 + break; 1.952 + } 1.953 + return available; 1.954 +} 1.955 + 1.956 +bool BufferedFile::Flush() 1.957 +{ 1.958 + FlushBuffer(); 1.959 + return pFile->Flush(); 1.960 +} 1.961 + 1.962 +// Seeking could be optimized better.. 1.963 +int BufferedFile::Seek(int offset, int origin) 1.964 +{ 1.965 + if (BufferMode == ReadBuffer) 1.966 + { 1.967 + if (origin == Seek_Cur) 1.968 + { 1.969 + // Seek can fall either before or after Pos in the buffer, 1.970 + // but it must be within bounds. 1.971 + if (((unsigned(offset) + Pos)) <= DataSize) 1.972 + { 1.973 + Pos += offset; 1.974 + return int (FilePos - DataSize + Pos); 1.975 + } 1.976 + 1.977 + // Lightweight buffer "Flush". We do this to avoid an extra seek 1.978 + // back operation which would take place if we called FlushBuffer directly. 1.979 + origin = Seek_Set; 1.980 + OVR_ASSERT(((FilePos - DataSize + Pos) + (UInt64)offset) < ~(UInt64)0); 1.981 + offset = (int)(FilePos - DataSize + Pos) + offset; 1.982 + Pos = DataSize = 0; 1.983 + } 1.984 + else if (origin == Seek_Set) 1.985 + { 1.986 + if (((unsigned)offset - (FilePos-DataSize)) <= DataSize) 1.987 + { 1.988 + OVR_ASSERT((FilePos-DataSize) < ~(UInt64)0); 1.989 + Pos = (unsigned)offset - (unsigned)(FilePos-DataSize); 1.990 + return offset; 1.991 + } 1.992 + Pos = DataSize = 0; 1.993 + } 1.994 + else 1.995 + { 1.996 + FlushBuffer(); 1.997 + } 1.998 + } 1.999 + else 1.1000 + { 1.1001 + FlushBuffer(); 1.1002 + } 1.1003 + 1.1004 + /* 1.1005 + // Old Seek Logic 1.1006 + if (origin == Seek_Cur && offset + Pos < DataSize) 1.1007 + { 1.1008 + //OVR_ASSERT((FilePos - DataSize) >= (FilePos - DataSize + Pos + offset)); 1.1009 + Pos += offset; 1.1010 + OVR_ASSERT(int (Pos) >= 0); 1.1011 + return int (FilePos - DataSize + Pos); 1.1012 + } 1.1013 + else if (origin == Seek_Set && unsigned(offset) >= FilePos - DataSize && unsigned(offset) < FilePos) 1.1014 + { 1.1015 + Pos = unsigned(offset - FilePos + DataSize); 1.1016 + OVR_ASSERT(int (Pos) >= 0); 1.1017 + return int (FilePos - DataSize + Pos); 1.1018 + } 1.1019 + 1.1020 + FlushBuffer(); 1.1021 + */ 1.1022 + 1.1023 + 1.1024 + FilePos = pFile->Seek(offset,origin); 1.1025 + return int (FilePos); 1.1026 +} 1.1027 + 1.1028 +SInt64 BufferedFile::LSeek(SInt64 offset, int origin) 1.1029 +{ 1.1030 + if (BufferMode == ReadBuffer) 1.1031 + { 1.1032 + if (origin == Seek_Cur) 1.1033 + { 1.1034 + // Seek can fall either before or after Pos in the buffer, 1.1035 + // but it must be within bounds. 1.1036 + if (((unsigned(offset) + Pos)) <= DataSize) 1.1037 + { 1.1038 + Pos += (unsigned)offset; 1.1039 + return SInt64(FilePos - DataSize + Pos); 1.1040 + } 1.1041 + 1.1042 + // Lightweight buffer "Flush". We do this to avoid an extra seek 1.1043 + // back operation which would take place if we called FlushBuffer directly. 1.1044 + origin = Seek_Set; 1.1045 + offset = (SInt64)(FilePos - DataSize + Pos) + offset; 1.1046 + Pos = DataSize = 0; 1.1047 + } 1.1048 + else if (origin == Seek_Set) 1.1049 + { 1.1050 + if (((UInt64)offset - (FilePos-DataSize)) <= DataSize) 1.1051 + { 1.1052 + Pos = (unsigned)((UInt64)offset - (FilePos-DataSize)); 1.1053 + return offset; 1.1054 + } 1.1055 + Pos = DataSize = 0; 1.1056 + } 1.1057 + else 1.1058 + { 1.1059 + FlushBuffer(); 1.1060 + } 1.1061 + } 1.1062 + else 1.1063 + { 1.1064 + FlushBuffer(); 1.1065 + } 1.1066 + 1.1067 +/* 1.1068 + OVR_ASSERT(BufferMode != NoBuffer); 1.1069 + 1.1070 + if (origin == Seek_Cur && offset + Pos < DataSize) 1.1071 + { 1.1072 + Pos += int (offset); 1.1073 + return FilePos - DataSize + Pos; 1.1074 + } 1.1075 + else if (origin == Seek_Set && offset >= SInt64(FilePos - DataSize) && offset < SInt64(FilePos)) 1.1076 + { 1.1077 + Pos = unsigned(offset - FilePos + DataSize); 1.1078 + return FilePos - DataSize + Pos; 1.1079 + } 1.1080 + 1.1081 + FlushBuffer(); 1.1082 + */ 1.1083 + 1.1084 + FilePos = pFile->LSeek(offset,origin); 1.1085 + return FilePos; 1.1086 +} 1.1087 + 1.1088 +int BufferedFile::CopyFromStream(File *pstream, int byteSize) 1.1089 +{ 1.1090 + // We can't rely on overridden Write() 1.1091 + // because delegation doesn't override virtual pointers 1.1092 + // So, just re-implement 1.1093 + UByte buff[0x4000]; 1.1094 + int count = 0; 1.1095 + int szRequest, szRead, szWritten; 1.1096 + 1.1097 + while(byteSize) 1.1098 + { 1.1099 + szRequest = (byteSize > int(sizeof(buff))) ? int(sizeof(buff)) : byteSize; 1.1100 + 1.1101 + szRead = pstream->Read(buff,szRequest); 1.1102 + szWritten = 0; 1.1103 + if (szRead > 0) 1.1104 + szWritten = Write(buff,szRead); 1.1105 + 1.1106 + count +=szWritten; 1.1107 + byteSize-=szWritten; 1.1108 + if (szWritten < szRequest) 1.1109 + break; 1.1110 + } 1.1111 + return count; 1.1112 +} 1.1113 + 1.1114 +// Closing files 1.1115 +bool BufferedFile::Close() 1.1116 +{ 1.1117 + switch(BufferMode) 1.1118 + { 1.1119 + case WriteBuffer: 1.1120 + FlushBuffer(); 1.1121 + break; 1.1122 + case ReadBuffer: 1.1123 + // No need to seek back on close 1.1124 + BufferMode = NoBuffer; 1.1125 + break; 1.1126 + default: 1.1127 + break; 1.1128 + } 1.1129 + return pFile->Close(); 1.1130 +} 1.1131 + 1.1132 + 1.1133 +// ***** Global path helpers 1.1134 + 1.1135 +// Find trailing short filename in a path. 1.1136 +const char* OVR_CDECL GetShortFilename(const char* purl) 1.1137 +{ 1.1138 + UPInt len = OVR_strlen(purl); 1.1139 + for (UPInt i=len; i>0; i--) 1.1140 + if (purl[i]=='\\' || purl[i]=='/') 1.1141 + return purl+i+1; 1.1142 + return purl; 1.1143 +} 1.1144 + 1.1145 +} // OVR 1.1146 +