pic32/libraries/SD/utility/SdFile.cpp
//------------------------------------------------------------------------------
/**
 * Read data from a file starting at the current position.
 *
 * \param[out] buf Pointer to the location that will receive the data.
 *
 * \param[in] nbyte Maximum number of bytes to read.
 *
 * \return For success read() returns the number of bytes read.
 * A value less than \a nbyte, including zero, will be returned
 * if end of file is reached.
 * If an error occurs, read() returns -1.  Possible errors include
 * read() called before a file has been opened, corrupt file system
 * or an I/O error occurred.
 */
Read data from a file starting at the current position.
\param[out] buf Pointer to the location that will receive the data.

\param[in] nbyte Maximum number of bytes to read.

\return For success read() returns the number of bytes read.
A value less than \a nbyte, including zero, will be returned
if end of file is reached.
If an error occurs, read() returns -1.  Possible errors include
read() called before a file has been opened, corrupt file system
or an I/O error occurred.
Read data from a file starting at the current position . \ param [ out ] buf Pointer to the location that will receive the data . \ param [ in ] nbyte Maximum number of bytes to read . \ return For success read () returns the number of bytes read . A value less than \ a nbyte including zero will be returned if end of file is reached . If an error occurs read () returns - 1 . Possible errors include read () called before a file has been opened corrupt file system or an I / O error occurred .
int16_t SdFile::read(void* buf, uint16_t nbyte) {
  uint8_t* dst = reinterpret_cast<uint8_t*>(buf);
  if (!isOpen() || !(flags_ & O_READ)) return -1;
  if (nbyte > (fileSize_ - curPosition_)) nbyte = fileSize_ - curPosition_;
  uint16_t toRead = nbyte;
  while (toRead > 0) {
    uint32_t block;  
    uint16_t offset = curPosition_ & 0X1FF;  
    if (type_ == FAT_FILE_TYPE_ROOT16) {
      block = vol_->rootDirStart() + (curPosition_ >> 9);
    } else {
      uint8_t blockOfCluster = vol_->blockOfCluster(curPosition_);
      if (offset == 0 && blockOfCluster == 0) {
        if (curPosition_ == 0) {
          curCluster_ = firstCluster_;
        } else {
          if (!vol_->fatGet(curCluster_, &curCluster_)) return -1;
        }
      }
      block = vol_->clusterStartBlock(curCluster_) + blockOfCluster;
    }
    uint16_t n = toRead;
    if (n > (512 - offset)) n = 512 - offset;
    if ((unbufferedRead() || n == 512) &&
      block != SdVolume::cacheBlockNumber_) {
      if (!vol_->readData(block, offset, n, dst)) return -1;
      dst += n;
    } else {
      if (!SdVolume::cacheRawBlock(block, SdVolume::CACHE_FOR_READ)) return -1;
      uint8_t* src = SdVolume::cacheBuffer_.data + offset;
      uint8_t* end = src + n;
      while (src != end) *dst++ = *src++;
    }
    curPosition_ += n;
    toRead -= n;
  }
  return nbyte;
}
int16_t SdFile :: read ( void * buf , uint16_t nbyte ) { uint8_t * dst = reinterpret_cast < uint8_t * > ( buf ) ; if ( ! isOpen ( ) || ! ( flags_ & O_READ ) ) return -1 ; if ( nbyte > ( fileSize_ - curPosition_ ) ) nbyte = fileSize_ - curPosition_ ; uint16_t toRead = nbyte ; while ( toRead > 0 ) { uint32_t block ; uint16_t offset = curPosition_ & 0 X1FF ; if ( type_ == FAT_FILE_TYPE_ROOT16 ) { block = vol_ -> rootDirStart ( ) + ( curPosition_ >> 9 ) ; } else { uint8_t blockOfCluster = vol_ -> blockOfCluster ( curPosition_ ) ; if ( offset == 0 && blockOfCluster == 0 ) { if ( curPosition_ == 0 ) { curCluster_ = firstCluster_ ; } else { if ( ! vol_ -> fatGet ( curCluster_ , & curCluster_ ) ) return -1 ; } } block = vol_ -> clusterStartBlock ( curCluster_ ) + blockOfCluster ; } uint16_t n = toRead ; if ( n > ( 512 - offset ) ) n = 512 - offset ; if ( ( unbufferedRead ( ) || n == 512 ) && block != SdVolume :: cacheBlockNumber_ ) { if ( ! vol_ -> readData ( block , offset , n , dst ) ) return -1 ; dst += n ; } else { if ( ! SdVolume :: cacheRawBlock ( block , SdVolume :: CACHE_FOR_READ ) ) return -1 ; uint8_t * src = SdVolume :: cacheBuffer_ . data + offset ; uint8_t * end = src + n ; while ( src != end ) * dst ++ = * src ++ ; } curPosition_ += n ; toRead -= n ; } return nbyte ; }
Read data from a file starting at the current position.
Read data from a file starting at the current position .
// error if not open or write only // max bytes left in file // amount left to read // raw device block number // offset in block // start of new cluster // use first cluster in file // get next cluster from FAT // amount to be read from current block // no buffering needed if n == 512 or user requests no buffering // read block to cache and copy data to caller