From: "Daniel P. Berrange" <berrange@redhat.com>
To: qemu-devel@nongnu.org
Cc: Eric Blake <eblake@redhat.com>,
Markus Armbruster <armbru@redhat.com>,
"Dr . David Alan Gilbert" <dgilbert@redhat.com>,
Paolo Bonzini <pbonzini@redhat.com>,
qemu-block@nongnu.org, "Daniel P. Berrange" <berrange@redhat.com>
Subject: [Qemu-devel] [PATCH v2 2/6] qapi: convert StringOutputVisitor to use qemu_szutostr
Date: Fri, 9 Sep 2016 18:41:57 +0100 [thread overview]
Message-ID: <1473442921-2968-3-git-send-email-berrange@redhat.com> (raw)
In-Reply-To: <1473442921-2968-1-git-send-email-berrange@redhat.com>
Instead inlining code for formatting sized integers,
call out to the new qemu_szutostr function.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
---
qapi/string-output-visitor.c | 20 +++++---------------
tests/test-string-output-visitor.c | 22 ++++++++++++++++++++++
2 files changed, 27 insertions(+), 15 deletions(-)
diff --git a/qapi/string-output-visitor.c b/qapi/string-output-visitor.c
index 94ac821..dfb15b5 100644
--- a/qapi/string-output-visitor.c
+++ b/qapi/string-output-visitor.c
@@ -15,6 +15,7 @@
#include "qapi/string-output-visitor.h"
#include "qapi/visitor-impl.h"
#include "qemu/host-utils.h"
+#include "qemu/cutils.h"
#include <math.h>
#include "qemu/range.h"
@@ -211,10 +212,8 @@ static void print_type_size(Visitor *v, const char *name, uint64_t *obj,
Error **errp)
{
StringOutputVisitor *sov = to_sov(v);
- static const char suffixes[] = { 'B', 'K', 'M', 'G', 'T', 'P', 'E' };
- uint64_t div, val;
+ char *szval;
char *out;
- int i;
if (!sov->human) {
out = g_strdup_printf("%"PRIu64, *obj);
@@ -222,20 +221,11 @@ static void print_type_size(Visitor *v, const char *name, uint64_t *obj,
return;
}
- val = *obj;
+ szval = qemu_szutostr_full(*obj, '\0', true, " ");
- /* The exponent (returned in i) minus one gives us
- * floor(log2(val * 1024 / 1000). The correction makes us
- * switch to the higher power when the integer part is >= 1000.
- */
- frexp(val / (1000.0 / 1024.0), &i);
- i = (i - 1) / 10;
- assert(i < ARRAY_SIZE(suffixes));
- div = 1ULL << (i * 10);
-
- out = g_strdup_printf("%"PRIu64" (%0.3g %c%s)", val,
- (double)val/div, suffixes[i], i ? "iB" : "");
+ out = g_strdup_printf("%"PRIu64" (%s)", *obj, szval);
string_output_set(sov, out);
+ g_free(szval);
}
static void print_type_bool(Visitor *v, const char *name, bool *obj,
diff --git a/tests/test-string-output-visitor.c b/tests/test-string-output-visitor.c
index 444844a..dd6e00f 100644
--- a/tests/test-string-output-visitor.c
+++ b/tests/test-string-output-visitor.c
@@ -87,6 +87,24 @@ static void test_visitor_out_int(TestOutputVisitorData *data,
}
}
+static void test_visitor_out_size(TestOutputVisitorData *data,
+ const void *unused)
+{
+ uint64_t value = 1729;
+ Error *err = NULL;
+ char *str;
+
+ visit_type_size(data->ov, NULL, &value, &err);
+ g_assert(!err);
+
+ str = visitor_get(data);
+ if (data->human) {
+ g_assert_cmpstr(str, ==, "1729 (1.69 KiB)");
+ } else {
+ g_assert_cmpstr(str, ==, "1729");
+ }
+}
+
static void test_visitor_out_intList(TestOutputVisitorData *data,
const void *unused)
{
@@ -240,6 +258,10 @@ int main(int argc, char **argv)
&out_visitor_data, test_visitor_out_int, false);
output_visitor_test_add("/string-visitor/output/int-human",
&out_visitor_data, test_visitor_out_int, true);
+ output_visitor_test_add("/string-visitor/output/size",
+ &out_visitor_data, test_visitor_out_size, false);
+ output_visitor_test_add("/string-visitor/output/size-human",
+ &out_visitor_data, test_visitor_out_size, true);
output_visitor_test_add("/string-visitor/output/bool",
&out_visitor_data, test_visitor_out_bool, false);
output_visitor_test_add("/string-visitor/output/bool-human",
--
2.7.4
next prev parent reply other threads:[~2016-09-09 17:42 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-09-09 17:41 [Qemu-devel] [PATCH v2 0/6] Stable output of blockdev format specific info Daniel P. Berrange
2016-09-09 17:41 ` [Qemu-devel] [PATCH v2 1/6] cutils: add helpers for formatting sized values Daniel P. Berrange
2016-09-09 17:41 ` Daniel P. Berrange [this message]
2016-09-09 17:41 ` [Qemu-devel] [PATCH v2 3/6] qapi: assert that visitor impls have required callbacks Daniel P. Berrange
2016-09-09 17:41 ` [Qemu-devel] [PATCH v2 4/6] qapi: add a text output visitor for pretty printing types Daniel P. Berrange
2016-09-09 17:42 ` [Qemu-devel] [PATCH v2 5/6] block: convert to use the qemu_szutostr functions Daniel P. Berrange
2016-09-09 17:42 ` [Qemu-devel] [PATCH v2 6/6] block: convert to use qapi_stringify_ImageInfoSpecific Daniel P. Berrange
2016-09-13 13:44 ` [Qemu-devel] [PATCH v2 0/6] Stable output of blockdev format specific info Markus Armbruster
2016-09-13 13:52 ` Daniel P. Berrange
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1473442921-2968-3-git-send-email-berrange@redhat.com \
--to=berrange@redhat.com \
--cc=armbru@redhat.com \
--cc=dgilbert@redhat.com \
--cc=eblake@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.