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