From patchwork Wed Apr 6 18:28:37 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Max Reitz X-Patchwork-Id: 607134 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3qgDlr6hH1z9sCy for ; Thu, 7 Apr 2016 04:29:44 +1000 (AEST) Received: from localhost ([::1]:44974 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ansD1-0008R3-3I for incoming@patchwork.ozlabs.org; Wed, 06 Apr 2016 14:29:43 -0400 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 Received: from mx1.redhat.com ([209.132.183.28]:50306) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ansCG-0006rV-HM; Wed, 06 Apr 2016 14:28:56 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 32F7E64D09; Wed, 6 Apr 2016 18:28:56 +0000 (UTC) Received: from localhost (ovpn-116-130.ams2.redhat.com [10.36.116.130]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u36ISsGH028460 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Wed, 6 Apr 2016 14:28:55 -0400 From: Max Reitz To: qemu-block@nongnu.org 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> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Wed, 06 Apr 2016 18:28:56 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Cc: Kevin Wolf , Markus Armbruster , qemu-devel@nongnu.org, Max Reitz , Paolo Bonzini , Luiz Capitulino Subject: [Qemu-devel] [PATCH v3 01/14] qdict: Add qdict_change_key() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This is a shorthand function for changing a QDict's entry's key. Signed-off-by: Max Reitz Reviewed-by: Eric Blake --- 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)