27 #include "libsigrok-internal.h"
30 #define LOG_PREFIX "strutil"
61 SR_PRIV int sr_atol(
const char *str,
long *ret)
67 tmp = strtol(str, &endptr, 10);
69 if (!endptr || *endptr || errno) {
93 SR_PRIV int sr_atoi(
const char *str,
int *ret)
97 if (sr_atol(str, &tmp) !=
SR_OK)
100 if ((
int) tmp != tmp) {
123 SR_PRIV int sr_atod(
const char *str,
double *ret)
129 tmp = strtof(str, &endptr);
131 if (!endptr || *endptr || errno) {
155 SR_PRIV int sr_atof(
const char *str,
float *ret)
159 if (sr_atod(str, &tmp) !=
SR_OK)
162 if ((
float) tmp != tmp) {
185 SR_PRIV int sr_atof_ascii(
const char *str,
float *ret)
191 tmp = g_ascii_strtod(str, &endptr);
193 if (!endptr || *endptr || errno) {
232 int64_t fractional = 0;
233 int64_t denominator = 1;
234 int32_t fractional_len = 0;
235 int32_t exponent = 0;
238 integral = g_ascii_strtoll(str, &endptr, 10);
243 if (*endptr ==
'.') {
244 const char* start = endptr + 1;
245 fractional = g_ascii_strtoll(start, &endptr, 10);
248 fractional_len = endptr - start;
251 if ((*endptr ==
'E') || (*endptr ==
'e')) {
252 exponent = g_ascii_strtoll(endptr + 1, &endptr, 10);
260 for (
int i = 0; i < fractional_len; i++)
262 exponent -= fractional_len;
265 integral += fractional;
267 integral -= fractional;
269 while (exponent > 0) {
274 while (exponent < 0) {
280 ret->
q = denominator;
305 uint64_t quot, divisor[] = {
309 const char *p, prefix[] =
"\0kMGTPE";
310 char fmt[16], fract[20] =
"", *f;
315 for (i = 0; (quot = x / divisor[i]) >= 1000; i++);
318 sprintf(fmt,
".%%0%d"PRIu64, i * 3);
319 f = fract + sprintf(fract, fmt, x % divisor[i]) - 1;
321 while (f >= fract && strchr(
"0.", *f))
327 return g_strdup_printf(
"%" PRIu64
"%s %.1s%s", quot, fract, p, unit);
372 freq = 1 / ((double)v_p / v_q);
374 o = g_malloc0(30 + 1);
377 v = (double)v_p / v_q * 1000000000000.0;
378 prec = ((v - (uint64_t)v) < FLT_MIN) ? 0 : 3;
379 r = snprintf(o, 30,
"%.*f ps", prec, v);
380 }
else if (freq >
SR_MHZ(1)) {
381 v = (double)v_p / v_q * 1000000000.0;
382 prec = ((v - (uint64_t)v) < FLT_MIN) ? 0 : 3;
383 r = snprintf(o, 30,
"%.*f ns", prec, v);
384 }
else if (freq >
SR_KHZ(1)) {
385 v = (double)v_p / v_q * 1000000.0;
386 prec = ((v - (uint64_t)v) < FLT_MIN) ? 0 : 3;
387 r = snprintf(o, 30,
"%.*f us", prec, v);
388 }
else if (freq > 1) {
389 v = (double)v_p / v_q * 1000.0;
390 prec = ((v - (uint64_t)v) < FLT_MIN) ? 0 : 3;
391 r = snprintf(o, 30,
"%.*f ms", prec, v);
393 v = (double)v_p / v_q;
394 prec = ((v - (uint64_t)v) < FLT_MIN) ? 0 : 3;
395 r = snprintf(o, 30,
"%.*f s", prec, v);
428 o = g_malloc0(30 + 1);
431 r = snprintf(o, 30,
"%" PRIu64
"mV", v_p);
433 r = snprintf(o, 30,
"%" PRIu64
"V", v_p);
435 r = snprintf(o, 30,
"%gV", (
float)v_p / (
float)v_q);
465 int multiplier, done;
469 *size = strtoull(sizestring, &s, 10);
473 while (s && *s && multiplier == 0 && !done) {
478 frac_part = g_ascii_strtod(s, &s);
498 if (multiplier > 0) {
500 *size += frac_part * multiplier;
504 if (s && *s && g_ascii_strcasecmp(s,
"Hz"))
537 time_msec = strtoull(timestring, &s, 10);
538 if (time_msec == 0 && s == timestring)
546 else if (!strcmp(s,
"ms"))
561 if (!g_ascii_strncasecmp(boolstr,
"true", 4) ||
562 !g_ascii_strncasecmp(boolstr,
"yes", 3) ||
563 !g_ascii_strncasecmp(boolstr,
"on", 2) ||
564 !g_ascii_strncasecmp(boolstr,
"1", 1))
575 *p = strtoull(periodstr, &s, 10);
576 if (*p == 0 && s == periodstr)
583 if (!strcmp(s,
"fs"))
584 *q = 1000000000000000ULL;
585 else if (!strcmp(s,
"ps"))
586 *q = 1000000000000ULL;
587 else if (!strcmp(s,
"ns"))
589 else if (!strcmp(s,
"us"))
591 else if (!strcmp(s,
"ms"))
593 else if (!strcmp(s,
"s"))
608 *p = strtoull(voltstr, &s, 10);
609 if (*p == 0 && s == voltstr)
616 if (!g_ascii_strcasecmp(s,
"mv"))
618 else if (!g_ascii_strcasecmp(s,
"v"))
Generic/unspecified error.
int sr_parse_rational(const char *str, struct sr_rational *ret)
Convert a string representation of a numeric value to a sr_rational.
gboolean sr_parse_boolstring(const char *boolstr)
int sr_parse_sizestring(const char *sizestring, uint64_t *size)
Convert a "natural" string representation of a size value to uint64_t.
The public libsigrok header file to be used by frontends.
char * sr_voltage_string(uint64_t v_p, uint64_t v_q)
Convert a numeric voltage value to the "natural" string representation of its voltage value...
char * sr_si_string_u64(uint64_t x, const char *unit)
Convert a numeric value value to its "natural" string representation in SI units. ...
uint64_t sr_parse_timestring(const char *timestring)
Convert a "natural" string representation of a time value to an uint64_t value in milliseconds...
int sr_parse_period(const char *periodstr, uint64_t *p, uint64_t *q)
char * sr_period_string(uint64_t v_p, uint64_t v_q)
Convert a numeric period value to the "natural" string representation of its period value...
int sr_parse_voltage(const char *voltstr, uint64_t *p, uint64_t *q)
uint64_t q
Denominator of the rational number.
char * sr_samplerate_string(uint64_t samplerate)
Convert a numeric samplerate value to its "natural" string representation.
int64_t p
Numerator of the rational number.