* Sets a file's position. * * \param[in] pos The new position in bytes from the beginning of the file. * * \return true for success, false for failure. */
| 1913 | * \return true for success, false for failure. |
| 1914 | */ |
| 1915 | bool SdBaseFile::seekSet(const uint32_t pos) { |
| 1916 | uint32_t nCur, nNew; |
| 1917 | // error if file not open or seek past end of file |
| 1918 | if (!isOpen() || pos > fileSize_) return false; |
| 1919 | |
| 1920 | if (type_ == FAT_FILE_TYPE_ROOT_FIXED) { |
| 1921 | curPosition_ = pos; |
| 1922 | return true; |
| 1923 | } |
| 1924 | if (pos == 0) { |
| 1925 | curCluster_ = curPosition_ = 0; // set position to start of file |
| 1926 | return true; |
| 1927 | } |
| 1928 | |
| 1929 | // calculate cluster index for cur and new position |
| 1930 | nCur = (curPosition_ - 1) >> (vol_->clusterSizeShift_ + 9); |
| 1931 | nNew = (pos - 1) >> (vol_->clusterSizeShift_ + 9); |
| 1932 | |
| 1933 | if (nNew < nCur || curPosition_ == 0) |
| 1934 | curCluster_ = firstCluster_; // must follow chain from first cluster |
| 1935 | else |
| 1936 | nNew -= nCur; // advance from curPosition |
| 1937 | |
| 1938 | while (nNew--) |
| 1939 | if (!vol_->fatGet(curCluster_, &curCluster_)) return false; |
| 1940 | |
| 1941 | curPosition_ = pos; |
| 1942 | return true; |
| 1943 | } |
| 1944 | |
| 1945 | void SdBaseFile::setpos(filepos_t * const pos) { |
| 1946 | curPosition_ = pos->position; |
no test coverage detected