]>
Commit | Line | Data |
---|---|---|
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> | |
9e4f8cf9 | 24 | #include <errno.h> |
45c59c8b BV |
25 | #include "libsigrok.h" |
26 | #include "libsigrok-internal.h" | |
25e7d9b1 | 27 | |
2ad1deb8 | 28 | /** @cond PRIVATE */ |
3544f848 | 29 | #define LOG_PREFIX "strutil" |
2ad1deb8 | 30 | /** @endcond */ |
a885ce3e | 31 | |
393fb9cb UH |
32 | /** |
33 | * @file | |
34 | * | |
35 | * Helper functions for handling or converting libsigrok-related strings. | |
36 | */ | |
37 | ||
7b870c38 UH |
38 | /** |
39 | * @defgroup grp_strutil String utilities | |
40 | * | |
41 | * Helper functions for handling or converting libsigrok-related strings. | |
42 | * | |
43 | * @{ | |
44 | */ | |
45 | ||
9e4f8cf9 | 46 | /** |
df823ac4 UH |
47 | * @private |
48 | * | |
9e4f8cf9 DJ |
49 | * Convert a string representation of a numeric value to a long integer. The |
50 | * conversion is strict and will fail if the complete string does not represent | |
51 | * a valid long integer. The function sets errno according to the details of the | |
52 | * failure. | |
53 | * | |
54 | * @param str The string representation to convert. | |
55 | * @param ret Pointer to long where the result of the conversion will be stored. | |
56 | * | |
57 | * @return SR_OK if conversion is successful, otherwise SR_ERR. | |
58 | * | |
59 | * @since 0.3.0 | |
60 | */ | |
8d558c7a | 61 | SR_PRIV int sr_atol(const char *str, long *ret) |
9e4f8cf9 DJ |
62 | { |
63 | long tmp; | |
64 | char *endptr = NULL; | |
65 | ||
66 | errno = 0; | |
67 | tmp = strtol(str, &endptr, 0); | |
68 | ||
69 | if (!endptr || *endptr || errno) { | |
70 | if (!errno) | |
71 | errno = EINVAL; | |
72 | return SR_ERR; | |
73 | } | |
74 | ||
75 | *ret = tmp; | |
76 | return SR_OK; | |
77 | } | |
78 | ||
79 | /** | |
df823ac4 UH |
80 | * @private |
81 | * | |
9e4f8cf9 DJ |
82 | * Convert a string representation of a numeric value to an integer. The |
83 | * conversion is strict and will fail if the complete string does not represent | |
84 | * a valid integer. The function sets errno according to the details of the | |
85 | * failure. | |
86 | * | |
87 | * @param str The string representation to convert. | |
88 | * @param ret Pointer to int where the result of the conversion will be stored. | |
89 | * | |
90 | * @return SR_OK if conversion is successful, otherwise SR_ERR. | |
91 | * | |
92 | * @since 0.3.0 | |
93 | */ | |
8d558c7a | 94 | SR_PRIV int sr_atoi(const char *str, int *ret) |
9e4f8cf9 DJ |
95 | { |
96 | long tmp; | |
97 | ||
98 | if (sr_atol(str, &tmp) != SR_OK) | |
99 | return SR_ERR; | |
100 | ||
101 | if ((int) tmp != tmp) { | |
102 | errno = ERANGE; | |
103 | return SR_ERR; | |
104 | } | |
105 | ||
106 | *ret = (int) tmp; | |
107 | return SR_OK; | |
108 | } | |
109 | ||
110 | /** | |
df823ac4 UH |
111 | * @private |
112 | * | |
9e4f8cf9 DJ |
113 | * Convert a string representation of a numeric value to a double. The |
114 | * conversion is strict and will fail if the complete string does not represent | |
115 | * a valid double. The function sets errno according to the details of the | |
116 | * failure. | |
117 | * | |
118 | * @param str The string representation to convert. | |
119 | * @param ret Pointer to double where the result of the conversion will be stored. | |
120 | * | |
121 | * @return SR_OK if conversion is successful, otherwise SR_ERR. | |
122 | * | |
123 | * @since 0.3.0 | |
124 | */ | |
8d558c7a | 125 | SR_PRIV int sr_atod(const char *str, double *ret) |
9e4f8cf9 DJ |
126 | { |
127 | double tmp; | |
128 | char *endptr = NULL; | |
129 | ||
130 | errno = 0; | |
131 | tmp = strtof(str, &endptr); | |
132 | ||
133 | if (!endptr || *endptr || errno) { | |
134 | if (!errno) | |
135 | errno = EINVAL; | |
136 | return SR_ERR; | |
137 | } | |
138 | ||
139 | *ret = tmp; | |
140 | return SR_OK; | |
141 | } | |
142 | ||
143 | /** | |
df823ac4 UH |
144 | * @private |
145 | * | |
9e4f8cf9 DJ |
146 | * Convert a string representation of a numeric value to a float. The |
147 | * conversion is strict and will fail if the complete string does not represent | |
148 | * a valid float. The function sets errno according to the details of the | |
149 | * failure. | |
150 | * | |
151 | * @param str The string representation to convert. | |
152 | * @param ret Pointer to float where the result of the conversion will be stored. | |
153 | * | |
154 | * @return SR_OK if conversion is successful, otherwise SR_ERR. | |
155 | * | |
156 | * @since 0.3.0 | |
157 | */ | |
8d558c7a | 158 | SR_PRIV int sr_atof(const char *str, float *ret) |
9e4f8cf9 DJ |
159 | { |
160 | double tmp; | |
161 | ||
162 | if (sr_atod(str, &tmp) != SR_OK) | |
163 | return SR_ERR; | |
164 | ||
165 | if ((float) tmp != tmp) { | |
166 | errno = ERANGE; | |
167 | return SR_ERR; | |
168 | } | |
169 | ||
170 | *ret = (float) tmp; | |
171 | return SR_OK; | |
172 | } | |
173 | ||
9806c2d5 DJ |
174 | /** |
175 | * @private | |
176 | * | |
177 | * Convert a string representation of a numeric value to a float. The | |
178 | * conversion is strict and will fail if the complete string does not represent | |
179 | * a valid float. The function sets errno according to the details of the | |
180 | * failure. This version ignores the locale. | |
181 | * | |
182 | * @param str The string representation to convert. | |
183 | * @param ret Pointer to float where the result of the conversion will be stored. | |
184 | * | |
185 | * @return SR_OK if conversion is successful, otherwise SR_ERR. | |
186 | * | |
187 | * @since 0.3.0 | |
188 | */ | |
189 | SR_PRIV int sr_atof_ascii(const char *str, float *ret) | |
190 | { | |
191 | double tmp; | |
192 | char *endptr = NULL; | |
193 | ||
194 | errno = 0; | |
195 | tmp = g_ascii_strtod(str, &endptr); | |
196 | ||
197 | if (!endptr || *endptr || errno) { | |
198 | if (!errno) | |
199 | errno = EINVAL; | |
200 | return SR_ERR; | |
201 | } | |
202 | ||
203 | /* FIXME This fails unexpectedly. Some other method to safel downcast | |
204 | * needs to be found. Checking against FLT_MAX doesn't work as well. */ | |
205 | /* | |
206 | if ((float) tmp != tmp) { | |
207 | errno = ERANGE; | |
208 | sr_dbg("ERANGEEEE %e != %e", (float) tmp, tmp); | |
209 | return SR_ERR; | |
210 | } | |
211 | */ | |
212 | ||
213 | *ret = (float) tmp; | |
214 | return SR_OK; | |
215 | } | |
216 | ||
25e7d9b1 | 217 | /** |
b07b42f3 UH |
218 | * Convert a numeric value value to its "natural" string representation |
219 | * in SI units. | |
25e7d9b1 | 220 | * |
4cc9aea1 JH |
221 | * E.g. a value of 3000000, with units set to "W", would be converted |
222 | * to "3 MW", 20000 to "20 kW", 31500 would become "31.5 kW". | |
25e7d9b1 | 223 | * |
4cc9aea1 JH |
224 | * @param x The value to convert. |
225 | * @param unit The unit to append to the string, or NULL if the string | |
226 | * has no units. | |
44dae539 | 227 | * |
133a37bf UH |
228 | * @return A g_try_malloc()ed string representation of the samplerate value, |
229 | * or NULL upon errors. The caller is responsible to g_free() the | |
230 | * memory. | |
47117241 UH |
231 | * |
232 | * @since 0.2.0 | |
25e7d9b1 | 233 | */ |
4cc9aea1 | 234 | SR_API char *sr_si_string_u64(uint64_t x, const char *unit) |
25e7d9b1 | 235 | { |
094e6b81 PS |
236 | uint8_t i; |
237 | uint64_t quot, divisor[] = { | |
b07b42f3 UH |
238 | SR_HZ(1), SR_KHZ(1), SR_MHZ(1), SR_GHZ(1), |
239 | SR_GHZ(1000), SR_GHZ(1000 * 1000), SR_GHZ(1000 * 1000 * 1000), | |
094e6b81 PS |
240 | }; |
241 | const char *p, prefix[] = "\0kMGTPE"; | |
69d83be9 | 242 | char fmt[16], fract[20] = "", *f; |
094e6b81 | 243 | |
a885ce3e | 244 | if (unit == NULL) |
4cc9aea1 | 245 | unit = ""; |
25e7d9b1 | 246 | |
094e6b81 PS |
247 | for (i = 0; (quot = x / divisor[i]) >= 1000; i++); |
248 | ||
249 | if (i) { | |
69d83be9 | 250 | sprintf(fmt, ".%%0%d"PRIu64, i * 3); |
094e6b81 PS |
251 | f = fract + sprintf(fract, fmt, x % divisor[i]) - 1; |
252 | ||
253 | while (f >= fract && strchr("0.", *f)) | |
254 | *f-- = 0; | |
133a37bf | 255 | } |
25e7d9b1 | 256 | |
094e6b81 PS |
257 | p = prefix + i; |
258 | ||
259 | return g_strdup_printf("%" PRIu64 "%s %.1s%s", quot, fract, p, unit); | |
4cc9aea1 | 260 | } |
25e7d9b1 | 261 | |
4cc9aea1 JH |
262 | /** |
263 | * Convert a numeric samplerate value to its "natural" string representation. | |
264 | * | |
265 | * E.g. a value of 3000000 would be converted to "3 MHz", 20000 to "20 kHz", | |
266 | * 31500 would become "31.5 kHz". | |
267 | * | |
268 | * @param samplerate The samplerate in Hz. | |
269 | * | |
270 | * @return A g_try_malloc()ed string representation of the samplerate value, | |
271 | * or NULL upon errors. The caller is responsible to g_free() the | |
272 | * memory. | |
47117241 UH |
273 | * |
274 | * @since 0.1.0 | |
4cc9aea1 JH |
275 | */ |
276 | SR_API char *sr_samplerate_string(uint64_t samplerate) | |
277 | { | |
278 | return sr_si_string_u64(samplerate, "Hz"); | |
25e7d9b1 | 279 | } |
2a3f9541 | 280 | |
2a3f9541 | 281 | /** |
dfcc0bf9 | 282 | * Convert a numeric frequency value to the "natural" string representation |
2a3f9541 BV |
283 | * of its period. |
284 | * | |
2507648e | 285 | * E.g. a value of 3000000 would be converted to "3 us", 20000 to "50 ms". |
2a3f9541 BV |
286 | * |
287 | * @param frequency The frequency in Hz. | |
44dae539 | 288 | * |
133a37bf UH |
289 | * @return A g_try_malloc()ed string representation of the frequency value, |
290 | * or NULL upon errors. The caller is responsible to g_free() the | |
291 | * memory. | |
47117241 UH |
292 | * |
293 | * @since 0.1.0 | |
2a3f9541 | 294 | */ |
1a081ca6 | 295 | SR_API char *sr_period_string(uint64_t frequency) |
2a3f9541 BV |
296 | { |
297 | char *o; | |
298 | int r; | |
299 | ||
133a37bf UH |
300 | /* Allocate enough for a uint64_t as string + " ms". */ |
301 | if (!(o = g_try_malloc0(30 + 1))) { | |
a885ce3e | 302 | sr_err("%s: o malloc failed", __func__); |
2a3f9541 | 303 | return NULL; |
133a37bf | 304 | } |
2a3f9541 | 305 | |
59df0c77 | 306 | if (frequency >= SR_GHZ(1)) |
2a3f9541 | 307 | r = snprintf(o, 30, "%" PRIu64 " ns", frequency / 1000000000); |
59df0c77 | 308 | else if (frequency >= SR_MHZ(1)) |
2507648e | 309 | r = snprintf(o, 30, "%" PRIu64 " us", frequency / 1000000); |
59df0c77 | 310 | else if (frequency >= SR_KHZ(1)) |
2a3f9541 BV |
311 | r = snprintf(o, 30, "%" PRIu64 " ms", frequency / 1000); |
312 | else | |
313 | r = snprintf(o, 30, "%" PRIu64 " s", frequency); | |
314 | ||
315 | if (r < 0) { | |
316 | /* Something went wrong... */ | |
133a37bf | 317 | g_free(o); |
2a3f9541 BV |
318 | return NULL; |
319 | } | |
320 | ||
321 | return o; | |
322 | } | |
40f5ddac | 323 | |
79afc8ca | 324 | /** |
e0e15067 BV |
325 | * Convert a numeric voltage value to the "natural" string representation |
326 | * of its voltage value. The voltage is specified as a rational number's | |
327 | * numerator and denominator. | |
79afc8ca BV |
328 | * |
329 | * E.g. a value of 300000 would be converted to "300mV", 2 to "2V". | |
330 | * | |
e0e15067 BV |
331 | * @param v_p The voltage numerator. |
332 | * @param v_q The voltage denominator. | |
79afc8ca BV |
333 | * |
334 | * @return A g_try_malloc()ed string representation of the voltage value, | |
335 | * or NULL upon errors. The caller is responsible to g_free() the | |
336 | * memory. | |
47117241 UH |
337 | * |
338 | * @since 0.2.0 | |
79afc8ca | 339 | */ |
e0e15067 | 340 | SR_API char *sr_voltage_string(uint64_t v_p, uint64_t v_q) |
79afc8ca | 341 | { |
79afc8ca | 342 | int r; |
d5a669a9 | 343 | char *o; |
79afc8ca BV |
344 | |
345 | if (!(o = g_try_malloc0(30 + 1))) { | |
a885ce3e | 346 | sr_err("%s: o malloc failed", __func__); |
79afc8ca BV |
347 | return NULL; |
348 | } | |
349 | ||
e0e15067 BV |
350 | if (v_q == 1000) |
351 | r = snprintf(o, 30, "%" PRIu64 "mV", v_p); | |
352 | else if (v_q == 1) | |
353 | r = snprintf(o, 30, "%" PRIu64 "V", v_p); | |
79afc8ca | 354 | else |
e0e15067 | 355 | r = snprintf(o, 30, "%gV", (float)v_p / (float)v_q); |
79afc8ca BV |
356 | |
357 | if (r < 0) { | |
358 | /* Something went wrong... */ | |
359 | g_free(o); | |
360 | return NULL; | |
361 | } | |
362 | ||
363 | return o; | |
364 | } | |
365 | ||
dfcc0bf9 | 366 | /** |
bf978d35 | 367 | * Parse a trigger specification string. |
dfcc0bf9 | 368 | * |
9c5332d2 UH |
369 | * @param sdi The device instance for which the trigger specification is |
370 | * intended. Must not be NULL. Also, sdi->driver and | |
371 | * sdi->driver->info_get must not be NULL. | |
bf978d35 | 372 | * @param triggerstring The string containing the trigger specification for |
ba7dd8bb | 373 | * one or more channels of this device. Entries for multiple channels are |
bf978d35 | 374 | * comma-separated. Triggers are specified in the form key=value, |
ba7dd8bb | 375 | * where the key is a channel number (or channel name) and the value is |
bf978d35 UH |
376 | * the requested trigger type. Valid trigger types currently |
377 | * include 'r' (rising edge), 'f' (falling edge), 'c' (any pin value | |
378 | * change), '0' (low value), or '1' (high value). | |
379 | * Example: "1=r,sck=f,miso=0,7=c" | |
44dae539 | 380 | * |
bf978d35 UH |
381 | * @return Pointer to a list of trigger types (strings), or NULL upon errors. |
382 | * The pointer list (if non-NULL) has as many entries as the | |
ba7dd8bb | 383 | * respective device has channels (all physically available channels, |
bf978d35 UH |
384 | * not just enabled ones). Entries of the list which don't have |
385 | * a trigger value set in 'triggerstring' are NULL, the other entries | |
386 | * contain the respective trigger type which is requested for the | |
ba7dd8bb | 387 | * respective channel (e.g. "r", "c", and so on). |
47117241 UH |
388 | * |
389 | * @since 0.2.0 | |
dfcc0bf9 | 390 | */ |
92ae7984 | 391 | SR_API char **sr_parse_triggerstring(const struct sr_dev_inst *sdi, |
1a081ca6 | 392 | const char *triggerstring) |
40f5ddac BV |
393 | { |
394 | GSList *l; | |
003595ac | 395 | GVariant *gvar; |
ba7dd8bb UH |
396 | struct sr_channel *ch; |
397 | int max_channels, channelnum, i; | |
b7f578be JH |
398 | char **tokens, **triggerlist, *trigger, *tc; |
399 | const char *trigger_types; | |
40f5ddac BV |
400 | gboolean error; |
401 | ||
ba7dd8bb | 402 | max_channels = g_slist_length(sdi->channels); |
40f5ddac | 403 | error = FALSE; |
b53738ba | 404 | |
ba7dd8bb | 405 | if (!(triggerlist = g_try_malloc0(max_channels * sizeof(char *)))) { |
a885ce3e | 406 | sr_err("%s: triggerlist malloc failed", __func__); |
b53738ba UH |
407 | return NULL; |
408 | } | |
409 | ||
8f996b89 ML |
410 | if (sdi->driver->config_list(SR_CONF_TRIGGER_TYPE, |
411 | &gvar, sdi, NULL) != SR_OK) { | |
a885ce3e | 412 | sr_err("%s: Device doesn't support any triggers.", __func__); |
40f5ddac | 413 | return NULL; |
bf978d35 | 414 | } |
003595ac | 415 | trigger_types = g_variant_get_string(gvar, NULL); |
40f5ddac | 416 | |
ba7dd8bb | 417 | tokens = g_strsplit(triggerstring, ",", max_channels); |
40f5ddac | 418 | for (i = 0; tokens[i]; i++) { |
ba7dd8bb UH |
419 | channelnum = -1; |
420 | for (l = sdi->channels; l; l = l->next) { | |
421 | ch = (struct sr_channel *)l->data; | |
422 | if (ch->enabled | |
423 | && !strncmp(ch->name, tokens[i], | |
424 | strlen(ch->name))) { | |
425 | channelnum = ch->index; | |
17ff1124 | 426 | break; |
40f5ddac | 427 | } |
40f5ddac BV |
428 | } |
429 | ||
ba7dd8bb UH |
430 | if (channelnum < 0 || channelnum >= max_channels) { |
431 | sr_err("Invalid channel."); | |
40f5ddac BV |
432 | error = TRUE; |
433 | break; | |
434 | } | |
435 | ||
436 | if ((trigger = strchr(tokens[i], '='))) { | |
437 | for (tc = ++trigger; *tc; tc++) { | |
438 | if (strchr(trigger_types, *tc) == NULL) { | |
a885ce3e | 439 | sr_err("Unsupported trigger " |
0aeb0ccd | 440 | "type '%c'.", *tc); |
40f5ddac BV |
441 | error = TRUE; |
442 | break; | |
443 | } | |
444 | } | |
445 | if (!error) | |
ba7dd8bb | 446 | triggerlist[channelnum] = g_strdup(trigger); |
40f5ddac BV |
447 | } |
448 | } | |
449 | g_strfreev(tokens); | |
003595ac | 450 | g_variant_unref(gvar); |
40f5ddac BV |
451 | |
452 | if (error) { | |
ba7dd8bb | 453 | for (i = 0; i < max_channels; i++) |
66410a86 | 454 | g_free(triggerlist[i]); |
40f5ddac BV |
455 | g_free(triggerlist); |
456 | triggerlist = NULL; | |
457 | } | |
458 | ||
459 | return triggerlist; | |
460 | } | |
461 | ||
dfcc0bf9 UH |
462 | /** |
463 | * Convert a "natural" string representation of a size value to uint64_t. | |
464 | * | |
465 | * E.g. a value of "3k" or "3 K" would be converted to 3000, a value | |
466 | * of "15M" would be converted to 15000000. | |
467 | * | |
468 | * Value representations other than decimal (such as hex or octal) are not | |
469 | * supported. Only 'k' (kilo), 'm' (mega), 'g' (giga) suffixes are supported. | |
470 | * Spaces (but not other whitespace) between value and suffix are allowed. | |
471 | * | |
472 | * @param sizestring A string containing a (decimal) size value. | |
f64c1414 | 473 | * @param size Pointer to uint64_t which will contain the string's size value. |
dfcc0bf9 | 474 | * |
44dae539 | 475 | * @return SR_OK upon success, SR_ERR upon errors. |
47117241 UH |
476 | * |
477 | * @since 0.1.0 | |
dfcc0bf9 | 478 | */ |
1a081ca6 | 479 | SR_API int sr_parse_sizestring(const char *sizestring, uint64_t *size) |
40f5ddac | 480 | { |
f64c1414 | 481 | int multiplier, done; |
580f3099 | 482 | double frac_part; |
40f5ddac BV |
483 | char *s; |
484 | ||
f64c1414 | 485 | *size = strtoull(sizestring, &s, 10); |
40f5ddac | 486 | multiplier = 0; |
580f3099 | 487 | frac_part = 0; |
f64c1414 BV |
488 | done = FALSE; |
489 | while (s && *s && multiplier == 0 && !done) { | |
40f5ddac BV |
490 | switch (*s) { |
491 | case ' ': | |
492 | break; | |
580f3099 DJ |
493 | case '.': |
494 | frac_part = g_ascii_strtod(s, &s); | |
495 | break; | |
40f5ddac BV |
496 | case 'k': |
497 | case 'K': | |
59df0c77 | 498 | multiplier = SR_KHZ(1); |
40f5ddac BV |
499 | break; |
500 | case 'm': | |
501 | case 'M': | |
59df0c77 | 502 | multiplier = SR_MHZ(1); |
40f5ddac BV |
503 | break; |
504 | case 'g': | |
505 | case 'G': | |
59df0c77 | 506 | multiplier = SR_GHZ(1); |
40f5ddac BV |
507 | break; |
508 | default: | |
f64c1414 BV |
509 | done = TRUE; |
510 | s--; | |
40f5ddac BV |
511 | } |
512 | s++; | |
513 | } | |
580f3099 | 514 | if (multiplier > 0) { |
f64c1414 | 515 | *size *= multiplier; |
580f3099 DJ |
516 | *size += frac_part * multiplier; |
517 | } else | |
518 | *size += frac_part; | |
40f5ddac | 519 | |
f64c1414 BV |
520 | if (*s && strcasecmp(s, "Hz")) |
521 | return SR_ERR; | |
522 | ||
523 | return SR_OK; | |
40f5ddac BV |
524 | } |
525 | ||
dfcc0bf9 UH |
526 | /** |
527 | * Convert a "natural" string representation of a time value to an | |
528 | * uint64_t value in milliseconds. | |
529 | * | |
530 | * E.g. a value of "3s" or "3 s" would be converted to 3000, a value | |
531 | * of "15ms" would be converted to 15. | |
532 | * | |
533 | * Value representations other than decimal (such as hex or octal) are not | |
534 | * supported. Only lower-case "s" and "ms" time suffixes are supported. | |
535 | * Spaces (but not other whitespace) between value and suffix are allowed. | |
536 | * | |
537 | * @param timestring A string containing a (decimal) time value. | |
538 | * @return The string's time value as uint64_t, in milliseconds. | |
539 | * | |
6b2d8d3e UH |
540 | * @todo Add support for "m" (minutes) and others. |
541 | * @todo Add support for picoseconds? | |
542 | * @todo Allow both lower-case and upper-case? If no, document it. | |
47117241 UH |
543 | * |
544 | * @since 0.1.0 | |
dfcc0bf9 | 545 | */ |
1a081ca6 | 546 | SR_API uint64_t sr_parse_timestring(const char *timestring) |
40f5ddac BV |
547 | { |
548 | uint64_t time_msec; | |
549 | char *s; | |
550 | ||
6b2d8d3e UH |
551 | /* TODO: Error handling, logging. */ |
552 | ||
40f5ddac BV |
553 | time_msec = strtoull(timestring, &s, 10); |
554 | if (time_msec == 0 && s == timestring) | |
555 | return 0; | |
556 | ||
557 | if (s && *s) { | |
558 | while (*s == ' ') | |
559 | s++; | |
560 | if (!strcmp(s, "s")) | |
561 | time_msec *= 1000; | |
562 | else if (!strcmp(s, "ms")) | |
563 | ; /* redundant */ | |
564 | else | |
565 | return 0; | |
566 | } | |
567 | ||
568 | return time_msec; | |
569 | } | |
4d436e71 | 570 | |
47117241 | 571 | /** @since 0.1.0 */ |
1a081ca6 | 572 | SR_API gboolean sr_parse_boolstring(const char *boolstr) |
4d436e71 GM |
573 | { |
574 | if (!boolstr) | |
575 | return FALSE; | |
576 | ||
993526f8 BV |
577 | if (!g_ascii_strncasecmp(boolstr, "true", 4) || |
578 | !g_ascii_strncasecmp(boolstr, "yes", 3) || | |
579 | !g_ascii_strncasecmp(boolstr, "on", 2) || | |
580 | !g_ascii_strncasecmp(boolstr, "1", 1)) | |
4d436e71 GM |
581 | return TRUE; |
582 | ||
583 | return FALSE; | |
584 | } | |
76f4c610 | 585 | |
47117241 | 586 | /** @since 0.2.0 */ |
76e107d6 | 587 | SR_API int sr_parse_period(const char *periodstr, uint64_t *p, uint64_t *q) |
76f4c610 BV |
588 | { |
589 | char *s; | |
590 | ||
76e107d6 BV |
591 | *p = strtoull(periodstr, &s, 10); |
592 | if (*p == 0 && s == periodstr) | |
76f4c610 BV |
593 | /* No digits found. */ |
594 | return SR_ERR_ARG; | |
595 | ||
596 | if (s && *s) { | |
597 | while (*s == ' ') | |
598 | s++; | |
8c012adb | 599 | if (!strcmp(s, "fs")) |
76e107d6 | 600 | *q = 1000000000000000ULL; |
8c012adb | 601 | else if (!strcmp(s, "ps")) |
76e107d6 | 602 | *q = 1000000000000ULL; |
8c012adb | 603 | else if (!strcmp(s, "ns")) |
76e107d6 | 604 | *q = 1000000000ULL; |
76f4c610 | 605 | else if (!strcmp(s, "us")) |
76e107d6 | 606 | *q = 1000000; |
76f4c610 | 607 | else if (!strcmp(s, "ms")) |
76e107d6 | 608 | *q = 1000; |
76f4c610 | 609 | else if (!strcmp(s, "s")) |
76e107d6 | 610 | *q = 1; |
76f4c610 BV |
611 | else |
612 | /* Must have a time suffix. */ | |
613 | return SR_ERR_ARG; | |
614 | } | |
615 | ||
616 | return SR_OK; | |
617 | } | |
618 | ||
47117241 | 619 | /** @since 0.2.0 */ |
76e107d6 | 620 | SR_API int sr_parse_voltage(const char *voltstr, uint64_t *p, uint64_t *q) |
79afc8ca BV |
621 | { |
622 | char *s; | |
623 | ||
76e107d6 BV |
624 | *p = strtoull(voltstr, &s, 10); |
625 | if (*p == 0 && s == voltstr) | |
79afc8ca BV |
626 | /* No digits found. */ |
627 | return SR_ERR_ARG; | |
628 | ||
629 | if (s && *s) { | |
630 | while (*s == ' ') | |
631 | s++; | |
632 | if (!strcasecmp(s, "mv")) | |
76e107d6 | 633 | *q = 1000L; |
79afc8ca | 634 | else if (!strcasecmp(s, "v")) |
76e107d6 | 635 | *q = 1; |
79afc8ca BV |
636 | else |
637 | /* Must have a base suffix. */ | |
638 | return SR_ERR_ARG; | |
639 | } | |
640 | ||
641 | return SR_OK; | |
642 | } | |
643 | ||
7b870c38 | 644 | /** @} */ |