version 2.8
gfile_fs_rom.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.org/license.html
6  */
7 
8 /********************************************************
9  * The ROM file-system
10  ********************************************************/
11 
12 #include "../../gfx.h"
13 
14 #if GFX_USE_GFILE && GFILE_NEED_ROMFS
15 
16 #include "gfile_fs.h"
17 
18 #include <string.h>
19 
20 // What directory file formats do we understand
21 #define ROMFS_DIR_VER_MAX 0x0000
22 
23 // Compression Formats
24 #define ROMFS_CMP_UNCOMPRESSED 0
25 
26 typedef struct ROMFS_DIRENTRY {
27  uint16_t ver; // Directory Entry Version
28  uint16_t cmp; // Compression format
29  const struct ROMFS_DIRENTRY * next; // The next entry
30  const char * name; // The file name
31  long int size; // The file size
32  const char * file; // The file data
33 } ROMFS_DIRENTRY;
34 
35 #define ROMFS_DIRENTRY_HEAD 0
36 #include "romfs_files.h"
37 static const ROMFS_DIRENTRY const *FsROMHead = ROMFS_DIRENTRY_HEAD;
38 
39 typedef struct ROMFileList {
40  gfileList fl;
41  const ROMFS_DIRENTRY *pdir;
42 } ROMFileList;
43 
44 
45 static bool_t ROMExists(const char *fname);
46 static long int ROMFilesize(const char *fname);
47 static bool_t ROMOpen(GFILE *f, const char *fname);
48 static void ROMClose(GFILE *f);
49 static int ROMRead(GFILE *f, void *buf, int size);
50 static bool_t ROMSetpos(GFILE *f, long int pos);
51 static long int ROMGetsize(GFILE *f);
52 static bool_t ROMEof(GFILE *f);
53 #if GFILE_NEED_FILELISTS
54  static gfileList *ROMFlOpen(const char *path, bool_t dirs);
55  static const char *ROMFlRead(gfileList *pfl);
56  static void ROMFlClose(gfileList *pfl);
57 #endif
58 
59 const GFILEVMT FsROMVMT = {
60  GFSFLG_CASESENSITIVE|GFSFLG_SEEKABLE|GFSFLG_FAST, // flags
61  'S', // prefix
62  0, ROMExists, ROMFilesize, 0,
63  ROMOpen, ROMClose, ROMRead, 0,
64  ROMSetpos, ROMGetsize, ROMEof,
65  0, 0, 0,
66  #if GFILE_NEED_FILELISTS
67  ROMFlOpen, ROMFlRead, ROMFlClose
68  #endif
69 };
70 
71 static const ROMFS_DIRENTRY *ROMFindFile(const char *fname)
72 {
73  const ROMFS_DIRENTRY *p;
74 
75  for(p = FsROMHead; p; p = p->next) {
76  if (p->ver <= ROMFS_DIR_VER_MAX && p->cmp == ROMFS_CMP_UNCOMPRESSED && !strcmp(p->name, fname))
77  break;
78  }
79  return p;
80 }
81 
82 static bool_t ROMExists(const char *fname)
83 {
84  return ROMFindFile(fname) != 0;
85 }
86 
87 static long int ROMFilesize(const char *fname)
88 {
89  const ROMFS_DIRENTRY *p;
90 
91  if (!(p = ROMFindFile(fname))) return -1;
92  return p->size;
93 }
94 
95 static bool_t ROMOpen(GFILE *f, const char *fname)
96 {
97  const ROMFS_DIRENTRY *p;
98 
99  if (!(p = ROMFindFile(fname))) return FALSE;
100  f->obj = (void *)p;
101  return TRUE;
102 }
103 
104 static void ROMClose(GFILE *f)
105 {
106  (void)f;
107 }
108 
109 static int ROMRead(GFILE *f, void *buf, int size)
110 {
111  const ROMFS_DIRENTRY *p;
112 
113  p = (const ROMFS_DIRENTRY *)f->obj;
114  if (p->size - f->pos < size)
115  size = p->size - f->pos;
116  if (size <= 0) return 0;
117  memcpy(buf, p->file+f->pos, size);
118  return size;
119 }
120 
121 static bool_t ROMSetpos(GFILE *f, long int pos)
122 {
123  return pos <= ((const ROMFS_DIRENTRY *)f->obj)->size;
124 }
125 
126 static long int ROMGetsize(GFILE *f)
127 {
128  return ((const ROMFS_DIRENTRY *)f->obj)->size;
129 }
130 
131 static bool_t ROMEof(GFILE *f)
132 {
133  return f->pos >= ((const ROMFS_DIRENTRY *)f->obj)->size;
134 }
135 
136 #if GFILE_NEED_FILELISTS
137  static gfileList *ROMFlOpen(const char *path, bool_t dirs) {
138  ROMFileList * p;
139  (void) path;
140 
141  // We don't support directories or path searching
142  if (dirs)
143  return 0;
144 
145  // Allocate the list buffer
146  if (!(p = gfxAlloc(sizeof(ROMFileList))))
147  return 0;
148 
149  // Initialize it and return it.
150  p->pdir = 0;
151  return &p->fl;
152  }
153 
154  static const char *ROMFlRead(gfileList *pfl) {
155  #define rfl ((ROMFileList *)pfl)
156 
157  // Is it the first entry
158  if (!rfl->pdir) {
159  rfl->pdir = FsROMHead;
160  return FsROMHead->name;
161  }
162 
163  // Is it not the last entry
164  if (rfl->pdir->next) {
165  rfl->pdir = rfl->pdir->next;
166  return rfl->pdir->name;
167  }
168 
169  return 0;
170  #undef rfl
171  }
172 
173  static void ROMFlClose(gfileList *pfl) {
174  gfxFree(pfl);
175  }
176 #endif
177 
178 #endif //GFX_USE_GFILE && GFILE_NEED_ROMFS
struct GFILE GFILE
A file pointer.
Definition: gfile.h:34
#define FALSE
Generic &#39;false&#39; boolean constant.
Definition: gfx.h:31
void * gfxAlloc(size_t sz)
Allocate memory.
void gfxFree(void *ptr)
Free memory.
GFILE file system header.
#define TRUE
Generic &#39;true&#39; boolean constant.
Definition: gfx.h:38