This is a shorthand function for changing a QDict's entry's key. Signed-off-by: Max Reitz <mre...@redhat.com> --- 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 71b8eb0..223f746 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 9833bd0..bbfe39f 100644 --- a/qobject/qdict.c +++ b/qobject/qdict.c @@ -168,6 +168,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.7.1