nosound: fix implicit declaration of memset
[rofl0r-gnuboy.git] / rcvars.c
blob027d24dac584053f61a8c4ccb5ae13f296f98244
3 #undef _GNU_SOURCE
4 #define _GNU_SOURCE
5 #include <string.h>
6 #include <stdlib.h>
7 #include <ctype.h>
10 #include "defs.h"
11 #include "rc.h"
18 static rcvar_t *rcvars;
20 static int nvars;
26 void rc_export(rcvar_t *v)
28 const rcvar_t end = RCV_END;
30 if (!v) return;
31 nvars++;
32 rcvars = realloc(rcvars, sizeof (rcvar_t) * (nvars+1));
33 if (!rcvars)
34 die("out of memory adding rcvar %s\n", v->name);
35 rcvars[nvars-1] = *v;
36 rcvars[nvars] = end;
39 void rc_exportvars(rcvar_t *vars)
41 while(vars->type)
42 rc_export(vars++);
47 int rc_findvar(char *name)
49 int i;
50 if (!rcvars) return -1;
51 for (i = 0; rcvars[i].name; i++)
52 if (!strcmp(rcvars[i].name, name))
53 break;
54 if (!rcvars[i].name)
55 return -1;
56 return i;
60 int my_atoi(const char *s)
62 int a = 0;
63 if (*s == '0')
65 s++;
66 if (*s == 'x' || *s == 'X')
68 s++;
69 while (*s)
71 if (isdigit(*s))
72 a = (a<<4) + *s - '0';
73 else if (strchr("ABCDEF", *s))
74 a = (a<<4) + *s - 'A' + 10;
75 else if (strchr("abcdef", *s))
76 a = (a<<4) + *s - 'a' + 10;
77 else return a;
78 s++;
80 return a;
82 while (*s)
84 if (strchr("01234567", *s))
85 a = (a<<3) + *s - '0';
86 else return a;
87 s++;
89 return a;
91 if (*s == '-')
93 s++;
94 for (;;)
96 if (isdigit(*s))
97 a = (a*10) + *s - '0';
98 else return -a;
99 s++;
102 while (*s)
104 if (isdigit(*s))
105 a = (a*10) + *s - '0';
106 else return a;
107 s++;
109 return a;
113 /* i - index of variable in rcvars array
114 c - count of values in v string array
115 v - the actual values as string array */
116 int rc_setvar_n(int i, int c, char **v)
118 int j;
119 int *n;
120 char **s;
122 switch (rcvars[i].type)
124 case rcv_int:
125 if (c < 1) return -1;
126 n = (int *)rcvars[i].mem;
127 *n = my_atoi(v[0]);
128 return 0;
129 case rcv_string:
130 if (c < 1) return -1;
131 s = (char **)rcvars[i].mem;
132 if (*s) free(*s);
133 *s = strdup(v[0]);
134 if (!*s)
135 die("out of memory setting rcvar %s\n", rcvars[i].name);
136 return 0;
137 case rcv_vector:
138 if (c > rcvars[i].len)
139 c = rcvars[i].len;
140 for (j = 0; j < c ; j++)
141 ((int *)rcvars[i].mem)[j] = my_atoi(v[j]);
142 return 0;
143 case rcv_bool:
144 if (c < 1 || atoi(v[0]) || strchr("yYtT", v[0][0]))
145 *(int *)rcvars[i].mem = 1;
146 else if (strchr("0nNfF", v[0][0]))
147 *(int *)rcvars[i].mem = 0;
148 else
149 return -1;
150 return 0;
152 return -1;
156 int rc_setvar(char *name, int c, char **v)
158 int i;
160 i = rc_findvar(name);
161 if (i < 0) return i;
163 return rc_setvar_n(i, c, v);
167 void *rc_getmem_n(int i)
169 return rcvars[i].mem;
173 void *rc_getmem(char *name)
175 int i;
176 i = rc_findvar(name);
177 if (i < 0) return NULL;
178 return rcvars[i].mem;
181 int rc_getint_n(int i)
183 if (i < 0) return 0;
184 switch (rcvars[i].type)
186 case rcv_int:
187 case rcv_bool:
188 return *(int *)rcvars[i].mem;
190 return 0;
193 int *rc_getvec_n(int i)
195 if (i < 0) return NULL;
196 switch (rcvars[i].type)
198 case rcv_int:
199 case rcv_bool:
200 case rcv_vector:
201 return (int *)rcvars[i].mem;
203 return NULL;
206 char *rc_getstr_n(int i)
208 if (i < 0) return 0;
209 switch (rcvars[i].type)
211 case rcv_string:
212 return *(char **)rcvars[i].mem;
214 return 0;
217 int rc_getint(char *name)
219 return rc_getint_n(rc_findvar(name));
222 int *rc_getvec(char *name)
224 return rc_getvec_n(rc_findvar(name));
227 char *rc_getstr(char *name)
229 return rc_getstr_n(rc_findvar(name));
232 const char *rc_type_to_string(rcvtype_t type) {
233 switch (type) {
234 case rcv_int: return "int";
235 case rcv_string: return "str";
236 case rcv_vector: return "vec";
237 case rcv_bool: return "bool";
238 default: return "unknown";