Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Nm1s0-0001dq-IK for qemu-devel@nongnu.org; Mon, 01 Mar 2010 04:24:56 -0500 Received: from [199.232.76.173] (port=52315 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Nm1rz-0001dX-T7 for qemu-devel@nongnu.org; Mon, 01 Mar 2010 04:24:55 -0500 Received: from Debian-exim by monty-python.gnu.org with spam-scanned (Exim 4.60) (envelope-from ) id 1Nm1ry-0008QL-1h for qemu-devel@nongnu.org; Mon, 01 Mar 2010 04:24:55 -0500 Received: from mx1.redhat.com ([209.132.183.28]:28833) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1Nm1rx-0008QH-Le for qemu-devel@nongnu.org; Mon, 01 Mar 2010 04:24:53 -0500 Received: from int-mx05.intmail.prod.int.phx2.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.18]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o219OqOf017961 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Mon, 1 Mar 2010 04:24:53 -0500 From: Markus Armbruster Subject: Re: [Qemu-devel] [PATCH RFC 43/48] qemu-option: Functions to convert to/from QDict. References: <1267034160-3517-1-git-send-email-armbru@redhat.com> <1267034160-3517-44-git-send-email-armbru@redhat.com> <20100226164655.36487bf8@redhat.com> Date: Mon, 01 Mar 2010 10:24:50 +0100 In-Reply-To: <20100226164655.36487bf8@redhat.com> (Luiz Capitulino's message of "Fri, 26 Feb 2010 16:46:55 -0300") Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Luiz Capitulino Cc: qemu-devel@nongnu.org Luiz Capitulino writes: > On Wed, 24 Feb 2010 18:55:55 +0100 > Markus Armbruster wrote: > >> FIXME Only string options are implemented. >> >> Signed-off-by: Markus Armbruster >> --- >> qemu-option.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ >> qemu-option.h | 3 +++ >> 2 files changed, 57 insertions(+), 0 deletions(-) >> >> diff --git a/qemu-option.c b/qemu-option.c >> index ab488e4..f86aac0 100644 >> --- a/qemu-option.c >> +++ b/qemu-option.c >> @@ -29,6 +29,8 @@ >> #include "qemu-common.h" >> #include "qemu-error.h" >> #include "qemu-option.h" >> +#include "qobject.h" >> +#include "qstring.h" >> >> /* >> * Extracts the name of an option from the parameter string (p points at the >> @@ -777,6 +779,58 @@ QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, const char *fi >> return opts; >> } >> >> +static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque) >> +{ >> + const char *value; >> + >> + if (!strcmp(key, "id")) { >> + return; >> + } >> + >> + switch (qobject_type(obj)) { >> + case QTYPE_QSTRING: >> + value = qstring_get_str(qobject_to_qstring(obj)); >> + break; >> + default: >> + abort(); // FIXME implement >> + } >> + qemu_opt_set(opaque, key, value); > > qstring_get_str() returns a pointer to the stored string, are you sure > you want to pass it down without cloning it? qemu_opt_set() strdups both its name and its value argument. > We allow this violation in QString because most handlers that get a string > wants only temporary read-only access. In this case the violation is very > convenient, as otherwise we would have to add tons of free()s around. > >> +} >> + >> +QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict) >> +{ >> + const char *id; >> + QemuOpts *opts; >> + >> + id = qdict_haskey(qdict, "id") ? qdict_get_str(qdict, "id") : NULL; > > You should use qdict_get_try_str() here. Yes, thanks. >> + opts = qemu_opts_create(list, id, 1); >> + if (opts == NULL) >> + return NULL; >> + >> + qdict_iter(qdict, qemu_opts_from_qdict_1, opts); >> + return opts; >> +} >> + >> +static int qemu_opts_to_qdict_1(const char *name, const char *value, >> + void *opaque) >> +{ >> + qdict_put(opaque, name, qstring_from_str(value)); >> + // FIXME obey QemuOptType >> + return 0; >> +} >> + >> +QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict) >> +{ >> + if (!qdict) { >> + qdict = qdict_new(); >> + } >> + if (opts->id) { >> + qdict_put(qdict, "id", qstring_from_str(opts->id)); >> + } >> + qemu_opt_foreach(opts, qemu_opts_to_qdict_1, qdict, 0); >> + return qdict; >> +} >> + >> /* Validate parsed opts against descriptions where no >> * descriptions were provided in the QemuOptsList. >> */ >> diff --git a/qemu-option.h b/qemu-option.h >> index f3f1de7..d735386 100644 >> --- a/qemu-option.h >> +++ b/qemu-option.h >> @@ -28,6 +28,7 @@ >> >> #include >> #include "qemu-queue.h" >> +#include "qdict.h" >> >> enum QEMUOptionParType { >> OPT_FLAG, >> @@ -118,6 +119,8 @@ void qemu_opts_del(QemuOpts *opts); >> int qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc); >> int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname); >> QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, const char *firstname); >> +QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict); >> +QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict); >> >> typedef int (*qemu_opts_loopfunc)(QemuOpts *opts, void *opaque); >> int qemu_opts_print(QemuOpts *opts, void *dummy);