2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libsigrok.h"
25 #include "libsigrok-internal.h"
30 * Helper functions for handling or converting libsigrok-related strings.
34 * @defgroup grp_strutil String utilities
36 * Helper functions for handling or converting libsigrok-related strings.
42 * Convert a numeric value value to its "natural" string representation.
45 * E.g. a value of 3000000, with units set to "W", would be converted
46 * to "3 MW", 20000 to "20 kW", 31500 would become "31.5 kW".
48 * @param x The value to convert.
49 * @param unit The unit to append to the string, or NULL if the string
52 * @return A g_try_malloc()ed string representation of the samplerate value,
53 * or NULL upon errors. The caller is responsible to g_free() the
56 SR_API char *sr_si_string_u64(uint64_t x, const char *unit)
61 if ((x >= SR_GHZ(1)) && (x % SR_GHZ(1) == 0)) {
62 return g_strdup_printf("%" PRIu64 " G%s", x / SR_GHZ(1), unit);
63 } else if ((x >= SR_GHZ(1)) && (x % SR_GHZ(1) != 0)) {
64 return g_strdup_printf("%" PRIu64 ".%" PRIu64 " G%s",
65 x / SR_GHZ(1), x % SR_GHZ(1), unit);
66 } else if ((x >= SR_MHZ(1)) && (x % SR_MHZ(1) == 0)) {
67 return g_strdup_printf("%" PRIu64 " M%s",
69 } else if ((x >= SR_MHZ(1)) && (x % SR_MHZ(1) != 0)) {
70 return g_strdup_printf("%" PRIu64 ".%" PRIu64 " M%s",
71 x / SR_MHZ(1), x % SR_MHZ(1), unit);
72 } else if ((x >= SR_KHZ(1)) && (x % SR_KHZ(1) == 0)) {
73 return g_strdup_printf("%" PRIu64 " k%s",
75 } else if ((x >= SR_KHZ(1)) && (x % SR_KHZ(1) != 0)) {
76 return g_strdup_printf("%" PRIu64 ".%" PRIu64 " k%s",
77 x / SR_KHZ(1), x % SR_KHZ(1), unit);
79 return g_strdup_printf("%" PRIu64 " %s", x, unit);
82 sr_err("strutil: %s: Error creating SI units string.",
88 * Convert a numeric samplerate value to its "natural" string representation.
90 * E.g. a value of 3000000 would be converted to "3 MHz", 20000 to "20 kHz",
91 * 31500 would become "31.5 kHz".
93 * @param samplerate The samplerate in Hz.
95 * @return A g_try_malloc()ed string representation of the samplerate value,
96 * or NULL upon errors. The caller is responsible to g_free() the
99 SR_API char *sr_samplerate_string(uint64_t samplerate)
101 return sr_si_string_u64(samplerate, "Hz");
105 * Convert a numeric frequency value to the "natural" string representation
108 * E.g. a value of 3000000 would be converted to "3 us", 20000 to "50 ms".
110 * @param frequency The frequency in Hz.
112 * @return A g_try_malloc()ed string representation of the frequency value,
113 * or NULL upon errors. The caller is responsible to g_free() the
116 SR_API char *sr_period_string(uint64_t frequency)
121 /* Allocate enough for a uint64_t as string + " ms". */
122 if (!(o = g_try_malloc0(30 + 1))) {
123 sr_err("strutil: %s: o malloc failed", __func__);
127 if (frequency >= SR_GHZ(1))
128 r = snprintf(o, 30, "%" PRIu64 " ns", frequency / 1000000000);
129 else if (frequency >= SR_MHZ(1))
130 r = snprintf(o, 30, "%" PRIu64 " us", frequency / 1000000);
131 else if (frequency >= SR_KHZ(1))
132 r = snprintf(o, 30, "%" PRIu64 " ms", frequency / 1000);
134 r = snprintf(o, 30, "%" PRIu64 " s", frequency);
137 /* Something went wrong... */
146 * Convert a numeric frequency value to the "natural" string representation
147 * of its voltage value.
149 * E.g. a value of 300000 would be converted to "300mV", 2 to "2V".
151 * @param voltage The voltage represented as a rational number, with the
152 * denominator a divisor of 1V.
154 * @return A g_try_malloc()ed string representation of the voltage value,
155 * or NULL upon errors. The caller is responsible to g_free() the
158 SR_API char *sr_voltage_string(struct sr_rational *voltage)
163 if (!(o = g_try_malloc0(30 + 1))) {
164 sr_err("strutil: %s: o malloc failed", __func__);
168 if (voltage->q == 1000)
169 r = snprintf(o, 30, "%" PRIu64 "mV", voltage->p);
170 else if (voltage->q == 1)
171 r = snprintf(o, 30, "%" PRIu64 "V", voltage->p);
176 /* Something went wrong... */
185 * Parse a trigger specification string.
187 * @param sdi The device instance for which the trigger specification is
188 * intended. Must not be NULL. Also, sdi->driver and
189 * sdi->driver->info_get must not be NULL.
190 * @param triggerstring The string containing the trigger specification for
191 * one or more probes of this device. Entries for multiple probes are
192 * comma-separated. Triggers are specified in the form key=value,
193 * where the key is a probe number (or probe name) and the value is
194 * the requested trigger type. Valid trigger types currently
195 * include 'r' (rising edge), 'f' (falling edge), 'c' (any pin value
196 * change), '0' (low value), or '1' (high value).
197 * Example: "1=r,sck=f,miso=0,7=c"
199 * @return Pointer to a list of trigger types (strings), or NULL upon errors.
200 * The pointer list (if non-NULL) has as many entries as the
201 * respective device has probes (all physically available probes,
202 * not just enabled ones). Entries of the list which don't have
203 * a trigger value set in 'triggerstring' are NULL, the other entries
204 * contain the respective trigger type which is requested for the
205 * respective probe (e.g. "r", "c", and so on).
207 SR_API char **sr_parse_triggerstring(const struct sr_dev_inst *sdi,
208 const char *triggerstring)
211 struct sr_probe *probe;
212 int max_probes, probenum, i;
213 char **tokens, **triggerlist, *trigger, *tc;
214 const char *trigger_types;
217 max_probes = g_slist_length(sdi->probes);
220 if (!(triggerlist = g_try_malloc0(max_probes * sizeof(char *)))) {
221 sr_err("strutil: %s: triggerlist malloc failed", __func__);
225 if (sdi->driver->info_get(SR_DI_TRIGGER_TYPES,
226 (const void **)&trigger_types, sdi) != SR_OK) {
227 sr_err("strutil: %s: Device doesn't support any triggers.", __func__);
231 tokens = g_strsplit(triggerstring, ",", max_probes);
232 for (i = 0; tokens[i]; i++) {
234 for (l = sdi->probes; l; l = l->next) {
235 probe = (struct sr_probe *)l->data;
237 && !strncmp(probe->name, tokens[i],
238 strlen(probe->name))) {
239 probenum = probe->index;
244 if (probenum < 0 || probenum >= max_probes) {
245 sr_err("strutil: Invalid probe.");
250 if ((trigger = strchr(tokens[i], '='))) {
251 for (tc = ++trigger; *tc; tc++) {
252 if (strchr(trigger_types, *tc) == NULL) {
253 sr_err("strutil: Unsupported trigger "
260 triggerlist[probenum] = g_strdup(trigger);
266 for (i = 0; i < max_probes; i++)
267 g_free(triggerlist[i]);
276 * Convert a "natural" string representation of a size value to uint64_t.
278 * E.g. a value of "3k" or "3 K" would be converted to 3000, a value
279 * of "15M" would be converted to 15000000.
281 * Value representations other than decimal (such as hex or octal) are not
282 * supported. Only 'k' (kilo), 'm' (mega), 'g' (giga) suffixes are supported.
283 * Spaces (but not other whitespace) between value and suffix are allowed.
285 * @param sizestring A string containing a (decimal) size value.
286 * @param size Pointer to uint64_t which will contain the string's size value.
288 * @return SR_OK upon success, SR_ERR upon errors.
290 SR_API int sr_parse_sizestring(const char *sizestring, uint64_t *size)
292 int multiplier, done;
295 *size = strtoull(sizestring, &s, 10);
298 while (s && *s && multiplier == 0 && !done) {
304 multiplier = SR_KHZ(1);
308 multiplier = SR_MHZ(1);
312 multiplier = SR_GHZ(1);
323 if (*s && strcasecmp(s, "Hz"))
330 * Convert a "natural" string representation of a time value to an
331 * uint64_t value in milliseconds.
333 * E.g. a value of "3s" or "3 s" would be converted to 3000, a value
334 * of "15ms" would be converted to 15.
336 * Value representations other than decimal (such as hex or octal) are not
337 * supported. Only lower-case "s" and "ms" time suffixes are supported.
338 * Spaces (but not other whitespace) between value and suffix are allowed.
340 * @param timestring A string containing a (decimal) time value.
341 * @return The string's time value as uint64_t, in milliseconds.
343 * @todo Add support for "m" (minutes) and others.
344 * @todo Add support for picoseconds?
345 * @todo Allow both lower-case and upper-case? If no, document it.
347 SR_API uint64_t sr_parse_timestring(const char *timestring)
352 /* TODO: Error handling, logging. */
354 time_msec = strtoull(timestring, &s, 10);
355 if (time_msec == 0 && s == timestring)
363 else if (!strcmp(s, "ms"))
372 SR_API gboolean sr_parse_boolstring(const char *boolstr)
377 if (!g_ascii_strncasecmp(boolstr, "true", 4) ||
378 !g_ascii_strncasecmp(boolstr, "yes", 3) ||
379 !g_ascii_strncasecmp(boolstr, "on", 2) ||
380 !g_ascii_strncasecmp(boolstr, "1", 1))
386 SR_API int sr_parse_period(const char *periodstr, struct sr_rational *r)
390 r->p = strtoull(periodstr, &s, 10);
391 if (r->p == 0 && s == periodstr)
392 /* No digits found. */
398 if (!strcmp(s, "ns"))
400 else if (!strcmp(s, "us"))
402 else if (!strcmp(s, "ms"))
404 else if (!strcmp(s, "s"))
407 /* Must have a time suffix. */
415 SR_API int sr_parse_voltage(const char *voltstr, struct sr_rational *r)
419 r->p = strtoull(voltstr, &s, 10);
420 if (r->p == 0 && s == voltstr)
421 /* No digits found. */
427 if (!strcasecmp(s, "mv"))
429 else if (!strcasecmp(s, "v"))
432 /* Must have a base suffix. */