summaryrefslogtreecommitdiff
path: root/apps/plugins/rockboy/rcvars.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/rockboy/rcvars.c')
-rw-r--r--apps/plugins/rockboy/rcvars.c233
1 files changed, 233 insertions, 0 deletions
diff --git a/apps/plugins/rockboy/rcvars.c b/apps/plugins/rockboy/rcvars.c
new file mode 100644
index 0000000000..e37657dbad
--- /dev/null
+++ b/apps/plugins/rockboy/rcvars.c
@@ -0,0 +1,233 @@
1
2
3
4
5#include <string.h>
6#include "rockmacros.h"
7
8#include "defs.h"
9#include "rc.h"
10
11
12
13
14
15
16static rcvar_t rcvars[150];
17
18static int nvars;
19
20
21
22
23
24void rc_export(rcvar_t *v)
25{
26 const rcvar_t end = RCV_END;
27
28 if (!v) return;
29 nvars++;
30// rcvars = realloc(rcvars, sizeof (rcvar_t) * (nvars+1));
31// if (!rcvars)
32// die("out of memory adding rcvar %s\n", v->name);
33 rcvars[nvars-1] = *v;
34 rcvars[nvars] = end;
35}
36
37void rc_exportvars(rcvar_t *vars)
38{
39 while(vars->type)
40 rc_export(vars++);
41}
42
43
44
45int rc_findvar(char *name)
46{
47 int i;
48 if (!rcvars) return -1;
49 for (i = 0; rcvars[i].name; i++)
50 if (!strcmp(rcvars[i].name, name))
51 break;
52 if (!rcvars[i].name)
53 return -1;
54 return i;
55}
56
57
58int my_atoi(const char *s)
59{
60 int a = 0;
61 if (*s == '0')
62 {
63 s++;
64 if (*s == 'x' || *s == 'X')
65 {
66 s++;
67 while (*s)
68 {
69 if (isdigit(*s))
70 a = (a<<4) + *s - '0';
71 else if (strchr("ABCDEF", *s))
72 a = (a<<4) + *s - 'A' + 10;
73 else if (strchr("abcdef", *s))
74 a = (a<<4) + *s - 'a' + 10;
75 else return a;
76 s++;
77 }
78 return a;
79 }
80 while (*s)
81 {
82 if (strchr("01234567", *s))
83 a = (a<<3) + *s - '0';
84 else return a;
85 s++;
86 }
87 return a;
88 }
89 if (*s == '-')
90 {
91 s++;
92 for (;;)
93 {
94 if (isdigit(*s))
95 a = (a*10) + *s - '0';
96 else return -a;
97 s++;
98 }
99 }
100 while (*s)
101 {
102 if (isdigit(*s))
103 a = (a*10) + *s - '0';
104 else return a;
105 s++;
106 }
107 return a;
108}
109
110
111int rc_setvar_n(int i, int c, char **v)
112{
113 int j;
114 int *n;
115 char **s;
116
117 switch (rcvars[i].type)
118 {
119 case rcv_int:
120 if (c < 1) return -1;
121 n = (int *)rcvars[i].mem;
122 *n = my_atoi(v[0]);
123 return 0;
124 case rcv_string:
125 if (c < 1) return -1;
126 s = (char **)rcvars[i].mem;
127// if (*s) free(*s);
128 strcpy(*s,v[0]);
129 if (!*s)
130 die("out of memory setting rcvar %s\n", rcvars[i].name);
131 return 0;
132 case rcv_vector:
133 if (c > rcvars[i].len)
134 c = rcvars[i].len;
135 for (j = 0; j < c ; j++)
136 ((int *)rcvars[i].mem)[j] = my_atoi(v[j]);
137 return 0;
138 case rcv_bool:
139 if (c < 1 || atoi(v[0]) || strchr("yYtT", v[0][0]))
140 *(int *)rcvars[i].mem = 1;
141 else if (strchr("0nNfF", v[0][0]))
142 *(int *)rcvars[i].mem = 0;
143 else
144 return -1;
145 return 0;
146 }
147 return -1;
148}
149
150
151int rc_setvar(char *name, int c, char **v)
152{
153 int i;
154
155 i = rc_findvar(name);
156 if (i < 0) return i;
157
158 return rc_setvar_n(i, c, v);
159}
160
161
162void *rc_getmem_n(int i)
163{
164 return rcvars[i].mem;
165}
166
167
168void *rc_getmem(char *name)
169{
170 int i;
171 i = rc_findvar(name);
172 if (i < 0) return NULL;
173 return rcvars[i].mem;
174}
175
176int rc_getint_n(int i)
177{
178 if (i < 0) return 0;
179 switch (rcvars[i].type)
180 {
181 case rcv_int:
182 case rcv_bool:
183 return *(int *)rcvars[i].mem;
184 }
185 return 0;
186}
187
188int *rc_getvec_n(int i)
189{
190 if (i < 0) return NULL;
191 switch (rcvars[i].type)
192 {
193 case rcv_int:
194 case rcv_bool:
195 case rcv_vector:
196 return (int *)rcvars[i].mem;
197 }
198 return NULL;
199}
200
201char *rc_getstr_n(int i)
202{
203 if (i < 0) return 0;
204 switch (rcvars[i].type)
205 {
206 case rcv_string:
207 return *(char **)rcvars[i].mem;
208 }
209 return 0;
210}
211
212int rc_getint(char *name)
213{
214 return rc_getint_n(rc_findvar(name));
215}
216
217int *rc_getvec(char *name)
218{
219 return rc_getvec_n(rc_findvar(name));
220}
221
222char *rc_getstr(char *name)
223{
224 return rc_getstr_n(rc_findvar(name));
225}
226
227
228
229
230
231
232
233