From patchwork Thu Jul 26 17:49:54 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cole Robinson X-Patchwork-Id: 142988 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp741495ljj; Thu, 26 Jul 2018 10:50:12 -0700 (PDT) X-Google-Smtp-Source: AAOMgpcWZ0/+t9McFzcCs/ddPv5ORfavGERjsobd5CYVSubZUyZQ55z1e9AU9o/p5DSKhYzx1M8V X-Received: by 2002:a37:ae03:: with SMTP id x3-v6mr2768116qke.170.1532627412481; Thu, 26 Jul 2018 10:50:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532627412; cv=none; d=google.com; s=arc-20160816; b=F6ztZRyIaYuzOmvRq/PCHe1jb0Q4Saja4jgi1XSYcwbbNhuoLuTXcFhN9BU5O8SL6L n24NkxHbsLzIeCaLqAsaNWDz/0KYS9vPdCqihNAJ7PwcOwIuFYWHXIzz1C2yXxzS2PWm F9LEFPCh+OVPyXLR/EFMb5agzOBML98kbW6dlvqZ5iH2mJiImmDUgWJFc7PDAXyPyfj2 bBJCu912ezFzHRqjAnAsKdCmTH1n0wwWdD0rsPBSHafc3T/kkismJ7/q1tOSmqWbMTuR vJ1I7SZeeibJ0YtOREFkW1Q5osZW3lPKznBjTLgAykch3VgfAADzpvBRCSncU0gHxRiX j5Dw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:sender:content-transfer-encoding:mime-version :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:references :in-reply-to:message-id:date:to:from:delivered-to :arc-authentication-results; bh=72N994ul0WyNmX7pPUl81B3+1L3OtqQrKQfdHf4muHo=; b=LVnikqp/AnSCt8CYciHq43C9Wf+lfcR0+e34CZKeTWLVNm0pt53gypYacR1Mu3/EVc TE+Ugcvoc6/JsjuFzc8Aan4fvlk6Kh9y86E4sCPv4UNexi5M9j6yvBbZexBLkQoLwtzV r8sUAeT7huElp81LjW5hlhc6DDJ/5HJSfhsibJ2tmoHsLMGPF3phASbbqysJBT5tPESd 1TXl/Zkbyby52CLrpvGtELbFbYEalXkssROYDQSo6x/cjxDrBUimCZrsL5/geR5KKuyr JlaFyNxCNhoxEP9vm4MJoSXjPvNsvoWTGhvahxIRaFEUoWB2qvPI9DzYGQr0OlAi3xGX d3iQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of libvir-list-bounces@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com. [209.132.183.28]) by mx.google.com with ESMTPS id c40-v6si41135qtb.208.2018.07.26.10.50.12 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 26 Jul 2018 10:50:12 -0700 (PDT) Received-SPF: pass (google.com: domain of libvir-list-bounces@redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; Authentication-Results: mx.google.com; spf=pass (google.com: domain of libvir-list-bounces@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 76FDF30832EE; Thu, 26 Jul 2018 17:50:10 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3393F6047F; Thu, 26 Jul 2018 17:50:10 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 93CCF18037ED; Thu, 26 Jul 2018 17:50:09 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w6QHo71d029332 for ; Thu, 26 Jul 2018 13:50:07 -0400 Received: by smtp.corp.redhat.com (Postfix) id 1CEE31C5B0; Thu, 26 Jul 2018 17:50:07 +0000 (UTC) Delivered-To: libvirt-list@redhat.com Received: from worklaptop.bos.redhat.com (dhcp-17-157.bos.redhat.com [10.18.17.157]) by smtp.corp.redhat.com (Postfix) with ESMTP id 03D851C5A2; Thu, 26 Jul 2018 17:50:06 +0000 (UTC) From: Cole Robinson To: libvirt-list@redhat.com Date: Thu, 26 Jul 2018 13:49:54 -0400 Message-Id: In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH RFC 1/2] util: Add VIR_ENUM_IMPL_LABEL X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.44]); Thu, 26 Jul 2018 17:50:11 +0000 (UTC) This allows passing in a string label describing the enum, which can be used to autogenerate error messages Signed-off-by: Cole Robinson --- src/util/virutil.c | 20 ++++++++++++++++---- src/util/virutil.h | 15 ++++++++++----- 2 files changed, 26 insertions(+), 9 deletions(-) -- 2.17.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list diff --git a/src/util/virutil.c b/src/util/virutil.c index a908422feb..6d23a26a74 100644 --- a/src/util/virutil.c +++ b/src/util/virutil.c @@ -444,16 +444,22 @@ virParseVersionString(const char *str, unsigned long *version, int virEnumFromString(const char *const*types, unsigned int ntypes, - const char *type) + const char *type, + const char * const label) { size_t i; if (!type) - return -1; + goto error; for (i = 0; i < ntypes; i++) if (STREQ(types[i], type)) return i; + error: + if (label) { + virReportError(VIR_ERR_INVALID_ARG, + _("Unknown '%s' value '%s'"), label, type); + } return -1; } @@ -540,10 +546,16 @@ virFormatIntPretty(unsigned long long val, const char *virEnumToString(const char *const*types, unsigned int ntypes, - int type) + int type, + const char * const label) { - if (type < 0 || type >= ntypes) + if (type < 0 || type >= ntypes) { + if (label) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unknown '%s' internal value %d"), label, type); + } return NULL; + } return types[type]; } diff --git a/src/util/virutil.h b/src/util/virutil.h index 1ba9635bd9..345c9e053d 100644 --- a/src/util/virutil.h +++ b/src/util/virutil.h @@ -76,26 +76,31 @@ char *virIndexToDiskName(int idx, const char *prefix); int virEnumFromString(const char *const*types, unsigned int ntypes, - const char *type); + const char *type, + const char *errmsg); const char *virEnumToString(const char *const*types, unsigned int ntypes, - int type); + int type, + const char *errmsg); -# define VIR_ENUM_IMPL(name, lastVal, ...) \ +# define VIR_ENUM_IMPL_LABEL(name, label, lastVal, ...) \ static const char *const name ## TypeList[] = { __VA_ARGS__ }; \ verify(ARRAY_CARDINALITY(name ## TypeList) == lastVal); \ const char *name ## TypeToString(int type) { \ return virEnumToString(name ## TypeList, \ ARRAY_CARDINALITY(name ## TypeList), \ - type); \ + type, label); \ } \ int name ## TypeFromString(const char *type) { \ return virEnumFromString(name ## TypeList, \ ARRAY_CARDINALITY(name ## TypeList), \ - type); \ + type, label); \ } +# define VIR_ENUM_IMPL(name, lastVal, ...) \ + VIR_ENUM_IMPL_LABEL(name, NULL, lastVal, __VA_ARGS__) + # define VIR_ENUM_DECL(name) \ const char *name ## TypeToString(int type); \ int name ## TypeFromString(const char*type);