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)
71 return g_strdup_printf(
"%" PRIu64
" G%s", x /
SR_GHZ(1), unit);
73 return g_strdup_printf(
"%" PRIu64
".%" PRIu64
" G%s",
76 return g_strdup_printf(
"%" PRIu64
" M%s",
79 return g_strdup_printf(
"%" PRIu64
".%" PRIu64
" M%s",
82 return g_strdup_printf(
"%" PRIu64
" k%s",
85 return g_strdup_printf(
"%" PRIu64
".%" PRIu64
" k%s",
88 return g_strdup_printf(
"%" PRIu64
" %s", x, unit);
91 sr_err(
"%s: Error creating SI units string.", __func__);
130 if (!(o = g_try_malloc0(30 + 1))) {
131 sr_err(
"%s: o malloc failed", __func__);
135 if (frequency >=
SR_GHZ(1))
136 r = snprintf(o, 30,
"%" PRIu64
" ns", frequency / 1000000000);
137 else if (frequency >=
SR_MHZ(1))
138 r = snprintf(o, 30,
"%" PRIu64
" us", frequency / 1000000);
139 else if (frequency >=
SR_KHZ(1))
140 r = snprintf(o, 30,
"%" PRIu64
" ms", frequency / 1000);
142 r = snprintf(o, 30,
"%" PRIu64
" s", frequency);
172 if (!(o = g_try_malloc0(30 + 1))) {
173 sr_err(
"%s: o malloc failed", __func__);
178 r = snprintf(o, 30,
"%" PRIu64
"mV", v_p);
180 r = snprintf(o, 30,
"%" PRIu64
"V", v_p);
182 r = snprintf(o, 30,
"%gV", (
float)v_p / (
float)v_q);
217 const char *triggerstring)
222 int max_probes, probenum, i;
223 char **tokens, **triggerlist, *
trigger, *tc;
224 const char *trigger_types;
227 max_probes = g_slist_length(sdi->
probes);
230 if (!(triggerlist = g_try_malloc0(max_probes *
sizeof(
char *)))) {
231 sr_err(
"%s: triggerlist malloc failed", __func__);
236 sr_err(
"%s: Device doesn't support any triggers.", __func__);
239 trigger_types = g_variant_get_string(gvar, NULL);
241 tokens = g_strsplit(triggerstring,
",", max_probes);
242 for (i = 0; tokens[i]; i++) {
244 for (l = sdi->
probes; l; l = l->next) {
247 && !strncmp(probe->
name, tokens[i],
248 strlen(probe->
name))) {
249 probenum = probe->
index;
254 if (probenum < 0 || probenum >= max_probes) {
260 if ((trigger = strchr(tokens[i],
'='))) {
261 for (tc = ++trigger; *tc; tc++) {
262 if (strchr(trigger_types, *tc) == NULL) {
263 sr_err(
"Unsupported trigger "
270 triggerlist[probenum] = g_strdup(trigger);
274 g_variant_unref(gvar);
277 for (i = 0; i < max_probes; i++)
278 g_free(triggerlist[i]);
303 int multiplier, done;
306 *size = strtoull(sizestring, &s, 10);
309 while (s && *s && multiplier == 0 && !done) {
334 if (*s && strcasecmp(s,
"Hz"))
365 time_msec = strtoull(timestring, &s, 10);
366 if (time_msec == 0 && s == timestring)
374 else if (!strcmp(s,
"ms"))
388 if (!g_ascii_strncasecmp(boolstr,
"true", 4) ||
389 !g_ascii_strncasecmp(boolstr,
"yes", 3) ||
390 !g_ascii_strncasecmp(boolstr,
"on", 2) ||
391 !g_ascii_strncasecmp(boolstr,
"1", 1))
401 *p = strtoull(periodstr, &s, 10);
402 if (*p == 0 && s == periodstr)
409 if (!strcmp(s,
"fs"))
410 *q = 1000000000000000ULL;
411 else if (!strcmp(s,
"ps"))
412 *q = 1000000000000ULL;
413 else if (!strcmp(s,
"ns"))
415 else if (!strcmp(s,
"us"))
417 else if (!strcmp(s,
"ms"))
419 else if (!strcmp(s,
"s"))
434 *p = strtoull(voltstr, &s, 10);
435 if (*p == 0 && s == voltstr)
442 if (!strcasecmp(s,
"mv"))
444 else if (!strcasecmp(s,
"v"))