28 #include "libsigrok-internal.h"
31 #define LOG_PREFIX "strutil"
62 SR_PRIV int sr_atol(
const char *str,
long *ret)
68 tmp = strtol(str, &endptr, 10);
70 if (!endptr || *endptr || errno) {
94 SR_PRIV int sr_atoi(
const char *str,
int *ret)
98 if (sr_atol(str, &tmp) !=
SR_OK)
101 if ((
int) tmp != tmp) {
124 SR_PRIV int sr_atod(
const char *str,
double *ret)
130 tmp = strtof(str, &endptr);
132 if (!endptr || *endptr || errno) {
156 SR_PRIV int sr_atof(
const char *str,
float *ret)
160 if (sr_atod(str, &tmp) !=
SR_OK)
163 if ((
float) tmp != tmp) {
186 SR_PRIV int sr_atof_ascii(
const char *str,
float *ret)
192 tmp = g_ascii_strtod(str, &endptr);
194 if (!endptr || *endptr || errno) {
234 uint64_t quot, divisor[] = {
238 const char *p, prefix[] =
"\0kMGTPE";
239 char fmt[16], fract[20] =
"", *f;
244 for (i = 0; (quot = x / divisor[i]) >= 1000; i++);
247 sprintf(fmt,
".%%0%d"PRIu64, i * 3);
248 f = fract + sprintf(fract, fmt, x % divisor[i]) - 1;
250 while (f >= fract && strchr(
"0.", *f))
256 return g_strdup_printf(
"%" PRIu64
"%s %.1s%s", quot, fract, p, unit);
298 o = g_malloc0(30 + 1);
300 if (frequency >=
SR_GHZ(1))
301 r = snprintf(o, 30,
"%" PRIu64
" ns", frequency / 1000000000);
302 else if (frequency >=
SR_MHZ(1))
303 r = snprintf(o, 30,
"%" PRIu64
" us", frequency / 1000000);
304 else if (frequency >=
SR_KHZ(1))
305 r = snprintf(o, 30,
"%" PRIu64
" ms", frequency / 1000);
307 r = snprintf(o, 30,
"%" PRIu64
" s", frequency);
339 o = g_malloc0(30 + 1);
342 r = snprintf(o, 30,
"%" PRIu64
"mV", v_p);
344 r = snprintf(o, 30,
"%" PRIu64
"V", v_p);
346 r = snprintf(o, 30,
"%gV", (
float)v_p / (
float)v_q);
376 int multiplier, done;
380 *size = strtoull(sizestring, &s, 10);
384 while (s && *s && multiplier == 0 && !done) {
389 frac_part = g_ascii_strtod(s, &s);
409 if (multiplier > 0) {
411 *size += frac_part * multiplier;
415 if (s && *s && g_ascii_strcasecmp(s,
"Hz"))
448 time_msec = strtoull(timestring, &s, 10);
449 if (time_msec == 0 && s == timestring)
457 else if (!strcmp(s,
"ms"))
472 if (!g_ascii_strncasecmp(boolstr,
"true", 4) ||
473 !g_ascii_strncasecmp(boolstr,
"yes", 3) ||
474 !g_ascii_strncasecmp(boolstr,
"on", 2) ||
475 !g_ascii_strncasecmp(boolstr,
"1", 1))
486 *p = strtoull(periodstr, &s, 10);
487 if (*p == 0 && s == periodstr)
494 if (!strcmp(s,
"fs"))
495 *q = 1000000000000000ULL;
496 else if (!strcmp(s,
"ps"))
497 *q = 1000000000000ULL;
498 else if (!strcmp(s,
"ns"))
500 else if (!strcmp(s,
"us"))
502 else if (!strcmp(s,
"ms"))
504 else if (!strcmp(s,
"s"))
519 *p = strtoull(voltstr, &s, 10);
520 if (*p == 0 && s == voltstr)
527 if (!g_ascii_strcasecmp(s,
"mv"))
529 else if (!g_ascii_strcasecmp(s,
"v"))
The public libsigrok header file to be used by frontends.
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)
Generic/unspecified error.
int sr_parse_voltage(const char *voltstr, uint64_t *p, uint64_t *q)
char * sr_samplerate_string(uint64_t samplerate)
Convert a numeric samplerate value to its "natural" string representation.
int sr_parse_sizestring(const char *sizestring, uint64_t *size)
Convert a "natural" string representation of a size value to uint64_t.
gboolean sr_parse_boolstring(const char *boolstr)
char * sr_period_string(uint64_t frequency)
Convert a numeric frequency value to the "natural" string representation of its period.
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...