version 2.8
gfile_fs_strings.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 virtual string file
10  ********************************************************/
11 
12 #include "../../gfx.h"
13 
14 #if GFX_USE_GFILE && GFILE_NEED_STRINGS
15 
16 #include "gfile_fs.h"
17 
18 #include <string.h>
19 
20 // Special String VMT
21 static int StringRead(GFILE *f, void *buf, int size) {
22  int res;
23  char *p;
24 
25  if (!f->obj)
26  return 0;
27 
28  p = ((char *)f->obj) + f->pos;
29  for(res = 0; res < size && *p; res++, p++, buf = ((char *)buf)+1)
30  ((char *)buf)[0] = *p;
31  return res;
32 }
33 static int StringWrite(GFILE *f, const void *buf, int size) {
34  if (!f->obj)
35  return 0;
36 
37  if ((f->flags & GFILEFLG_APPEND)) {
38  while(((char *)f->obj)[f->pos])
39  f->pos++;
40  }
41  memcpy(((char *)f->obj)+f->pos, buf, size);
42  ((char *)f->obj)[f->pos+size] = 0;
43  return size;
44 }
45 
46 static const GFILEVMT StringVMT = {
47  0, // flags
48  '_', // prefix
49  0, 0, 0, 0,
50  0, 0, StringRead, StringWrite,
51  0, 0, 0,
52  0, 0, 0,
53  #if GFILE_NEED_FILELISTS
54  0, 0, 0,
55  #endif
56 };
57 
58 static void gfileOpenStringFromStaticGFILE(GFILE *f, char *str) {
59  if ((f->flags & GFILEFLG_TRUNC) && str)
60  str[0] = 0;
61  f->vmt = &StringVMT;
62  f->obj = str;
63  f->pos = 0;
64  f->flags |= GFILEFLG_OPEN|GFILEFLG_CANSEEK;
65 }
66 
67 GFILE *gfileOpenString(char *str, const char *mode) {
68  GFILE *f;
69 
70  // Get an empty file and set the flags
71  if (!str || !(f = _gfileFindSlot(mode)))
72  return 0;
73 
74  // File is open - fill in all the details
75  gfileOpenStringFromStaticGFILE(f, str);
76  return f;
77 }
78 
79 #if GFILE_NEED_PRINTG
80  int snprintg(char *buf, int maxlen, const char *fmt, ...) {
81  int res;
82  GFILE f;
83  va_list ap;
84 
85  if (maxlen <= 1) {
86  if (maxlen == 1) {
87  *buf = 0;
88  return 0;
89  }
90  maxlen += 1;
91  }
92 
93  f.flags = GFILEFLG_WRITE|GFILEFLG_TRUNC;
94  gfileOpenStringFromStaticGFILE(&f, buf);
95 
96  va_start(ap, fmt);
97  res = vfnprintg(&f, maxlen-1, fmt, ap);
98  va_end(ap);
99  return res;
100  }
101  int vsnprintg(char *buf, int maxlen, const char *fmt, va_list arg) {
102  GFILE f;
103 
104  if (maxlen <= 1) {
105  if (maxlen == 1) {
106  *buf = 0;
107  return 0;
108  }
109  maxlen += 1;
110  }
111 
112  f.flags = GFILEFLG_WRITE|GFILEFLG_TRUNC;
113  gfileOpenStringFromStaticGFILE(&f, buf);
114 
115  return vfnprintg(&f, maxlen-1, fmt, arg);
116  }
117 #endif
118 
119 #if GFILE_NEED_SCANG
120  int sscang(const char *buf, const char *fmt, ...) {
121  int res;
122  GFILE f;
123  va_list ap;
124 
125  f.flags = GFILEFLG_READ;
126  gfileOpenStringFromStaticGFILE(&f, (char *)buf);
127 
128  va_start(ap, fmt);
129  res = vfscang(&f, fmt, ap);
130  va_end(ap);
131  return res;
132  }
133 
134  int vsscang(const char *buf, const char *fmt, va_list arg) {
135  GFILE f;
136 
137  f.flags = GFILEFLG_READ;
138  gfileOpenStringFromStaticGFILE(&f, (char *)buf);
139 
140  return vfscang(&f, fmt, arg);
141  }
142 #endif
143 
144 #endif //GFX_USE_GFILE && GFILE_NEED_STRINGS
struct GFILE GFILE
A file pointer.
Definition: gfile.h:34
GFILE * gfileOpenString(char *str, const char *mode)
Open file from a null terminated C string.
GFILE file system header.