From patchwork Wed Jun 30 14:52:59 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: 57415 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 1EC95B6EED for ; Thu, 1 Jul 2010 00:57:04 +1000 (EST) Received: from localhost ([127.0.0.1]:35872 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OTyij-0005jt-8C for incoming@patchwork.ozlabs.org; Wed, 30 Jun 2010 10:57:01 -0400 Received: from [140.186.70.92] (port=52460 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OTyfM-0004JK-ME for qemu-devel@nongnu.org; Wed, 30 Jun 2010 10:53:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OTyfD-0007C1-P4 for qemu-devel@nongnu.org; Wed, 30 Jun 2010 10:53:31 -0400 Received: from mail-gw0-f45.google.com ([74.125.83.45]:32984) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OTyfD-0007Bq-MU for qemu-devel@nongnu.org; Wed, 30 Jun 2010 10:53:23 -0400 Received: by gwj20 with SMTP id 20so471824gwj.4 for ; Wed, 30 Jun 2010 07:53:23 -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=JB0KNfZx78+s/dAwUT43l9WaU0e/kjyypoM5ZNSLsgmo5C6KIHqfDRaKOcQ4XjCH6/ tq6ZstLCz/20i/eAWCvyD3b9c4bHgh0c0cRqOyuhrb9H3DS7Ju0PCOgdaWkctTq+83Ez GrNeAkvBPHeBn2p3aMch1Kp5ZYMR8Fl3A/e5Q= 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=lauEveJoV+SZK3ujvmgW7KHSkHuZSZ/U/kRU7B2VNt0j8zwdcHsxOFOG9+4WBZ/3sO O9va8ZCbfh+ILfOnTHzSsSF8zB8GRQ7qoLkaNeQF4fD0L7uwbRQdMxapzfcrEAEkho/V myeubDJihRoLZZdL4LW/wtt1/e4Qmr6eBkCXg= Received: by 10.100.29.19 with SMTP id c19mr11091652anc.191.1277909600298; Wed, 30 Jun 2010 07:53:20 -0700 (PDT) Received: from localhost.localdomain (curitiba.ic.unicamp.br [143.106.7.130]) by mx.google.com with ESMTPS id t5sm38169951ani.25.2010.06.30.07.53.17 (version=TLSv1/SSLv3 cipher=RC4-MD5); Wed, 30 Jun 2010 07:53:19 -0700 (PDT) From: Miguel Di Ciurcio Filho To: qemu-devel@nongnu.org Date: Wed, 30 Jun 2010 11:52:59 -0300 Message-Id: <1277909584-13012-3-git-send-email-miguel.filho@gmail.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1277909584-13012-1-git-send-email-miguel.filho@gmail.com> References: <1277909584-13012-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 v2 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);