]> sigrok.org Git - libsigrok.git/blame - strutil.c
Drop link-mso19/nexus-osciprime in preparation for release.
[libsigrok.git] / strutil.c
CommitLineData
25e7d9b1 1/*
50985c20 2 * This file is part of the libsigrok project.
25e7d9b1
UH
3 *
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5 *
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.
10 *
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.
15 *
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
19 */
20
21#include <stdint.h>
22#include <stdlib.h>
23#include <string.h>
45c59c8b
BV
24#include "libsigrok.h"
25#include "libsigrok-internal.h"
25e7d9b1 26
a885ce3e
UH
27/* Message logging helpers with driver-specific prefix string. */
28#define DRIVER_LOG_DOMAIN "strutil: "
29#define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
30#define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
31#define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
32#define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
33#define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
34#define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
35
393fb9cb
UH
36/**
37 * @file
38 *
39 * Helper functions for handling or converting libsigrok-related strings.
40 */
41
7b870c38
UH
42/**
43 * @defgroup grp_strutil String utilities
44 *
45 * Helper functions for handling or converting libsigrok-related strings.
46 *
47 * @{
48 */
49
25e7d9b1 50/**
4cc9aea1
JH
51 * Convert a numeric value value to its "natural" string representation.
52 * in SI units
25e7d9b1 53 *
4cc9aea1
JH
54 * E.g. a value of 3000000, with units set to "W", would be converted
55 * to "3 MW", 20000 to "20 kW", 31500 would become "31.5 kW".
25e7d9b1 56 *
4cc9aea1
JH
57 * @param x The value to convert.
58 * @param unit The unit to append to the string, or NULL if the string
59 * has no units.
44dae539 60 *
133a37bf
UH
61 * @return A g_try_malloc()ed string representation of the samplerate value,
62 * or NULL upon errors. The caller is responsible to g_free() the
63 * memory.
25e7d9b1 64 */
4cc9aea1 65SR_API char *sr_si_string_u64(uint64_t x, const char *unit)
25e7d9b1 66{
a885ce3e 67 if (unit == NULL)
4cc9aea1 68 unit = "";
25e7d9b1 69
4cc9aea1
JH
70 if ((x >= SR_GHZ(1)) && (x % SR_GHZ(1) == 0)) {
71 return g_strdup_printf("%" PRIu64 " G%s", x / SR_GHZ(1), unit);
72 } else if ((x >= SR_GHZ(1)) && (x % SR_GHZ(1) != 0)) {
73 return g_strdup_printf("%" PRIu64 ".%" PRIu64 " G%s",
74 x / SR_GHZ(1), x % SR_GHZ(1), unit);
75 } else if ((x >= SR_MHZ(1)) && (x % SR_MHZ(1) == 0)) {
76 return g_strdup_printf("%" PRIu64 " M%s",
77 x / SR_MHZ(1), unit);
78 } else if ((x >= SR_MHZ(1)) && (x % SR_MHZ(1) != 0)) {
79 return g_strdup_printf("%" PRIu64 ".%" PRIu64 " M%s",
80 x / SR_MHZ(1), x % SR_MHZ(1), unit);
81 } else if ((x >= SR_KHZ(1)) && (x % SR_KHZ(1) == 0)) {
82 return g_strdup_printf("%" PRIu64 " k%s",
83 x / SR_KHZ(1), unit);
84 } else if ((x >= SR_KHZ(1)) && (x % SR_KHZ(1) != 0)) {
85 return g_strdup_printf("%" PRIu64 ".%" PRIu64 " k%s",
86 x / SR_KHZ(1), x % SR_KHZ(1), unit);
c69e35a7 87 } else {
4cc9aea1 88 return g_strdup_printf("%" PRIu64 " %s", x, unit);
133a37bf 89 }
25e7d9b1 90
a885ce3e 91 sr_err("%s: Error creating SI units string.", __func__);
4cc9aea1
JH
92 return NULL;
93}
25e7d9b1 94
4cc9aea1
JH
95/**
96 * Convert a numeric samplerate value to its "natural" string representation.
97 *
98 * E.g. a value of 3000000 would be converted to "3 MHz", 20000 to "20 kHz",
99 * 31500 would become "31.5 kHz".
100 *
101 * @param samplerate The samplerate in Hz.
102 *
103 * @return A g_try_malloc()ed string representation of the samplerate value,
104 * or NULL upon errors. The caller is responsible to g_free() the
105 * memory.
106 */
107SR_API char *sr_samplerate_string(uint64_t samplerate)
108{
109 return sr_si_string_u64(samplerate, "Hz");
25e7d9b1 110}
2a3f9541 111
2a3f9541 112/**
dfcc0bf9 113 * Convert a numeric frequency value to the "natural" string representation
2a3f9541
BV
114 * of its period.
115 *
2507648e 116 * E.g. a value of 3000000 would be converted to "3 us", 20000 to "50 ms".
2a3f9541
BV
117 *
118 * @param frequency The frequency in Hz.
44dae539 119 *
133a37bf
UH
120 * @return A g_try_malloc()ed string representation of the frequency value,
121 * or NULL upon errors. The caller is responsible to g_free() the
122 * memory.
2a3f9541 123 */
1a081ca6 124SR_API char *sr_period_string(uint64_t frequency)
2a3f9541
BV
125{
126 char *o;
127 int r;
128
133a37bf
UH
129 /* Allocate enough for a uint64_t as string + " ms". */
130 if (!(o = g_try_malloc0(30 + 1))) {
a885ce3e 131 sr_err("%s: o malloc failed", __func__);
2a3f9541 132 return NULL;
133a37bf 133 }
2a3f9541 134
59df0c77 135 if (frequency >= SR_GHZ(1))
2a3f9541 136 r = snprintf(o, 30, "%" PRIu64 " ns", frequency / 1000000000);
59df0c77 137 else if (frequency >= SR_MHZ(1))
2507648e 138 r = snprintf(o, 30, "%" PRIu64 " us", frequency / 1000000);
59df0c77 139 else if (frequency >= SR_KHZ(1))
2a3f9541
BV
140 r = snprintf(o, 30, "%" PRIu64 " ms", frequency / 1000);
141 else
142 r = snprintf(o, 30, "%" PRIu64 " s", frequency);
143
144 if (r < 0) {
145 /* Something went wrong... */
133a37bf 146 g_free(o);
2a3f9541
BV
147 return NULL;
148 }
149
150 return o;
151}
40f5ddac 152
79afc8ca 153/**
e0e15067
BV
154 * Convert a numeric voltage value to the "natural" string representation
155 * of its voltage value. The voltage is specified as a rational number's
156 * numerator and denominator.
79afc8ca
BV
157 *
158 * E.g. a value of 300000 would be converted to "300mV", 2 to "2V".
159 *
e0e15067
BV
160 * @param v_p The voltage numerator.
161 * @param v_q The voltage denominator.
79afc8ca
BV
162 *
163 * @return A g_try_malloc()ed string representation of the voltage value,
164 * or NULL upon errors. The caller is responsible to g_free() the
165 * memory.
166 */
e0e15067 167SR_API char *sr_voltage_string(uint64_t v_p, uint64_t v_q)
79afc8ca 168{
79afc8ca 169 int r;
d5a669a9 170 char *o;
79afc8ca
BV
171
172 if (!(o = g_try_malloc0(30 + 1))) {
a885ce3e 173 sr_err("%s: o malloc failed", __func__);
79afc8ca
BV
174 return NULL;
175 }
176
e0e15067
BV
177 if (v_q == 1000)
178 r = snprintf(o, 30, "%" PRIu64 "mV", v_p);
179 else if (v_q == 1)
180 r = snprintf(o, 30, "%" PRIu64 "V", v_p);
79afc8ca 181 else
e0e15067 182 r = snprintf(o, 30, "%gV", (float)v_p / (float)v_q);
79afc8ca
BV
183
184 if (r < 0) {
185 /* Something went wrong... */
186 g_free(o);
187 return NULL;
188 }
189
190 return o;
191}
192
dfcc0bf9 193/**
bf978d35 194 * Parse a trigger specification string.
dfcc0bf9 195 *
9c5332d2
UH
196 * @param sdi The device instance for which the trigger specification is
197 * intended. Must not be NULL. Also, sdi->driver and
198 * sdi->driver->info_get must not be NULL.
bf978d35
UH
199 * @param triggerstring The string containing the trigger specification for
200 * one or more probes of this device. Entries for multiple probes are
201 * comma-separated. Triggers are specified in the form key=value,
202 * where the key is a probe number (or probe name) and the value is
203 * the requested trigger type. Valid trigger types currently
204 * include 'r' (rising edge), 'f' (falling edge), 'c' (any pin value
205 * change), '0' (low value), or '1' (high value).
206 * Example: "1=r,sck=f,miso=0,7=c"
44dae539 207 *
bf978d35
UH
208 * @return Pointer to a list of trigger types (strings), or NULL upon errors.
209 * The pointer list (if non-NULL) has as many entries as the
210 * respective device has probes (all physically available probes,
211 * not just enabled ones). Entries of the list which don't have
212 * a trigger value set in 'triggerstring' are NULL, the other entries
213 * contain the respective trigger type which is requested for the
214 * respective probe (e.g. "r", "c", and so on).
dfcc0bf9 215 */
92ae7984 216SR_API char **sr_parse_triggerstring(const struct sr_dev_inst *sdi,
1a081ca6 217 const char *triggerstring)
40f5ddac
BV
218{
219 GSList *l;
003595ac 220 GVariant *gvar;
1afe8989 221 struct sr_probe *probe;
40f5ddac 222 int max_probes, probenum, i;
b7f578be
JH
223 char **tokens, **triggerlist, *trigger, *tc;
224 const char *trigger_types;
40f5ddac
BV
225 gboolean error;
226
92ae7984 227 max_probes = g_slist_length(sdi->probes);
40f5ddac 228 error = FALSE;
b53738ba
UH
229
230 if (!(triggerlist = g_try_malloc0(max_probes * sizeof(char *)))) {
a885ce3e 231 sr_err("%s: triggerlist malloc failed", __func__);
b53738ba
UH
232 return NULL;
233 }
234
003595ac 235 if (sdi->driver->config_list(SR_CONF_TRIGGER_TYPE, &gvar, sdi) != SR_OK) {
a885ce3e 236 sr_err("%s: Device doesn't support any triggers.", __func__);
40f5ddac 237 return NULL;
bf978d35 238 }
003595ac 239 trigger_types = g_variant_get_string(gvar, NULL);
40f5ddac 240
31fc1fbc 241 tokens = g_strsplit(triggerstring, ",", max_probes);
40f5ddac 242 for (i = 0; tokens[i]; i++) {
17ff1124
BV
243 probenum = -1;
244 for (l = sdi->probes; l; l = l->next) {
245 probe = (struct sr_probe *)l->data;
246 if (probe->enabled
247 && !strncmp(probe->name, tokens[i],
248 strlen(probe->name))) {
249 probenum = probe->index;
250 break;
40f5ddac 251 }
40f5ddac
BV
252 }
253
31fc1fbc 254 if (probenum < 0 || probenum >= max_probes) {
a885ce3e 255 sr_err("Invalid probe.");
40f5ddac
BV
256 error = TRUE;
257 break;
258 }
259
260 if ((trigger = strchr(tokens[i], '='))) {
261 for (tc = ++trigger; *tc; tc++) {
262 if (strchr(trigger_types, *tc) == NULL) {
a885ce3e 263 sr_err("Unsupported trigger "
0aeb0ccd 264 "type '%c'.", *tc);
40f5ddac
BV
265 error = TRUE;
266 break;
267 }
268 }
269 if (!error)
31fc1fbc 270 triggerlist[probenum] = g_strdup(trigger);
40f5ddac
BV
271 }
272 }
273 g_strfreev(tokens);
003595ac 274 g_variant_unref(gvar);
40f5ddac
BV
275
276 if (error) {
277 for (i = 0; i < max_probes; i++)
66410a86 278 g_free(triggerlist[i]);
40f5ddac
BV
279 g_free(triggerlist);
280 triggerlist = NULL;
281 }
282
283 return triggerlist;
284}
285
dfcc0bf9
UH
286/**
287 * Convert a "natural" string representation of a size value to uint64_t.
288 *
289 * E.g. a value of "3k" or "3 K" would be converted to 3000, a value
290 * of "15M" would be converted to 15000000.
291 *
292 * Value representations other than decimal (such as hex or octal) are not
293 * supported. Only 'k' (kilo), 'm' (mega), 'g' (giga) suffixes are supported.
294 * Spaces (but not other whitespace) between value and suffix are allowed.
295 *
296 * @param sizestring A string containing a (decimal) size value.
f64c1414 297 * @param size Pointer to uint64_t which will contain the string's size value.
dfcc0bf9 298 *
44dae539 299 * @return SR_OK upon success, SR_ERR upon errors.
dfcc0bf9 300 */
1a081ca6 301SR_API int sr_parse_sizestring(const char *sizestring, uint64_t *size)
40f5ddac 302{
f64c1414 303 int multiplier, done;
40f5ddac
BV
304 char *s;
305
f64c1414 306 *size = strtoull(sizestring, &s, 10);
40f5ddac 307 multiplier = 0;
f64c1414
BV
308 done = FALSE;
309 while (s && *s && multiplier == 0 && !done) {
40f5ddac
BV
310 switch (*s) {
311 case ' ':
312 break;
313 case 'k':
314 case 'K':
59df0c77 315 multiplier = SR_KHZ(1);
40f5ddac
BV
316 break;
317 case 'm':
318 case 'M':
59df0c77 319 multiplier = SR_MHZ(1);
40f5ddac
BV
320 break;
321 case 'g':
322 case 'G':
59df0c77 323 multiplier = SR_GHZ(1);
40f5ddac
BV
324 break;
325 default:
f64c1414
BV
326 done = TRUE;
327 s--;
40f5ddac
BV
328 }
329 s++;
330 }
331 if (multiplier > 0)
f64c1414 332 *size *= multiplier;
40f5ddac 333
f64c1414
BV
334 if (*s && strcasecmp(s, "Hz"))
335 return SR_ERR;
336
337 return SR_OK;
40f5ddac
BV
338}
339
dfcc0bf9
UH
340/**
341 * Convert a "natural" string representation of a time value to an
342 * uint64_t value in milliseconds.
343 *
344 * E.g. a value of "3s" or "3 s" would be converted to 3000, a value
345 * of "15ms" would be converted to 15.
346 *
347 * Value representations other than decimal (such as hex or octal) are not
348 * supported. Only lower-case "s" and "ms" time suffixes are supported.
349 * Spaces (but not other whitespace) between value and suffix are allowed.
350 *
351 * @param timestring A string containing a (decimal) time value.
352 * @return The string's time value as uint64_t, in milliseconds.
353 *
6b2d8d3e
UH
354 * @todo Add support for "m" (minutes) and others.
355 * @todo Add support for picoseconds?
356 * @todo Allow both lower-case and upper-case? If no, document it.
dfcc0bf9 357 */
1a081ca6 358SR_API uint64_t sr_parse_timestring(const char *timestring)
40f5ddac
BV
359{
360 uint64_t time_msec;
361 char *s;
362
6b2d8d3e
UH
363 /* TODO: Error handling, logging. */
364
40f5ddac
BV
365 time_msec = strtoull(timestring, &s, 10);
366 if (time_msec == 0 && s == timestring)
367 return 0;
368
369 if (s && *s) {
370 while (*s == ' ')
371 s++;
372 if (!strcmp(s, "s"))
373 time_msec *= 1000;
374 else if (!strcmp(s, "ms"))
375 ; /* redundant */
376 else
377 return 0;
378 }
379
380 return time_msec;
381}
4d436e71 382
1a081ca6 383SR_API gboolean sr_parse_boolstring(const char *boolstr)
4d436e71
GM
384{
385 if (!boolstr)
386 return FALSE;
387
993526f8
BV
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))
4d436e71
GM
392 return TRUE;
393
394 return FALSE;
395}
76f4c610 396
76e107d6 397SR_API int sr_parse_period(const char *periodstr, uint64_t *p, uint64_t *q)
76f4c610
BV
398{
399 char *s;
400
76e107d6
BV
401 *p = strtoull(periodstr, &s, 10);
402 if (*p == 0 && s == periodstr)
76f4c610
BV
403 /* No digits found. */
404 return SR_ERR_ARG;
405
406 if (s && *s) {
407 while (*s == ' ')
408 s++;
8c012adb 409 if (!strcmp(s, "fs"))
76e107d6 410 *q = 1000000000000000ULL;
8c012adb 411 else if (!strcmp(s, "ps"))
76e107d6 412 *q = 1000000000000ULL;
8c012adb 413 else if (!strcmp(s, "ns"))
76e107d6 414 *q = 1000000000ULL;
76f4c610 415 else if (!strcmp(s, "us"))
76e107d6 416 *q = 1000000;
76f4c610 417 else if (!strcmp(s, "ms"))
76e107d6 418 *q = 1000;
76f4c610 419 else if (!strcmp(s, "s"))
76e107d6 420 *q = 1;
76f4c610
BV
421 else
422 /* Must have a time suffix. */
423 return SR_ERR_ARG;
424 }
425
426 return SR_OK;
427}
428
429
76e107d6 430SR_API int sr_parse_voltage(const char *voltstr, uint64_t *p, uint64_t *q)
79afc8ca
BV
431{
432 char *s;
433
76e107d6
BV
434 *p = strtoull(voltstr, &s, 10);
435 if (*p == 0 && s == voltstr)
79afc8ca
BV
436 /* No digits found. */
437 return SR_ERR_ARG;
438
439 if (s && *s) {
440 while (*s == ' ')
441 s++;
442 if (!strcasecmp(s, "mv"))
76e107d6 443 *q = 1000L;
79afc8ca 444 else if (!strcasecmp(s, "v"))
76e107d6 445 *q = 1;
79afc8ca
BV
446 else
447 /* Must have a base suffix. */
448 return SR_ERR_ARG;
449 }
450
451 return SR_OK;
452}
453
7b870c38 454/** @} */