mirror of
https://github.com/mii443/qemu.git
synced 2025-08-22 15:15:46 +00:00
qapi: merge QInt and QFloat in QNum
We would like to use a same QObject type to represent numbers, whether they are int, uint, or floats. Getters will allow some compatibility between the various types if the number fits other representations. Add a few more tests while at it. Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com> Message-Id: <20170607163635.17635-7-marcandre.lureau@redhat.com> Reviewed-by: Markus Armbruster <armbru@redhat.com> [parse_stats_intervals() simplified a bit, comment in test_visitor_in_int_overflow() tidied up, suppress bogus warnings] Signed-off-by: Markus Armbruster <armbru@redhat.com>
This commit is contained in:
committed by
Markus Armbruster
parent
58634047b7
commit
01b2ffcedd
@ -132,12 +132,11 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent)
|
||||
case QTYPE_QNULL:
|
||||
qstring_append(str, "null");
|
||||
break;
|
||||
case QTYPE_QINT: {
|
||||
QInt *val = qobject_to_qint(obj);
|
||||
char buffer[1024];
|
||||
|
||||
snprintf(buffer, sizeof(buffer), "%" PRId64, qint_get_int(val));
|
||||
case QTYPE_QNUM: {
|
||||
QNum *val = qobject_to_qnum(obj);
|
||||
char *buffer = qnum_to_string(val);
|
||||
qstring_append(str, buffer);
|
||||
g_free(buffer);
|
||||
break;
|
||||
}
|
||||
case QTYPE_QSTRING: {
|
||||
@ -234,34 +233,6 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent)
|
||||
qstring_append(str, "]");
|
||||
break;
|
||||
}
|
||||
case QTYPE_QFLOAT: {
|
||||
QFloat *val = qobject_to_qfloat(obj);
|
||||
char buffer[1024];
|
||||
int len;
|
||||
|
||||
/* FIXME: snprintf() is locale dependent; but JSON requires
|
||||
* numbers to be formatted as if in the C locale. Dependence
|
||||
* on C locale is a pervasive issue in QEMU. */
|
||||
/* FIXME: This risks printing Inf or NaN, which are not valid
|
||||
* JSON values. */
|
||||
/* FIXME: the default precision of 6 for %f often causes
|
||||
* rounding errors; we should be using DBL_DECIMAL_DIG (17),
|
||||
* and only rounding to a shorter number if the result would
|
||||
* still produce the same floating point value. */
|
||||
len = snprintf(buffer, sizeof(buffer), "%f", qfloat_get_double(val));
|
||||
while (len > 0 && buffer[len - 1] == '0') {
|
||||
len--;
|
||||
}
|
||||
|
||||
if (len && buffer[len - 1] == '.') {
|
||||
buffer[len - 1] = 0;
|
||||
} else {
|
||||
buffer[len] = 0;
|
||||
}
|
||||
|
||||
qstring_append(str, buffer);
|
||||
break;
|
||||
}
|
||||
case QTYPE_QBOOL: {
|
||||
QBool *val = qobject_to_qbool(obj);
|
||||
|
||||
|
Reference in New Issue
Block a user