X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fstrutil.c;h=5f0deb7708f374327b6c5d33e7649ca3da342948;hb=688e44ae06610d64ef90b720cd61262e5b606d55;hp=02afc034c67249acdcd5078518c3793b5d114780;hpb=d2391b5453cd54b4bb087bdfbf2964202ba29098;p=libsigrok.git diff --git a/src/strutil.c b/src/strutil.c index 02afc034..5f0deb77 100644 --- a/src/strutil.c +++ b/src/strutil.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include "libsigrok-internal.h" @@ -233,13 +234,19 @@ SR_API int sr_parse_rational(const char *str, struct sr_rational *ret) int64_t denominator = 1; int32_t fractional_len = 0; int32_t exponent = 0; + bool is_negative = false; errno = 0; integral = g_ascii_strtoll(str, &endptr, 10); - if (errno) + if (str == endptr && (str[0] == '-' || str[0] == '+') && str[1] == '.') + endptr += 1; + else if (errno) return SR_ERR; + if (integral < 0 || str[0] == '-') + is_negative = true; + if (*endptr == '.') { const char* start = endptr + 1; fractional = g_ascii_strtoll(start, &endptr, 10); @@ -261,7 +268,7 @@ SR_API int sr_parse_rational(const char *str, struct sr_rational *ret) integral *= 10; exponent -= fractional_len; - if (integral >= 0) + if (!is_negative) integral += fractional; else integral -= fractional; @@ -412,11 +419,11 @@ SR_API char *sr_period_string(uint64_t v_p, uint64_t v_q) SR_API char *sr_voltage_string(uint64_t v_p, uint64_t v_q) { if (v_q == 1000) - return g_strdup_printf("%" PRIu64 "mV", v_p); + return g_strdup_printf("%" PRIu64 " mV", v_p); else if (v_q == 1) - return g_strdup_printf("%" PRIu64 "V", v_p); + return g_strdup_printf("%" PRIu64 " V", v_p); else - return g_strdup_printf("%gV", (float)v_p / (float)v_q); + return g_strdup_printf("%g V", (float)v_p / (float)v_q); } /**