99 lines
2.3 KiB
C
99 lines
2.3 KiB
C
|
#include "vfs.h"
|
||
|
#define MaxDevices 256
|
||
|
|
||
|
#define ReturnInvalid() { File ret; ret.Type = FileTypeInvalid; return ret;}
|
||
|
|
||
|
unsigned CurrentDevice = 0;
|
||
|
unsigned DeviceCount = 0;
|
||
|
|
||
|
FileSystemPointer _FileSystems[MaxDevices];
|
||
|
|
||
|
File VFSOpenFile (const char* FileName)
|
||
|
{
|
||
|
unsigned Device = CurrentDevice;
|
||
|
|
||
|
// No filename specified.
|
||
|
if (!FileName) ReturnInvalid();
|
||
|
|
||
|
// File length
|
||
|
int len = strlen(FileName);
|
||
|
// File name without drive
|
||
|
char* Name = 0;
|
||
|
|
||
|
// Check if relative or absolute path
|
||
|
int i;
|
||
|
for (i=0; i < len; i++)
|
||
|
if (FileName[i] == ':') {
|
||
|
Name = &FileName[i+1];
|
||
|
FileName[i] = 0;
|
||
|
len = i;
|
||
|
}
|
||
|
|
||
|
// If absolute path, find device
|
||
|
if (Name != 0) {
|
||
|
for (i=0; i < MaxDevices; i++)
|
||
|
if (_FileSystems[i]) {
|
||
|
if (strcmp(_FileSystems[i]->Name, FileName) == 0) Device = i;
|
||
|
}
|
||
|
}
|
||
|
// Drive is nonexistant or current drive not mounted
|
||
|
if (i==MaxDevices || !_FileSystems[Device]) ReturnInvalid();
|
||
|
|
||
|
// Open file
|
||
|
return _FileSystems[Device]->Open((Name) ? Name : FileName);
|
||
|
}
|
||
|
|
||
|
|
||
|
int VFSInstallFileSystem(FileSystemPointer fs)
|
||
|
{
|
||
|
// Sanity check
|
||
|
if (!fs) return 0;
|
||
|
|
||
|
// Verify device does not exist
|
||
|
int i;
|
||
|
for (i=0; i < DeviceCount; i++)
|
||
|
if (_FileSystems[i])
|
||
|
if (strcmp (fs->Name, _FileSystems[i]->Name) == 0) return 0;
|
||
|
|
||
|
_FileSystems[DeviceCount] = fs; DeviceCount++;
|
||
|
}
|
||
|
|
||
|
void VFSUninstallFileSystem(FileSystemPointer fs)
|
||
|
{
|
||
|
if (!fs) return;
|
||
|
|
||
|
int i;
|
||
|
for (i=0; i<DeviceCount; i++)
|
||
|
if (_FileSystems[i] == fs) {
|
||
|
_FileSystems[i] = 0;
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
void VFSUninstallFileSystemByName(const char* fs)
|
||
|
{
|
||
|
if (!fs) return;
|
||
|
|
||
|
int i;
|
||
|
for (i=0; i<DeviceCount; i++)
|
||
|
if (_FileSystems[i])
|
||
|
if (strcmp(_FileSystems[i]->Name, fs) == 0){
|
||
|
_FileSystems[i] = 0;
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void VFSReadFile (FilePointer f, unsigned char* buffer, unsigned len)
|
||
|
{
|
||
|
if (!f || !_FileSystems[f->Device]) return;
|
||
|
_FileSystems[f->Device]->Read (f, buffer, len);
|
||
|
}
|
||
|
|
||
|
void VFSCloseFile (FilePointer f)
|
||
|
{
|
||
|
if (!f || !_FileSystems[f->Device]) return;
|
||
|
_FileSystems[f->Device]->Close(f);
|
||
|
}
|
||
|
|