From 15584a4f81881e593cc45e518be9d4f245360a17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20W=C3=A4chtler?= Date: Mon, 28 Jul 2008 20:24:08 +0200 Subject: [PATCH] fix $fscan("%d") for values > 32 bit The old code returns a vpiIntVal for $fscanf("%d") format strings. This limits the maximum input range arbitrarily to 32 bit. This patch implements %d parsing similiar to %b and %x. '?' and 'X' chars are not accepted for %d format. --- vpi/sys_scanf.c | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/vpi/sys_scanf.c b/vpi/sys_scanf.c index c2b9a4898..fb58ff919 100644 --- a/vpi/sys_scanf.c +++ b/vpi/sys_scanf.c @@ -281,7 +281,6 @@ static int scan_format(vpiHandle sys, struct byte_source*src, vpiHandle argv) int suppress_flag = 0; int length_field = -1; int code = 0; - int sign_flag = 1; PLI_INT32 value; char*tmp; @@ -373,30 +372,36 @@ static int scan_format(vpiHandle sys, struct byte_source*src, vpiHandle argv) break; case 'd': - match_fail = 1; - /* Decimal integer */ - ch = byte_getc(src); - if (ch == '-') { - sign_flag = -1; - ch = byte_getc(src); - } + /* decimal integer */ + tmp = malloc(2); value = 0; - while ( isdigit(ch) ) { + tmp[0] = 0; + match_fail = 1; + + ch = byte_getc(src); + while (isdigit(ch)) { match_fail = 0; - value *= 10; - value += ch - '0'; + tmp[value++] = ch; + tmp = realloc(tmp, value+1); + tmp[value] = 0; ch = byte_getc(src); } + byte_ungetc(src, ch); - if (match_fail) + if (match_fail) { + free(tmp); break; + } + /* Matched a decimal value, put it to an argument. */ item = vpi_scan(argv); assert(item); - val.format = vpiIntVal; - val.value.integer = value * sign_flag; + val.format = vpiDecStrVal; + val.value.str = tmp; vpi_put_value(item, &val, 0, vpiNoDelay); + + free(tmp); rc += 1; break;