From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Nl69R-0004Cz-Kk for qemu-devel@nongnu.org; Fri, 26 Feb 2010 14:47:05 -0500 Received: from [199.232.76.173] (port=53245 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Nl69R-0004Cj-2n for qemu-devel@nongnu.org; Fri, 26 Feb 2010 14:47:05 -0500 Received: from Debian-exim by monty-python.gnu.org with spam-scanned (Exim 4.60) (envelope-from ) id 1Nl69P-0007LX-HF for qemu-devel@nongnu.org; Fri, 26 Feb 2010 14:47:04 -0500 Received: from mx1.redhat.com ([209.132.183.28]:36072) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1Nl69P-0007LT-1M for qemu-devel@nongnu.org; Fri, 26 Feb 2010 14:47:03 -0500 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o1QJl2U8016328 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Fri, 26 Feb 2010 14:47:02 -0500 Date: Fri, 26 Feb 2010 16:46:55 -0300 From: Luiz Capitulino Subject: Re: [Qemu-devel] [PATCH RFC 43/48] qemu-option: Functions to convert to/from QDict. Message-ID: <20100226164655.36487bf8@redhat.com> In-Reply-To: <1267034160-3517-44-git-send-email-armbru@redhat.com> References: <1267034160-3517-1-git-send-email-armbru@redhat.com> <1267034160-3517-44-git-send-email-armbru@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Markus Armbruster Cc: qemu-devel@nongnu.org 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? 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. > + 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);