]> sigrok.org Git - libsigrok.git/blame_incremental - strutil.c
Doxygen: Add @file items for the relevant files.
[libsigrok.git] / strutil.c
... / ...
CommitLineData
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>
24#include "libsigrok.h"
25#include "libsigrok-internal.h"
26
27/**
28 * @file
29 *
30 * Helper functions for handling or converting libsigrok-related strings.
31 */
32
33/**
34 * @defgroup grp_strutil String utilities
35 *
36 * Helper functions for handling or converting libsigrok-related strings.
37 *
38 * @{
39 */
40
41/**
42 * Convert a numeric value value to its "natural" string representation.
43 * in SI units
44 *
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".
47 *
48 * @param x The value to convert.
49 * @param unit The unit to append to the string, or NULL if the string
50 * has no units.
51 *
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
54 * memory.
55 */
56SR_API char *sr_si_string_u64(uint64_t x, const char *unit)
57{
58 if(unit == NULL)
59 unit = "";
60
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",
68 x / SR_MHZ(1), unit);
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",
74 x / SR_KHZ(1), unit);
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);
78 } else {
79 return g_strdup_printf("%" PRIu64 " %s", x, unit);
80 }
81
82 sr_err("strutil: %s: Error creating SI units string.",
83 __func__);
84 return NULL;
85}
86
87/**
88 * Convert a numeric samplerate value to its "natural" string representation.
89 *
90 * E.g. a value of 3000000 would be converted to "3 MHz", 20000 to "20 kHz",
91 * 31500 would become "31.5 kHz".
92 *
93 * @param samplerate The samplerate in Hz.
94 *
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
97 * memory.
98 */
99SR_API char *sr_samplerate_string(uint64_t samplerate)
100{
101 return sr_si_string_u64(samplerate, "Hz");
102}
103
104/**
105 * Convert a numeric frequency value to the "natural" string representation
106 * of its period.
107 *
108 * E.g. a value of 3000000 would be converted to "3 us", 20000 to "50 ms".
109 *
110 * @param frequency The frequency in Hz.
111 *
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
114 * memory.
115 */
116SR_API char *sr_period_string(uint64_t frequency)
117{
118 char *o;
119 int r;
120
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__);
124 return NULL;
125 }
126
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);
133 else
134 r = snprintf(o, 30, "%" PRIu64 " s", frequency);
135
136 if (r < 0) {
137 /* Something went wrong... */
138 g_free(o);
139 return NULL;
140 }
141
142 return o;
143}
144
145/**
146 * Convert a numeric frequency value to the "natural" string representation
147 * of its voltage value.
148 *
149 * E.g. a value of 300000 would be converted to "300mV", 2 to "2V".
150 *
151 * @param voltage The voltage represented as a rational number, with the
152 * denominator a divisor of 1V.
153 *
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
156 * memory.
157 */
158SR_API char *sr_voltage_string(struct sr_rational *voltage)
159{
160 char *o;
161 int r;
162
163 if (!(o = g_try_malloc0(30 + 1))) {
164 sr_err("strutil: %s: o malloc failed", __func__);
165 return NULL;
166 }
167
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);
172 else
173 r = -1;
174
175 if (r < 0) {
176 /* Something went wrong... */
177 g_free(o);
178 return NULL;
179 }
180
181 return o;
182}
183
184/**
185 * Parse a trigger specification string.
186 *
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"
198 *
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).
206 */
207SR_API char **sr_parse_triggerstring(const struct sr_dev_inst *sdi,
208 const char *triggerstring)
209{
210 GSList *l;
211 struct sr_probe *probe;
212 int max_probes, probenum, i;
213 char **tokens, **triggerlist, *trigger, *tc;
214 const char *trigger_types;
215 gboolean error;
216
217 max_probes = g_slist_length(sdi->probes);
218 error = FALSE;
219
220 if (!(triggerlist = g_try_malloc0(max_probes * sizeof(char *)))) {
221 sr_err("strutil: %s: triggerlist malloc failed", __func__);
222 return NULL;
223 }
224
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__);
228 return NULL;
229 }
230
231 tokens = g_strsplit(triggerstring, ",", max_probes);
232 for (i = 0; tokens[i]; i++) {
233 probenum = -1;
234 for (l = sdi->probes; l; l = l->next) {
235 probe = (struct sr_probe *)l->data;
236 if (probe->enabled
237 && !strncmp(probe->name, tokens[i],
238 strlen(probe->name))) {
239 probenum = probe->index;
240 break;
241 }
242 }
243
244 if (probenum < 0 || probenum >= max_probes) {
245 sr_err("strutil: Invalid probe.");
246 error = TRUE;
247 break;
248 }
249
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 "
254 "type '%c'.", *tc);
255 error = TRUE;
256 break;
257 }
258 }
259 if (!error)
260 triggerlist[probenum] = g_strdup(trigger);
261 }
262 }
263 g_strfreev(tokens);
264
265 if (error) {
266 for (i = 0; i < max_probes; i++)
267 g_free(triggerlist[i]);
268 g_free(triggerlist);
269 triggerlist = NULL;
270 }
271
272 return triggerlist;
273}
274
275/**
276 * Convert a "natural" string representation of a size value to uint64_t.
277 *
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.
280 *
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.
284 *
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.
287 *
288 * @return SR_OK upon success, SR_ERR upon errors.
289 */
290SR_API int sr_parse_sizestring(const char *sizestring, uint64_t *size)
291{
292 int multiplier, done;
293 char *s;
294
295 *size = strtoull(sizestring, &s, 10);
296 multiplier = 0;
297 done = FALSE;
298 while (s && *s && multiplier == 0 && !done) {
299 switch (*s) {
300 case ' ':
301 break;
302 case 'k':
303 case 'K':
304 multiplier = SR_KHZ(1);
305 break;
306 case 'm':
307 case 'M':
308 multiplier = SR_MHZ(1);
309 break;
310 case 'g':
311 case 'G':
312 multiplier = SR_GHZ(1);
313 break;
314 default:
315 done = TRUE;
316 s--;
317 }
318 s++;
319 }
320 if (multiplier > 0)
321 *size *= multiplier;
322
323 if (*s && strcasecmp(s, "Hz"))
324 return SR_ERR;
325
326 return SR_OK;
327}
328
329/**
330 * Convert a "natural" string representation of a time value to an
331 * uint64_t value in milliseconds.
332 *
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.
335 *
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.
339 *
340 * @param timestring A string containing a (decimal) time value.
341 * @return The string's time value as uint64_t, in milliseconds.
342 *
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.
346 */
347SR_API uint64_t sr_parse_timestring(const char *timestring)
348{
349 uint64_t time_msec;
350 char *s;
351
352 /* TODO: Error handling, logging. */
353
354 time_msec = strtoull(timestring, &s, 10);
355 if (time_msec == 0 && s == timestring)
356 return 0;
357
358 if (s && *s) {
359 while (*s == ' ')
360 s++;
361 if (!strcmp(s, "s"))
362 time_msec *= 1000;
363 else if (!strcmp(s, "ms"))
364 ; /* redundant */
365 else
366 return 0;
367 }
368
369 return time_msec;
370}
371
372SR_API gboolean sr_parse_boolstring(const char *boolstr)
373{
374 if (!boolstr)
375 return FALSE;
376
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))
381 return TRUE;
382
383 return FALSE;
384}
385
386SR_API int sr_parse_period(const char *periodstr, struct sr_rational *r)
387{
388 char *s;
389
390 r->p = strtoull(periodstr, &s, 10);
391 if (r->p == 0 && s == periodstr)
392 /* No digits found. */
393 return SR_ERR_ARG;
394
395 if (s && *s) {
396 while (*s == ' ')
397 s++;
398 if (!strcmp(s, "ns"))
399 r->q = 1000000000L;
400 else if (!strcmp(s, "us"))
401 r->q = 1000000;
402 else if (!strcmp(s, "ms"))
403 r->q = 1000;
404 else if (!strcmp(s, "s"))
405 r->q = 1;
406 else
407 /* Must have a time suffix. */
408 return SR_ERR_ARG;
409 }
410
411 return SR_OK;
412}
413
414
415SR_API int sr_parse_voltage(const char *voltstr, struct sr_rational *r)
416{
417 char *s;
418
419 r->p = strtoull(voltstr, &s, 10);
420 if (r->p == 0 && s == voltstr)
421 /* No digits found. */
422 return SR_ERR_ARG;
423
424 if (s && *s) {
425 while (*s == ' ')
426 s++;
427 if (!strcasecmp(s, "mv"))
428 r->q = 1000L;
429 else if (!strcasecmp(s, "v"))
430 r->q = 1;
431 else
432 /* Must have a base suffix. */
433 return SR_ERR_ARG;
434 }
435
436 return SR_OK;
437}
438
439/** @} */