From f64c14141b0c90fa78cacbc502dd2a41239376fd Mon Sep 17 00:00:00 2001 From: Bert Vermeulen Date: Sun, 27 Nov 2011 19:31:25 +0100 Subject: [PATCH] fix CLI size string specification accept "hz" as optional qualifier but nothing else properly return an error instead of quietly returning zero size --- session_file.c | 2 +- sigrok-proto.h | 2 +- strutil.c | 25 ++++++++++++++----------- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/session_file.c b/session_file.c index 0e3c56be..ec98065d 100644 --- a/session_file.c +++ b/session_file.c @@ -107,7 +107,7 @@ int sr_session_load(const char *filename) device->plugin->set_configuration(devcnt, SR_HWCAP_CAPTUREFILE, val); g_ptr_array_add(capturefiles, val); } else if (!strcmp(keys[j], "samplerate")) { - tmp_u64 = sr_parse_sizestring(val); + sr_parse_sizestring(val, &tmp_u64); device->plugin->set_configuration(devcnt, SR_HWCAP_SAMPLERATE, &tmp_u64); } else if (!strcmp(keys[j], "unitsize")) { tmp_u64 = strtoull(val, NULL, 10); diff --git a/sigrok-proto.h b/sigrok-proto.h index 9a9614c5..728ffc73 100644 --- a/sigrok-proto.h +++ b/sigrok-proto.h @@ -142,7 +142,7 @@ char *sr_samplerate_string(uint64_t samplerate); char *sr_period_string(uint64_t frequency); char **sr_parse_triggerstring(struct sr_device *device, const char *triggerstring); -uint64_t sr_parse_sizestring(const char *sizestring); +int sr_parse_sizestring(const char *sizestring, uint64_t *size); uint64_t sr_parse_timestring(const char *timestring); gboolean sr_parse_boolstring(const char *boolstring); diff --git a/strutil.c b/strutil.c index 380d78f5..6a9d8622 100644 --- a/strutil.c +++ b/strutil.c @@ -186,19 +186,19 @@ char **sr_parse_triggerstring(struct sr_device *device, * Spaces (but not other whitespace) between value and suffix are allowed. * * @param sizestring A string containing a (decimal) size value. - * @return The string's size value as uint64_t. + * @param size Pointer to uint64_t which will contain the string's size value. + * @return SR_OK or error code * - * TODO: Error handling. */ -uint64_t sr_parse_sizestring(const char *sizestring) +int sr_parse_sizestring(const char *sizestring, uint64_t *size) { - int multiplier; - uint64_t val; + int multiplier, done; char *s; - val = strtoull(sizestring, &s, 10); + *size = strtoull(sizestring, &s, 10); multiplier = 0; - while (s && *s && multiplier == 0) { + done = FALSE; + while (s && *s && multiplier == 0 && !done) { switch (*s) { case ' ': break; @@ -215,15 +215,18 @@ uint64_t sr_parse_sizestring(const char *sizestring) multiplier = SR_GHZ(1); break; default: - val = 0; - multiplier = -1; + done = TRUE; + s--; } s++; } if (multiplier > 0) - val *= multiplier; + *size *= multiplier; - return val; + if (*s && strcasecmp(s, "Hz")) + return SR_ERR; + + return SR_OK; } /**