f256/f256lib/f_file.c
2024-03-27 19:54:24 -05:00

492 lines
10 KiB
C

/*
* Copyright (c) 2024 Scott Duensing, scott@kangaroopunch.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef WITHOUT_FILE
#include <string.h>
#ifndef F256LIB_AMALGAMATED_BUILD
#include "f_file.h"
#endif
#define MAX_DRIVES 8
static char _dirStream[MAX_DRIVES];
static bool findName(char *name, int16_t *offset);
static int16_t kernelRead(uint8_t fd, void *buf, uint16_t nbytes);
static int16_t kernelWrite(uint8_t fd, void *buf, uint16_t nbytes);
static char *pathWithoutDrive(char *path, byte *drive);
#pragma push_macro("close")
#undef close
int8_t fileClose(uint8_t *fd) {
kernelArgs->file.close.stream = *fd;
kernelCall(File.Close);
free(fd);
for (;;) {
kernelNextEvent();
switch (kernelEventData.type) {
case kernelEvent(file.CLOSED):
case kernelEvent(file.ERROR):
return -1;
default:
continue;
}
}
return 0;
}
#pragma pop_macro("close")
#pragma push_macro("close")
#undef close
int8_t fileCloseDir(char *dir) {
if (!dir) return -1;
for (;;) {
if (*dir) {
kernelArgs->directory.close.stream = *dir;
kernelCall(Directory.Close);
if (!kernelError) {
*dir = 0;
}
}
kernelNextEvent();
if (kernelEventData.type == kernelEvent(directory.CLOSED)) {
dir = NULL;
return 0;
}
}
}
#pragma pop_macro("close")
int8_t fileMakeDir(char *dir) {
byte drive;
dir = pathWithoutDrive(dir, &drive);
kernelArgs->directory.mkdir.drive = drive;
kernelArgs->common.buf = dir;
kernelArgs->common.buflen = strlen(dir);
kernelCall(Directory.MkDir);
if (kernelError) return -1;
for (;;) {
kernelNextEvent();
if (kernelEventData.type == kernelEvent(directory.CREATED)) break;
if (kernelEventData.type == kernelEvent(directory.ERROR)) return -1;
}
return 0;
}
uint8_t *fileOpen(char *fname, char *mode) {
uint8_t ret = 0;
uint8_t m = 0; // Default to READ.
uint8_t *fd;
byte drive;
char *c;
fname = pathWithoutDrive(fname, &drive);
c = mode;
while (*c != 0) {
if (*c == 'w') m = 1; // WRITE
if (*c == 'a') m = 2; // APPEND
c++;
}
kernelArgs->common.buf = (uint8_t *)fname;
kernelArgs->common.buflen = strlen(fname);
kernelArgs->file.open.drive = drive;
kernelArgs->file.open.mode = m;
ret = kernelCall(File.Open);
if (kernelError) return NULL;
for (;;) {
kernelNextEvent();
switch (kernelEventData.type) {
case kernelEvent(file.OPENED):
fd = (uint8_t *)malloc(sizeof(uint8_t));
*fd = ret;
return fd;
case kernelEvent(file.NOT_FOUND):
case kernelEvent(file.ERROR):
return NULL;
default:
continue;
}
}
}
char *fileOpenDir(char *name) {
byte drive;
char stream;
char *dir;
name = pathWithoutDrive(name, &drive);
if (_dirStream[drive]) return NULL; // Only one at a time.
kernelArgs->directory.open.drive = drive;
kernelArgs->common.buf = name;
kernelArgs->common.buflen = strlen(name);
stream = kernelCall(Directory.Open);
if (kernelError) return NULL;
for (;;) {
kernelNextEvent();
if (kernelEventData.type == kernelEvent(directory.OPENED)) break;
if (kernelEventData.type == kernelEvent(directory.ERROR)) return NULL;
}
_dirStream[drive] = stream;
dir = &_dirStream[drive];
return dir;
}
int16_t fileRead(void *buf, uint16_t nbytes, uint16_t nmemb, uint8_t *fd) {
char *data = (char *)buf;
int16_t read = 0;
uint16_t bytes = nbytes * nmemb;
int16_t returned;
while (read < bytes) {
returned = kernelRead(*fd, data + read, bytes - read);
if (returned < 0) return -1;
if (returned == 0) break;
read += returned;
}
return read / nbytes;
}
#pragma push_macro("EOF")
#pragma push_macro("FILE")
#undef EOF
#undef FILE
fileDirEntT *fileReadDir(char *dir) {
static fileDirEntT dirent;
uint16_t len;
if (!dir) return NULL;
kernelArgs->directory.read.stream = *dir;
kernelCall(Directory.Read);
if (kernelError) return NULL;
for (;;) {
kernelNextEvent();
switch (kernelEventData.type) {
case kernelEvent(directory.VOLUME):
dirent.d_blocks = 0;
dirent.d_type = 2;
break;
case kernelEvent(directory.FILE):
kernelArgs->common.buf = &dirent.d_blocks;
kernelArgs->common.buflen = sizeof(dirent.d_blocks);
kernelCall(ReadExt);
dirent.d_type = (dirent.d_blocks == 0);
break;
case kernelEvent(directory.FREE):
// dirent doesn't care about these types of records.
kernelArgs->directory.read.stream = *dir;
kernelCall(Directory.Read);
if (!kernelError) continue;
// Fall through.
case kernelEvent(directory.EOF):
case kernelEvent(directory.ERROR):
return NULL;
default:
continue;
}
// Copy the name.
len = kernelEventData.directory.file.len;
if (len >= sizeof(dirent.d_name)) {
len = sizeof(dirent.d_name) - 1;
}
if (len > 0) {
kernelArgs->common.buf = &dirent.d_name;
kernelArgs->common.buflen = len;
kernelCall(ReadData);
}
dirent.d_name[len] = 0;
return &dirent;
}
}
#pragma pop_macro("FILE")
#pragma pop_macro("EOF")
int8_t fileRemoveDir(char *dir) {
byte drive;
dir = pathWithoutDrive(dir, &drive);
kernelArgs->directory.mkdir.drive = drive;
kernelArgs->common.buf = dir;
kernelArgs->common.buflen = strlen(dir);
kernelCall(Directory.RmDir);
if (kernelError) return -1;
for (;;) {
kernelNextEvent();
if (kernelEventData.type == kernelEvent(directory.DELETED)) break;
if (kernelEventData.type == kernelEvent(directory.ERROR)) return -1;
}
return 0;
}
int8_t fileRename(char *name, char *to) {
byte drive;
byte drive2;
int16_t path1;
int16_t path2;
name = pathWithoutDrive(name, &drive);
to = pathWithoutDrive(to, &drive2);
// ensure that the paths match
if (false
|| (drive != drive2)
|| !findName(name, &path1)
|| !findName(to, &path2)
|| (path1 != path2)
|| (strncmp(name, to, path1) != 0)
) {
return -1;
}
to += path2;
kernelArgs->file.delete.drive = drive;
kernelArgs->common.buf = name;
kernelArgs->common.buflen = strlen(name);
kernelArgs->common.ext = to;
kernelArgs->common.extlen = strlen(to);
kernelCall(File.Rename);
if (kernelError) return -1;
for (;;) {
kernelNextEvent();
if (kernelEventData.type == kernelEvent(file.RENAMED)) break;
if (kernelEventData.type == kernelEvent(file.ERROR)) return -1;
}
return 0;
}
void fileReset(void) {
byte x;
for (x=0; x<MAX_DRIVES; x++) _dirStream[x] = 0;
}
int8_t fileSeek(uint8_t *fd, uint32_t offset, uint8_t whence) {
if (whence != SEEK_SET) return -1;
//***TODO*** We should do this manually for IEC devices.
kernelArgs->file.seek.stream = *fd;
kernelArgs->file.seek.offset = offset;
kernelCall(File.Seek);
if (kernelError) return -1;
return 0;
}
int8_t fileUnlink(char *name) {
byte drive;
name = pathWithoutDrive(name, &drive);
kernelArgs->file.delete.drive = drive;
kernelArgs->common.buf = name;
kernelArgs->common.buflen = strlen(name);
kernelCall(File.Delete);
if (kernelError) return -1;
for (;;) {
kernelNextEvent();
if (kernelEventData.type == kernelEvent(file.DELETED)) break;
if (kernelEventData.type == kernelEvent(file.ERROR)) return -1;
}
return 0;
}
int16_t fileWrite(void *buf, uint16_t nbytes, uint16_t nmemb, uint8_t *fd) {
uint8_t *data = (uint8_t *)buf;
int16_t total = 0;
int16_t bytes = nbytes * nmemb;
uint8_t writing;
int16_t written;
while (bytes) {
if (bytes > 254) {
writing = 254;
} else {
writing = bytes;
}
written = kernelWrite(*fd, data + total, writing);
if (written <= 0) {
return -1;
}
total += written;
bytes -= written;
}
return total / nbytes;
}
static bool findName(char *name, int16_t *offset) {
int16_t i;
int16_t pos;
for (i = pos = 0; name[i]; i++) {
if (name[i] == '/') {
pos = i+1;
if (!name[pos]) {
// No base name found!
return false;
}
}
}
*offset = pos;
return true;
}
#pragma push_macro("EOF")
#undef EOF
static int16_t kernelRead(uint8_t fd, void *buf, uint16_t nbytes) {
// STDIN
if (fd == 0) {
*(char *)buf = getchar();
return 1;
}
if (nbytes > 256) nbytes = 256;
kernelArgs->file.read.stream = fd;
kernelArgs->file.read.buflen = nbytes;
kernelCall(File.Read);
if (kernelError) return -1;
for(;;) {
kernelNextEvent();
switch (kernelEventData.type) {
case kernelEvent(file.DATA):
kernelArgs->common.buf = buf;
kernelArgs->common.buflen = kernelEventData.file.data.delivered;
kernelCall(ReadData);
if (!kernelEventData.file.data.delivered) return 256;
return kernelEventData.file.data.delivered;
case kernelEvent(file.EOF):
return 0;
case kernelEvent(file.ERROR):
return -1;
default:
continue;
}
}
}
#pragma pop_macro("EOF")
static int16_t kernelWrite(uint8_t fd, void *buf, uint16_t nbytes) {
int16_t i;
char *text;
// STDOUT
if (fd == 1) {
text = (char *)buf;
for (i = 0; i < nbytes; i++) {
__putchar(text[i]);
}
return i;
}
kernelArgs->file.read.stream = fd;
kernelArgs->common.buf = buf;
kernelArgs->common.buflen = nbytes;
kernelCall(File.Write);
if (kernelError) return -1;
for (;;) {
kernelNextEvent();
if (kernelEventData.type == kernelEvent(file.WROTE)) return kernelEventData.file.data.delivered;
if (kernelEventData.type == kernelEvent(file.ERROR)) return -1;
}
}
static char *pathWithoutDrive(char *path, byte *drive) {
*drive = 0;
if (strlen(path) < 2) return path;
if (path[1] != ':') return path;
if ((*path >= '0') && (*path <= '7')) *drive = *path - '0';
return (path + 2);
}
#endif