diff options
author | Kevin Wolf <kwolf@redhat.com> | 2013-07-08 17:11:58 +0200 |
---|---|---|
committer | Kevin Wolf <kwolf@redhat.com> | 2013-07-26 22:01:31 +0200 |
commit | f660dc6a2e97756596b2e79ce6127a3034f2308b (patch) | |
tree | 2fce1bb3445ea9a74b60715ccf00e213bdbd0f0d /qobject | |
parent | 29c4e2b50d95f4a15c3dd62b39f3402f05a34907 (diff) | |
download | qemu-f660dc6a2e97756596b2e79ce6127a3034f2308b.zip qemu-f660dc6a2e97756596b2e79ce6127a3034f2308b.tar.gz qemu-f660dc6a2e97756596b2e79ce6127a3034f2308b.tar.bz2 |
Implement qdict_flatten()
qdict_flatten(): For each nested QDict with key x, all fields with key y
are moved to this QDict and their key is renamed to "x.y". This operation
is applied recursively for nested QDicts.
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Diffstat (limited to 'qobject')
-rw-r--r-- | qobject/qdict.c | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/qobject/qdict.c b/qobject/qdict.c index ed381f9..472f106 100644 --- a/qobject/qdict.c +++ b/qobject/qdict.c @@ -476,3 +476,54 @@ static void qdict_destroy_obj(QObject *obj) g_free(qdict); } + +static void qdict_do_flatten(QDict *qdict, QDict *target, const char *prefix) +{ + QObject *value; + const QDictEntry *entry, *next; + const char *new_key; + bool delete; + + entry = qdict_first(qdict); + + while (entry != NULL) { + + next = qdict_next(qdict, entry); + value = qdict_entry_value(entry); + new_key = NULL; + delete = false; + + if (prefix) { + qobject_incref(value); + new_key = g_strdup_printf("%s.%s", prefix, entry->key); + qdict_put_obj(target, new_key, value); + delete = true; + } + + if (qobject_type(value) == QTYPE_QDICT) { + qdict_do_flatten(qobject_to_qdict(value), target, + new_key ? new_key : entry->key); + delete = true; + } + + if (delete) { + qdict_del(qdict, entry->key); + + /* Restart loop after modifying the iterated QDict */ + entry = qdict_first(qdict); + continue; + } + + entry = next; + } +} + +/** + * qdict_flatten(): For each nested QDict with key x, all fields with key y + * are moved to this QDict and their key is renamed to "x.y". This operation + * is applied recursively for nested QDicts. + */ +void qdict_flatten(QDict *qdict) +{ + qdict_do_flatten(qdict, qdict, NULL); +} |