[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v4 01/10] QObject API: introduce qdict_to_qstring()
From: |
Miguel Di Ciurcio Filho |
Subject: |
[Qemu-devel] [PATCH v4 01/10] QObject API: introduce qdict_to_qstring() function |
Date: |
Tue, 18 May 2010 14:07:40 -0300 |
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 <address@hidden>
---
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 <stdint.h>
@@ -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);
--
1.7.1
- [Qemu-devel] [PATCH v4 09/10] net: socket: introduce info_dict, (continued)
- [Qemu-devel] [PATCH v4 09/10] net: socket: introduce info_dict, Miguel Di Ciurcio Filho, 2010/05/18
- [Qemu-devel] [PATCH v4 08/10] net: slirp: introduce info_dict, Miguel Di Ciurcio Filho, 2010/05/18
- [Qemu-devel] [PATCH v4 04/10] net: xen: introduce info_dict, Miguel Di Ciurcio Filho, 2010/05/18
- [Qemu-devel] [PATCH v4 07/10] net: dump: introduce info_dict, Miguel Di Ciurcio Filho, 2010/05/18
- [Qemu-devel] [PATCH v4 06/10] net: vde: introduce info_dict, Miguel Di Ciurcio Filho, 2010/05/18
- [Qemu-devel] [PATCH v4 05/10] net: tap/tap-win32: introduce info_dict, Miguel Di Ciurcio Filho, 2010/05/18
- [Qemu-devel] [PATCH v4 03/10] net: various devices: add qemu_format_nic_info_dict, Miguel Di Ciurcio Filho, 2010/05/18
- [Qemu-devel] [PATCH v4 02/10] net: introduce qemu_nic_format_info_dict and VLANClientState->info_dict, Miguel Di Ciurcio Filho, 2010/05/18
- [Qemu-devel] [PATCH v4 01/10] QObject API: introduce qdict_to_qstring() function,
Miguel Di Ciurcio Filho <=
- [Qemu-devel] Re: [PATCH v4 0/10] Introduce 'info netdevices' with QMP support, Luiz Capitulino, 2010/05/27