Advertisement
holmbergfan

Untitled

Aug 13th, 2022
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. inline bool FileLoader::readBytes(unsigned char* buffer, unsigned int size, long pos)
  2. {
  3.     if(m_use_cache){
  4.         //seek at pos
  5.         unsigned long reading, remain = size, bufferPos = 0;
  6.         do{
  7.             //prepare cache
  8.             unsigned long i = getCacheBlock(pos);
  9.             if(i == NO_VALID_CACHE)
  10.                 return false;
  11.  
  12.             m_cache_index = i;
  13.             m_cache_offset = pos - m_cached_data[i].base;
  14.  
  15.             //get maximun read block size and calculate remaining bytes
  16.             reading = std::min(remain, m_cached_data[i].size - m_cache_offset);
  17.             remain = remain - reading;
  18.  
  19.             //read it
  20.             memcpy(buffer + bufferPos, m_cached_data[m_cache_index].data + m_cache_offset, reading);
  21.  
  22.             //update variables
  23.             m_cache_offset = m_cache_offset + reading;
  24.             bufferPos = bufferPos + reading;
  25.             pos = pos + reading;
  26.         }while(remain > 0);
  27.         return true;
  28.     }
  29.     else{
  30.         if(fseek(m_file, pos, SEEK_SET)){
  31.             m_lastError = ERROR_SEEK_ERROR;
  32.             return false;
  33.         }
  34.         size_t value = fread(buffer, 1, size, m_file);
  35.         if(value != size){
  36.             m_lastError = ERROR_EOF;
  37.             return false;
  38.         }
  39.         else{
  40.             return true;
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement