[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [RFC v2 2/7] qemu-option: opt_set(): split it up into more
From: |
Dong Xu Wang |
Subject: |
[Qemu-devel] [RFC v2 2/7] qemu-option: opt_set(): split it up into more functions |
Date: |
Thu, 27 Sep 2012 13:14:20 +0800 |
The new functions are opts_accepts_any() and find_desc_by_name(), which
are also going to be used by qemu_opts_validate() (see next commit).
This also makes opt_set() slightly more readable.
Signed-off-by: Luiz Capitulino <address@hidden>
Signed-off-by: Dong Xu Wang <address@hidden>
---
qemu-option.c | 40 ++++++++++++++++++++++++----------------
1 files changed, 24 insertions(+), 16 deletions(-)
diff --git a/qemu-option.c b/qemu-option.c
index 0b81e77..d8b0988 100644
--- a/qemu-option.c
+++ b/qemu-option.c
@@ -602,26 +602,36 @@ static void qemu_opt_del(QemuOpt *opt)
g_free(opt);
}
-static void opt_set(QemuOpts *opts, const char *name, const char *value,
- bool prepend, Error **errp)
+static bool opts_accepts_any(const QemuOpts *opts)
+{
+ return opts->list->desc[0].name == NULL;
+}
+
+static const QemuOptDesc *find_desc_by_name(const QemuOptDesc *desc,
+ const char *name)
{
- QemuOpt *opt;
- const QemuOptDesc *desc = opts->list->desc;
- Error *local_err = NULL;
int i;
for (i = 0; desc[i].name != NULL; i++) {
if (strcmp(desc[i].name, name) == 0) {
- break;
+ return &desc[i];
}
}
- if (desc[i].name == NULL) {
- if (i == 0) {
- /* empty list -> allow any */;
- } else {
- error_set(errp, QERR_INVALID_PARAMETER, name);
- return;
- }
+
+ return NULL;
+}
+
+static void opt_set(QemuOpts *opts, const char *name, const char *value,
+ bool prepend, Error **errp)
+{
+ QemuOpt *opt;
+ const QemuOptDesc *desc;
+ Error *local_err = NULL;
+
+ desc = find_desc_by_name(opts->list->desc, name);
+ if (!desc && !opts_accepts_any(opts)) {
+ error_set(errp, QERR_INVALID_PARAMETER, name);
+ return;
}
opt = g_malloc0(sizeof(*opt));
@@ -632,9 +642,7 @@ static void opt_set(QemuOpts *opts, const char *name, const
char *value,
} else {
QTAILQ_INSERT_TAIL(&opts->head, opt, next);
}
- if (desc[i].name != NULL) {
- opt->desc = desc+i;
- }
+ opt->desc = desc;
if (value) {
opt->str = g_strdup(value);
}
--
1.7.1
- [Qemu-devel] [RFC v2 0/7] replace QEMUOptionParameter with QemuOpts parser, Dong Xu Wang, 2012/09/27
- [Qemu-devel] [RFC v2 1/7] qemu-option: qemu_opt_set_bool(): fix code duplication, Dong Xu Wang, 2012/09/27
- [Qemu-devel] [RFC v2 2/7] qemu-option: opt_set(): split it up into more functions,
Dong Xu Wang <=
- [Qemu-devel] [RFC v2 3/7] qemu-option: qemu_opts_validate(): fix duplicated code, Dong Xu Wang, 2012/09/27
- [Qemu-devel] [RFC v2 4/7] introduce qemu_opts_create_nofail function, Dong Xu Wang, 2012/09/27
- [Qemu-devel] [RFC v2 5/7] use qemu_opts_create_nofail, Dong Xu Wang, 2012/09/27
- [Qemu-devel] [RFC v2 6/7] create new function: qemu_opt_set_number, Dong Xu Wang, 2012/09/27
- [Qemu-devel] [RFC v2 7/7] remove QEMUOptionParameter, Dong Xu Wang, 2012/09/27