From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59758) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UwUca-0002GF-W0 for qemu-devel@nongnu.org; Tue, 09 Jul 2013 05:54:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UwUcY-0007XY-Hx for qemu-devel@nongnu.org; Tue, 09 Jul 2013 05:54:08 -0400 Received: from mx1.redhat.com ([209.132.183.28]:39257) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UwUcY-0007X6-0a for qemu-devel@nongnu.org; Tue, 09 Jul 2013 05:54:06 -0400 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r699s5Wq031293 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Tue, 9 Jul 2013 05:54:05 -0400 From: Kevin Wolf Date: Tue, 9 Jul 2013 11:53:35 +0200 Message-Id: <1373363617-4723-10-git-send-email-kwolf@redhat.com> In-Reply-To: <1373363617-4723-1-git-send-email-kwolf@redhat.com> References: <1373363617-4723-1-git-send-email-kwolf@redhat.com> Subject: [Qemu-devel] [RFC PATCH 09/11] Implement qdict_flatten() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: kwolf@redhat.com, armbru@redhat.com, stefanha@redhat.com, lcapitulino@redhat.com Signed-off-by: Kevin Wolf --- include/qapi/qmp/qdict.h | 1 + qobject/qdict.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/include/qapi/qmp/qdict.h b/include/qapi/qmp/qdict.h index 685b2e3..b261570 100644 --- a/include/qapi/qmp/qdict.h +++ b/include/qapi/qmp/qdict.h @@ -65,5 +65,6 @@ int qdict_get_try_bool(const QDict *qdict, const char *key, int def_value); const char *qdict_get_try_str(const QDict *qdict, const char *key); QDict *qdict_clone_shallow(const QDict *src); +void qdict_flatten(QObject *obj); #endif /* QDICT_H */ diff --git a/qobject/qdict.c b/qobject/qdict.c index ed381f9..1c38943 100644 --- a/qobject/qdict.c +++ b/qobject/qdict.c @@ -476,3 +476,50 @@ static void qdict_destroy_obj(QObject *obj) g_free(qdict); } + +static void qdict_do_flatten(QDict *qdict, QDict *target, const char *prefix) +{ + QObject *value; + const QDictEntry *entry, *next; + const char *new_key; + bool delete; + + entry = qdict_first(qdict); + + while (entry != NULL) { + + next = qdict_next(qdict, entry); + value = qdict_entry_value(entry); + new_key = NULL; + delete = false; + + if (prefix) { + qobject_incref(value); + new_key = g_strdup_printf("%s.%s", prefix, entry->key); + qdict_put_obj(target, new_key, value); + delete = true; + } + + if (qobject_type(value) == QTYPE_QDICT) { + qdict_do_flatten(qobject_to_qdict(value), target, + new_key ? new_key : entry->key); + delete = true; + } + + if (delete) { + qdict_del(qdict, entry->key); + } + + entry = next; + } +} + +/** + * qdict_flatten(): For each nested QDict with key x, all fields with key y + * are moved to this QDict and their key is renamed to "x.y". + */ +void qdict_flatten(QObject *obj) +{ + QDict *qdict = qobject_to_qdict(obj); + qdict_do_flatten(qdict, qdict, NULL); +} -- 1.8.1.4