9 //#define debug_printf(args...) printf(args)
10 #define debug_printf(args...) do {} while(0)
12 /* XXX to remove ?? */
15 #define U16_MIN 0x0000
16 #define U16_MAX 0xFFFF
17 #define U32_MIN 0x00000000
18 #define U32_MAX 0xFFFFFFFF
21 #define S16_MIN 0x8000
22 #define S16_MAX 0x7FFF
23 #define S32_MIN 0x80000000
24 #define S32_MAX 0x7FFFFFFF
27 struct token_ops token_num_ops = {
29 .complete_get_nb = NULL,
30 .complete_get_elt = NULL,
31 .get_help = get_help_num,
35 enum num_parse_state_t {
44 FIRST_OK, /* not used */
55 /* Keep it sync with enum in .h */
56 static const prog_char help1[] = "UINT8";
57 static const prog_char help2[] = "UINT16";
58 static const prog_char help3[] = "UINT32";
59 static const prog_char help4[] = "INT8";
60 static const prog_char help5[] = "INT16";
61 static const prog_char help6[] = "INT32";
62 #ifndef CONFIG_MODULE_PARSE_NO_FLOAT
63 static const prog_char help7[] = "FLOAT";
65 static const prog_char * num_help[] = {
66 help1, help2, help3, help4,
68 #ifndef CONFIG_MODULE_PARSE_NO_FLOAT
74 add_to_res(uint8_t c, uint32_t * res, uint8_t base)
77 if ( (U32_MAX - c) / base < *res ) {
81 *res = *res * base + c ;
86 /* parse an int or a float */
88 parse_num(parse_pgm_token_hdr_t * tk, const char * srcbuf, void * res)
90 struct token_num_data nd;
91 enum num_parse_state_t st = START;
92 const char * buf = srcbuf;
94 uint32_t res1=0, res2=0, res3=1;
96 memcpy_P(&nd, &((struct token_num *)tk)->num_data, sizeof(nd));
98 while ( st != ERROR && c && ! isendoftoken(c) ) {
99 debug_printf("%c %x -> ", c, c);
108 #ifndef CONFIG_MODULE_PARSE_NO_FLOAT
114 else if (c >= '1' && c <= '9') {
115 if (add_to_res(c - '0', &res1, 10) < 0)
132 #ifndef CONFIG_MODULE_PARSE_NO_FLOAT
138 else if (c >= '0' && c <= '7') {
139 if (add_to_res(c - '0', &res1, 10) < 0)
150 if (c >= '0' && c <= '9') {
151 if (add_to_res(c - '0', &res1, 10) < 0)
156 #ifndef CONFIG_MODULE_PARSE_NO_FLOAT
168 if (c >= '0' && c <= '9') {
169 if (add_to_res(c - '0', &res1, 10) < 0)
172 #ifndef CONFIG_MODULE_PARSE_NO_FLOAT
183 if (c >= '0' && c <= '9') {
184 if (add_to_res(c - '0', &res1, 10) < 0)
187 #ifndef CONFIG_MODULE_PARSE_NO_FLOAT
201 if (c >= '0' && c <= '9') {
202 if (add_to_res(c - '0', &res1, 16) < 0)
205 else if (c >= 'a' && c <= 'f') {
206 if (add_to_res(c - 'a' + 10, &res1, 16) < 0)
209 else if (c >= 'A' && c <= 'F') {
210 if (add_to_res(c - 'A' + 10, &res1, 16) < 0)
220 if (c >= '0' && c <= '7') {
221 if (add_to_res(c - '0', &res1, 8) < 0)
233 if (c >= '0' && c <= '1') {
234 if (add_to_res(c - '0', &res1, 2) < 0)
242 #ifndef CONFIG_MODULE_PARSE_NO_FLOAT
244 if (c >= '0' && c <= '9') {
245 if (add_to_res(c - '0', &res2, 10) < 0)
257 if (c >= '0' && c <= '9') {
258 if (add_to_res(c - '0', &res2, 10) < 0)
270 if (c >= '0' && c <= '9') {
271 if (add_to_res(c - '0', &res2, 10) < 0)
273 if (add_to_res(0, &res3, 10) < 0)
282 if (c >= '0' && c <= '9') {
283 if (add_to_res(c - '0', &res2, 10) < 0)
285 if (add_to_res(0, &res3, 10) < 0)
295 debug_printf("not impl ");
299 /* XXX uint32_t et %d */
300 debug_printf("(%d) (%d) (%d)\n", res1, res2, res3);
306 if (buf-srcbuf > 127)
316 if ( nd.type == INT8 && res1 <= S08_MAX ) {
318 *(int8_t *)res = (int8_t) res1;
321 else if ( nd.type == INT16 && res1 <= S16_MAX ) {
323 *(int16_t *)res = (int16_t) res1;
326 else if ( nd.type == INT32 && res1 <= S32_MAX ) {
328 *(int32_t *)res = (int32_t) res1;
331 else if ( nd.type == UINT8 && res1 <= U08_MAX ) {
333 *(uint8_t *)res = (uint8_t) res1;
336 else if (nd.type == UINT16 && res1 <= U16_MAX ) {
338 *(uint16_t *)res = (uint16_t) res1;
341 else if ( nd.type == UINT32 ) {
343 *(uint32_t *)res = (uint32_t) res1;
346 #ifndef CONFIG_MODULE_PARSE_NO_FLOAT
347 else if ( nd.type == FLOAT ) {
349 *(float *)res = (float)res1;
359 if ( nd.type == INT8 && res1 <= S08_MAX + 1 ) {
361 *(int8_t *)res = - (int8_t) res1;
364 else if ( nd.type == INT16 && res1 <= (uint16_t)S16_MAX + 1 ) {
366 *(int16_t *)res = - (int16_t) res1;
369 else if ( nd.type == INT32 && res1 <= (uint32_t)S32_MAX + 1 ) {
371 *(int32_t *)res = - (int32_t) res1;
374 #ifndef CONFIG_MODULE_PARSE_NO_FLOAT
375 else if ( nd.type == FLOAT ) {
377 *(float *)res = - (float)res1;
386 #ifndef CONFIG_MODULE_PARSE_NO_FLOAT
389 if ( nd.type == FLOAT ) {
391 *(float *)res = (float)res1 + ((float)res2 / (float)res3);
402 if ( nd.type == FLOAT ) {
404 *(float *)res = - ((float)res1 + ((float)res2 / (float)res3));
414 debug_printf("error\n");
421 /* parse an int or a float */
423 get_help_num(parse_pgm_token_hdr_t * tk, char * dstbuf, uint8_t size)
425 struct token_num_data nd;
427 memcpy_P(&nd, &((struct token_num *)tk)->num_data, sizeof(nd));
429 /* should not happen.... don't so this test */
430 /* if (nd.type >= (sizeof(num_help)/sizeof(const char *))) */
433 strncpy_P(dstbuf, num_help[nd.type], size);
434 dstbuf[size-1] = '\0';