diff mbox

[V4,06/10] create new function: qemu_opt_set_number

Message ID 1351169848-28223-7-git-send-email-wdongxu@linux.vnet.ibm.com
State New
Headers show

Commit Message

Robert Wang Oct. 25, 2012, 12:57 p.m. UTC
Signed-off-by: Dong Xu Wang <wdongxu@linux.vnet.ibm.com>
---
 qemu-option.c |   24 ++++++++++++++++++++++++
 qemu-option.h |    1 +
 2 files changed, 25 insertions(+), 0 deletions(-)

Comments

Stefan Hajnoczi Oct. 26, 2012, 9:02 a.m. UTC | #1
On Thu, Oct 25, 2012 at 08:57:24PM +0800, Dong Xu Wang wrote:
> diff --git a/qemu-option.c b/qemu-option.c
> index d7d5ea9..eeb2c9c 100644
> --- a/qemu-option.c
> +++ b/qemu-option.c
> @@ -695,6 +695,30 @@ int qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val)
>      return 0;
>  }
>  
> +int qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val)
> +{
> +    char buffer[1024];
> +    QemuOpt *opt;
> +    const QemuOptDesc *desc = opts->list->desc;
> +
> +    snprintf(buffer, sizeof(buffer), "%" PRId64, val);

g_strdup_printf() is a nice replacement for fixed-size buffer +
snprintf() + g_strdup():

http://developer.gnome.org/glib/2.28/glib-String-Utility-Functions.html#g-strdup-printf

Stefan
Robert Wang Oct. 29, 2012, 8:03 a.m. UTC | #2
On Fri, Oct 26, 2012 at 5:02 PM, Stefan Hajnoczi <stefanha@gmail.com> wrote:
> On Thu, Oct 25, 2012 at 08:57:24PM +0800, Dong Xu Wang wrote:
>> diff --git a/qemu-option.c b/qemu-option.c
>> index d7d5ea9..eeb2c9c 100644
>> --- a/qemu-option.c
>> +++ b/qemu-option.c
>> @@ -695,6 +695,30 @@ int qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val)
>>      return 0;
>>  }
>>
>> +int qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val)
>> +{
>> +    char buffer[1024];
>> +    QemuOpt *opt;
>> +    const QemuOptDesc *desc = opts->list->desc;
>> +
>> +    snprintf(buffer, sizeof(buffer), "%" PRId64, val);
>
> g_strdup_printf() is a nice replacement for fixed-size buffer +
> snprintf() + g_strdup():
>
> http://developer.gnome.org/glib/2.28/glib-String-Utility-Functions.html#g-strdup-printf
>
Okay.

> Stefan
>
diff mbox

Patch

diff --git a/qemu-option.c b/qemu-option.c
index d7d5ea9..eeb2c9c 100644
--- a/qemu-option.c
+++ b/qemu-option.c
@@ -695,6 +695,30 @@  int qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val)
     return 0;
 }
 
+int qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val)
+{
+    char buffer[1024];
+    QemuOpt *opt;
+    const QemuOptDesc *desc = opts->list->desc;
+
+    snprintf(buffer, sizeof(buffer), "%" PRId64, val);
+    opt = g_malloc0(sizeof(*opt));
+    opt->desc = find_desc_by_name(desc, name);
+    if (!opt->desc && !opts_accepts_any(opts)) {
+        qerror_report(QERR_INVALID_PARAMETER, name);
+        g_free(opt);
+        return -1;
+    }
+
+    opt->name = g_strdup(name);
+    opt->opts = opts;
+    opt->value.uint = val;
+    opt->str = g_strdup(buffer);
+    QTAILQ_INSERT_TAIL(&opts->head, opt, next);
+
+    return 0;
+}
+
 int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
                      int abort_on_failure)
 {
diff --git a/qemu-option.h b/qemu-option.h
index b0f8d1e..002dd07 100644
--- a/qemu-option.h
+++ b/qemu-option.h
@@ -126,6 +126,7 @@  int qemu_opt_set(QemuOpts *opts, const char *name, const char *value);
 void qemu_opt_set_err(QemuOpts *opts, const char *name, const char *value,
                       Error **errp);
 int qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val);
+int qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val);
 typedef int (*qemu_opt_loopfunc)(const char *name, const char *value, void *opaque);
 int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
                      int abort_on_failure);