switched to the new logging here and there some cleanups worked on i2S base class for files id3 parsing
195 lines
5.0 KiB
C++
195 lines
5.0 KiB
C++
class File : public ::File {
|
|
|
|
static constexpr const char* NAME = "FAT32_File";
|
|
static constexpr const uint32_t F_EOF = 0xFFFFFFFF;
|
|
|
|
FS& fs;
|
|
DirHandle h;
|
|
|
|
uint32_t curAbsPos = 0; // absolute position withithin the file
|
|
uint32_t posInCluster = 0; // position within the current cluster
|
|
uint32_t iCurCluster = 0; // current cluster index (within list of clusters)
|
|
|
|
std::vector<ClusterNr> clusters; // all clusters the file uses
|
|
|
|
public:
|
|
|
|
File(FS& fs, const DirHandle& h) : fs(fs), h(h) {
|
|
Log::addInfo(NAME, "init @ cluster %d", getFirstCluster());
|
|
getAllClusters(h.getFirstCluster());
|
|
}
|
|
|
|
/** the file's USED size */
|
|
uint32_t size() const {return h.getSize();}
|
|
|
|
/** the file's ALLOCATED size */
|
|
uint32_t getAllocatedSize() const {return clusters.size() * fs.tmp.bytesPerCluster;}
|
|
|
|
/** the file's first cluster */
|
|
ClusterNr getFirstCluster() const {return h.getFirstCluster();}
|
|
|
|
/** get the file's name */
|
|
std::string getName() const {return h.getName();}
|
|
|
|
|
|
/** current position within the file */
|
|
uint32_t curPos() const {
|
|
return curAbsPos;
|
|
}
|
|
|
|
/** seek to a new position within the file */
|
|
void seekTo(uint32_t pos) {
|
|
_seekTo(pos);
|
|
}
|
|
|
|
/** read x bytes from the file */
|
|
uint32_t read(uint32_t size, uint8_t* dst) {
|
|
|
|
Log::addInfo(NAME, "read %d bytes", size);
|
|
|
|
uint32_t remaining = size;
|
|
uint32_t totalRead = 0;
|
|
|
|
while(remaining) {
|
|
const uint32_t read = _read(remaining, dst);
|
|
if (read == F_EOF) {
|
|
Log::addInfo(NAME, "EOF"); break;
|
|
}
|
|
remaining -= read;
|
|
dst += read;
|
|
totalRead += read;
|
|
}
|
|
|
|
return totalRead;
|
|
|
|
}
|
|
|
|
/* write the given data into the file */
|
|
uint32_t write(uint32_t size, const uint8_t* src) {
|
|
|
|
Log::addInfo(NAME, "write %d bytes", size);
|
|
|
|
uint32_t remaining = size;
|
|
uint32_t totalWritten = 0;
|
|
|
|
while(remaining) {
|
|
const uint32_t written = _write(remaining, src);
|
|
remaining -= written;
|
|
src += written;
|
|
totalWritten += written;
|
|
//callback(totalWritten*100/size);
|
|
}
|
|
|
|
// update the file header (filesize might have changed)
|
|
fs.write(h);
|
|
|
|
return totalWritten;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
void setSize(uint32_t size) {h.setSize(size);}
|
|
|
|
/** fetch the list of all clusters the file is using */
|
|
void getAllClusters(ClusterNr startCluster) {
|
|
|
|
// initial cluster
|
|
clusters.push_back(startCluster);
|
|
|
|
// all following clusters
|
|
while(true) {
|
|
startCluster = fs.getNextCluster(startCluster);
|
|
if (!startCluster.isEndOfClusters()) {
|
|
clusters.push_back(startCluster);
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
|
|
// debug
|
|
//char buf[1024];
|
|
//char* dst = buf; dst += sprintf(dst, "clusters: ");
|
|
//for (ClusterNr nr : clusters) {dst += sprintf(dst, "%d ", nr);}
|
|
//Log::addInfo(NAME, buf);
|
|
|
|
}
|
|
|
|
void _seekTo(uint32_t pos) {
|
|
this->curAbsPos = pos;
|
|
this->iCurCluster = pos / fs.tmp.bytesPerCluster;
|
|
this->posInCluster = pos - (this->iCurCluster * fs.tmp.bytesPerCluster);
|
|
}
|
|
|
|
int32_t _read(uint32_t readSize, uint8_t* dst) {
|
|
|
|
// EOF reached?
|
|
if (curAbsPos >= size()) {
|
|
return F_EOF;
|
|
}
|
|
|
|
// end of current cluster reached? -> switch to the next one
|
|
if (posInCluster == fs.tmp.bytesPerCluster) {
|
|
++iCurCluster;
|
|
posInCluster = 0;
|
|
Log::addInfo(NAME, "next cluster [%d] %d", iCurCluster, clusters[iCurCluster]);
|
|
}
|
|
|
|
// how many bytes are left in the current cluster?
|
|
const uint32_t remainingInCluster = fs.tmp.bytesPerCluster - posInCluster;
|
|
|
|
// determine how many bytes to read
|
|
const uint32_t toRead = std::min(remainingInCluster, readSize);
|
|
|
|
const uint32_t offset = fs.clusterToAbsPos(clusters[iCurCluster]) + posInCluster;
|
|
const uint32_t read = fs.dev.read(offset, toRead, dst);
|
|
posInCluster += read;
|
|
curAbsPos += read;
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
int32_t _write(uint32_t writeSize, const uint8_t* src) {
|
|
|
|
// do we need to append more free sectors to the end of the file?
|
|
if (curAbsPos >= getAllocatedSize()) {
|
|
if (clusters.empty()) {
|
|
const ClusterNr newCluster = fs.allocFreeCluster(0);
|
|
clusters.push_back(newCluster);
|
|
Log::addInfo(NAME, "allocFirstCluster: %d", newCluster);
|
|
} else {
|
|
const ClusterNr prevCluster = clusters.back();
|
|
const ClusterNr newCluster = fs.allocFreeCluster(prevCluster);
|
|
clusters.push_back(newCluster);
|
|
Log::addInfo(NAME, "allocNextCluster(%d): %d", prevCluster, newCluster);
|
|
}
|
|
}
|
|
|
|
// end of current cluster reached? -> switch to the next one
|
|
if (posInCluster == fs.tmp.bytesPerCluster) {
|
|
++iCurCluster;
|
|
posInCluster = 0;
|
|
Log::addInfo(NAME, "next cluster [%d] %d", iCurCluster, clusters[iCurCluster]);
|
|
}
|
|
|
|
// how many bytes are left in the current cluster?
|
|
const uint32_t remainingInCluster = fs.tmp.bytesPerCluster - posInCluster;
|
|
|
|
// determine how many bytes to write
|
|
const uint32_t toWrite = std::min(remainingInCluster, writeSize);
|
|
|
|
const uint32_t offset = fs.clusterToAbsPos(clusters[iCurCluster]) + posInCluster;
|
|
const uint32_t written = fs.dev.write(offset, toWrite, src);
|
|
posInCluster += written;
|
|
curAbsPos += written;
|
|
|
|
// adjust the number of bytes used
|
|
if (this->size() < curAbsPos) {this->setSize(curAbsPos);}
|
|
|
|
return written;
|
|
|
|
}
|
|
|
|
};
|