From patchwork Sat Oct 17 07:55:36 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 36296 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id CF64CB7334 for ; Sat, 17 Oct 2009 19:09:28 +1100 (EST) Received: from localhost ([127.0.0.1]:38904 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Mz4Ls-0002Lo-OP for incoming@patchwork.ozlabs.org; Sat, 17 Oct 2009 04:09:24 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Mz48n-0005pm-E3 for qemu-devel@nongnu.org; Sat, 17 Oct 2009 03:55:53 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Mz48j-0005ly-2J for qemu-devel@nongnu.org; Sat, 17 Oct 2009 03:55:52 -0400 Received: from [199.232.76.173] (port=40197 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Mz48i-0005ls-C0 for qemu-devel@nongnu.org; Sat, 17 Oct 2009 03:55:48 -0400 Received: from mx1.redhat.com ([209.132.183.28]:57909) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1Mz48h-0001yP-NS for qemu-devel@nongnu.org; Sat, 17 Oct 2009 03:55:48 -0400 Received: from int-mx04.intmail.prod.int.phx2.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.17]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id n9H7tkvZ030751 for ; Sat, 17 Oct 2009 03:55:47 -0400 Received: from localhost.localdomain (vpn1-5-47.ams2.redhat.com [10.36.5.47]) by int-mx04.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id n9H7taRk019772; Sat, 17 Oct 2009 03:55:46 -0400 From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Sat, 17 Oct 2009 09:55:36 +0200 Message-Id: <1255766136-3028-8-git-send-email-pbonzini@redhat.com> In-Reply-To: <1255766136-3028-1-git-send-email-pbonzini@redhat.com> References: <1255766136-3028-1-git-send-email-pbonzini@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.17 X-detected-operating-system: by monty-python.gnu.org: Genre and OS details not recognized. Cc: Luiz Capitulino Subject: [Qemu-devel] [PATCH 7/7] add formatted printing of QObjects X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This patch adds a function to format a QDict according to a string that is printf like but specifies how to navigate the QDict. All parts are printed as JSON, but this is fine since this will auto-escape strings and integers print naturally. More sophisticated flags can be added later, e.g. %#{foo} would print a string without escaping. Cc: Luiz Capitulino Signed-off-by: Paolo Bonzini --- Makefile | 6 +++--- check-json.c | 39 +++++++++++++++++++++++++++++++++++++++ qstring.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ qstring.h | 3 +++ 4 files changed, 96 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 2483057..f98b70c 100644 --- a/Makefile +++ b/Makefile @@ -211,10 +211,10 @@ qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx $(call quiet-command,sh $(SRC_PATH)/hxtool -h < $< > $@," GEN $@") check-json: check-json.o qint.o qstring.o qdict.o qlist.o qemu-malloc.o -check-qint: check-qint.o qint.o qstring.o qemu-malloc.o -check-qstring: check-qstring.o qstring.o qemu-malloc.o +check-qint: check-qint.o qint.o qstring.o qdict.o qemu-malloc.o +check-qstring: check-qstring.o qint.o qstring.o qdict.o qemu-malloc.o check-qdict: check-qdict.o qdict.o qint.o qstring.o qemu-malloc.o -check-qlist: check-qlist.o qlist.o qint.o qstring.o qemu-malloc.o +check-qlist: check-qlist.o qlist.o qint.o qstring.o qdict.o qemu-malloc.o clean: # avoid old build problems by removing potentially incorrect old files diff --git a/check-json.c b/check-json.c index 9f4cfbe..eaff53b 100644 --- a/check-json.c +++ b/check-json.c @@ -21,6 +21,13 @@ QDECREF(result); \ } while (0) +#define format_expect(qobj, format, expected) \ + do { \ + QString *result = qstring_format (format, qobj); \ + fail_unless (!strcmp (qstring_get_str (result), expected)); \ + QDECREF(result); \ + } while (0) + START_TEST(qint_json_test) { QInt *qi; @@ -88,6 +95,37 @@ START_TEST(nested_json_test) } END_TEST +START_TEST(format_test) +{ + QDict *qdict_in, *qdict_out; + QList *qlist; + + format_expect(NULL, "test", "test"); + format_expect(NULL, "test %%", "test %"); + + qdict_in = qdict_new(); + format_expect(qdict_in, "test %%", "test %"); + format_expect(qdict_in, "test %{} test", "test {} test"); + format_expect(qdict_in, "%%%{}%%", "%{}%"); + + qdict_put_obj(qdict_in, "in", QOBJECT(qint_from_int(42))); + format_expect(qdict_in, "%{}", "{\"in\":42}"); + format_expect(qdict_in, "test %{in} test", "test 42 test"); + + qlist = qlist_new(); + qlist_append(qlist, qdict_in); + QINCREF(qlist); + + qdict_out = qdict_new(); + qdict_put_obj(qdict_out, "list", QOBJECT(qlist)); + qdict_put_obj(qdict_out, "in", QOBJECT(qdict_in)); + format_expect(qdict_out, "%{list}", "[{\"in\":42}]"); + format_expect(qdict_out, "%{in.in}", "42"); + format_expect(qdict_out, "%{in.in}%{in.in}", "4242"); + QDECREF(qdict_out); +} +END_TEST + static Suite *QObject_json_suite(void) { Suite *s; @@ -102,6 +140,7 @@ static Suite *QObject_json_suite(void) tcase_add_test(qobject_json_tcase, qdict_json_test); tcase_add_test(qobject_json_tcase, qlist_json_test); tcase_add_test(qobject_json_tcase, nested_json_test); + tcase_add_test(qobject_json_tcase, format_test); return s; } diff --git a/qstring.c b/qstring.c index 10c1952..90d9295 100644 --- a/qstring.c +++ b/qstring.c @@ -11,6 +11,7 @@ */ #include "qobject.h" #include "qstring.h" +#include "qdict.h" #include "qemu-common.h" static void qstring_destroy_obj(QObject *obj); @@ -79,6 +80,14 @@ QString *qstring_json_from_qobject_obj(const QObject *qobject) return qstring; } +QString *qstring_format(const char *format, const QDict *root) +{ + QString *qstring; + + qstring = qstring_new(); + qstring_append_format(qstring, format, root); + return qstring; +} /** * qstring_append(): Append a regular C string to a QString @@ -137,6 +146,48 @@ void qstring_append_escaped(QString *qstring, const char *str) } } +void qstring_append_format(QString *qstring, const char *format, + const struct QDict *root) +{ + char buf[256], *p; + while (format && *format) { + const QObject *next = QOBJECT(root); + + if (*format != '%' || *++format == '%') { + qstring_append_ch (qstring, *format++); + continue; + } + if (*format++ != '{') + abort (); + + next = QOBJECT(root); + if (*format == '}') { + format++; + } else { + do { + if (qobject_type(next) != QTYPE_QDICT) { + format = strchr (format, '}'); + if (!format) { + return; + } + next = NULL; + } else { + p = buf; + while (*format != '.' && *format != '}') + *p++ = *format++; + *p = 0; + next = qdict_get(qobject_to_qdict(next), buf); + } + } while (*format++ != '}'); + } + + if (next) { + qobject_encode_json(next, qstring); + } + } +} + + /** * qstring_append_ch(): Append a character to a QString */ diff --git a/qstring.h b/qstring.h index efc1d7a..2d1fec9 100644 --- a/qstring.h +++ b/qstring.h @@ -11,6 +11,7 @@ typedef struct QString { QString *qstring_new(void); QString *qstring_from_str(const char *str); +QString *qstring_format(const char *format, const struct QDict *qdict); QString *qstring_json_from_qobject_obj(const QObject *obj); #define qstring_json_from_qobject(obj) \ @@ -18,6 +19,8 @@ QString *qstring_json_from_qobject_obj(const QObject *obj); const char *qstring_get_str(const QString *qstring); void qstring_append(QString *qstring, const char *str); +void qstring_append_format(QString *qstring, const char *format, + const struct QDict *qdict); void qstring_append_escaped(QString *qstring, const char *str); void qstring_append_ch(QString *qstring, char c); QString *qobject_to_qstring(const QObject *obj);