µGFX  2.9
version 2.9
gfile_fs_mem.c
1 /*
2  * This file is subject to the terms of the GFX License. If a copy of
3  * the license was not distributed with this file, you can obtain one at:
4  *
5  * http://ugfx.io/license.html
6  */
7 
8 /********************************************************
9  * The virtual memory file-system
10  ********************************************************/
11 
12 #include "../../gfx.h"
13 
14 #if GFX_USE_GFILE && GFILE_NEED_MEMFS
15 
16 #include "gfile_fs.h"
17 
18 #include <string.h>
19 
20 static int MEMRead(GFILE *f, void *buf, int size);
21 static int MEMWrite(GFILE *f, const void *buf, int size);
22 static gBool MEMSetpos(GFILE *f, gFileSize pos);
23 
24 static const GFILEVMT FsMemVMT = {
25  GFSFLG_SEEKABLE|GFSFLG_WRITEABLE, // flags
26  0, // prefix
27  0, 0, 0, 0,
28  0, 0, MEMRead, MEMWrite,
29  MEMSetpos, 0, 0,
30  0, 0, 0,
31  #if GFILE_NEED_FILELISTS
32  0, 0, 0,
33  #endif
34 };
35 
36 static int MEMRead(GFILE *f, void *buf, int size) {
37  memcpy(buf, ((char *)f->obj)+f->pos, size);
38  return size;
39 }
40 static int MEMWrite(GFILE *f, const void *buf, int size) {
41  memcpy(((char *)f->obj)+f->pos, buf, size);
42  return size;
43 }
44 static gBool MEMSetpos(GFILE *f, gFileSize pos) {
45  (void) f;
46  (void) pos;
47  return gTrue;
48 }
49 
50 GFILE * gfileOpenMemory(void *memptr, const char *mode) {
51  GFILE *f;
52 
53  // Get an empty file and set the flags
54  if (!(f = _gfileFindSlot(mode)))
55  return 0;
56 
57  // File is open - fill in all the details
58  f->vmt = &FsMemVMT;
59  f->obj = memptr;
60  f->pos = 0;
61  f->flags |= GFILEFLG_OPEN|GFILEFLG_CANSEEK;
62  return f;
63 }
64 
65 #endif //GFX_USE_GFILE && GFILE_NEED_MEMFS
GFILE file system header.
struct GFILE GFILE
A file pointer.
Definition: gfile.h:34
GFILE * gfileOpenMemory(void *memptr, const char *mode)
Open file from a memory pointer.