diff options
author | inmarket <andrewh@inmarket.com.au> | 2014-01-05 00:02:53 +1000 |
---|---|---|
committer | inmarket <andrewh@inmarket.com.au> | 2014-01-05 00:02:53 +1000 |
commit | fb29f601f1d5e4c74af9c0d7f53cdf9b57f2cb85 (patch) | |
tree | cbe3be841dc50a9996859c72e8c08b8bc537296b /src/gfile | |
parent | 07869da90938e375e71081d30757cc767596b431 (diff) | |
download | uGFX-fb29f601f1d5e4c74af9c0d7f53cdf9b57f2cb85.tar.gz uGFX-fb29f601f1d5e4c74af9c0d7f53cdf9b57f2cb85.tar.bz2 uGFX-fb29f601f1d5e4c74af9c0d7f53cdf9b57f2cb85.zip |
Start of GFILE module
Diffstat (limited to 'src/gfile')
-rw-r--r-- | src/gfile/gfile.c | 225 | ||||
-rw-r--r-- | src/gfile/gfile.mk | 1 | ||||
-rw-r--r-- | src/gfile/inc_fatfs.c | 15 | ||||
-rw-r--r-- | src/gfile/inc_nativefs.c | 81 | ||||
-rw-r--r-- | src/gfile/inc_ramfs.c | 15 | ||||
-rw-r--r-- | src/gfile/inc_romfs.c | 97 |
6 files changed, 434 insertions, 0 deletions
diff --git a/src/gfile/gfile.c b/src/gfile/gfile.c new file mode 100644 index 00000000..bf76e8bc --- /dev/null +++ b/src/gfile/gfile.c @@ -0,0 +1,225 @@ +/* + * This file is subject to the terms of the GFX License. If a copy of + * the license was not distributed with this file, you can obtain one at: + * + * http://ugfx.org/license.html + */ + +/** + * @file src/gfile/gfile.c + * @brief GFILE code. + * + */ + +#define GFILE_IMPLEMENTATION +#include "gfx.h" + +#if GFX_USE_GFILE + +// The chain of FileSystems +#define GFILE_CHAINHEAD 0 + +// The table of GFILE's +static GFILE gfileArr[GFILE_MAX_GFILES]; +GFILE *gfileStdIn; +GFILE *gfileStdOut; +GFILE *gfileStdErr; + +/** + * The order of the file-systems below determines the order + * that they are searched to find a file. + * The last defined is the first searched. + */ + +/******************************************************** + * The RAM file-system VMT + ********************************************************/ +#if GFILE_NEED_RAMFS + #include "../src/gfile/inc_ramfs.c" +#endif + +/******************************************************** + * The FAT file-system VMT + ********************************************************/ +#ifndef GFILE_NEED_FATFS + #include "../src/gfile/inc_fatfs.c" +#endif + +/******************************************************** + * The native file-system + ********************************************************/ +#if GFILE_NEED_NATIVEFS + #include "../src/gfile/inc_nativefs.c" +#endif + +/******************************************************** + * The ROM file-system VMT + ********************************************************/ +#if GFILE_NEED_ROMFS + #include "../src/gfile/inc_romfs.c" +#endif + +/******************************************************** + * IO routines + ********************************************************/ + +/** + * The chain of file systems. + */ +static const GFILEVMT const * FsChain = GFILE_CHAINHEAD; + +/** + * The init routine + */ +void _gfileInit(void) { + #if GFILE_NEED_NATIVEFS + NativeStdIn.flags = GFILEFLG_OPEN|GFILEFLG_READ; + NativeStdIn.vmt = &FsNativeVMT; + NativeStdIn.obj = (void *)stdin; + NativeStdIn.pos = 0; + gfileStdIn = &NativeStdIn; + NativeStdOut.flags = GFILEFLG_OPEN|GFILEFLG_WRITE|GFILEFLG_APPEND; + NativeStdOut.vmt = &FsNativeVMT; + NativeStdOut.obj = (void *)stdout; + NativeStdOut.pos = 0; + gfileStdOut = &NativeStdOut; + NativeStdErr.flags = GFILEFLG_OPEN|GFILEFLG_WRITE|GFILEFLG_APPEND; + NativeStdErr.vmt = &FsNativeVMT; + NativeStdErr.obj = (void *)stderr; + NativeStdErr.pos = 0; + gfileStdErr = &NativeStdErr; + #endif +} + +bool_t gfileExists(const char *fname) { + const GFILEVMT *p; + + if (fname[0] && fname[1] == '|') { + for(p = FsChain; p; p = p->next) { + if (p->prefix == fname[0]) + return p->exists && p->exists(fname+2); + } + } else { + for(p = FsChain; p; p = p->next) { + if (p->exists && p->exists(fname)) + return TRUE; + } + } + return FALSE; +} + +bool_t gfileDelete(const char *fname) { + const GFILEVMT *p; + + if (fname[0] && fname[1] == '|') { + for(p = FsChain; p; p = p->next) { + if (p->prefix == fname[0]) + return p->del && p->del(fname+2); + } + } else { + for(p = FsChain; p; p = p->next) { + if (p->del && p->del(fname)) + return TRUE; + } + } + return FALSE; +} + +long int gfileGetFilesize(const char *fname) { + const GFILEVMT *p; + + if (fname[0] && fname[1] == '|') { + for(p = FsChain; p; p = p->next) { + if (p->prefix == fname[0]) + return p->filesize ? p->filesize(fname+2) : -1; + } + } else { + long int res; + + for(p = FsChain; p; p = p->next) { + if (p->filesize && (res = p->filesize(fname)) != -1) + return res; + } + } + return -1; +} + +bool_t gfileRename(const char *oldname, const char *newname) { + const GFILEVMT *p; + + if ((oldname[0] && oldname[1] == '|') || (newname[0] && newname[1])) { + char ch; + + if (oldname[0] && oldname[1] == '|') { + ch = oldname[0]; + oldname += 2; + if (newname[0] && newname[1] == '|') { + if (newname[0] != ch) + return FALSE; + newname += 2; + } + } else { + ch = newname[0]; + newname += 2; + } + for(p = FsChain; p; p = p->next) { + if (p->prefix == ch) + return p->ren && p->ren(oldname, newname); + } + } else { + for(p = FsChain; p; p = p->next) { + if (p->ren && p->ren(oldname,newname)) + return TRUE; + } + } + return FALSE; +} + +GFILE *gfileOpen(const char *fname, const char *mode) { + +} + +void gfileClose(GFILE *f) { + +} + +size_t gfileRead(GFILE *f, char *buf, size_t len) { + +} + +size_t gfileWrite(GFILE *f, const char *buf, size_t len) { + +} + +long int gfileGetPos(GFILE *f) { + +} + +bool_t gfileSetPos(GFILE *f, long int pos) { + +} + +long int gfileGetSize(GFILE *f) { + +} + +/******************************************************** + * printg routines + ********************************************************/ +#if GFILE_NEED_PRINTG +#endif + +/******************************************************** + * scang routines + ********************************************************/ +#if GFILE_NEED_SCANG +#endif + +/******************************************************** + * stdio emulation routines + ********************************************************/ +#ifndef GFILE_NEED_STDIO + #define GFILE_NEED_STDIO FALSE +#endif + +#endif /* GFX_USE_GFILE */ diff --git a/src/gfile/gfile.mk b/src/gfile/gfile.mk new file mode 100644 index 00000000..381bd6f6 --- /dev/null +++ b/src/gfile/gfile.mk @@ -0,0 +1 @@ +GFXSRC += $(GFXLIB)/src/gfile/gfile.c diff --git a/src/gfile/inc_fatfs.c b/src/gfile/inc_fatfs.c new file mode 100644 index 00000000..d49cfe7a --- /dev/null +++ b/src/gfile/inc_fatfs.c @@ -0,0 +1,15 @@ +/* + * This file is subject to the terms of the GFX License. If a copy of + * the license was not distributed with this file, you can obtain one at: + * + * http://ugfx.org/license.html + */ + +/** + * This file is included by src/gfile/gfile.c + */ + +/******************************************************** + * The FAT file-system VMT + ********************************************************/ +#error "GFILE: FATFS Not implemented yet" diff --git a/src/gfile/inc_nativefs.c b/src/gfile/inc_nativefs.c new file mode 100644 index 00000000..7828ff84 --- /dev/null +++ b/src/gfile/inc_nativefs.c @@ -0,0 +1,81 @@ +/* + * This file is subject to the terms of the GFX License. If a copy of + * the license was not distributed with this file, you can obtain one at: + * + * http://ugfx.org/license.html + */ + +/** + * This file is included by src/gfile/gfile.c + */ + +/******************************************************** + * The native file-system + ********************************************************/ + +#include <stdio.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> + +static GFILE NativeStdIn; +static GFILE NativeStdOut; +static GFILE NativeStdErr; + +static bool_t NativeDel(const char *fname); +static bool_t NativeExists(const char *fname); +static long int NativeFilesize(const char *fname); +static bool_t NativeRen(const char *oldname, const char *newname); +static bool_t NativeOpen(GFILE *f, const char *fname, const char *mode); +static void NativeClose(GFILE *f); +static int NativeRead(GFILE *f, char *buf, int size); +static int NativeWrite(GFILE *f, char *buf, int size); +static bool_t NativeSetpos(GFILE *f, long int pos); +static long int NativeGetsize(GFILE *f); +static bool_t NativeEof(GFILE *f); + +static const GFILEVMT FsNativeVMT = { + GFILE_CHAINHEAD, // next + 'N', // prefix + #if !defined(WIN32) && !GFX_USE_OS_WIN32 + GFSFLG_CASESENSITIVE| + #endif + GFSFLG_WRITEABLE|GFSFLG_SEEKABLE|GFSFLG_FAST, // flags + NativeDel, NativeExists, NativeFilesize, NativeRen, + NativeOpen, NativeClose, NativeRead, NativeWrite, + NativeSetpos, NativeGetsize, NativeEof, +}; +#undef GFILE_CHAINHEAD +#define GFILE_CHAINHEAD &FsNativeVMT + +static bool_t NativeDel(const char *fname) { return remove(fname) ? FALSE : TRUE; } +static bool_t NativeExists(const char *fname) { return access(fname, 0) ? FALSE : TRUE; } +static long int NativeFilesize(const char *fname) { + struct stat st; + if (stat(fname, &st)) return -1; + return st.st_size; +} +static bool_t NativeRen(const char *oldname, const char *newname) { return rename(oldname, newname) ? FALSE : TRUE }; +static bool_t NativeOpen(GFILE *f, const char *fname, const char *mode) { + FILE *fd; + + if (!(fd = fopen(fname, mode))) + return FALSE; + f->vmt = &FsNativeVMT; + f->obj = (void *)fd; + return TRUE; +} +static void NativeClose(GFILE *f) { fclose((FILE *)f->obj); } +static int NativeRead(GFILE *f, char *buf, int size) { return fread(buf, 1, size, (FILE *)f->obj); } +static int NativeWrite(GFILE *f, char *buf, int size) { return fwrite(buf, 1, size, (FILE *)f->obj); } +static bool_t NativeSetpos(GFILE *f, long int pos) { + if (fseek((FILE *)f->obj, pos, SEEK_SET)) return FALSE; + f->pos = pos; + return TRUE; +} +static long int NativeGetsize(GFILE *f) { + struct stat st; + if (fstat(fileno((FILE *)f->obj), &st)) return -1; + return st.st_size; +} +static bool_t NativeEof(GFILE *f) { return feof((FILE *)f->obj) ? TRUE : FALSE; } diff --git a/src/gfile/inc_ramfs.c b/src/gfile/inc_ramfs.c new file mode 100644 index 00000000..b0f0d052 --- /dev/null +++ b/src/gfile/inc_ramfs.c @@ -0,0 +1,15 @@ +/* + * This file is subject to the terms of the GFX License. If a copy of + * the license was not distributed with this file, you can obtain one at: + * + * http://ugfx.org/license.html + */ + +/** + * This file is included by src/gfile/gfile.c + */ + +/******************************************************** + * The RAM file-system VMT + ********************************************************/ +#error "GFILE: RAMFS Not implemented yet" diff --git a/src/gfile/inc_romfs.c b/src/gfile/inc_romfs.c new file mode 100644 index 00000000..321dc9b1 --- /dev/null +++ b/src/gfile/inc_romfs.c @@ -0,0 +1,97 @@ +/* + * This file is subject to the terms of the GFX License. If a copy of + * the license was not distributed with this file, you can obtain one at: + * + * http://ugfx.org/license.html + */ + +/** + * This file is included by src/gfile/gfile.c + */ + +/******************************************************** + * The ROM file-system VMT + ********************************************************/ + +#include <string.h> + +typedef struct ROMFS_DIRENTRY { + const struct ROMFS_DIRENTRY * next; + const char * name; + long int size; + const char * file; +} ROMFS_DIRENTRY; + +#define ROMFS_DIRENTRY_HEAD 0 + +#include "romfs_files.h" + +static const ROMFS_DIRENTRY const *FsROMHead = ROMFS_DIRENTRY_HEAD; + +static bool_t ROMExists(const char *fname); +static long int ROMFilesize(const char *fname); +static bool_t ROMOpen(GFILE *f, const char *fname, const char *mode); +static void ROMClose(GFILE *f); +static int ROMRead(GFILE *f, char *buf, int size); +static bool_t ROMSetpos(GFILE *f, long int pos); +static long int ROMGetsize(GFILE *f); +static bool_t ROMEof(GFILE *f); + +static const GFILEVMT FsROMVMT = { + GFILE_CHAINHEAD, // next + 'S', // prefix + GFSFLG_CASESENSITIVE|GFSFLG_SEEKABLE|GFSFLG_FAST, // flags + 0, ROMExists, ROMFilesize, 0, + ROMOpen, ROMClose, ROMRead, 0, + ROMSetpos, ROMGetsize, ROMEof, +}; +#undef GFILE_CHAINHEAD +#define GFILE_CHAINHEAD &FsROMVMT + +static ROMFS_DIRENTRY *ROMFindFile(const char *fname) { + const ROMFS_DIRENTRY *p; + + for(p = FsROMHead; p; p = p->next) { + if (!strcmp(p->name, fname)) + break; + } + return p; +} +static bool_t ROMExists(const char *fname) { return ROMFindFile(fname) != 0; } +static long int ROMFilesize(const char *fname) { + const ROMFS_DIRENTRY *p; + + if (!(p = ROMFindFile(fname))) return -1; + return p->size; +} +static bool_t ROMOpen(GFILE *f, const char *fname, const char *mode) { + const ROMFS_DIRENTRY *p; + + // Check mode + if (mode[0] != 'r') return FALSE; + while(*++mode) { + switch(*mode) { + case '+': case 'w': case 'a': + return FALSE; + } + } + + if (!(p = ROMFindFile(fname))) return FALSE; + f->vmt = &FsROMVMT; + f->obj = (void *)p; + return TRUE; +} +static void ROMClose(GFILE *f) { (void)f; } +static int ROMRead(GFILE *f, char *buf, int size) { + const ROMFS_DIRENTRY *p; + + p = (const ROMFS_DIRENTRY *)f->obj; + if (p->size - f->pos < size) + size = p->size - f->pos; + if (size <= 0) return 0; + memcpy(buf, p->file+f->pos, size); + return size; +} +static bool_t ROMSetpos(GFILE *f, long int pos) { return pos <= ((const ROMFS_DIRENTRY *)f->obj)->size; } +static long int ROMGetsize(GFILE *f) { return ((const ROMFS_DIRENTRY *)f->obj)->size; } +static bool_t ROMEof(GFILE *f) { return f->pos >= ((const ROMFS_DIRENTRY *)f->obj)->size; } |