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.
This commit is contained in:
Holger Wächtler 2008-07-28 20:24:08 +02:00 committed by Stephen Williams
parent aeec93a322
commit 15584a4f81
1 changed files with 19 additions and 14 deletions

View File

@ -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;