qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH 5/8] qemu-option: factor out parse_option_size


From: Gerd Hoffmann
Subject: [Qemu-devel] [PATCH 5/8] qemu-option: factor out parse_option_size
Date: Wed, 22 Jul 2009 12:25:04 +0200

Signed-off-by: Gerd Hoffmann <address@hidden>
---
 qemu-option.c |   62 +++++++++++++++++++++++++++++++++-----------------------
 qemu-option.h |    1 +
 2 files changed, 37 insertions(+), 26 deletions(-)

diff --git a/qemu-option.c b/qemu-option.c
index 45f0c8f..390c97d 100644
--- a/qemu-option.c
+++ b/qemu-option.c
@@ -118,6 +118,40 @@ int parse_option_bool(const char *name, const char *value, 
int *ret)
     return 0;
 }
 
+int parse_option_size(const char *name, const char *value, uint64_t *ret)
+{
+    char *postfix;
+    double sizef;
+
+    if (value != NULL) {
+        sizef = strtod(value, &postfix);
+        switch (*postfix) {
+        case 'T':
+            sizef *= 1024;
+        case 'G':
+            sizef *= 1024;
+        case 'M':
+            sizef *= 1024;
+        case 'K':
+        case 'k':
+            sizef *= 1024;
+        case 'b':
+        case '\0':
+            *ret = (uint64_t) sizef;
+            break;
+        default:
+            fprintf(stderr, "Option '%s' needs size as parameter\n", name);
+            fprintf(stderr, "You may use k, M, G or T suffixes for "
+                    "kilobytes, megabytes, gigabytes and terabytes.\n");
+            return -1;
+        }
+    } else {
+        fprintf(stderr, "Option '%s' needs a parameter\n", name);
+        return -1;
+    }
+    return 0;
+}
+
 /*
  * Sets the value of a parameter in a given option list. The parsing of the
  * value depends on the type of option:
@@ -165,34 +199,10 @@ int set_option_parameter(QEMUOptionParameter *list, const 
char *name,
         break;
 
     case OPT_SIZE:
-        if (value != NULL) {
-            double sizef = strtod(value, (char**) &value);
-
-            switch (*value) {
-            case 'T':
-                sizef *= 1024;
-            case 'G':
-                sizef *= 1024;
-            case 'M':
-                sizef *= 1024;
-            case 'K':
-            case 'k':
-                sizef *= 1024;
-            case 'b':
-            case '\0':
-                list->value.n = (uint64_t) sizef;
-                break;
-            default:
-                fprintf(stderr, "Option '%s' needs size as parameter\n", name);
-                fprintf(stderr, "You may use k, M, G or T suffixes for "
-                    "kilobytes, megabytes, gigabytes and terabytes.\n");
-                return -1;
-            }
-        } else {
-            fprintf(stderr, "Option '%s' needs a parameter\n", name);
+        if (-1 == parse_option_size(name, value, &list->value.n))
             return -1;
-        }
         break;
+
     default:
         fprintf(stderr, "Bug: Option '%s' has an unknown type\n", name);
         return -1;
diff --git a/qemu-option.h b/qemu-option.h
index cb8b1c3..9525e87 100644
--- a/qemu-option.h
+++ b/qemu-option.h
@@ -48,6 +48,7 @@ const char *get_opt_name(char *buf, int buf_size, const char 
*p, char delim);
 const char *get_opt_value(char *buf, int buf_size, const char *p);
 
 int parse_option_bool(const char *name, const char *value, int *ret);
+int parse_option_size(const char *name, const char *value, uint64_t *ret);
 
 /*
  * The following functions take a parameter list as input. This is a pointer to
-- 
1.6.2.5





reply via email to

[Prev in Thread] Current Thread [Next in Thread]