summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/fscanf.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lua/fscanf.c')
-rw-r--r--apps/plugins/lua/fscanf.c291
1 files changed, 0 insertions, 291 deletions
diff --git a/apps/plugins/lua/fscanf.c b/apps/plugins/lua/fscanf.c
deleted file mode 100644
index 9f5f129d3c..0000000000
--- a/apps/plugins/lua/fscanf.c
+++ /dev/null
@@ -1,291 +0,0 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copied from firmware/common/sscanf.c
11 * Original author: Tomasz Malesinski
12 *
13 * Copyright (C) 2010 Maurus Cuelenaere
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
19 *
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
22 *
23 ****************************************************************************/
24
25#include "rocklibc.h"
26
27static int parse_dec(int (*peek)(void *userp),
28 void (*pop)(void *userp),
29 void *userp,
30 long *vp)
31{
32 long v = 0;
33 int n = 0;
34 int minus = 0;
35 char ch;
36
37 if ((*peek)(userp) == '-')
38 {
39 (*pop)(userp);
40 n++;
41 minus = 1;
42 }
43
44 ch = (*peek)(userp);
45 if (!isdigit(ch))
46 return -1;
47
48 do
49 {
50 v = v * 10 + ch - '0';
51 (*pop)(userp);
52 n++;
53 ch = (*peek)(userp);
54 } while (isdigit(ch));
55
56 *vp = minus ? -v : v;
57 return n;
58}
59
60static int parse_chars(int (*peek)(void *userp),
61 void (*pop)(void *userp),
62 void *userp,
63 char *vp,
64 bool fake)
65{
66 int n = 0;
67
68 char *pt=vp;
69
70 while (!isspace((*peek)(userp)))
71 {
72 if(fake==false)
73 *(pt++) = (*peek)(userp);
74
75 n++;
76 (*pop)(userp);
77 }
78
79 if(fake==false)
80 (*pt)='\0';
81
82 return n;
83}
84
85static int parse_hex(int (*peek)(void *userp),
86 void (*pop)(void *userp),
87 void *userp,
88 unsigned long *vp)
89{
90 unsigned long v = 0;
91 int n = 0;
92 char ch;
93
94 ch = (*peek)(userp);
95 if (!isxdigit(ch))
96 return -1;
97
98 do
99 {
100 if (ch >= 'a')
101 ch = ch - 'a' + 10;
102 else if (ch >= 'A')
103 ch = ch - 'A' + 10;
104 else
105 ch = ch - '0';
106 v = v * 16 + ch;
107 (*pop)(userp);
108 n++;
109 ch = (*peek)(userp);
110 } while (isxdigit(ch));
111
112 *vp = v;
113 return n;
114}
115
116static int skip_spaces(int (*peek)(void *userp),
117 void (*pop)(void *userp),
118 void *userp)
119{
120 int n = 0;
121 while (isspace((*peek)(userp))) {
122 n++;
123 (*pop)(userp);
124 }
125 return n;
126}
127
128static int scan(int (*peek)(void *userp),
129 void (*pop)(void *userp),
130 void *userp,
131 const char *fmt,
132 va_list ap)
133{
134 char ch;
135 int n = 0;
136 int n_chars = 0;
137 int r;
138 long lval;
139 bool skip=false;
140 unsigned long ulval;
141
142 while ((ch = *fmt++) != '\0')
143 {
144 bool literal = false;
145
146 if (ch == '%')
147 {
148 ch = *fmt++;
149
150 if(ch== '*') /* We should process this, but not store it in an argument */
151 {
152 ch=*fmt++;
153 skip=true;
154 }
155 else
156 {
157 skip=false;
158 }
159
160 switch (ch)
161 {
162 case 'x':
163 n_chars += skip_spaces(peek, pop, userp);
164 if ((r = parse_hex(peek, pop, userp, &ulval)) >= 0)
165 {
166 if(skip==false)
167 {
168 *(va_arg(ap, unsigned int *)) = ulval;
169 n++;
170 }
171 n_chars += r;
172 }
173 else
174 return n;
175 break;
176 case 'd':
177 n_chars += skip_spaces(peek, pop, userp);
178 if ((r = parse_dec(peek, pop, userp, &lval)) >= 0)
179 {
180 if(skip==false)
181 {
182 *(va_arg(ap, int *)) = lval;
183 n++;
184 }
185 n_chars += r;
186 }
187 else
188 return n;
189 break;
190 case 'n':
191 if(skip==false)
192 {
193 *(va_arg(ap, int *)) = n_chars;
194 n++;
195 }
196 break;
197 case 'l':
198 n_chars += skip_spaces(peek, pop, userp);
199 ch = *fmt++;
200 switch (ch)
201 {
202 case 'x':
203 if ((r = parse_hex(peek, pop, userp, &ulval)) >= 0)
204 {
205 if(skip==false)
206 {
207 *(va_arg(ap, unsigned long *)) = ulval;
208 n++;
209 }
210 n_chars += r;
211 }
212 else
213 return n;
214 break;
215 case 'd':
216 if ((r = parse_dec(peek, pop, userp, &lval)) >= 0)
217 {
218 if(skip==false)
219 {
220 *(va_arg(ap, long *)) = lval;
221 n++;
222 }
223 n_chars += r;
224 }
225 else
226 return n;
227 break;
228 case '\0':
229 return n;
230 default:
231 literal = true;
232 break;
233 }
234 break;
235 case 's':
236 n_chars += skip_spaces(peek, pop, userp);
237 n_chars += parse_chars(peek,pop, userp,skip?0:va_arg(ap, char *), skip );
238 if(skip==false)
239 {
240 n++;
241 }
242 break;
243 case '\0':
244 return n;
245 default:
246 literal = true;
247 break;
248 }
249 } else
250 literal = true;
251
252 if (literal)
253 {
254 n_chars += skip_spaces(peek, pop, userp);
255 if ((*peek)(userp) != ch)
256 continue;
257 else
258 {
259 (*pop)(userp);
260 n_chars++;
261 }
262 }
263 }
264 return n;
265}
266
267static int fspeek(void *userp)
268{
269 int fd = *((int*) userp);
270 char buf = 0;
271 if(rb->read(fd, &buf, 1) == 1)
272 rb->lseek(fd, -1, SEEK_CUR);
273 return buf;
274}
275
276static void fspop(void *userp)
277{
278 int fd = *((int*) userp);
279 rb->lseek(fd, 1, SEEK_CUR);
280}
281
282int PREFIX(fscanf)(int fd, const char *fmt, ...)
283{
284 int r;
285 va_list ap;
286
287 va_start(ap, fmt);
288 r = scan(fspeek, fspop, &fd, fmt, ap);
289 va_end(ap);
290 return r;
291}