From patchwork Tue May 18 17:07:40 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: 52905 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 EBDC4B7D85 for ; Wed, 19 May 2010 03:37:51 +1000 (EST) Received: from localhost ([127.0.0.1]:39124 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OEQjk-0000Rb-VN for incoming@patchwork.ozlabs.org; Tue, 18 May 2010 13:37:49 -0400 Received: from [140.186.70.92] (port=60996 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OEQRh-0006IH-3T for qemu-devel@nongnu.org; Tue, 18 May 2010 13:19:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OEQH5-0005Qt-D6 for qemu-devel@nongnu.org; Tue, 18 May 2010 13:08:21 -0400 Received: from mail-yw0-f184.google.com ([209.85.211.184]:51679) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OEQH5-0005QE-AC for qemu-devel@nongnu.org; Tue, 18 May 2010 13:08:11 -0400 Received: by mail-yw0-f184.google.com with SMTP id 14so3433770ywh.25 for ; Tue, 18 May 2010 10:08:11 -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=uTpUt51YlYo1Pc4c+IsUgTFRAo8e3urVc+zTmcRX0qk=; b=FCAxiBWBdUWl/LO8QC4jtTwYE5VAg7665PflrJlKRK8sV/oPxxKg0TS+sAQ7RA2Rex GDkEjKtQSGpYZjaO9LNrNkW6qldLloi+1zfHdQfWqDSnzDYBljwMys4VAq5OdF/30awt A7eW671Ay5f3PjZglXddl3HWT/avcsvmccGS4= 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=G561011dsh+y/JqoXCBV4s0qfl7YdCZxu8S7qB6yZMv+5yJhwgj+VFMlL+2FMOI3RU LZ3Yp0ggRAiyT3nDP1yvX3CEwteUkCxLutsUO+kCsf9Ag3Y2YkYJflvhn/sbln3EWOhU QBFo0TNA4jwO/mbeLEbgAaQVTScVZc822E+2M= Received: by 10.150.2.10 with SMTP id 10mr8398784ybb.212.1274202490738; Tue, 18 May 2010 10:08:10 -0700 (PDT) Received: from localhost.localdomain (quake.ic.unicamp.br [143.106.7.51]) by mx.google.com with ESMTPS id k30sm58427272ybb.19.2010.05.18.10.08.08 (version=TLSv1/SSLv3 cipher=RC4-MD5); Tue, 18 May 2010 10:08:10 -0700 (PDT) From: Miguel Di Ciurcio Filho To: qemu-devel@nongnu.org Date: Tue, 18 May 2010 14:07:40 -0300 Message-Id: <1274202469-9332-2-git-send-email-miguel.filho@gmail.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1274202469-9332-1-git-send-email-miguel.filho@gmail.com> References: <1274202469-9332-1-git-send-email-miguel.filho@gmail.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) Cc: armbru@redhat.com, lcapitulino@redhat.com Subject: [Qemu-devel] [PATCH v4 01/10] 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 | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ qdict.h | 9 +++++++++ 2 files changed, 64 insertions(+), 0 deletions(-) diff --git a/qdict.c b/qdict.c index 175bc17..19c053f 100644 --- a/qdict.c +++ b/qdict.c @@ -267,6 +267,61 @@ const char *qdict_get_str(const QDict *qdict, const char *key) return qstring_get_str(qobject_to_qstring(obj)); } +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->qdict_iter_current_key++; + + if (pack->qdict_iter_current_key < pack->qdict_iter_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->qdict_iter_current_key = 0; + pack->qdict_iter_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..8a54733 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 @@ -32,6 +33,13 @@ typedef struct QDict { QLIST_HEAD(,QDictEntry) table[QDICT_HASH_SIZE]; } QDict; +struct qstring_pack { + QString *str; + size_t qdict_iter_total_keys; + size_t qdict_iter_current_key; + const char *separator; +}; + /* Object API */ QDict *qdict_new(void); size_t qdict_size(const QDict *qdict); @@ -55,6 +63,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);