qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH 1/8] QObject API: introduce qdict_to_qstring() funct


From: Miguel Di Ciurcio Filho
Subject: [Qemu-devel] [PATCH 1/8] QObject API: introduce qdict_to_qstring() function
Date: Thu, 10 Jun 2010 18:36:58 -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 |   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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]