remove obsolete filemanager methods

This commit is contained in:
Jorj Bauer 2020-06-28 09:21:27 -04:00
parent 1e762440a5
commit fc47415592
6 changed files with 122 additions and 300 deletions

View File

@ -103,16 +103,9 @@ class FileManager {
virtual int8_t openFile(const char *name) = 0;
virtual void closeFile(int8_t fd) = 0;
virtual void truncate(int8_t fd) = 0;
virtual const char *fileName(int8_t fd) = 0;
virtual int8_t readDir(const char *where, const char *suffix, char *outputFN, int8_t startIdx, uint16_t maxlen) = 0;
virtual void seekBlock(int8_t fd, uint16_t block, bool isNib = false) = 0;
virtual bool readTrack(int8_t fd, uint8_t *toWhere, bool isNib = false) = 0;
virtual bool readBlock(int8_t fd, uint8_t *toWhere, bool isNib = false) = 0;
virtual bool writeBlock(int8_t fd, uint8_t *fromWhere, bool isNib = false) = 0;
virtual bool writeTrack(int8_t fd, uint8_t *fromWhere, bool isNib = false) = 0;
virtual uint8_t readByteAt(int8_t fd, uint32_t pos) = 0;
virtual bool writeByteAt(int8_t fd, uint8_t v, uint32_t pos) = 0;
@ -128,33 +121,9 @@ class FileManager {
virtual bool setSeekPosition(int8_t fd, uint32_t pos) = 0;
virtual void seekToEnd(int8_t fd) = 0;
int write(int8_t fd, const void *buf, int nbyte) {
uint8_t *p = (uint8_t *)buf;
for (int i=0; i<nbyte; i++) {
if (!writeByte(fd, p[i]))
return -1;
}
return nbyte;
};
int read(int8_t fd, void *buf, int nbyte) {
uint8_t *p = (uint8_t *)buf;
for (int i=0; i<nbyte; i++) {
p[i] = readByte(fd); // FIXME: no error handling
}
return nbyte;
};
int seek(int8_t fd, int offset, int whence) {
if (whence == SEEK_CUR && offset == 0) {
return fileSeekPositions[fd];
}
if (whence == SEEK_SET) {
if (!setSeekPosition(fd, offset))
return -1;
return offset;
}
// Other cases not supported yet
return -1;
};
virtual int write(int8_t fd, const void *buf, int nbyte) = 0;
virtual int read(int8_t fd, void *buf, int nbyte) = 0;
virtual int lseek(int8_t fd, int offset, int whence) = 0;
protected:
unsigned long fileSeekPositions[MAXFILES];

View File

@ -56,12 +56,6 @@ void NixFileManager::closeFile(int8_t fd)
cachedNames[fd][0] = '\0';
}
void NixFileManager::truncate(int8_t fd)
{
FILE *f = fopen(cachedNames[fd], "w+");
fclose(f);
}
const char *NixFileManager::fileName(int8_t fd)
{
if (fd < 0 || fd >= numCached)
@ -159,127 +153,6 @@ int8_t NixFileManager::readDir(const char *where, const char *suffix, char *outp
return idx;
}
void NixFileManager::seekBlock(int8_t fd, uint16_t block, bool isNib)
{
if (fd < 0 || fd >= numCached)
return;
if (isNib) {
fileSeekPositions[fd] = block * 416;
} else {
fileSeekPositions[fd] = block * 256;
}
}
bool NixFileManager::readTrack(int8_t fd, uint8_t *toWhere, bool isNib)
{
if (fd < 0 || fd >= numCached)
return false;
if (cachedNames[fd][0] == 0)
return false;
// open, seek, read, close.
bool ret = false;
int ffd = open(cachedNames[fd], O_RDONLY);
if (ffd != -1) {
lseek(ffd, fileSeekPositions[fd], SEEK_SET);
if (isNib) {
ret = (read(ffd, toWhere, 0x1A00) == 0x1A00);
} else {
ret = (read(ffd, toWhere, 256 * 16) == 256 * 16);
}
close(ffd);
}
return ret;
}
bool NixFileManager::readBlock(int8_t fd, uint8_t *toWhere, bool isNib)
{
// open, seek, read, close.
if (fd < 0 || fd >= numCached)
return false;
if (cachedNames[fd][0] == 0)
return false;
// open, seek, read, close.
bool ret = false;
int ffd = open(cachedNames[fd], O_RDONLY);
if (ffd != -1) {
lseek(ffd, fileSeekPositions[fd], SEEK_SET);
if (isNib) {
ret = (read(ffd, toWhere, 416) == 416);
} else {
ret = (read(ffd, toWhere, 256) == 256);
}
close(ffd);
}
return ret;
}
bool NixFileManager::writeBlock(int8_t fd, uint8_t *fromWhere, bool isNib)
{
// open, seek, write, close.
if (fd < 0 || fd >= numCached)
return false;
if (cachedNames[fd][0] == 0)
return false;
// don't know how to do this without seeking through the nibblized
// track data, so just give up for now
if (isNib)
return false;
// open, seek, write, close.
int ffd = open(cachedNames[fd], O_WRONLY);
if (ffd != -1) {
if (lseek(ffd, fileSeekPositions[fd], SEEK_SET) != fileSeekPositions[fd]) {
printf("ERROR: failed to seek to %lu\n", fileSeekPositions[fd]);
return false;
}
if (write(ffd, fromWhere, 256) != 256) {
printf("ERROR: failed to write 256 bytes\n");
return false;
}
close(ffd);
}
return true;
}
bool NixFileManager::writeTrack(int8_t fd, uint8_t *fromWhere, bool isNib)
{
// open, seek, write, close.
if (fd < 0 || fd >= numCached)
return false;
if (cachedNames[fd][0] == 0)
return false;
// open, seek, write, close.
int ffd = open(cachedNames[fd], O_WRONLY);
if (ffd != -1) {
if (lseek(ffd, fileSeekPositions[fd], SEEK_SET) != fileSeekPositions[fd]) {
printf("ERROR: failed to seek to %lu\n", fileSeekPositions[fd]);
return false;
}
int16_t wrsize = 256 * 16;
if (isNib)
wrsize = 0x1A00;
if (write(ffd, fromWhere, wrsize) != wrsize) {
printf("ERROR: failed to write bytes\n");
return false;
}
close(ffd);
}
return true;
}
uint8_t NixFileManager::readByteAt(int8_t fd, uint32_t pos)
{
if (fd < 0 || fd >= numCached)
@ -421,3 +294,36 @@ void NixFileManager::seekToEnd(int8_t fd)
fclose(f);
}
}
int NixFileManager::write(int8_t fd, const void *buf, int nbyte)
{
uint8_t *p = (uint8_t *)buf;
for (int i=0; i<nbyte; i++) {
if (!writeByte(fd, p[i]))
return -1;
}
return nbyte;
};
int NixFileManager::read(int8_t fd, void *buf, int nbyte)
{
uint8_t *p = (uint8_t *)buf;
for (int i=0; i<nbyte; i++) {
p[i] = readByte(fd); // FIXME: no error handling
}
return nbyte;
};
int NixFileManager::lseek(int8_t fd, int offset, int whence)
{
if (whence == SEEK_CUR && offset == 0) {
return fileSeekPositions[fd];
}
if (whence == SEEK_SET) {
if (!setSeekPosition(fd, offset))
return -1;
return offset;
}
// Other cases not supported yet
return -1;
};

View File

@ -12,16 +12,9 @@ class NixFileManager : public FileManager {
virtual int8_t openFile(const char *name);
virtual void closeFile(int8_t fd);
virtual void truncate(int8_t fd);
virtual const char *fileName(int8_t fd);
virtual int8_t readDir(const char *where, const char *suffix, char *outputFN, int8_t startIdx, uint16_t maxlen);
virtual void seekBlock(int8_t fd, uint16_t block, bool isNib = false);
virtual bool readTrack(int8_t fd, uint8_t *toWhere, bool isNib = false);
virtual bool readBlock(int8_t fd, uint8_t *toWhere, bool isNib = false);
virtual bool writeBlock(int8_t fd, uint8_t *fromWhere, bool isNib = false);
virtual bool writeTrack(int8_t fd, uint8_t *fromWhere, bool isNib = false);
virtual uint8_t readByteAt(int8_t fd, uint32_t pos);
virtual bool writeByteAt(int8_t fd, uint8_t v, uint32_t pos);
@ -34,6 +27,10 @@ class NixFileManager : public FileManager {
virtual bool setSeekPosition(int8_t fd, uint32_t pos);
virtual void seekToEnd(int8_t fd);
virtual int write(int8_t fd, const void *buf, int nbyte);
virtual int read(int8_t fd, void *buf, int nbyte);
virtual int lseek(int8_t fd, int offset, int whence);
private:
int8_t numCached;

View File

@ -69,11 +69,6 @@ void TeensyFileManager::closeFile(int8_t fd)
cachedNames[fd][0] = '\0';
}
void TeensyFileManager::truncate(int8_t fd)
{
/* Not used in the code anywhere, yet, and unimplemented here... */
}
const char *TeensyFileManager::fileName(int8_t fd)
{
if (fd < 0 || fd >= numCached)
@ -155,118 +150,6 @@ int8_t TeensyFileManager::readDir(const char *where, const char *suffix, char *o
/* NOTREACHED */
}
void TeensyFileManager::seekBlock(int8_t fd, uint16_t block, bool isNib)
{
if (fd < 0 || fd >= numCached)
return;
fileSeekPositions[fd] = block * (isNib ? 416 : 256);
}
bool TeensyFileManager::readTrack(int8_t fd, uint8_t *toWhere, bool isNib)
{
if (fd < 0 || fd >= numCached)
return false;
if (cachedNames[fd][0] == 0)
return false;
// open, seek, read, close.
File f = sd.open(cachedNames[fd], FILE_READ);
if (!f) {
Serial.println("failed to open");
return false;
}
if (!f.seek(fileSeekPositions[fd])) {
Serial.println("readTrack: seek failed");
f.close();
return false;
}
int nRead = f.read(toWhere, isNib ? 0x1a00 : (256 * 16));
f.close();
return (nRead == (isNib ? 0x1a00 : (256 * 16)));
}
bool TeensyFileManager::readBlock(int8_t fd, uint8_t *toWhere, bool isNib)
{
// open, seek, read, close.
if (fd < 0 || fd >= numCached)
return false;
if (cachedNames[fd][0] == 0)
return false;
// open, seek, read, close.
File f = sd.open(cachedNames[fd], FILE_READ);
if (!f) {
Serial.println("failed to open");
return false;
}
if (!f.seek(fileSeekPositions[fd])) {
Serial.println("readBlock: seek failed");
f.close();
return false;
}
int nRead = f.read(toWhere, isNib ? 416 : 256);
f.close();
return (nRead == (isNib ? 416 : 256));
}
bool TeensyFileManager::writeBlock(int8_t fd, uint8_t *fromWhere, bool isNib)
{
// open, seek, write, close.
if (fd < 0 || fd >= numCached)
return false;
if (cachedNames[fd][0] == 0)
return false;
// can't write just a single block of a nibblized track
if (isNib)
return false;
// open, seek, write, close.
File f = sd.open(cachedNames[fd], FILE_WRITE);
if (!f ||
!f.seek(fileSeekPositions[fd])) {
f.close();
return false;
}
int nWritten = f.write(fromWhere, 256);
f.close();
return (nWritten == 256);
}
bool TeensyFileManager::writeTrack(int8_t fd, uint8_t *fromWhere, bool isNib)
{
// open, seek, write, close.
if (fd < 0 || fd >= numCached)
return false;
if (cachedNames[fd][0] == 0)
return false;
// open, seek, write, close.
File f = sd.open(cachedNames[fd], FILE_WRITE);
if (!f)
return false;
if (!f.seek(fileSeekPositions[fd])) {
f.close();
return false;
}
int nWritten = f.write(fromWhere, isNib ? 0x1a00 : (256*16));
f.close();
return (nWritten == (isNib ? 0x1a00 : (256*16)));
}
bool TeensyFileManager::_prepCache(int8_t fd)
{
if (rawFd == -1 ||
@ -427,3 +310,75 @@ void TeensyFileManager::seekToEnd(int8_t fd)
f.close();
}
int TeensyFileManager::write(int8_t fd, const void *buf, int nbyte)
{
// open, seek, write, close.
if (fd < 0 || fd >= numCached) {
Serial.println("failed write - invalid fd");
return -1;
}
if (cachedNames[fd][0] == 0) {
Serial.println("failed write - no cache name");
return -1;
}
_prepCache(fd);
uint32_t pos = fileSeekPositions[fd];
if (!rawFile.seek(pos)) {
return -1;
}
if (rawFile.write(buf, nbyte) != nbyte) {
return -1;
}
fileSeekPositions[fd] += nbyte;
rawFile.close();
return nbyte;
};
int TeensyFileManager::read(int8_t fd, void *buf, int nbyte)
{
// open, seek, read, close.
if (fd < 0 || fd >= numCached)
return -1;
if (cachedNames[fd][0] == 0)
return -1;
_prepCache(fd);
uint32_t pos = fileSeekPositions[fd];
if (!rawFile.seek(pos)) {
Serial.print("readByte: seek failed to byte ");
Serial.println(pos);
return -1;
}
if (rawFile.read(buf, nbyte) != nbyte)
return -1;
fileSeekPositions[fd] += nbyte;
rawFile.close();
return nbyte;
};
int TeensyFileManager::lseek(int8_t fd, int offset, int whence)
{
if (whence == SEEK_CUR && offset == 0) {
return fileSeekPositions[fd];
}
if (whence == SEEK_SET) {
if (!setSeekPosition(fd, offset))
return -1;
return offset;
}
// Other cases not supported yet
return -1;
};

View File

@ -12,16 +12,9 @@ class TeensyFileManager : public FileManager {
virtual int8_t openFile(const char *name);
virtual void closeFile(int8_t fd);
virtual void truncate(int8_t fd);
virtual const char *fileName(int8_t fd);
virtual int8_t readDir(const char *where, const char *suffix, char *outputFN, int8_t startIdx, uint16_t maxlen);
virtual void seekBlock(int8_t fd, uint16_t block, bool isNib = false);
virtual bool readTrack(int8_t fd, uint8_t *toWhere, bool isNib = false);
virtual bool readBlock(int8_t fd, uint8_t *toWhere, bool isNib = false);
virtual bool writeBlock(int8_t fd, uint8_t *fromWhere, bool isNib = false);
virtual bool writeTrack(int8_t fd, uint8_t *fromWhere, bool isNib = false);
virtual uint8_t readByteAt(int8_t fd, uint32_t pos);
virtual bool writeByteAt(int8_t fd, uint8_t v, uint32_t pos);
@ -34,6 +27,10 @@ class TeensyFileManager : public FileManager {
virtual bool setSeekPosition(int8_t fd, uint32_t pos);
virtual void seekToEnd(int8_t fd);
virtual int write(int8_t fd, const void *buf, int nbyte);
virtual int read(int8_t fd, void *buf, int nbyte);
virtual int lseek(int8_t fd, int offset, int whence);
private:
bool _prepCache(int8_t fd);

View File

@ -40,13 +40,11 @@ static time_t getTeensy3Time() { return Teensy3Clock.get(); }
void setup()
{
Serial.begin(230400);
/* while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("hi");
*/
delay(100); // let the serial port connect if it's gonna
/*
while (!Serial) {
yield();
}*/
delay(100); // let the power settle
enableFaultHandler();