Patchwork [1/2] Introduce strtosz_suffix()

login
register
mail settings
Submitter Jes Sorensen
Date Dec. 9, 2010, 12:13 p.m.
Message ID <1291896814-3554-2-git-send-email-Jes.Sorensen@redhat.com>
Download mbox | patch
Permalink /patch/74931/
State New
Headers show

Comments

Jes Sorensen - Dec. 9, 2010, 12:13 p.m.
From: Jes Sorensen <Jes.Sorensen@redhat.com>

This introduces strtosz_suffix() which allows the caller to specify a
default suffix in case the non default of MB is wanted.

strtosz() is kept as a wrapper for strtosz_suffix() which keeps it's
current default of MB.

Signed-off-by: Jes Sorensen <Jes.Sorensen@redhat.com>
---
 cutils.c      |   17 ++++++++++++++---
 qemu-common.h |    7 +++++++
 2 files changed, 21 insertions(+), 3 deletions(-)
Stefan Hajnoczi - Dec. 9, 2010, 12:53 p.m.
On Thu, Dec 09, 2010 at 01:13:33PM +0100, Jes.Sorensen@redhat.com wrote:
> @@ -371,3 +377,8 @@ fail:
> 
>      return retval;
>  }
> +
> +ssize_t strtosz(const char *nptr, char **end)
> +{
> +    return strtosz_suffix(nptr, end, 0);

This obscures what the default is, please use STRTOSZ_DEFSUFFIX_MB.  0
isn't very meaningful and requires the reader to dig into strtosz().

> diff --git a/qemu-common.h b/qemu-common.h
> index de82c2e..dc44cd6 100644
> --- a/qemu-common.h
> +++ b/qemu-common.h
> @@ -149,7 +149,14 @@ time_t mktimegm(struct tm *tm);
>  int qemu_fls(int i);
>  int qemu_fdatasync(int fd);
>  int fcntl_setfl(int fd, int flag);
> +
> +#define STRTOSZ_DEFSUFFIX_TB	'T'
> +#define STRTOSZ_DEFSUFFIX_GB	'G'
> +#define STRTOSZ_DEFSUFFIX_MB	'M'
> +#define STRTOSZ_DEFSUFFIX_KB	'K'
> +#define STRTOSZ_DEFSUFFIX_B	'B'
>  ssize_t strtosz(const char *nptr, char **end);
> +ssize_t strtosz_suffix(const char *nptr, char **end, const char);

An argument name would be nice: const char unit?

Stefan
Jes Sorensen - Dec. 9, 2010, 1:06 p.m.
On 12/09/10 13:53, Stefan Hajnoczi wrote:
> On Thu, Dec 09, 2010 at 01:13:33PM +0100, Jes.Sorensen@redhat.com wrote:
>> @@ -371,3 +377,8 @@ fail:
>>
>>      return retval;
>>  }
>> +
>> +ssize_t strtosz(const char *nptr, char **end)
>> +{
>> +    return strtosz_suffix(nptr, end, 0);
> 
> This obscures what the default is, please use STRTOSZ_DEFSUFFIX_MB.  0
> isn't very meaningful and requires the reader to dig into strtosz().

It doesn't make much different to me, but ok, that is easy to fix.

>> diff --git a/qemu-common.h b/qemu-common.h
>> index de82c2e..dc44cd6 100644
>> --- a/qemu-common.h
>> +++ b/qemu-common.h
>> @@ -149,7 +149,14 @@ time_t mktimegm(struct tm *tm);
>>  int qemu_fls(int i);
>>  int qemu_fdatasync(int fd);
>>  int fcntl_setfl(int fd, int flag);
>> +
>> +#define STRTOSZ_DEFSUFFIX_TB	'T'
>> +#define STRTOSZ_DEFSUFFIX_GB	'G'
>> +#define STRTOSZ_DEFSUFFIX_MB	'M'
>> +#define STRTOSZ_DEFSUFFIX_KB	'K'
>> +#define STRTOSZ_DEFSUFFIX_B	'B'
>>  ssize_t strtosz(const char *nptr, char **end);
>> +ssize_t strtosz_suffix(const char *nptr, char **end, const char);
> 
> An argument name would be nice: const char unit?

I'll add that in the same round.

Cheers,
Jes

Patch

diff --git a/cutils.c b/cutils.c
index 28089aa..1d24d9a 100644
--- a/cutils.c
+++ b/cutils.c
@@ -291,10 +291,10 @@  int fcntl_setfl(int fd, int flag)
  * value must be terminated by whitespace, ',' or '\0'. Return -1 on
  * error.
  */
-ssize_t strtosz(const char *nptr, char **end)
+ssize_t strtosz_suffix(const char *nptr, char **end, const char default_suffix)
 {
     ssize_t retval = -1;
-    char *endptr, c;
+    char *endptr, c, d;
     int mul_required = 0;
     double val, mul, integral, fraction;
 
@@ -313,10 +313,16 @@  ssize_t strtosz(const char *nptr, char **end)
      * part of a multi token argument.
      */
     c = *endptr;
+    d = c;
     if (isspace(c) || c == '\0' || c == ',') {
         c = 0;
+        if (default_suffix) {
+            d = default_suffix;
+        } else {
+            d = c;
+        }
     }
-    switch (c) {
+    switch (d) {
     case 'B':
     case 'b':
         mul = 1;
@@ -371,3 +377,8 @@  fail:
 
     return retval;
 }
+
+ssize_t strtosz(const char *nptr, char **end)
+{
+    return strtosz_suffix(nptr, end, 0);
+}
diff --git a/qemu-common.h b/qemu-common.h
index de82c2e..dc44cd6 100644
--- a/qemu-common.h
+++ b/qemu-common.h
@@ -149,7 +149,14 @@  time_t mktimegm(struct tm *tm);
 int qemu_fls(int i);
 int qemu_fdatasync(int fd);
 int fcntl_setfl(int fd, int flag);
+
+#define STRTOSZ_DEFSUFFIX_TB	'T'
+#define STRTOSZ_DEFSUFFIX_GB	'G'
+#define STRTOSZ_DEFSUFFIX_MB	'M'
+#define STRTOSZ_DEFSUFFIX_KB	'K'
+#define STRTOSZ_DEFSUFFIX_B	'B'
 ssize_t strtosz(const char *nptr, char **end);
+ssize_t strtosz_suffix(const char *nptr, char **end, const char);
 
 /* path.c */
 void init_paths(const char *prefix);