25 #include "libsigrok-internal.h"
28 #define LOG_PREFIX "strutil: "
29 #define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args)
30 #define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args)
31 #define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args)
32 #define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args)
33 #define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args)
34 #define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args)
68 uint64_t quot, divisor[] = {
72 const char *p, prefix[] =
"\0kMGTPE";
73 char fmt[8], fract[20] =
"", *f;
78 for (i = 0; (quot = x / divisor[i]) >= 1000; i++);
81 sprintf(fmt,
".%%0%dlu", i * 3);
82 f = fract + sprintf(fract, fmt, x % divisor[i]) - 1;
84 while (f >= fract && strchr(
"0.", *f))
90 return g_strdup_printf(
"%" PRIu64
"%s %.1s%s", quot, fract, p, unit);
128 if (!(o = g_try_malloc0(30 + 1))) {
129 sr_err(
"%s: o malloc failed", __func__);
133 if (frequency >=
SR_GHZ(1))
134 r = snprintf(o, 30,
"%" PRIu64
" ns", frequency / 1000000000);
135 else if (frequency >=
SR_MHZ(1))
136 r = snprintf(o, 30,
"%" PRIu64
" us", frequency / 1000000);
137 else if (frequency >=
SR_KHZ(1))
138 r = snprintf(o, 30,
"%" PRIu64
" ms", frequency / 1000);
140 r = snprintf(o, 30,
"%" PRIu64
" s", frequency);
170 if (!(o = g_try_malloc0(30 + 1))) {
171 sr_err(
"%s: o malloc failed", __func__);
176 r = snprintf(o, 30,
"%" PRIu64
"mV", v_p);
178 r = snprintf(o, 30,
"%" PRIu64
"V", v_p);
180 r = snprintf(o, 30,
"%gV", (
float)v_p / (
float)v_q);
215 const char *triggerstring)
220 int max_probes, probenum, i;
221 char **tokens, **triggerlist, *
trigger, *tc;
222 const char *trigger_types;
225 max_probes = g_slist_length(sdi->
probes);
228 if (!(triggerlist = g_try_malloc0(max_probes *
sizeof(
char *)))) {
229 sr_err(
"%s: triggerlist malloc failed", __func__);
234 sr_err(
"%s: Device doesn't support any triggers.", __func__);
237 trigger_types = g_variant_get_string(gvar, NULL);
239 tokens = g_strsplit(triggerstring,
",", max_probes);
240 for (i = 0; tokens[i]; i++) {
242 for (l = sdi->
probes; l; l = l->next) {
245 && !strncmp(probe->
name, tokens[i],
246 strlen(probe->
name))) {
247 probenum = probe->
index;
252 if (probenum < 0 || probenum >= max_probes) {
258 if ((trigger = strchr(tokens[i],
'='))) {
259 for (tc = ++trigger; *tc; tc++) {
260 if (strchr(trigger_types, *tc) == NULL) {
261 sr_err(
"Unsupported trigger "
268 triggerlist[probenum] = g_strdup(trigger);
272 g_variant_unref(gvar);
275 for (i = 0; i < max_probes; i++)
276 g_free(triggerlist[i]);
301 int multiplier, done;
304 *size = strtoull(sizestring, &s, 10);
307 while (s && *s && multiplier == 0 && !done) {
332 if (*s && strcasecmp(s,
"Hz"))
363 time_msec = strtoull(timestring, &s, 10);
364 if (time_msec == 0 && s == timestring)
372 else if (!strcmp(s,
"ms"))
386 if (!g_ascii_strncasecmp(boolstr,
"true", 4) ||
387 !g_ascii_strncasecmp(boolstr,
"yes", 3) ||
388 !g_ascii_strncasecmp(boolstr,
"on", 2) ||
389 !g_ascii_strncasecmp(boolstr,
"1", 1))
399 *p = strtoull(periodstr, &s, 10);
400 if (*p == 0 && s == periodstr)
407 if (!strcmp(s,
"fs"))
408 *q = 1000000000000000ULL;
409 else if (!strcmp(s,
"ps"))
410 *q = 1000000000000ULL;
411 else if (!strcmp(s,
"ns"))
413 else if (!strcmp(s,
"us"))
415 else if (!strcmp(s,
"ms"))
417 else if (!strcmp(s,
"s"))
432 *p = strtoull(voltstr, &s, 10);
433 if (*p == 0 && s == voltstr)
440 if (!strcasecmp(s,
"mv"))
442 else if (!strcasecmp(s,
"v"))