This helper function is used to extract specific QemuOpts item from an existing QemuOptsList which matches specific patterns.
Signed-off-by: Peter Xu <pet...@redhat.com> --- include/qemu/option.h | 2 ++ util/qemu-option.c | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+) 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; -- 2.7.4