12 #include "../../gfx.h"
14 #if GFX_USE_GFILE && GFILE_NEED_FATFS
23 static gBool fatfsDel(
const char* fname);
24 static gBool fatfsExists(
const char* fname);
25 static gFileSize fatfsFileSize(
const char* fname);
26 static gBool fatfsRename(
const char* oldname,
const char* newname);
27 static gBool fatfsOpen(
GFILE* f,
const char* fname);
28 static void fatfsClose(
GFILE* f);
29 static int fatfsRead(
GFILE* f,
void* buf,
int size);
30 static int fatfsWrite(
GFILE* f,
const void* buf,
int size);
31 static gBool fatfsSetPos(
GFILE* f, gFileSize pos);
32 static gFileSize fatfsGetSize(
GFILE* f);
33 static gBool fatfsEOF(
GFILE* f);
34 static gBool fatfsMount(
const char* drive);
35 static gBool fatfsUnmount(
const char* drive);
36 static gBool fatfsSync(
GFILE* f);
37 #if GFILE_NEED_FILELISTS && _FS_MINIMIZE <= 1
38 static gfileList *fatfsFlOpen(
const char *path, gBool dirs);
39 static const char *fatfsFlRead(gfileList *pfl);
40 static void fatfsFlClose(gfileList *pfl);
43 const GFILEVMT FsFatFSVMT = {
44 GFSFLG_WRITEABLE | GFSFLG_SEEKABLE,
57 fatfsMount, fatfsUnmount, fatfsSync,
58 #if GFILE_NEED_FILELISTS
60 fatfsFlOpen, fatfsFlRead, fatfsFlClose
68 typedef struct fatfsList {
73 char lfn[_MAX_LFN + 1];
78 static gBool fatfs_mounted = gFalse;
79 static FATFS fatfs_fs;
81 static BYTE fatfs_flags2mode(
GFILE* f)
85 if (f->flags & GFILEFLG_READ)
87 if (f->flags & GFILEFLG_WRITE)
89 if (f->flags & GFILEFLG_APPEND)
90 mode |= FA_OPEN_APPEND;
91 if (f->flags & GFILEFLG_TRUNC)
92 mode |= FA_CREATE_ALWAYS;
98 static gBool fatfsDel(
const char* fname)
102 ferr = f_unlink( (
const TCHAR*)fname );
109 static gBool fatfsExists(
const char* fname)
114 ferr = f_stat( (
const TCHAR*)fname, &fno);
121 static gFileSize fatfsFileSize(
const char* fname)
126 ferr = f_stat( (
const TCHAR*)fname, &fno );
130 return (gFileSize)fno.fsize;
133 static gBool fatfsRename(
const char* oldname,
const char* newname)
137 ferr = f_rename( (
const TCHAR*)oldname, (
const TCHAR*)newname );
144 static gBool fatfsOpen(
GFILE* f,
const char* fname)
148 #if !GFILE_NEED_NOAUTOMOUNT
149 if (!fatfs_mounted && !fatfsMount(
""))
156 if (f_open(fd, fname, fatfs_flags2mode(f)) != FR_OK) {
165 #if !GFILE_NEED_NOAUTOSYNC
167 if (f->flags & GFILEFLG_WRITE) {
168 f_sync( (FIL*)f->obj );
175 static void fatfsClose(
GFILE* f)
177 if ((FIL*)f->obj != 0) {
178 f_close( (FIL*)f->obj );
183 static int fatfsRead(
GFILE* f,
void* buf,
int size)
187 f_read( (FIL*)f->obj, buf, size, (UINT*)&br);
192 static int fatfsWrite(
GFILE* f,
const void* buf,
int size)
196 f_write( (FIL*)f->obj, buf, size, (UINT*)&wr);
197 #if !GFILE_NEED_NOAUTOSYNC
198 f_sync( (FIL*)f->obj );
204 static gBool fatfsSetPos(
GFILE* f, gFileSize pos)
208 ferr = f_lseek( (FIL*)f->obj, (DWORD)pos );
215 static gFileSize fatfsGetSize(
GFILE* f)
217 return (gFileSize)f_size( (FIL*)f->obj );
220 static gBool fatfsEOF(
GFILE* f)
222 if ( f_eof( (FIL*)f->obj ) != 0)
228 static gBool fatfsMount(
const char* drive)
232 if (!fatfs_mounted) {
233 ferr = f_mount(&fatfs_fs, drive, 1);
236 fatfs_mounted = gTrue;
243 static gBool fatfsUnmount(
const char* drive)
249 fatfs_mounted = gFalse;
256 static gBool fatfsSync(
GFILE *f)
260 ferr = f_sync( (FIL*)f->obj );
268 #if GFILE_NEED_FILELISTS && _FS_MINIMIZE <= 1
269 static gfileList *fatfsFlOpen(
const char *path, gBool dirs) {
273 if (!(p =
gfxAlloc(
sizeof(fatfsList))))
276 if (f_opendir(&p->dir, path) != FR_OK) {
283 static const char *fatfsFlRead(gfileList *pfl) {
284 #define ffl ((fatfsList *)pfl)
288 ffl->fno.lfname = ffl->lfn;
289 ffl->fno.lfsize =
sizeof(ffl->lfn);
293 if (f_readdir(&ffl->dir, &ffl->fno) != FR_OK || !ffl->fno.fname[0])
297 if (ffl->fno.fname[0] ==
'.')
continue;
301 if ((ffl->fno.fattrib & AM_DIR))
304 if (!(ffl->fno.fattrib & AM_DIR))
310 return ffl->fno.lfname[0] ? ffl->fno.lfname : ffl->fno.fname;
312 return ffl->fno.fname;
317 static void fatfsFlClose(gfileList *pfl) {
318 f_closedir(&((fatfsList *)pfl)->dir);
GFILE file system header.
struct GFILE GFILE
A file pointer.
void * gfxAlloc(gMemSize sz)
Allocate memory.
void gfxFree(void *ptr)
Free memory.