Patchwork [RFC,43/48] qemu-option: Functions to convert to/from QDict.

login
register
mail settings
Submitter Markus Armbruster
Date Feb. 24, 2010, 5:55 p.m.
Message ID <1267034160-3517-44-git-send-email-armbru@redhat.com>
Download mbox | patch
Permalink /patch/46158/
State New
Headers show

Comments

Markus Armbruster - Feb. 24, 2010, 5:55 p.m.
FIXME Only string options are implemented.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 qemu-option.c |   54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qemu-option.h |    3 +++
 2 files changed, 57 insertions(+), 0 deletions(-)
Luiz Capitulino - Feb. 26, 2010, 7:46 p.m.
On Wed, 24 Feb 2010 18:55:55 +0100
Markus Armbruster <armbru@redhat.com> wrote:

> FIXME Only string options are implemented.
> 
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
>  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 <stdint.h>
>  #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);
Markus Armbruster - March 1, 2010, 9:24 a.m.
Luiz Capitulino <lcapitulino@redhat.com> writes:

> On Wed, 24 Feb 2010 18:55:55 +0100
> Markus Armbruster <armbru@redhat.com> wrote:
>
>> FIXME Only string options are implemented.
>> 
>> Signed-off-by: Markus Armbruster <armbru@redhat.com>
>> ---
>>  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 <stdint.h>
>>  #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);

Patch

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);
+}
+
+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;
+    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 <stdint.h>
 #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);