aboutsummaryrefslogtreecommitdiffstats
path: root/src/gfile
diff options
context:
space:
mode:
authorinmarket <andrewh@inmarket.com.au>2014-02-07 01:34:38 +1000
committerinmarket <andrewh@inmarket.com.au>2014-02-07 01:34:38 +1000
commite72e2705381558ff2347543ed21b853a1168858c (patch)
treef0e3026640e71e4a78b9d8f46bb9daea7cee8743 /src/gfile
parent9ef10b9ec669bd6af0747b083b1b61b4a041e206 (diff)
downloaduGFX-e72e2705381558ff2347543ed21b853a1168858c.tar.gz
uGFX-e72e2705381558ff2347543ed21b853a1168858c.tar.bz2
uGFX-e72e2705381558ff2347543ed21b853a1168858c.zip
Add support for GFILEs based on BaseFileStreams and Memory Pointers
Diffstat (limited to 'src/gfile')
-rw-r--r--src/gfile/gfile.c76
-rw-r--r--src/gfile/inc_chibiosfs.c46
-rw-r--r--src/gfile/inc_memfs.c41
3 files changed, 162 insertions, 1 deletions
diff --git a/src/gfile/gfile.c b/src/gfile/gfile.c
index f9c306e4..23985367 100644
--- a/src/gfile/gfile.c
+++ b/src/gfile/gfile.c
@@ -32,6 +32,20 @@ GFILE *gfileStdErr;
*/
/********************************************************
+ * The ChibiOS BaseFileStream VMT
+ ********************************************************/
+#if GFILE_NEED_CHIBIOSFS && GFX_USE_OS_CHIBIOS
+ #include "../src/gfile/inc_chibiosfs.c"
+#endif
+
+/********************************************************
+ * The Memory Pointer VMT
+ ********************************************************/
+#if GFILE_NEED_MEMFS
+ #include "../src/gfile/inc_memfs.c"
+#endif
+
+/********************************************************
* The RAM file-system VMT
********************************************************/
#if GFILE_NEED_RAMFS
@@ -297,6 +311,66 @@ GFILE *gfileOpen(const char *fname, const char *mode) {
return FALSE;
}
+#if GFILE_NEED_CHIBIOSFS && GFX_USE_OS_CHIBIOS
+ GFILE * gfileOpenBaseFileStream(void *BaseFileStreamPtr, const char *mode) {
+ GFILE * f;
+
+ // First find an available GFILE slot.
+ for (f = gfileArr; f < &gfileArr[GFILE_MAX_GFILES]; f++) {
+ if (!(f->flags & GFILEFLG_OPEN)) {
+ // Get the flags
+ if (!(f->flags = mode2flags(mode)))
+ return FALSE;
+
+ // If we want write but the fs doesn't allow it then return
+ if ((f->flags & GFILEFLG_WRITE) && !(FsCHIBIOSVMT.flags & GFSFLG_WRITEABLE))
+ return FALSE;
+
+ // File is open - fill in all the details
+ f->vmt = &FsCHIBIOSVMT;
+ f->fd = BaseFileStreamPtr;
+ f->err = 0;
+ f->pos = 0;
+ f->flags |= GFILEFLG_OPEN|GFILEFLG_CANSEEK;
+ return TRUE;
+ }
+ }
+
+ // No available slot
+ return FALSE;
+ }
+#endif
+
+#if GFILE_NEED_MEMFS
+ GFILE * gfileOpenMemory(void *memptr, const char *mode) {
+ GFILE * f;
+
+ // First find an available GFILE slot.
+ for (f = gfileArr; f < &gfileArr[GFILE_MAX_GFILES]; f++) {
+ if (!(f->flags & GFILEFLG_OPEN)) {
+ // Get the flags
+ if (!(f->flags = mode2flags(mode)))
+ return FALSE;
+
+ // If we want write but the fs doesn't allow it then return
+ if ((f->flags & GFILEFLG_WRITE) && !(FsMemVMT.flags & GFSFLG_WRITEABLE))
+ return FALSE;
+
+ // File is open - fill in all the details
+ f->vmt = &FsMemVMT;
+ f->fd = memptr;
+ f->err = 0;
+ f->pos = 0;
+ f->flags |= GFILEFLG_OPEN|GFILEFLG_CANSEEK;
+ return TRUE;
+ }
+ }
+
+ // No available slot
+ return FALSE;
+ }
+#endif
+
void gfileClose(GFILE *f) {
if (!f || !(f->flags & GFILEFLG_OPEN))
return;
@@ -357,7 +431,7 @@ bool_t gfileEOF(GFILE *f) {
if (!f || !(f->flags & GFILEFLG_OPEN))
return TRUE;
if (!f->vmt->eof)
- return TRUE;
+ return FALSE;
return f->vmt->eof(f);
}
diff --git a/src/gfile/inc_chibiosfs.c b/src/gfile/inc_chibiosfs.c
new file mode 100644
index 00000000..b30a675f
--- /dev/null
+++ b/src/gfile/inc_chibiosfs.c
@@ -0,0 +1,46 @@
+/*
+ * 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 ChibiOS BaseFileStream file-system VMT
+ ********************************************************/
+
+static void ChibiOSBFSClose(GFILE *f);
+static int ChibiOSBFSRead(GFILE *f, char *buf, int size);
+static int ChibiOSBFSWrite(GFILE *f, char *buf, int size);
+static bool_t ChibiOSBFSSetpos(GFILE *f, long int pos);
+static long int ChibiOSBFSGetsize(GFILE *f);
+static bool_t ChibiOSBFSEof(GFILE *f);
+
+static const GFILEVMT FsCHIBIOSVMT = {
+ 0, // next
+ GFSFLG_SEEKABLE|GFSFLG_WRITEABLE, // flags
+ 0, // prefix
+ 0, 0, 0, 0,
+ 0, ChibiOSBFSClose, ChibiOSBFSRead, ChibiOSBFSWrite,
+ ChibiOSBFSSetpos, ChibiOSBFSGetsize, ChibiOSBFSEof,
+};
+
+static void ChibiOSBFSClose(GFILE *f) {
+ chFileStreamClose(((BaseFileStream *)f->fd));
+}
+static int ChibiOSBFSRead(GFILE *f, char *buf, int size) {
+ return chSequentialStreamRead(((BaseFileStream *)f->fd), (uint8_t *)buf, size);
+}
+static int ChibiOSBFSWrite(GFILE *f, char *buf, int size) {
+ return chSequentialStreamWrite(((BaseFileStream *)f->fd), (uint8_t *)buf, size);
+}
+static bool_t ChibiOSBFSSetpos(GFILE *f, long int pos) {
+ chFileStreamSeek(((BaseFileStream *)f->fd), pos);
+ return TRUE;
+}
+static long int ChibiOSBFSGetsize(GFILE *f) { return chFileStreamGetSize(((BaseFileStream *)f->fd)); }
+static bool_t ChibiOSBFSEof(GFILE *f) { return f->pos >= chFileStreamGetSize(((BaseFileStream *)f->fd)); }
diff --git a/src/gfile/inc_memfs.c b/src/gfile/inc_memfs.c
new file mode 100644
index 00000000..fee089ce
--- /dev/null
+++ b/src/gfile/inc_memfs.c
@@ -0,0 +1,41 @@
+/*
+ * 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 Memory pointer file-system VMT
+ ********************************************************/
+
+#include <string.h>
+
+static int MEMRead(GFILE *f, char *buf, int size);
+static int MEMWrite(GFILE *f, char *buf, int size);
+static bool_t MEMSetpos(GFILE *f, long int pos);
+
+static const GFILEVMT FsMemVMT = {
+ 0, // next
+ GFSFLG_SEEKABLE|GFSFLG_WRITEABLE, // flags
+ 0, // prefix
+ 0, 0, 0, 0,
+ 0, 0, MEMRead, MEMWrite,
+ MEMSetpos, 0, 0,
+};
+
+static int MEMRead(GFILE *f, char *buf, int size) {
+ memset(buf, ((char *)f->fd)+f->pos, size);
+ return size;
+}
+static int MEMWrite(GFILE *f, char *buf, int size) {
+ memset(((char *)f->fd)+f->pos, buf, size);
+ return size;
+}
+static bool_t MEMSetpos(GFILE *f, long int pos) {
+ return TRUE;
+}