Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. #include "kosSyst.h"
  2. #include "kosfile.h"
  3.  
  4.  
  5. CKosFile::CKosFile(char *fileName)
  6. {
  7.         //
  8.         this->fileInfo.bufferPtr = new Byte[FILE_BUFFER_SIZE];
  9.         //
  10.         this->filePointer = 0;
  11.         this->bufferPointer = 0;
  12.         this->validBuffer = false;
  13.         //
  14.         strcpy( this->fileInfo.fileURL, fileName );
  15. }
  16.  
  17.  
  18. CKosFile::~CKosFile(void)
  19. {
  20.         //
  21.         delete this->fileInfo.bufferPtr;
  22. }
  23.  
  24.  
  25. void CKosFile::ValidateBuffer()
  26. {
  27.         //
  28.         if ( this->validBuffer )
  29.         {
  30.                 //
  31.                 if ( this->filePointer < this->bufferPointer
  32.                         || this->filePointer >= (this->bufferPointer + FILE_BUFFER_SIZE) )
  33.                 {
  34.                         //
  35.                         this->validBuffer = false;
  36.                 }
  37.         }
  38. }
  39.  
  40.  
  41. void CKosFile::UpdateBuffer(void)
  42. {
  43.         //
  44.         if ( ! this->validBuffer )
  45.         {
  46.                 //
  47.                 this->fileInfo.OffsetLow = this->filePointer / OS_BLOCK_SIZE;
  48.                 this->fileInfo.OffsetHigh = 0;
  49.                 //
  50.                 this->bufferPointer = this->fileInfo.OffsetLow * OS_BLOCK_SIZE;
  51.                 //
  52.                 this->fileInfo.dataCount = FILE_BUFFER_BLOCKS;
  53.                 //
  54.                 this->fileInfo.rwMode = 0;
  55.                 //
  56.                 Dword rr = kos_FileSystemAccess( &(this->fileInfo) );
  57.                 this->validBuffer = ( rr == 0 ) || ( rr == 6 );
  58.         }
  59. }
  60.  
  61.  
  62. int CKosFile::Seek(int seekFrom, int seekStep)
  63. {
  64.         //
  65.         switch ( seekFrom )
  66.         {
  67.         //
  68.         case SEEK_SET:
  69.                 //
  70.                 this->filePointer = seekStep;
  71.                 break;
  72.         //
  73.         case SEEK_CUR:
  74.                 //
  75.                 this->filePointer += seekStep;
  76.                 break;
  77.         }
  78.         //
  79.         this->ValidateBuffer();
  80.         //
  81.         return this->filePointer;
  82. }
  83.  
  84.  
  85. int     CKosFile::Read(Byte *targetPtr, int readCount)
  86. {
  87.         int bufferLeast, result;
  88.  
  89.         //
  90.         result = 0;
  91.         //
  92.         do
  93.         {
  94.                 //
  95.                 this->UpdateBuffer();
  96.                 //
  97.                 if ( ! this->validBuffer ) return result;
  98.                 //
  99.                 bufferLeast = FILE_BUFFER_SIZE - (this->filePointer - this->bufferPointer);
  100.                 //
  101.                 if ( bufferLeast > readCount ) bufferLeast = readCount;
  102.                 //
  103.                 if ( bufferLeast )
  104.                 {
  105.                         //
  106.                         memcpy(
  107.                                 targetPtr,
  108.                                 this->fileInfo.bufferPtr + (this->filePointer - this->bufferPointer),
  109.                                 bufferLeast
  110.                                 );
  111.                         //
  112.                         targetPtr += bufferLeast;
  113.                         readCount -= bufferLeast;
  114.                         this->filePointer += bufferLeast;
  115.                         //
  116.                         result += bufferLeast;
  117.                 }
  118.                 //
  119.                 this->ValidateBuffer();
  120.         }
  121.         while ( readCount > 0 );
  122.         //
  123.         return result;
  124. }
  125.  
  126.  
  127. int CKosFile::Write(Byte *sourcePtr, int writeCount)
  128. {
  129.         return 0;
  130. }
  131.  
  132.