qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Igor Mammedov <imammedo@redhat.com>
To: qemu-devel@nongnu.org
Cc: mdroth@linux.vnet.ibm.com, ehabkost@redhat.com, afaerber@suse.de
Subject: [Qemu-devel] [PATCH 1/2] add visitor for parsing int[KMGT] input string
Date: Mon, 10 Dec 2012 22:33:06 +0100	[thread overview]
Message-ID: <1355175187-11470-2-git-send-email-imammedo@redhat.com> (raw)
In-Reply-To: <1355175187-11470-1-git-send-email-imammedo@redhat.com>

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 <imammedo@redhat.com>
---
 v3:
  - Fix errp check. Spotted-By: Andreas Färber <afaerber@suse.de>
  - 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[],
 {
 }
 
+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 = qapi_dealloc_type_str;
     v->visitor.type_number = qapi_dealloc_type_number;
     v->visitor.type_size = qapi_dealloc_type_size;
+    v->visitor.type_suffixed_int = qapi_dealloc_type_suffixed_int;
 
     QTAILQ_INIT(&v->stack);
 
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 char *strings[],
     g_free(enum_str);
     *obj = 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 multiplication
+ * factor of a suffix character that might follow number.
+ *   i.e. resulted *@obj = number * suffix.
+ *
+ * Where following suffixes are recognised:
+ *  no suffix = 1
+ *  K = @suffix_factor
+ *  M = K * @suffix_factor
+ *  G = M * @suffix_factor
+ *  T = 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);
 };
 
 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 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_suffixed_int(Visitor *v, int64_t *obj, const char *name,
+                             const int suffix_factor, Error **errp);
 
 #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 *present,
     *present = true;
 }
 
+static void parse_type_suffixed_int(Visitor *v, int64_t *obj, const char *name,
+                                    const int suffix_factor, Error **errp)
+{
+    StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v);
+    char *endp = (char *) siv->string;
+    long long val = 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 = strtosz_suffix_unit(siv->string, &endp,
+                                  STRTOSZ_DEFSUFFIX_B, suffix_factor);
+    }
+    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 +156,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_suffixed_int = parse_type_suffixed_int;
 
     v->string = str;
     return v;
-- 
1.7.11.7

  reply	other threads:[~2012-12-10 21:33 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-10 21:33 [Qemu-devel] [PATCH 0/2 v2] introduce visitor for parsing suffixed integer Igor Mammedov
2012-12-10 21:33 ` Igor Mammedov [this message]
2012-12-12 18:16   ` [Qemu-devel] [PATCH 1/2] add visitor for parsing int[KMGT] input string Eduardo Habkost
2012-12-14 13:20     ` Igor Mammedov
2012-12-14 18:18       ` Eduardo Habkost
2012-12-10 21:33 ` [Qemu-devel] [PATCH 2/2] target-i386: use visit_type_suffixed_int() to parse tsc_freq property value Igor Mammedov
2012-12-12 18:16   ` Eduardo Habkost
2012-12-23 20:34 ` [Qemu-devel] [PATCH 0/2 v2] introduce visitor for parsing suffixed integer Anthony Liguori
2012-12-24 14:03   ` Igor Mammedov
2012-12-26 12:51   ` Eduardo Habkost

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1355175187-11470-2-git-send-email-imammedo@redhat.com \
    --to=imammedo@redhat.com \
    --cc=afaerber@suse.de \
    --cc=ehabkost@redhat.com \
    --cc=mdroth@linux.vnet.ibm.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).