implement --rominfo command line option
[rofl0r-gnuboy.git] / rcvars.c
blobf024088dbb62ce410e02ceb3f69dda6ae055c3d6
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 int rc_setvar_n(int i, int c, char **v)
115 int j;
116 int *n;
117 char **s;
119 switch (rcvars[i].type)
121 case rcv_int:
122 if (c < 1) return -1;
123 n = (int *)rcvars[i].mem;
124 *n = my_atoi(v[0]);
125 return 0;
126 case rcv_string:
127 if (c < 1) return -1;
128 s = (char **)rcvars[i].mem;
129 if (*s) free(*s);
130 *s = strdup(v[0]);
131 if (!*s)
132 die("out of memory setting rcvar %s\n", rcvars[i].name);
133 return 0;
134 case rcv_vector:
135 if (c > rcvars[i].len)
136 c = rcvars[i].len;
137 for (j = 0; j < c ; j++)
138 ((int *)rcvars[i].mem)[j] = my_atoi(v[j]);
139 return 0;
140 case rcv_bool:
141 if (c < 1 || atoi(v[0]) || strchr("yYtT", v[0][0]))
142 *(int *)rcvars[i].mem = 1;
143 else if (strchr("0nNfF", v[0][0]))
144 *(int *)rcvars[i].mem = 0;
145 else
146 return -1;
147 return 0;
149 return -1;
153 int rc_setvar(char *name, int c, char **v)
155 int i;
157 i = rc_findvar(name);
158 if (i < 0) return i;
160 return rc_setvar_n(i, c, v);
164 void *rc_getmem_n(int i)
166 return rcvars[i].mem;
170 void *rc_getmem(char *name)
172 int i;
173 i = rc_findvar(name);
174 if (i < 0) return NULL;
175 return rcvars[i].mem;
178 int rc_getint_n(int i)
180 if (i < 0) return 0;
181 switch (rcvars[i].type)
183 case rcv_int:
184 case rcv_bool:
185 return *(int *)rcvars[i].mem;
187 return 0;
190 int *rc_getvec_n(int i)
192 if (i < 0) return NULL;
193 switch (rcvars[i].type)
195 case rcv_int:
196 case rcv_bool:
197 case rcv_vector:
198 return (int *)rcvars[i].mem;
200 return NULL;
203 char *rc_getstr_n(int i)
205 if (i < 0) return 0;
206 switch (rcvars[i].type)
208 case rcv_string:
209 return *(char **)rcvars[i].mem;
211 return 0;
214 int rc_getint(char *name)
216 return rc_getint_n(rc_findvar(name));
219 int *rc_getvec(char *name)
221 return rc_getvec_n(rc_findvar(name));
224 char *rc_getstr(char *name)
226 return rc_getstr_n(rc_findvar(name));