Received: from [140.186.70.92] (port=48304 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PTAiN-0002fT-In for qemu-devel@nongnu.org; Thu, 16 Dec 2010 05:05:37 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PTAiL-0003gJ-Uh for qemu-devel@nongnu.org; Thu, 16 Dec 2010 05:05:35 -0500 Received: from mx1.redhat.com ([209.132.183.28]:1025) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PTAiL-0003fj-L8 for qemu-devel@nongnu.org; Thu, 16 Dec 2010 05:05:33 -0500 From: Markus Armbruster Subject: Re: [Qemu-devel] [PATCH 1/2] Introduce strtosz_suffix() References: <1291900645-10661-1-git-send-email-Jes.Sorensen@redhat.com> <1291900645-10661-2-git-send-email-Jes.Sorensen@redhat.com> Date: Thu, 16 Dec 2010 11:05:16 +0100 In-Reply-To: <1291900645-10661-2-git-send-email-Jes.Sorensen@redhat.com> (Jes Sorensen's message of "Thu, 9 Dec 2010 14:17:24 +0100") Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Jes.Sorensen@redhat.com Cc: kwolf@redhat.com, qemu-devel@nongnu.org, stefanha@linux.vnet.ibm.com Sorry for the late review. Jes.Sorensen@redhat.com writes: > From: Jes Sorensen > > 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 > --- > cutils.c | 17 ++++++++++++++--- > qemu-common.h | 7 +++++++ > 2 files changed, 21 insertions(+), 3 deletions(-) > > diff --git a/cutils.c b/cutils.c > index 28089aa..7984bc1 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) Last parameter's const is of marginal uility. Matter of taste. > { > 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; > + } The else clause "d = c" is effectively "d = 0" (due to prior "c = 0"), which is the same as "d = default_suffix" (due to else's condition). Thus, you can replace the conditional by a simple d = default_suffix; > } > - 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, STRTOSZ_DEFSUFFIX_MB); > +} > diff --git a/qemu-common.h b/qemu-common.h > index de82c2e..1ed32e5 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' I don't see what these defines buy us over the literals, but if it makes you or other reviewers happier... > ssize_t strtosz(const char *nptr, char **end); > +ssize_t strtosz_suffix(const char *nptr, char **end, const char default_suffix); > > /* path.c */ > void init_paths(const char *prefix);