From patchwork Wed Jun 23 15:39:58 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miguel Di Ciurcio Filho X-Patchwork-Id: 56684 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 42A86B6EE8 for ; Thu, 24 Jun 2010 01:42:20 +1000 (EST) Received: from localhost ([127.0.0.1]:42566 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1ORS5h-00034J-9V for incoming@patchwork.ozlabs.org; Wed, 23 Jun 2010 11:42:17 -0400 Received: from [140.186.70.92] (port=41944 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1ORS4J-00031F-Gs for qemu-devel@nongnu.org; Wed, 23 Jun 2010 11:40:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1ORS4H-0006bl-MW for qemu-devel@nongnu.org; Wed, 23 Jun 2010 11:40:51 -0400 Received: from mail-gw0-f45.google.com ([74.125.83.45]:56880) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1ORS4H-0006bd-K9 for qemu-devel@nongnu.org; Wed, 23 Jun 2010 11:40:49 -0400 Received: by gwb19 with SMTP id 19so1465213gwb.4 for ; Wed, 23 Jun 2010 08:40:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:from:to:cc:subject:date :message-id:x-mailer:in-reply-to:references; bh=OwNkvgvoWXJKZOsMXosmmvLVtBY0mqWbp/gzU6M8Moo=; b=inwNWvJAQQhpgm0PE+bIl65gmIntTpBdoqZbsmDmtqsrNJFcLEuj6Mhb931EkGMnfq 2lajbdO7Npcys9YWRNb4Uv3PsGKF9/oTpmaZ5O6iGlipHbrd+cVH5lNPTRtG2O6Xmqk/ Mg1TYFiyi+1jyxOxZ28AimbrtRqjjhGxEdGG8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; b=KlBjfmHdH9pfBs6XMz46Qr3EM+dcX1DdL3yX8Y4SiKrgMwe9QUtFRyg0f0+f7q0E3R UcPiI6Blz4p5/JaySi9MzkBO5YKr6i/VKexII/6DQx0I76Fo+Wm1/ZcbLAP8DocLoJ1v cgN1PgIVb3d2psPRsBpg/9PCSBhonDPsbpN5Y= Received: by 10.101.177.39 with SMTP id e39mr6606075anp.8.1277307648985; Wed, 23 Jun 2010 08:40:48 -0700 (PDT) Received: from localhost.localdomain ([189.61.230.202]) by mx.google.com with ESMTPS id u6sm36591518ibu.0.2010.06.23.08.40.46 (version=TLSv1/SSLv3 cipher=RC4-MD5); Wed, 23 Jun 2010 08:40:48 -0700 (PDT) From: Miguel Di Ciurcio Filho To: qemu-devel@nongnu.org Date: Wed, 23 Jun 2010 12:39:58 -0300 Message-Id: <1277307603-28960-3-git-send-email-miguel.filho@gmail.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1277307603-28960-1-git-send-email-miguel.filho@gmail.com> References: <1277307603-28960-1-git-send-email-miguel.filho@gmail.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) Cc: avi@redhat.com, Miguel Di Ciurcio Filho , armbru@redhat.com, lcapitulino@redhat.com Subject: [Qemu-devel] [PATCH 2/7] QObject API: introduce qdict_to_qstring() function 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 is a helper function that converts a QDict to a QString, using the format: key1=value1 SEP key2=value2 SEP key3=value3 Handy for debugging and formating the Monitor output. Signed-off-by: Miguel Di Ciurcio Filho --- qdict.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ qdict.h | 2 ++ 2 files changed, 62 insertions(+), 0 deletions(-) diff --git a/qdict.c b/qdict.c index 175bc17..dc95199 100644 --- a/qdict.c +++ b/qdict.c @@ -267,6 +267,66 @@ const char *qdict_get_str(const QDict *qdict, const char *key) return qstring_get_str(qobject_to_qstring(obj)); } +struct qstring_pack { + QString *str; + size_t total_keys; + size_t current_key; + const char *separator; +}; + +static void qdict_to_qstring_iter(const char *key, QObject *obj, void *opaque) +{ + struct qstring_pack *pack = opaque; + qstring_append(pack->str, key); + qstring_append(pack->str, "="); + switch (qobject_type(obj)) { + case QTYPE_QSTRING: + qstring_append(pack->str, qstring_get_str(qobject_to_qstring(obj))); + break; + case QTYPE_QINT: + qstring_append_int(pack->str, qint_get_int(qobject_to_qint(obj))); + break; + case QTYPE_QBOOL: + qstring_append(pack->str, qbool_get_int(qobject_to_qbool(obj)) ? "true" : + "false" ); + break; + default: + qstring_append(pack->str, "NULL"); + } + + pack->current_key++; + + if (pack->current_key < pack->total_keys) { + qstring_append(pack->str, pack->separator); + } +} + +/** + * qdict_to_qstring(): Format a string with the keys and values of a QDict. + * + * Nested lists and dicts are not supported, yet. + * + * Return a pointer to a QString, with the following format: + * key1=value1 SEP key2=value2 SEP key3=value3 + */ +QString *qdict_to_qstring(const QDict *qdict, const char *separator) +{ + struct qstring_pack *pack; + QString *str; + str = qstring_new(); + + pack = qemu_malloc(sizeof(*pack)); + pack->str = str; + pack->current_key = 0; + pack->total_keys = qdict_size(qdict); + pack->separator = separator; + + qdict_iter(qdict, qdict_to_qstring_iter, pack); + + qemu_free(pack); + + return str; +} /** * qdict_get_try_int(): Try to get integer mapped by 'key' * diff --git a/qdict.h b/qdict.h index 5e5902c..0c64089 100644 --- a/qdict.h +++ b/qdict.h @@ -15,6 +15,7 @@ #include "qobject.h" #include "qlist.h" +#include "qstring.h" #include "qemu-queue.h" #include @@ -55,6 +56,7 @@ int qdict_get_bool(const QDict *qdict, const char *key); QList *qdict_get_qlist(const QDict *qdict, const char *key); QDict *qdict_get_qdict(const QDict *qdict, const char *key); const char *qdict_get_str(const QDict *qdict, const char *key); +QString *qdict_to_qstring(const QDict *qdict, const char *separator); int64_t qdict_get_try_int(const QDict *qdict, const char *key, int64_t err_value); const char *qdict_get_try_str(const QDict *qdict, const char *key);