Subversion Repositories Kolibri OS

Rev

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

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