aboutsummaryrefslogtreecommitdiffstats
path: root/src/gfile/gfile_fs_mem.c
blob: 7aa0ce82a6f11f913367ae47a9b2084c5c61a693 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
 * 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.io/license.html
 */

/********************************************************
 * The virtual memory file-system
 ********************************************************/

#include "../../gfx.h"

#if GFX_USE_GFILE && GFILE_NEED_MEMFS

#include "gfile_fs.h"

#include <string.h>

static int MEMRead(GFILE *f, void *buf, int size);
static int MEMWrite(GFILE *f, const void *buf, int size);
static gBool MEMSetpos(GFILE *f, gFileSize pos);

static const GFILEVMT FsMemVMT = {
	GFSFLG_SEEKABLE|GFSFLG_WRITEABLE,					// flags
	0,													// prefix
	0, 0, 0, 0,
	0, 0, MEMRead, MEMWrite,
	MEMSetpos, 0, 0,
	0, 0, 0,
	#if GFILE_NEED_FILELISTS
		0, 0, 0,
	#endif
};

static int MEMRead(GFILE *f, void *buf, int size) {
	memcpy(buf, ((char *)f->obj)+f->pos, size);
	return size;
}
static int MEMWrite(GFILE *f, const void *buf, int size) {
	memcpy(((char *)f->obj)+f->pos, buf, size);
	return size;
}
static gBool MEMSetpos(GFILE *f, gFileSize pos) {
	(void) f;
	(void) pos;
	return gTrue;
}

GFILE *	gfileOpenMemory(void *memptr, const char *mode) {
	GFILE	*f;

	// Get an empty file and set the flags
	if (!(f = _gfileFindSlot(mode)))
		return 0;

	// File is open - fill in all the details
	f->vmt = &FsMemVMT;
	f->obj = memptr;
	f->pos = 0;
	f->flags |= GFILEFLG_OPEN|GFILEFLG_CANSEEK;
	return f;
}

#endif //GFX_USE_GFILE && GFILE_NEED_MEMFS