1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copied from firmware/common/sscanf.c
11 * Original author: Tomasz Malesinski
13 * Copyright (C) 2010 Maurus Cuelenaere
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.
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
23 ****************************************************************************/
27 static int parse_dec(int (*peek
)(void *userp
),
28 void (*pop
)(void *userp
),
37 if ((*peek
)(userp
) == '-')
50 v
= v
* 10 + ch
- '0';
54 } while (isdigit(ch
));
60 static int parse_chars(int (*peek
)(void *userp
),
61 void (*pop
)(void *userp
),
70 while (!isspace((*peek
)(userp
)))
73 *(pt
++) = (*peek
)(userp
);
85 static int parse_hex(int (*peek
)(void *userp
),
86 void (*pop
)(void *userp
),
110 } while (isxdigit(ch
));
116 static int skip_spaces(int (*peek
)(void *userp
),
117 void (*pop
)(void *userp
),
121 while (isspace((*peek
)(userp
))) {
128 static int scan(int (*peek
)(void *userp
),
129 void (*pop
)(void *userp
),
142 while ((ch
= *fmt
++) != '\0')
144 bool literal
= false;
150 if(ch
== '*') /* We should process this, but not store it in an argument */
163 n_chars
+= skip_spaces(peek
, pop
, userp
);
164 if ((r
= parse_hex(peek
, pop
, userp
, &ulval
)) >= 0)
168 *(va_arg(ap
, unsigned int *)) = ulval
;
177 n_chars
+= skip_spaces(peek
, pop
, userp
);
178 if ((r
= parse_dec(peek
, pop
, userp
, &lval
)) >= 0)
182 *(va_arg(ap
, int *)) = lval
;
193 *(va_arg(ap
, int *)) = n_chars
;
198 n_chars
+= skip_spaces(peek
, pop
, userp
);
203 if ((r
= parse_hex(peek
, pop
, userp
, &ulval
)) >= 0)
207 *(va_arg(ap
, unsigned long *)) = ulval
;
216 if ((r
= parse_dec(peek
, pop
, userp
, &lval
)) >= 0)
220 *(va_arg(ap
, long *)) = lval
;
236 n_chars
+= skip_spaces(peek
, pop
, userp
);
237 n_chars
+= parse_chars(peek
,pop
, userp
,skip
?0:va_arg(ap
, char *), skip
);
254 n_chars
+= skip_spaces(peek
, pop
, userp
);
255 if ((*peek
)(userp
) != ch
)
267 static int fspeek(void *userp
)
269 int fd
= *((int*) userp
);
271 if(rb
->read(fd
, &buf
, 1) == 1)
272 rb
->lseek(fd
, -1, SEEK_CUR
);
276 static void fspop(void *userp
)
278 int fd
= *((int*) userp
);
279 rb
->lseek(fd
, 1, SEEK_CUR
);
282 int PREFIX(fscanf
)(int fd
, const char *fmt
, ...)
288 r
= scan(fspeek
, fspop
, &fd
, fmt
, ap
);