[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 2/5] Support human unit formats in strtobytes, eg. 1
From: |
Jes . Sorensen |
Subject: |
[Qemu-devel] [PATCH 2/5] Support human unit formats in strtobytes, eg. 1.0G |
Date: |
Wed, 15 Sep 2010 14:23:57 +0200 |
From: Jes Sorensen <address@hidden>
Signed-off-by: Jes Sorensen <address@hidden>
---
cutils.c | 27 ++++++++++++++++++++++++++-
1 files changed, 26 insertions(+), 1 deletions(-)
diff --git a/cutils.c b/cutils.c
index a3087fe..d34ed08 100644
--- a/cutils.c
+++ b/cutils.c
@@ -259,16 +259,38 @@ int fcntl_setfl(int fd, int flag)
*/
uint64_t strtobytes(const char *nptr, char **end)
{
- uint64_t value;
+ uint64_t value, value2;
char *endptr;
+ int divider = 0;
value = strtoll(nptr, &endptr, 0);
+ if (endptr[0] == '.') {
+ endptr++;
+ value2 = 0;
+ divider = 10;
+ while ((endptr[0] == '0') && (endptr[1] >= '0') && (endptr[1] <= '9'))
{
+ divider = divider * 10;
+ endptr++;
+ }
+
+ if ((endptr[0] >= '0') && (endptr[0] <= '9')) {
+ value2 = strtoll(endptr, &endptr, 0);
+ value = value * divider + value2;
+ } else {
+ value = 0;
+ goto fail;
+ }
+ }
switch (*endptr++) {
case 'K':
case 'k':
value <<= 10;
break;
case 0:
+ if (divider) {
+ value = 0;
+ break;
+ }
case 'M':
case 'm':
value <<= 20;
@@ -284,9 +306,12 @@ uint64_t strtobytes(const char *nptr, char **end)
default:
value = 0;
}
+ if (divider)
+ value /= divider;
if (end)
*end = endptr;
+fail:
return value;
}
--
1.7.2.2
- [Qemu-devel] [PATCH 0/5] Introduce strtobytes and make use of it, Jes . Sorensen, 2010/09/15
- [Qemu-devel] [PATCH 1/5] Introduce strtobytes() library function to convert string to byte count., Jes . Sorensen, 2010/09/15
- [Qemu-devel] [PATCH 4/5] Switch migrate_set_speed() to take an 'o' argument rather than a float., Jes . Sorensen, 2010/09/15
- [Qemu-devel] [PATCH 3/5] Add support for 'o' octet (bytes) format as monitor parameter., Jes . Sorensen, 2010/09/15
- [Qemu-devel] [PATCH 5/5] Remove obsolete 'f' double parameter type, Jes . Sorensen, 2010/09/15
- [Qemu-devel] [PATCH 2/5] Support human unit formats in strtobytes, eg. 1.0G,
Jes . Sorensen <=
- [Qemu-devel] Re: [PATCH 2/5] Support human unit formats in strtobytes, eg. 1.0G, Juan Quintela, 2010/09/15
- [Qemu-devel] Re: [PATCH 2/5] Support human unit formats in strtobytes, eg. 1.0G, Paolo Bonzini, 2010/09/15
- Re: [Qemu-devel] Re: [PATCH 2/5] Support human unit formats in strtobytes, eg. 1.0G, Anthony Liguori, 2010/09/15
- [Qemu-devel] Re: [PATCH 2/5] Support human unit formats in strtobytes, eg. 1.0G, Jes Sorensen, 2010/09/15
- [Qemu-devel] Re: [PATCH 2/5] Support human unit formats in strtobytes, eg. 1.0G, Paolo Bonzini, 2010/09/16
- [Qemu-devel] Re: [PATCH 2/5] Support human unit formats in strtobytes, eg. 1.0G, Jes Sorensen, 2010/09/16
- Re: [Qemu-devel] Re: [PATCH 2/5] Support human unit formats in strtobytes, eg. 1.0G, Avi Kivity, 2010/09/16
- Re: [Qemu-devel] Re: [PATCH 2/5] Support human unit formats in strtobytes, eg. 1.0G, Jes Sorensen, 2010/09/16
- Re: [Qemu-devel] Re: [PATCH 2/5] Support human unit formats in strtobytes, eg. 1.0G, Avi Kivity, 2010/09/16