From: Miguel Di Ciurcio Filho <miguel.filho@gmail.com>
To: qemu-devel@nongnu.org
Cc: armbru@redhat.com, lcapitulino@redhat.com,
Miguel Di Ciurcio Filho <miguel.filho@gmail.com>,
avi@redhat.com
Subject: [Qemu-devel] [PATCH 1/8] QObject API: introduce qdict_to_qstring() function
Date: Thu, 10 Jun 2010 18:36:58 -0300 [thread overview]
Message-ID: <1276205825-1922-2-git-send-email-miguel.filho@gmail.com> (raw)
In-Reply-To: <1276205825-1922-1-git-send-email-miguel.filho@gmail.com>
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 <miguel.filho@gmail.com>
---
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 <stdint.h>
@@ -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);
--
1.7.1
next prev parent reply other threads:[~2010-06-10 21:38 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-06-10 21:36 [Qemu-devel] [PATCH 0/8] QMP: Introduce query-netdev Miguel Di Ciurcio Filho
2010-06-10 21:36 ` Miguel Di Ciurcio Filho [this message]
2010-06-10 21:36 ` [Qemu-devel] [PATCH 2/8] QMP: Introduce the documentation for query-netdev and info netdev Miguel Di Ciurcio Filho
2010-06-11 11:37 ` Markus Armbruster
2010-06-11 11:47 ` Miguel Di Ciurcio Filho
2010-06-16 17:39 ` [Qemu-devel] " Luiz Capitulino
2010-06-16 21:55 ` Miguel Di Ciurcio Filho
2010-06-10 21:37 ` [Qemu-devel] [PATCH 3/8] net: Introduce VLANClientState->info_dict Miguel Di Ciurcio Filho
2010-06-16 17:40 ` [Qemu-devel] " Luiz Capitulino
2010-06-10 21:37 ` [Qemu-devel] [PATCH 4/8] net: tap/tap-win32: introduce info_dict Miguel Di Ciurcio Filho
2010-06-16 17:41 ` [Qemu-devel] " Luiz Capitulino
2010-06-10 21:37 ` [Qemu-devel] [PATCH 5/8] net: vde: " Miguel Di Ciurcio Filho
2010-06-10 21:37 ` [Qemu-devel] [PATCH 6/8] net: socket: " Miguel Di Ciurcio Filho
2010-06-10 21:37 ` [Qemu-devel] [PATCH 7/8] net: slirp: " Miguel Di Ciurcio Filho
2010-06-10 21:37 ` [Qemu-devel] [PATCH 8/8] monitor/net: introduce 'info netdev' with QMP support Miguel Di Ciurcio Filho
2010-06-16 17:44 ` [Qemu-devel] Re: [PATCH 0/8] QMP: Introduce query-netdev Luiz Capitulino
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1276205825-1922-2-git-send-email-miguel.filho@gmail.com \
--to=miguel.filho@gmail.com \
--cc=armbru@redhat.com \
--cc=avi@redhat.com \
--cc=lcapitulino@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).