strutil: support tera/peta/exa suffixes in symbolic size specs

Synchronize sr_parse_sizestring() with sr_si_string_u64() capabilities.
Add support for the T/P/E suffixes. Since this conversion helper deals
with integer values exclusively, there is no issue with case insensitive
matches. The value cannot be pico. Neither is there an ambiguity with
the 10e6 notation.

This addresses bug #763.

Fix a style nit while we are here. Put braces around both arms of a
complex conditional.
This commit is contained in:
Gerhard Sittig 2018-02-04 19:50:25 +01:00 committed by Uwe Hermann
parent 57a88297dd
commit 751ba4c8ed
1 changed files with 16 additions and 2 deletions

View File

@ -477,7 +477,8 @@ SR_API char *sr_voltage_string(uint64_t v_p, uint64_t v_q)
*/ */
SR_API int sr_parse_sizestring(const char *sizestring, uint64_t *size) SR_API int sr_parse_sizestring(const char *sizestring, uint64_t *size)
{ {
int multiplier, done; uint64_t multiplier;
int done;
double frac_part; double frac_part;
char *s; char *s;
@ -504,6 +505,18 @@ SR_API int sr_parse_sizestring(const char *sizestring, uint64_t *size)
case 'G': case 'G':
multiplier = SR_GHZ(1); multiplier = SR_GHZ(1);
break; break;
case 't':
case 'T':
multiplier = SR_GHZ(1000);
break;
case 'p':
case 'P':
multiplier = SR_GHZ(1000 * 1000);
break;
case 'e':
case 'E':
multiplier = SR_GHZ(1000 * 1000 * 1000);
break;
default: default:
done = TRUE; done = TRUE;
s--; s--;
@ -513,8 +526,9 @@ SR_API int sr_parse_sizestring(const char *sizestring, uint64_t *size)
if (multiplier > 0) { if (multiplier > 0) {
*size *= multiplier; *size *= multiplier;
*size += frac_part * multiplier; *size += frac_part * multiplier;
} else } else {
*size += frac_part; *size += frac_part;
}
if (s && *s && g_ascii_strcasecmp(s, "Hz")) if (s && *s && g_ascii_strcasecmp(s, "Hz"))
return SR_ERR; return SR_ERR;