Received: from eggs.gnu.org ([2001:4830:134:3::10]:36281) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WlnZn-0001p6-KU for qemu-devel@nongnu.org; Sat, 17 May 2014 18:59:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WlnZf-0002UV-1V for qemu-devel@nongnu.org; Sat, 17 May 2014 18:59:35 -0400 Received: from mx-v6.kamp.de ([2a02:248:0:51::16]:34412 helo=mx01.kamp.de) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WlnZe-0002Tt-NG for qemu-devel@nongnu.org; Sat, 17 May 2014 18:59:26 -0400 From: Peter Lieven Date: Sun, 18 May 2014 00:58:18 +0200 Message-Id: <1400367499-12465-3-git-send-email-pl@kamp.de> In-Reply-To: <1400367499-12465-1-git-send-email-pl@kamp.de> References: <1400367499-12465-1-git-send-email-pl@kamp.de> Subject: [Qemu-devel] [PATCHv5 2/3] blockdev: add a function to parse enum ids from strings List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: kwolf@redhat.com, famz@redhat.com, Peter Lieven , stefanha@redhat.com, pbonzini@redhat.com this adds a generic function to recover the enum id of a parameter given as a string. Signed-off-by: Peter Lieven --- blockdev.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/blockdev.c b/blockdev.c index 7810e9f..8358aa2 100644 --- a/blockdev.c +++ b/blockdev.c @@ -288,6 +288,23 @@ static int parse_block_error_action(const char *buf, bool is_read, Error **errp) } } +static inline int parse_enum_option(const char *lookup[], const char *buf, int max, + int def, Error **errp) +{ + int i; + if (!buf) { + return def; + } + for (i = 0; i < max; i++) { + if (!strcmp(buf, lookup[i])) { + return i; + } + } + error_setg(errp, "invalid parameter value: %s", + buf); + return def; +} + static bool check_throttle_config(ThrottleConfig *cfg, Error **errp) { if (throttle_conflicting(cfg)) { -- 1.7.9.5