From patchwork Thu Dec 6 21:12:04 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Igor Mammedov X-Patchwork-Id: 204322 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 00B762C00AC for ; Fri, 7 Dec 2012 08:14:21 +1100 (EST) Received: from localhost ([::1]:52166 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tgilv-0000kX-2N for incoming@patchwork.ozlabs.org; Thu, 06 Dec 2012 16:14:19 -0500 Received: from eggs.gnu.org ([208.118.235.92]:47824) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tgilo-0000jb-Bq for qemu-devel@nongnu.org; Thu, 06 Dec 2012 16:14:13 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Tgilm-0004t1-Tn for qemu-devel@nongnu.org; Thu, 06 Dec 2012 16:14:12 -0500 Received: from mx1.redhat.com ([209.132.183.28]:3735) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tgilm-0004sw-GH for qemu-devel@nongnu.org; Thu, 06 Dec 2012 16:14:10 -0500 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id qB6LCDh5012574 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 6 Dec 2012 16:12:13 -0500 Received: from thinkpad.com (vpn-232-156.phx2.redhat.com [10.3.232.156]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id qB6LCATW031127; Thu, 6 Dec 2012 16:12:12 -0500 From: Igor Mammedov To: qemu-devel@nongnu.org Date: Thu, 6 Dec 2012 22:12:04 +0100 Message-Id: <1354828325-16568-2-git-send-email-imammedo@redhat.com> In-Reply-To: <1354828325-16568-1-git-send-email-imammedo@redhat.com> References: <1354828325-16568-1-git-send-email-imammedo@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: mdroth@linux.vnet.ibm.com, ehabkost@redhat.com, afaerber@suse.de Subject: [Qemu-devel] [PATCH 1/2] qapi: add visitor for parsing int[KMGT] input string X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Caller of visit_type_unit_suffixed_int() will have to specify value of 'K' suffix via unit argument. For Kbytes it's 1024, for Khz it's 1000. Signed-off-by: Igor Mammedov Reviewed-by: Michael Roth Reviewed-by: Eduardo Habkost --- v2: - convert type_freq to type_unit_suffixed_int. - provide qapi_dealloc_type_unit_suffixed_int() impl. --- qapi/qapi-dealloc-visitor.c | 7 +++++++ qapi/qapi-visit-core.c | 13 +++++++++++++ qapi/qapi-visit-core.h | 8 ++++++++ qapi/string-input-visitor.c | 22 ++++++++++++++++++++++ 4 files changed, 50 insertions(+) diff --git a/qapi/qapi-dealloc-visitor.c b/qapi/qapi-dealloc-visitor.c index 75214e7..57e662c 100644 --- a/qapi/qapi-dealloc-visitor.c +++ b/qapi/qapi-dealloc-visitor.c @@ -143,6 +143,12 @@ static void qapi_dealloc_type_enum(Visitor *v, int *obj, const char *strings[], { } +static void qapi_dealloc_type_unit_suffixed_int(Visitor *v, int64_t *obj, + const char *name, + const int unit, Error **errp) +{ +} + Visitor *qapi_dealloc_get_visitor(QapiDeallocVisitor *v) { return &v->visitor; @@ -170,6 +176,7 @@ QapiDeallocVisitor *qapi_dealloc_visitor_new(void) v->visitor.type_str = qapi_dealloc_type_str; v->visitor.type_number = qapi_dealloc_type_number; v->visitor.type_size = qapi_dealloc_type_size; + v->visitor.type_unit_suffixed_int = qapi_dealloc_type_unit_suffixed_int; QTAILQ_INIT(&v->stack); diff --git a/qapi/qapi-visit-core.c b/qapi/qapi-visit-core.c index 7a82b63..dcbc1a9 100644 --- a/qapi/qapi-visit-core.c +++ b/qapi/qapi-visit-core.c @@ -311,3 +311,16 @@ void input_type_enum(Visitor *v, int *obj, const char *strings[], g_free(enum_str); *obj = value; } + +void visit_type_unit_suffixed_int(Visitor *v, int64_t *obj, const char *name, + const int unit, Error **errp) +{ + if (!error_is_set(errp)) { + return; + } + if (v->type_unit_suffixed_int) { + v->type_unit_suffixed_int(v, obj, name, unit, errp); + } else { + visit_type_int64(v, obj, name, errp); + } +} diff --git a/qapi/qapi-visit-core.h b/qapi/qapi-visit-core.h index 60aceda..04e690a 100644 --- a/qapi/qapi-visit-core.h +++ b/qapi/qapi-visit-core.h @@ -62,6 +62,12 @@ struct Visitor void (*type_int64)(Visitor *v, int64_t *obj, const char *name, Error **errp); /* visit_type_size() falls back to (*type_uint64)() if type_size is unset */ void (*type_size)(Visitor *v, uint64_t *obj, const char *name, Error **errp); + /* + * visit_unit_suffixed_int() falls back to (*type_int64)() + * if type_unit_suffixed_int is unset + */ + void (*type_unit_suffixed_int)(Visitor *v, int64_t *obj, const char *name, + const int unit, Error **errp); }; void visit_start_handle(Visitor *v, void **obj, const char *kind, @@ -91,5 +97,7 @@ void visit_type_size(Visitor *v, uint64_t *obj, const char *name, Error **errp); void visit_type_bool(Visitor *v, bool *obj, const char *name, Error **errp); void visit_type_str(Visitor *v, char **obj, const char *name, Error **errp); void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp); +void visit_type_unit_suffixed_int(Visitor *v, int64_t *obj, const char *name, + const int unit, Error **errp); #endif diff --git a/qapi/string-input-visitor.c b/qapi/string-input-visitor.c index 497eb9a..d2bd154 100644 --- a/qapi/string-input-visitor.c +++ b/qapi/string-input-visitor.c @@ -110,6 +110,27 @@ static void parse_start_optional(Visitor *v, bool *present, *present = true; } +static void parse_type_unit_suffixed_int(Visitor *v, int64_t *obj, + const char *name, const int unit, + Error **errp) +{ + StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v); + char *endp = (char *) siv->string; + long long val = 0; + + if (siv->string) { + val = strtosz_suffix_unit(siv->string, &endp, + STRTOSZ_DEFSUFFIX_B, unit); + } + if (!siv->string || val == -1 || *endp) { + error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, + "a value representable as a non-negative int64"); + return; + } + + *obj = val; +} + Visitor *string_input_get_visitor(StringInputVisitor *v) { return &v->visitor; @@ -132,6 +153,7 @@ StringInputVisitor *string_input_visitor_new(const char *str) v->visitor.type_str = parse_type_str; v->visitor.type_number = parse_type_number; v->visitor.start_optional = parse_start_optional; + v->visitor.type_unit_suffixed_int = parse_type_unit_suffixed_int; v->string = str; return v;