int32 conv_num(struct script_state* st, struct script_data* data) {
char* p;
int32 num;
script->get_val(st, data);
if( data_isint(data) )
{// nothing to convert
}
else if( data_isstring(data) )
{// string -> int
// the result does not overflow or underflow, it is capped instead
// ex: 999999999999 is capped to INT_MAX (2147483647)
p = data->u.str;
errno = 0;
num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
if( errno == ERANGE
#if LONG_MAX > INT_MAX
|| num < INT_MIN || num > INT_MAX
#endif
)
{
if( num <= INT_MIN )
{
num = INT_MIN;
ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
}
else//if( num >= INT_MAX )
{
num = INT_MAX;
ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
}
script_reportdata(data);
script_reportsrc(st);
}
if( data->type == C_STR )
aFree(p);
data->type = C_INT;
data->u.num = num;
}
#if 0
// FIXME this function is being used to retrieve the position of labels and
// probably other stuff [FlavioJS]
else
{// unsupported data type
ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
script_reportdata(data);
script_reportsrc(st);
data->type = C_INT;
data->u.num = 0;
}
#endif
return (int32)data->u.num;
}