f256/f256lib/file.c
2024-01-22 19:37:11 -06:00

185 lines
4.3 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.
*/
#include <string.h>
#include "file.h"
#define MAX_DRIVES 8
static char _dirStream[MAX_DRIVES];
static char *pathWithoutDrive(char *path, char *drive);
int16_t fileCloseDir(fileDirT *dir) {
if (!dir) return -1;
for (;;) {
if (*dir->stream) {
kernelArgs->directory.close.stream = *dir->stream;
kernelCall(Directory.Close);
if (!kernelError) {
*dir->stream = 0;
}
}
kernelNextEvent();
if (kernelEventData.type == kernelEvent(directory.CLOSED)) {
free(dir);
dir = NULL;
return 0;
}
}
}
fileDirT *fileOpenDir(char *name) {
char drive;
char stream;
fileDirT *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);
textPrint("kernelError = "); textPrintInt(kernelError); textPrint("\n");
textPrint("kernelReturn = "); textPrintInt(kernelReturn); textPrint("\n");
textPrint("stream = "); textPrintInt(stream); textPrint("\n");
if (kernelError) return NULL;
for (;;) {
kernelNextEvent();
if (kernelEventData.type != 0) {
textPrintInt(kernelEventData.type);
textPrint(" ");
}
if (kernelEventData.type == kernelEvent(directory.OPENED)) break;
if (kernelEventData.type == kernelEvent(directory.ERROR)) return NULL;
}
_dirStream[drive] = stream;
dir = (fileDirT *)malloc(sizeof(fileDirT));
dir->stream = &_dirStream[drive];
return dir;
}
fileDirEntT *fileReadDir(fileDirT *dir) {
static fileDirEntT dirent;
unsigned len;
if (!dir) return NULL;
textPrint("Calling Directory.Read.\n");
kernelArgs->directory.read.stream = *dir->stream;
kernelCall(Directory.Read);
if (kernelError) return NULL;
textPrint("Directory.Read called.\n");
for (;;) {
kernelNextEvent();
switch (kernelEventData.type) {
textPrintInt(kernelEventData.type);
textPrint(" ");
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->stream;
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;
}
}
void fileReset(void) {
byte x;
for (x=0; x<MAX_DRIVES; x++) _dirStream[x] = 0;
}
static char *pathWithoutDrive(char *path, char *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);
}