From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:40688) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TiAyV-0006Fn-Cv for qemu-devel@nongnu.org; Mon, 10 Dec 2012 16:33:21 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TiAyT-00084F-7X for qemu-devel@nongnu.org; Mon, 10 Dec 2012 16:33:19 -0500 Received: from mx1.redhat.com ([209.132.183.28]:58104) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TiAyT-00084A-0n for qemu-devel@nongnu.org; Mon, 10 Dec 2012 16:33:17 -0500 From: Igor Mammedov Date: Mon, 10 Dec 2012 22:33:06 +0100 Message-Id: <1355175187-11470-2-git-send-email-imammedo@redhat.com> In-Reply-To: <1355175187-11470-1-git-send-email-imammedo@redhat.com> References: <1355175187-11470-1-git-send-email-imammedo@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH 1/2] add visitor for parsing int[KMGT] input string List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: mdroth@linux.vnet.ibm.com, ehabkost@redhat.com, afaerber@suse.de Caller of visit_type_suffixed_int() have to specify value of 'K' suffix using suffix_factor argument. Example of selecting suffix_factor value: * Kbytes: 1024 * Khz: 1000 Signed-off-by: Igor Mammedov --- v3: - Fix errp check. Spotted-By: Andreas F=C3=A4rber - s/type_unit_suffixed_int/type_suffixed_int/ - use 'suffix_factor' instead of 'unit' - document visit_type_suffixed_int() - add comment on current impl. limitation v2: - convert type_freq to type_unit_suffixed_int. - provide qapi_dealloc_type_unit_suffixed_int() impl. --- qapi/qapi-dealloc-visitor.c | 8 ++++++++ qapi/qapi-visit-core.c | 35 +++++++++++++++++++++++++++++++++++ qapi/qapi-visit-core.h | 4 ++++ qapi/string-input-visitor.c | 25 +++++++++++++++++++++++++ 4 files changed, 72 insertions(+) diff --git a/qapi/qapi-dealloc-visitor.c b/qapi/qapi-dealloc-visitor.c index 75214e7..09c3b01 100644 --- a/qapi/qapi-dealloc-visitor.c +++ b/qapi/qapi-dealloc-visitor.c @@ -143,6 +143,13 @@ static void qapi_dealloc_type_enum(Visitor *v, int *= obj, const char *strings[], { } =20 +static void qapi_dealloc_type_suffixed_int(Visitor *v, int64_t *obj, + const char *name, + const int suffix_factor, + Error **errp) +{ +} + Visitor *qapi_dealloc_get_visitor(QapiDeallocVisitor *v) { return &v->visitor; @@ -170,6 +177,7 @@ QapiDeallocVisitor *qapi_dealloc_visitor_new(void) v->visitor.type_str =3D qapi_dealloc_type_str; v->visitor.type_number =3D qapi_dealloc_type_number; v->visitor.type_size =3D qapi_dealloc_type_size; + v->visitor.type_suffixed_int =3D qapi_dealloc_type_suffixed_int; =20 QTAILQ_INIT(&v->stack); =20 diff --git a/qapi/qapi-visit-core.c b/qapi/qapi-visit-core.c index 7a82b63..9f6b8ae 100644 --- a/qapi/qapi-visit-core.c +++ b/qapi/qapi-visit-core.c @@ -311,3 +311,38 @@ void input_type_enum(Visitor *v, int *obj, const cha= r *strings[], g_free(enum_str); *obj =3D value; } + +/** + * visit_type_suffixed_int: + * @v: visitor used for accesing external representation of value + * @obj: object that stores internal value + * @name: name of the option + * @suffix_factor: multiplication factor of suffix 'K' + * @errp: object to store error code + * + * Converts option value represented by @v taking in account multiplicat= ion + * factor of a suffix character that might follow number. + * i.e. resulted *@obj =3D number * suffix. + * + * Where following suffixes are recognised: + * no suffix =3D 1 + * K =3D @suffix_factor + * M =3D K * @suffix_factor + * G =3D M * @suffix_factor + * T =3D G * @suffix_factor + * + * In case @v doesn't provide type_suffixed_int() parser, convertion + * fall-backs to suffix-less type_int64() parser. + */ +void visit_type_suffixed_int(Visitor *v, int64_t *obj, const char *name, + const int suffix_factor, Error **errp) +{ + if (error_is_set(errp)) { + return; + } + if (v->type_suffixed_int) { + v->type_suffixed_int(v, obj, name, suffix_factor, 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..05e9940 100644 --- a/qapi/qapi-visit-core.h +++ b/qapi/qapi-visit-core.h @@ -62,6 +62,8 @@ 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); + void (*type_suffixed_int)(Visitor *v, int64_t *obj, const char *name= , + const int uffix_factor, Error **errp); }; =20 void visit_start_handle(Visitor *v, void **obj, const char *kind, @@ -91,5 +93,7 @@ void visit_type_size(Visitor *v, uint64_t *obj, const c= har *name, Error **errp); void visit_type_bool(Visitor *v, bool *obj, const char *name, Error **er= rp); void visit_type_str(Visitor *v, char **obj, const char *name, Error **er= rp); void visit_type_number(Visitor *v, double *obj, const char *name, Error = **errp); +void visit_type_suffixed_int(Visitor *v, int64_t *obj, const char *name, + const int suffix_factor, Error **errp); =20 #endif diff --git a/qapi/string-input-visitor.c b/qapi/string-input-visitor.c index 497eb9a..9641bce 100644 --- a/qapi/string-input-visitor.c +++ b/qapi/string-input-visitor.c @@ -110,6 +110,30 @@ static void parse_start_optional(Visitor *v, bool *p= resent, *present =3D true; } =20 +static void parse_type_suffixed_int(Visitor *v, int64_t *obj, const char= *name, + const int suffix_factor, Error **err= p) +{ + StringInputVisitor *siv =3D DO_UPCAST(StringInputVisitor, visitor, v= ); + char *endp =3D (char *) siv->string; + long long val =3D 0; + + if (siv->string) { + /* TODO: presently strtosz_suffix_unit() is limited to [0..INT64= _MAX] + * it should be fixed to handle [INT64_MIN..INT64_MAX], + * to cover whole int64_t range + */ + val =3D strtosz_suffix_unit(siv->string, &endp, + STRTOSZ_DEFSUFFIX_B, suffix_factor); + } + if (!siv->string || val =3D=3D -1 || *endp) { + error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, + "a value representable as a non-negative int64"); + return; + } + + *obj =3D val; +} + Visitor *string_input_get_visitor(StringInputVisitor *v) { return &v->visitor; @@ -132,6 +156,7 @@ StringInputVisitor *string_input_visitor_new(const ch= ar *str) v->visitor.type_str =3D parse_type_str; v->visitor.type_number =3D parse_type_number; v->visitor.start_optional =3D parse_start_optional; + v->visitor.type_suffixed_int =3D parse_type_suffixed_int; =20 v->string =3D str; return v; --=20 1.7.11.7