From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36068) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ansCJ-00075d-Os for qemu-devel@nongnu.org; Wed, 06 Apr 2016 14:29:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ansCI-0006sI-Jp for qemu-devel@nongnu.org; Wed, 06 Apr 2016 14:28:59 -0400 From: Max Reitz Date: Wed, 6 Apr 2016 20:28:37 +0200 Message-Id: <1459967330-4573-2-git-send-email-mreitz@redhat.com> In-Reply-To: <1459967330-4573-1-git-send-email-mreitz@redhat.com> References: <1459967330-4573-1-git-send-email-mreitz@redhat.com> Subject: [Qemu-devel] [PATCH v3 01/14] qdict: Add qdict_change_key() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-block@nongnu.org Cc: Kevin Wolf , Markus Armbruster , qemu-devel@nongnu.org, Max Reitz , Paolo Bonzini , Luiz Capitulino This is a shorthand function for changing a QDict's entry's key. Signed-off-by: Max Reitz --- include/qapi/qmp/qdict.h | 1 + qobject/qdict.c | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/include/qapi/qmp/qdict.h b/include/qapi/qmp/qdict.h index 8a3ac13..3e0d7df 100644 --- a/include/qapi/qmp/qdict.h +++ b/include/qapi/qmp/qdict.h @@ -39,6 +39,7 @@ size_t qdict_size(const QDict *qdict); void qdict_put_obj(QDict *qdict, const char *key, QObject *value); void qdict_del(QDict *qdict, const char *key); int qdict_haskey(const QDict *qdict, const char *key); +bool qdict_change_key(QDict *qdict, const char *old_key, const char *new_key); QObject *qdict_get(const QDict *qdict, const char *key); QDict *qobject_to_qdict(const QObject *obj); void qdict_iter(const QDict *qdict, diff --git a/qobject/qdict.c b/qobject/qdict.c index ae6ad06..2eacb3d 100644 --- a/qobject/qdict.c +++ b/qobject/qdict.c @@ -170,6 +170,29 @@ int qdict_haskey(const QDict *qdict, const char *key) } /** + * qdict_change_key(): Changes an entry's key + * + * Removes the entry with the key 'old_key' and inserts its associated value as + * a new entry with the key 'new_key'. + * + * Returns false if 'old_key' does not exist, true otherwise. + */ +bool qdict_change_key(QDict *qdict, const char *old_key, const char *new_key) +{ + QObject *value = qdict_get(qdict, old_key); + + if (!value) { + return false; + } + + qobject_incref(value); + qdict_del(qdict, old_key); + qdict_put_obj(qdict, new_key, value); + + return true; +} + +/** * qdict_size(): Return the size of the dictionary */ size_t qdict_size(const QDict *qdict) -- 2.8.0