qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Max Reitz <mreitz@redhat.com>
To: qemu-devel@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>, Fam Zheng <famz@redhat.com>,
	Stefan Hajnoczi <stefanha@redhat.com>,
	Max Reitz <mreitz@redhat.com>
Subject: [Qemu-devel] [PATCH v6 03/22] qdict: Add qdict_array_split()
Date: Thu, 19 Dec 2013 20:47:04 +0100	[thread overview]
Message-ID: <1387482443-10633-4-git-send-email-mreitz@redhat.com> (raw)
In-Reply-To: <1387482443-10633-1-git-send-email-mreitz@redhat.com>

This function splits a QDict consisting of entries prefixed by
incrementally enumerated indices into a QList of QDicts.

Signed-off-by: Max Reitz <mreitz@redhat.com>
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
---
 include/qapi/qmp/qdict.h |  1 +
 qobject/qdict.c          | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)

diff --git a/include/qapi/qmp/qdict.h b/include/qapi/qmp/qdict.h
index 5cefd80..1ddf97b 100644
--- a/include/qapi/qmp/qdict.h
+++ b/include/qapi/qmp/qdict.h
@@ -68,5 +68,6 @@ QDict *qdict_clone_shallow(const QDict *src);
 void qdict_flatten(QDict *qdict);
 
 void qdict_extract_subqdict(QDict *src, QDict **dst, const char *start);
+void qdict_array_split(QDict *src, QList **dst);
 
 #endif /* QDICT_H */
diff --git a/qobject/qdict.c b/qobject/qdict.c
index 17e14f0..fca1902 100644
--- a/qobject/qdict.c
+++ b/qobject/qdict.c
@@ -554,3 +554,39 @@ void qdict_extract_subqdict(QDict *src, QDict **dst, const char *start)
         entry = next;
     }
 }
+
+/**
+ * qdict_array_split(): This function creates a QList of QDicts. Every entry in
+ * the original QDict with a key prefixed "%u.", where %u designates an unsigned
+ * integer starting at 0 and incrementally counting up, will be moved to a new
+ * QDict at index %u in the output QList with the key prefix removed. The
+ * function terminates when there is no entry in the QDict with a prefix
+ * directly (incrementally) following the last one.
+ * Example: {"0.a": 42, "0.b": 23, "1.x": 0, "3.y": 1, "o.o": 7}
+ *      (or {"1.x": 0, "3.y": 1, "0.a": 42, "o.o": 7, "0.b": 23})
+ *       => [{"a": 42, "b": 23}, {"x": 0}]
+ *      and {"3.y": 1, "o.o": 7} (remainder of the old QDict)
+ */
+void qdict_array_split(QDict *src, QList **dst)
+{
+    unsigned i;
+
+    *dst = qlist_new();
+
+    for (i = 0; i < UINT_MAX; i++) {
+        QDict *subqdict;
+        char prefix[32];
+        size_t snprintf_ret;
+
+        snprintf_ret = snprintf(prefix, 32, "%u.", i);
+        assert(snprintf_ret < 32);
+
+        qdict_extract_subqdict(src, &subqdict, prefix);
+        if (!qdict_size(subqdict)) {
+            QDECREF(subqdict);
+            break;
+        }
+
+        qlist_append_obj(*dst, QOBJECT(subqdict));
+    }
+}
-- 
1.8.5.1

  parent reply	other threads:[~2013-12-19 19:46 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-12-19 19:47 [Qemu-devel] [PATCH v6 00/22] blkdebug/blkverify: Allow QMP configuration Max Reitz
2013-12-19 19:47 ` [Qemu-devel] [PATCH v6 01/22] blkdebug: Use errp for read_config() Max Reitz
2013-12-19 20:21   ` Eric Blake
2013-12-19 19:47 ` [Qemu-devel] [PATCH v6 02/22] blkdebug: Don't require sophisticated filename Max Reitz
2013-12-19 20:56   ` Eric Blake
2013-12-19 20:58     ` Max Reitz
2013-12-19 19:47 ` Max Reitz [this message]
2013-12-19 21:20   ` [Qemu-devel] [PATCH v6 03/22] qdict: Add qdict_array_split() Eric Blake
2013-12-20  9:38   ` Stefan Hajnoczi
2013-12-19 19:47 ` [Qemu-devel] [PATCH v6 04/22] qapi: extend qdict_flatten() for QLists Max Reitz
2013-12-19 21:49   ` Eric Blake
2013-12-20  9:46   ` Stefan Hajnoczi
2013-12-20 17:19     ` Max Reitz
2013-12-20 17:23       ` Max Reitz
2014-01-02  4:01         ` Stefan Hajnoczi
2013-12-19 19:47 ` [Qemu-devel] [PATCH v6 05/22] qemu-option: Add qemu_config_parse_qdict() Max Reitz
2013-12-19 22:15   ` Eric Blake
2013-12-19 19:47 ` [Qemu-devel] [PATCH v6 06/22] blkdebug: Always call read_config() Max Reitz
2013-12-19 23:06   ` Eric Blake
2013-12-19 19:47 ` [Qemu-devel] [PATCH v6 07/22] blkdebug: Use command-line in read_config() Max Reitz
2013-12-19 19:47 ` [Qemu-devel] [PATCH v6 08/22] block: Allow reference for bdrv_file_open() Max Reitz
2013-12-19 19:47 ` [Qemu-devel] [PATCH v6 09/22] block: Pass reference to bdrv_file_open() Max Reitz
2013-12-19 19:47 ` [Qemu-devel] [PATCH v6 10/22] block: Allow block devices without files Max Reitz
2013-12-19 19:47 ` [Qemu-devel] [PATCH v6 11/22] block: Add bdrv_open_image() Max Reitz
2013-12-19 19:47 ` [Qemu-devel] [PATCH v6 12/22] block: Use bdrv_open_image() in bdrv_open() Max Reitz
2013-12-19 19:47 ` [Qemu-devel] [PATCH v6 13/22] block: Allow recursive "file"s Max Reitz
2013-12-19 19:47 ` [Qemu-devel] [PATCH v6 14/22] blockdev: Move "file" to legacy_opts Max Reitz
2013-12-19 19:47 ` [Qemu-devel] [PATCH v6 15/22] blkdebug: Allow command-line file configuration Max Reitz
2013-12-19 19:47 ` [Qemu-devel] [PATCH v6 16/22] blkverify: Allow command-line configuration Max Reitz
2013-12-19 19:47 ` [Qemu-devel] [PATCH v6 17/22] blkverify: Don't require protocol filename Max Reitz
2013-12-19 19:47 ` [Qemu-devel] [PATCH v6 18/22] qapi: Add "errno" to the list of polluted words Max Reitz
2013-12-19 19:47 ` [Qemu-devel] [PATCH v6 19/22] qapi: QMP interface for blkdebug and blkverify Max Reitz
2013-12-19 19:47 ` [Qemu-devel] [PATCH v6 20/22] qemu-io: Make filename optional Max Reitz
2013-12-19 19:47 ` [Qemu-devel] [PATCH v6 21/22] iotests: Test new blkdebug/blkverify interface Max Reitz
2013-12-19 19:47 ` [Qemu-devel] [PATCH v6 22/22] iotests: Test file format nesting Max Reitz

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=1387482443-10633-4-git-send-email-mreitz@redhat.com \
    --to=mreitz@redhat.com \
    --cc=famz@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    /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).