diff mbox

[for-2.10,1/5] QemuOpts: introduce qemu_opts_extract()

Message ID 1490945793-21276-2-git-send-email-peterx@redhat.com
State New
Headers show

Commit Message

Peter Xu March 31, 2017, 7:36 a.m. UTC
This helper function is used to extract specific QemuOpts item from an
existing QemuOptsList which matches specific patterns.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 include/qemu/option.h |  2 ++
 util/qemu-option.c    | 24 ++++++++++++++++++++++++
 2 files changed, 26 insertions(+)
diff mbox

Patch

diff --git a/include/qemu/option.h b/include/qemu/option.h
index f7338db..355cee8 100644
--- a/include/qemu/option.h
+++ b/include/qemu/option.h
@@ -136,6 +136,8 @@  void qemu_opts_absorb_qdict(QemuOpts *opts, QDict *qdict, Error **errp);
 typedef int (*qemu_opts_loopfunc)(void *opaque, QemuOpts *opts, Error **errp);
 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func,
                       void *opaque, Error **errp);
+QemuOpts *qemu_opts_extract(QemuOptsList *list, qemu_opts_loopfunc func,
+                            void *opaque, Error **errp);
 void qemu_opts_print(QemuOpts *opts, const char *sep);
 void qemu_opts_print_help(QemuOptsList *list);
 void qemu_opts_free(QemuOptsList *list);
diff --git a/util/qemu-option.c b/util/qemu-option.c
index 5ce1b5c..7c34d88 100644
--- a/util/qemu-option.c
+++ b/util/qemu-option.c
@@ -1121,6 +1121,30 @@  int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func,
     return rc;
 }
 
+/*
+ * Extract specific QemuOpts from a QemuOptsList. For each QemuOpts
+ * item, if checks against func() returns zero, it'll be picked out
+ * from current QemuOptsList, then returned. If there are more than
+ * one QemuOpts that match the check, will only return the first one
+ * found.
+ */
+QemuOpts *qemu_opts_extract(QemuOptsList *list, qemu_opts_loopfunc func,
+                            void *opaque, Error **errp)
+{
+    QemuOpts *opts, *next_opts;
+
+    assert(list && func);
+
+    QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) {
+        if (func(opaque, opts, errp) == 0) {
+            QTAILQ_REMOVE(&list->head, opts, next);
+            return opts;
+        }
+    }
+
+    return NULL;
+}
+
 static size_t count_opts_list(QemuOptsList *list)
 {
     QemuOptDesc *desc = NULL;