Commit 7695019bce7f0b552203229e7b89494462ac286f
Committed by
Anthony Liguori
1 parent
67b1355b
qemu-option: factor out parse_option_size
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Showing
1 changed file
with
36 additions
and
26 deletions
qemu-option.c
| @@ -182,6 +182,40 @@ static int parse_option_bool(const char *name, const char *value, int *ret) | @@ -182,6 +182,40 @@ static int parse_option_bool(const char *name, const char *value, int *ret) | ||
| 182 | return 0; | 182 | return 0; |
| 183 | } | 183 | } |
| 184 | 184 | ||
| 185 | +static int parse_option_size(const char *name, const char *value, uint64_t *ret) | ||
| 186 | +{ | ||
| 187 | + char *postfix; | ||
| 188 | + double sizef; | ||
| 189 | + | ||
| 190 | + if (value != NULL) { | ||
| 191 | + sizef = strtod(value, &postfix); | ||
| 192 | + switch (*postfix) { | ||
| 193 | + case 'T': | ||
| 194 | + sizef *= 1024; | ||
| 195 | + case 'G': | ||
| 196 | + sizef *= 1024; | ||
| 197 | + case 'M': | ||
| 198 | + sizef *= 1024; | ||
| 199 | + case 'K': | ||
| 200 | + case 'k': | ||
| 201 | + sizef *= 1024; | ||
| 202 | + case 'b': | ||
| 203 | + case '\0': | ||
| 204 | + *ret = (uint64_t) sizef; | ||
| 205 | + break; | ||
| 206 | + default: | ||
| 207 | + fprintf(stderr, "Option '%s' needs size as parameter\n", name); | ||
| 208 | + fprintf(stderr, "You may use k, M, G or T suffixes for " | ||
| 209 | + "kilobytes, megabytes, gigabytes and terabytes.\n"); | ||
| 210 | + return -1; | ||
| 211 | + } | ||
| 212 | + } else { | ||
| 213 | + fprintf(stderr, "Option '%s' needs a parameter\n", name); | ||
| 214 | + return -1; | ||
| 215 | + } | ||
| 216 | + return 0; | ||
| 217 | +} | ||
| 218 | + | ||
| 185 | /* | 219 | /* |
| 186 | * Sets the value of a parameter in a given option list. The parsing of the | 220 | * Sets the value of a parameter in a given option list. The parsing of the |
| 187 | * value depends on the type of option: | 221 | * value depends on the type of option: |
| @@ -229,34 +263,10 @@ int set_option_parameter(QEMUOptionParameter *list, const char *name, | @@ -229,34 +263,10 @@ int set_option_parameter(QEMUOptionParameter *list, const char *name, | ||
| 229 | break; | 263 | break; |
| 230 | 264 | ||
| 231 | case OPT_SIZE: | 265 | case OPT_SIZE: |
| 232 | - if (value != NULL) { | ||
| 233 | - double sizef = strtod(value, (char**) &value); | ||
| 234 | - | ||
| 235 | - switch (*value) { | ||
| 236 | - case 'T': | ||
| 237 | - sizef *= 1024; | ||
| 238 | - case 'G': | ||
| 239 | - sizef *= 1024; | ||
| 240 | - case 'M': | ||
| 241 | - sizef *= 1024; | ||
| 242 | - case 'K': | ||
| 243 | - case 'k': | ||
| 244 | - sizef *= 1024; | ||
| 245 | - case 'b': | ||
| 246 | - case '\0': | ||
| 247 | - list->value.n = (uint64_t) sizef; | ||
| 248 | - break; | ||
| 249 | - default: | ||
| 250 | - fprintf(stderr, "Option '%s' needs size as parameter\n", name); | ||
| 251 | - fprintf(stderr, "You may use k, M, G or T suffixes for " | ||
| 252 | - "kilobytes, megabytes, gigabytes and terabytes.\n"); | ||
| 253 | - return -1; | ||
| 254 | - } | ||
| 255 | - } else { | ||
| 256 | - fprintf(stderr, "Option '%s' needs a parameter\n", name); | 266 | + if (-1 == parse_option_size(name, value, &list->value.n)) |
| 257 | return -1; | 267 | return -1; |
| 258 | - } | ||
| 259 | break; | 268 | break; |
| 269 | + | ||
| 260 | default: | 270 | default: |
| 261 | fprintf(stderr, "Bug: Option '%s' has an unknown type\n", name); | 271 | fprintf(stderr, "Bug: Option '%s' has an unknown type\n", name); |
| 262 | return -1; | 272 | return -1; |