]>
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 | |
2ea1fdf1 | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
25e7d9b1 UH |
18 | */ |
19 | ||
6ec6c43b | 20 | #include <config.h> |
25e7d9b1 UH |
21 | #include <stdint.h> |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
ba464a12 | 24 | #include <strings.h> |
9e4f8cf9 | 25 | #include <errno.h> |
1f488f50 | 26 | #include <stdbool.h> |
c1aae900 | 27 | #include <libsigrok/libsigrok.h> |
45c59c8b | 28 | #include "libsigrok-internal.h" |
25e7d9b1 | 29 | |
2ad1deb8 | 30 | /** @cond PRIVATE */ |
3544f848 | 31 | #define LOG_PREFIX "strutil" |
2ad1deb8 | 32 | /** @endcond */ |
a885ce3e | 33 | |
393fb9cb UH |
34 | /** |
35 | * @file | |
36 | * | |
37 | * Helper functions for handling or converting libsigrok-related strings. | |
38 | */ | |
39 | ||
7b870c38 UH |
40 | /** |
41 | * @defgroup grp_strutil String utilities | |
42 | * | |
43 | * Helper functions for handling or converting libsigrok-related strings. | |
44 | * | |
45 | * @{ | |
46 | */ | |
47 | ||
9e4f8cf9 | 48 | /** |
df823ac4 UH |
49 | * @private |
50 | * | |
1ba4a1cf | 51 | * Convert a string representation of a numeric value (base 10) to a long integer. The |
9e4f8cf9 DJ |
52 | * conversion is strict and will fail if the complete string does not represent |
53 | * a valid long integer. The function sets errno according to the details of the | |
54 | * failure. | |
55 | * | |
56 | * @param str The string representation to convert. | |
57 | * @param ret Pointer to long where the result of the conversion will be stored. | |
58 | * | |
1ba4a1cf MH |
59 | * @retval SR_OK Conversion successful. |
60 | * @retval SR_ERR Failure. | |
9e4f8cf9 | 61 | */ |
8d558c7a | 62 | SR_PRIV int sr_atol(const char *str, long *ret) |
9e4f8cf9 DJ |
63 | { |
64 | long tmp; | |
65 | char *endptr = NULL; | |
66 | ||
67 | errno = 0; | |
1ba4a1cf | 68 | tmp = strtol(str, &endptr, 10); |
9e4f8cf9 DJ |
69 | |
70 | if (!endptr || *endptr || errno) { | |
71 | if (!errno) | |
72 | errno = EINVAL; | |
73 | return SR_ERR; | |
74 | } | |
75 | ||
76 | *ret = tmp; | |
77 | return SR_OK; | |
78 | } | |
79 | ||
80 | /** | |
df823ac4 UH |
81 | * @private |
82 | * | |
1ba4a1cf | 83 | * Convert a string representation of a numeric value (base 10) to an integer. The |
9e4f8cf9 DJ |
84 | * conversion is strict and will fail if the complete string does not represent |
85 | * a valid integer. The function sets errno according to the details of the | |
86 | * failure. | |
87 | * | |
88 | * @param str The string representation to convert. | |
89 | * @param ret Pointer to int where the result of the conversion will be stored. | |
90 | * | |
1ba4a1cf MH |
91 | * @retval SR_OK Conversion successful. |
92 | * @retval SR_ERR Failure. | |
9e4f8cf9 | 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 | * | |
1ba4a1cf MH |
121 | * @retval SR_OK Conversion successful. |
122 | * @retval SR_ERR Failure. | |
9e4f8cf9 | 123 | */ |
8d558c7a | 124 | SR_PRIV int sr_atod(const char *str, double *ret) |
9e4f8cf9 DJ |
125 | { |
126 | double tmp; | |
127 | char *endptr = NULL; | |
128 | ||
129 | errno = 0; | |
130 | tmp = strtof(str, &endptr); | |
131 | ||
132 | if (!endptr || *endptr || errno) { | |
133 | if (!errno) | |
134 | errno = EINVAL; | |
135 | return SR_ERR; | |
136 | } | |
137 | ||
138 | *ret = tmp; | |
139 | return SR_OK; | |
140 | } | |
141 | ||
142 | /** | |
df823ac4 UH |
143 | * @private |
144 | * | |
9e4f8cf9 DJ |
145 | * Convert a string representation of a numeric value to a float. The |
146 | * conversion is strict and will fail if the complete string does not represent | |
147 | * a valid float. The function sets errno according to the details of the | |
148 | * failure. | |
149 | * | |
150 | * @param str The string representation to convert. | |
151 | * @param ret Pointer to float where the result of the conversion will be stored. | |
152 | * | |
1ba4a1cf MH |
153 | * @retval SR_OK Conversion successful. |
154 | * @retval SR_ERR Failure. | |
9e4f8cf9 | 155 | */ |
8d558c7a | 156 | SR_PRIV int sr_atof(const char *str, float *ret) |
9e4f8cf9 DJ |
157 | { |
158 | double tmp; | |
159 | ||
160 | if (sr_atod(str, &tmp) != SR_OK) | |
161 | return SR_ERR; | |
162 | ||
163 | if ((float) tmp != tmp) { | |
164 | errno = ERANGE; | |
165 | return SR_ERR; | |
166 | } | |
167 | ||
168 | *ret = (float) tmp; | |
169 | return SR_OK; | |
170 | } | |
171 | ||
4f0463a0 FS |
172 | /** |
173 | * @private | |
174 | * | |
175 | * Convert a string representation of a numeric value to a double. The | |
176 | * conversion is strict and will fail if the complete string does not represent | |
177 | * a valid double. The function sets errno according to the details of the | |
178 | * failure. This version ignores the locale. | |
179 | * | |
180 | * @param str The string representation to convert. | |
181 | * @param ret Pointer to double where the result of the conversion will be stored. | |
182 | * | |
183 | * @retval SR_OK Conversion successful. | |
184 | * @retval SR_ERR Failure. | |
185 | */ | |
186 | SR_PRIV int sr_atod_ascii(const char *str, double *ret) | |
187 | { | |
188 | double tmp; | |
189 | char *endptr = NULL; | |
190 | ||
191 | errno = 0; | |
192 | tmp = g_ascii_strtod(str, &endptr); | |
193 | ||
194 | if (!endptr || *endptr || errno) { | |
195 | if (!errno) | |
196 | errno = EINVAL; | |
197 | return SR_ERR; | |
198 | } | |
199 | ||
200 | *ret = tmp; | |
201 | return SR_OK; | |
202 | } | |
203 | ||
9806c2d5 DJ |
204 | /** |
205 | * @private | |
206 | * | |
207 | * Convert a string representation of a numeric value to a float. The | |
208 | * conversion is strict and will fail if the complete string does not represent | |
209 | * a valid float. The function sets errno according to the details of the | |
210 | * failure. This version ignores the locale. | |
211 | * | |
212 | * @param str The string representation to convert. | |
213 | * @param ret Pointer to float where the result of the conversion will be stored. | |
214 | * | |
1ba4a1cf MH |
215 | * @retval SR_OK Conversion successful. |
216 | * @retval SR_ERR Failure. | |
9806c2d5 DJ |
217 | */ |
218 | SR_PRIV int sr_atof_ascii(const char *str, float *ret) | |
219 | { | |
220 | double tmp; | |
221 | char *endptr = NULL; | |
222 | ||
223 | errno = 0; | |
224 | tmp = g_ascii_strtod(str, &endptr); | |
225 | ||
226 | if (!endptr || *endptr || errno) { | |
227 | if (!errno) | |
228 | errno = EINVAL; | |
229 | return SR_ERR; | |
230 | } | |
231 | ||
232 | /* FIXME This fails unexpectedly. Some other method to safel downcast | |
233 | * needs to be found. Checking against FLT_MAX doesn't work as well. */ | |
234 | /* | |
235 | if ((float) tmp != tmp) { | |
236 | errno = ERANGE; | |
237 | sr_dbg("ERANGEEEE %e != %e", (float) tmp, tmp); | |
238 | return SR_ERR; | |
239 | } | |
240 | */ | |
241 | ||
242 | *ret = (float) tmp; | |
243 | return SR_OK; | |
244 | } | |
245 | ||
5ec172d7 | 246 | /** |
5c436a3b UH |
247 | * Convert a string representation of a numeric value to a sr_rational. |
248 | * | |
249 | * The conversion is strict and will fail if the complete string does not | |
250 | * represent a valid number. The function sets errno according to the details | |
251 | * of the failure. This version ignores the locale. | |
5ec172d7 SB |
252 | * |
253 | * @param str The string representation to convert. | |
254 | * @param ret Pointer to sr_rational where the result of the conversion will be stored. | |
255 | * | |
256 | * @retval SR_OK Conversion successful. | |
257 | * @retval SR_ERR Failure. | |
258 | * | |
259 | * @since 0.5.0 | |
260 | */ | |
261 | SR_API int sr_parse_rational(const char *str, struct sr_rational *ret) | |
262 | { | |
263 | char *endptr = NULL; | |
264 | int64_t integral; | |
265 | int64_t fractional = 0; | |
266 | int64_t denominator = 1; | |
267 | int32_t fractional_len = 0; | |
268 | int32_t exponent = 0; | |
1f488f50 | 269 | bool is_negative = false; |
5ec172d7 SB |
270 | |
271 | errno = 0; | |
272 | integral = g_ascii_strtoll(str, &endptr, 10); | |
273 | ||
41c47f2c SB |
274 | if (str == endptr && (str[0] == '-' || str[0] == '+') && str[1] == '.') |
275 | endptr += 1; | |
276 | else if (errno) | |
5ec172d7 SB |
277 | return SR_ERR; |
278 | ||
41c47f2c | 279 | if (integral < 0 || str[0] == '-') |
1f488f50 | 280 | is_negative = true; |
41c47f2c | 281 | |
5ec172d7 SB |
282 | if (*endptr == '.') { |
283 | const char* start = endptr + 1; | |
284 | fractional = g_ascii_strtoll(start, &endptr, 10); | |
285 | if (errno) | |
286 | return SR_ERR; | |
287 | fractional_len = endptr - start; | |
288 | } | |
289 | ||
290 | if ((*endptr == 'E') || (*endptr == 'e')) { | |
291 | exponent = g_ascii_strtoll(endptr + 1, &endptr, 10); | |
292 | if (errno) | |
293 | return SR_ERR; | |
294 | } | |
295 | ||
296 | if (*endptr != '\0') | |
297 | return SR_ERR; | |
298 | ||
299 | for (int i = 0; i < fractional_len; i++) | |
300 | integral *= 10; | |
301 | exponent -= fractional_len; | |
302 | ||
41c47f2c | 303 | if (!is_negative) |
5ec172d7 SB |
304 | integral += fractional; |
305 | else | |
306 | integral -= fractional; | |
307 | ||
308 | while (exponent > 0) { | |
309 | integral *= 10; | |
310 | exponent--; | |
311 | } | |
312 | ||
313 | while (exponent < 0) { | |
314 | denominator *= 10; | |
315 | exponent++; | |
316 | } | |
317 | ||
318 | ret->p = integral; | |
319 | ret->q = denominator; | |
320 | ||
321 | return SR_OK; | |
322 | } | |
323 | ||
25e7d9b1 | 324 | /** |
b07b42f3 UH |
325 | * Convert a numeric value value to its "natural" string representation |
326 | * in SI units. | |
25e7d9b1 | 327 | * |
4cc9aea1 JH |
328 | * E.g. a value of 3000000, with units set to "W", would be converted |
329 | * to "3 MW", 20000 to "20 kW", 31500 would become "31.5 kW". | |
25e7d9b1 | 330 | * |
4cc9aea1 JH |
331 | * @param x The value to convert. |
332 | * @param unit The unit to append to the string, or NULL if the string | |
333 | * has no units. | |
44dae539 | 334 | * |
91219afc | 335 | * @return A newly allocated string representation of the samplerate value, |
133a37bf UH |
336 | * or NULL upon errors. The caller is responsible to g_free() the |
337 | * memory. | |
47117241 UH |
338 | * |
339 | * @since 0.2.0 | |
25e7d9b1 | 340 | */ |
4cc9aea1 | 341 | SR_API char *sr_si_string_u64(uint64_t x, const char *unit) |
25e7d9b1 | 342 | { |
094e6b81 PS |
343 | uint8_t i; |
344 | uint64_t quot, divisor[] = { | |
b07b42f3 UH |
345 | SR_HZ(1), SR_KHZ(1), SR_MHZ(1), SR_GHZ(1), |
346 | SR_GHZ(1000), SR_GHZ(1000 * 1000), SR_GHZ(1000 * 1000 * 1000), | |
094e6b81 PS |
347 | }; |
348 | const char *p, prefix[] = "\0kMGTPE"; | |
69d83be9 | 349 | char fmt[16], fract[20] = "", *f; |
094e6b81 | 350 | |
98fec29e | 351 | if (!unit) |
4cc9aea1 | 352 | unit = ""; |
25e7d9b1 | 353 | |
094e6b81 PS |
354 | for (i = 0; (quot = x / divisor[i]) >= 1000; i++); |
355 | ||
356 | if (i) { | |
69d83be9 | 357 | sprintf(fmt, ".%%0%d"PRIu64, i * 3); |
094e6b81 PS |
358 | f = fract + sprintf(fract, fmt, x % divisor[i]) - 1; |
359 | ||
360 | while (f >= fract && strchr("0.", *f)) | |
361 | *f-- = 0; | |
133a37bf | 362 | } |
25e7d9b1 | 363 | |
094e6b81 PS |
364 | p = prefix + i; |
365 | ||
366 | return g_strdup_printf("%" PRIu64 "%s %.1s%s", quot, fract, p, unit); | |
4cc9aea1 | 367 | } |
25e7d9b1 | 368 | |
4cc9aea1 JH |
369 | /** |
370 | * Convert a numeric samplerate value to its "natural" string representation. | |
371 | * | |
372 | * E.g. a value of 3000000 would be converted to "3 MHz", 20000 to "20 kHz", | |
373 | * 31500 would become "31.5 kHz". | |
374 | * | |
375 | * @param samplerate The samplerate in Hz. | |
376 | * | |
91219afc | 377 | * @return A newly allocated string representation of the samplerate value, |
4cc9aea1 JH |
378 | * or NULL upon errors. The caller is responsible to g_free() the |
379 | * memory. | |
47117241 UH |
380 | * |
381 | * @since 0.1.0 | |
4cc9aea1 JH |
382 | */ |
383 | SR_API char *sr_samplerate_string(uint64_t samplerate) | |
384 | { | |
385 | return sr_si_string_u64(samplerate, "Hz"); | |
25e7d9b1 | 386 | } |
2a3f9541 | 387 | |
2a3f9541 | 388 | /** |
9a17c23b UH |
389 | * Convert a numeric period value to the "natural" string representation |
390 | * of its period value. | |
2a3f9541 | 391 | * |
9a17c23b | 392 | * The period is specified as a rational number's numerator and denominator. |
2a3f9541 | 393 | * |
9a17c23b | 394 | * E.g. a pair of (1, 5) would be converted to "200 ms", (10, 100) to "100 ms". |
44dae539 | 395 | * |
9a17c23b UH |
396 | * @param v_p The period numerator. |
397 | * @param v_q The period denominator. | |
398 | * | |
399 | * @return A newly allocated string representation of the period value, | |
133a37bf UH |
400 | * or NULL upon errors. The caller is responsible to g_free() the |
401 | * memory. | |
47117241 | 402 | * |
9a17c23b | 403 | * @since 0.5.0 |
2a3f9541 | 404 | */ |
6984cfb2 | 405 | SR_API char *sr_period_string(uint64_t v_p, uint64_t v_q) |
2a3f9541 | 406 | { |
6984cfb2 | 407 | double freq, v; |
d2391b54 | 408 | int prec; |
6984cfb2 SA |
409 | |
410 | freq = 1 / ((double)v_p / v_q); | |
2a3f9541 | 411 | |
6984cfb2 SA |
412 | if (freq > SR_GHZ(1)) { |
413 | v = (double)v_p / v_q * 1000000000000.0; | |
414 | prec = ((v - (uint64_t)v) < FLT_MIN) ? 0 : 3; | |
d2391b54 | 415 | return g_strdup_printf("%.*f ps", prec, v); |
6984cfb2 SA |
416 | } else if (freq > SR_MHZ(1)) { |
417 | v = (double)v_p / v_q * 1000000000.0; | |
418 | prec = ((v - (uint64_t)v) < FLT_MIN) ? 0 : 3; | |
d2391b54 | 419 | return g_strdup_printf("%.*f ns", prec, v); |
6984cfb2 SA |
420 | } else if (freq > SR_KHZ(1)) { |
421 | v = (double)v_p / v_q * 1000000.0; | |
422 | prec = ((v - (uint64_t)v) < FLT_MIN) ? 0 : 3; | |
d2391b54 | 423 | return g_strdup_printf("%.*f us", prec, v); |
6984cfb2 SA |
424 | } else if (freq > 1) { |
425 | v = (double)v_p / v_q * 1000.0; | |
426 | prec = ((v - (uint64_t)v) < FLT_MIN) ? 0 : 3; | |
d2391b54 | 427 | return g_strdup_printf("%.*f ms", prec, v); |
6984cfb2 SA |
428 | } else { |
429 | v = (double)v_p / v_q; | |
430 | prec = ((v - (uint64_t)v) < FLT_MIN) ? 0 : 3; | |
d2391b54 | 431 | return g_strdup_printf("%.*f s", prec, v); |
2a3f9541 | 432 | } |
2a3f9541 | 433 | } |
40f5ddac | 434 | |
79afc8ca | 435 | /** |
e0e15067 BV |
436 | * Convert a numeric voltage value to the "natural" string representation |
437 | * of its voltage value. The voltage is specified as a rational number's | |
438 | * numerator and denominator. | |
79afc8ca BV |
439 | * |
440 | * E.g. a value of 300000 would be converted to "300mV", 2 to "2V". | |
441 | * | |
e0e15067 BV |
442 | * @param v_p The voltage numerator. |
443 | * @param v_q The voltage denominator. | |
79afc8ca | 444 | * |
91219afc | 445 | * @return A newly allocated string representation of the voltage value, |
79afc8ca BV |
446 | * or NULL upon errors. The caller is responsible to g_free() the |
447 | * memory. | |
47117241 UH |
448 | * |
449 | * @since 0.2.0 | |
79afc8ca | 450 | */ |
e0e15067 | 451 | SR_API char *sr_voltage_string(uint64_t v_p, uint64_t v_q) |
79afc8ca | 452 | { |
e0e15067 | 453 | if (v_q == 1000) |
b5df922e | 454 | return g_strdup_printf("%" PRIu64 " mV", v_p); |
e0e15067 | 455 | else if (v_q == 1) |
b5df922e | 456 | return g_strdup_printf("%" PRIu64 " V", v_p); |
79afc8ca | 457 | else |
b5df922e | 458 | return g_strdup_printf("%g V", (float)v_p / (float)v_q); |
79afc8ca BV |
459 | } |
460 | ||
dfcc0bf9 UH |
461 | /** |
462 | * Convert a "natural" string representation of a size value to uint64_t. | |
463 | * | |
464 | * E.g. a value of "3k" or "3 K" would be converted to 3000, a value | |
465 | * of "15M" would be converted to 15000000. | |
466 | * | |
467 | * Value representations other than decimal (such as hex or octal) are not | |
468 | * supported. Only 'k' (kilo), 'm' (mega), 'g' (giga) suffixes are supported. | |
469 | * Spaces (but not other whitespace) between value and suffix are allowed. | |
470 | * | |
471 | * @param sizestring A string containing a (decimal) size value. | |
f64c1414 | 472 | * @param size Pointer to uint64_t which will contain the string's size value. |
dfcc0bf9 | 473 | * |
44dae539 | 474 | * @return SR_OK upon success, SR_ERR upon errors. |
47117241 UH |
475 | * |
476 | * @since 0.1.0 | |
dfcc0bf9 | 477 | */ |
1a081ca6 | 478 | SR_API int sr_parse_sizestring(const char *sizestring, uint64_t *size) |
40f5ddac | 479 | { |
751ba4c8 GS |
480 | uint64_t multiplier; |
481 | int 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 | 507 | break; |
751ba4c8 GS |
508 | case 't': |
509 | case 'T': | |
510 | multiplier = SR_GHZ(1000); | |
511 | break; | |
512 | case 'p': | |
513 | case 'P': | |
514 | multiplier = SR_GHZ(1000 * 1000); | |
515 | break; | |
516 | case 'e': | |
517 | case 'E': | |
518 | multiplier = SR_GHZ(1000 * 1000 * 1000); | |
519 | break; | |
40f5ddac | 520 | default: |
f64c1414 BV |
521 | done = TRUE; |
522 | s--; | |
40f5ddac BV |
523 | } |
524 | s++; | |
525 | } | |
580f3099 | 526 | if (multiplier > 0) { |
f64c1414 | 527 | *size *= multiplier; |
580f3099 | 528 | *size += frac_part * multiplier; |
751ba4c8 | 529 | } else { |
580f3099 | 530 | *size += frac_part; |
751ba4c8 | 531 | } |
40f5ddac | 532 | |
34577da6 | 533 | if (s && *s && g_ascii_strcasecmp(s, "Hz")) |
f64c1414 BV |
534 | return SR_ERR; |
535 | ||
536 | return SR_OK; | |
40f5ddac BV |
537 | } |
538 | ||
dfcc0bf9 UH |
539 | /** |
540 | * Convert a "natural" string representation of a time value to an | |
541 | * uint64_t value in milliseconds. | |
542 | * | |
543 | * E.g. a value of "3s" or "3 s" would be converted to 3000, a value | |
544 | * of "15ms" would be converted to 15. | |
545 | * | |
546 | * Value representations other than decimal (such as hex or octal) are not | |
547 | * supported. Only lower-case "s" and "ms" time suffixes are supported. | |
548 | * Spaces (but not other whitespace) between value and suffix are allowed. | |
549 | * | |
550 | * @param timestring A string containing a (decimal) time value. | |
551 | * @return The string's time value as uint64_t, in milliseconds. | |
552 | * | |
6b2d8d3e UH |
553 | * @todo Add support for "m" (minutes) and others. |
554 | * @todo Add support for picoseconds? | |
555 | * @todo Allow both lower-case and upper-case? If no, document it. | |
47117241 UH |
556 | * |
557 | * @since 0.1.0 | |
dfcc0bf9 | 558 | */ |
1a081ca6 | 559 | SR_API uint64_t sr_parse_timestring(const char *timestring) |
40f5ddac BV |
560 | { |
561 | uint64_t time_msec; | |
562 | char *s; | |
563 | ||
6b2d8d3e UH |
564 | /* TODO: Error handling, logging. */ |
565 | ||
40f5ddac BV |
566 | time_msec = strtoull(timestring, &s, 10); |
567 | if (time_msec == 0 && s == timestring) | |
568 | return 0; | |
569 | ||
570 | if (s && *s) { | |
571 | while (*s == ' ') | |
572 | s++; | |
573 | if (!strcmp(s, "s")) | |
574 | time_msec *= 1000; | |
575 | else if (!strcmp(s, "ms")) | |
576 | ; /* redundant */ | |
577 | else | |
578 | return 0; | |
579 | } | |
580 | ||
581 | return time_msec; | |
582 | } | |
4d436e71 | 583 | |
47117241 | 584 | /** @since 0.1.0 */ |
1a081ca6 | 585 | SR_API gboolean sr_parse_boolstring(const char *boolstr) |
4d436e71 | 586 | { |
ad466f86 GS |
587 | /* |
588 | * Complete absence of an input spec is assumed to mean TRUE, | |
589 | * as in command line option strings like this: | |
590 | * ...:samplerate=100k:header:numchannels=4:... | |
591 | */ | |
592 | if (!boolstr || !*boolstr) | |
593 | return TRUE; | |
4d436e71 | 594 | |
993526f8 BV |
595 | if (!g_ascii_strncasecmp(boolstr, "true", 4) || |
596 | !g_ascii_strncasecmp(boolstr, "yes", 3) || | |
597 | !g_ascii_strncasecmp(boolstr, "on", 2) || | |
598 | !g_ascii_strncasecmp(boolstr, "1", 1)) | |
4d436e71 GM |
599 | return TRUE; |
600 | ||
601 | return FALSE; | |
602 | } | |
76f4c610 | 603 | |
47117241 | 604 | /** @since 0.2.0 */ |
76e107d6 | 605 | SR_API int sr_parse_period(const char *periodstr, uint64_t *p, uint64_t *q) |
76f4c610 BV |
606 | { |
607 | char *s; | |
608 | ||
76e107d6 BV |
609 | *p = strtoull(periodstr, &s, 10); |
610 | if (*p == 0 && s == periodstr) | |
76f4c610 BV |
611 | /* No digits found. */ |
612 | return SR_ERR_ARG; | |
613 | ||
614 | if (s && *s) { | |
615 | while (*s == ' ') | |
616 | s++; | |
8c012adb | 617 | if (!strcmp(s, "fs")) |
76e107d6 | 618 | *q = 1000000000000000ULL; |
8c012adb | 619 | else if (!strcmp(s, "ps")) |
76e107d6 | 620 | *q = 1000000000000ULL; |
8c012adb | 621 | else if (!strcmp(s, "ns")) |
76e107d6 | 622 | *q = 1000000000ULL; |
76f4c610 | 623 | else if (!strcmp(s, "us")) |
76e107d6 | 624 | *q = 1000000; |
76f4c610 | 625 | else if (!strcmp(s, "ms")) |
76e107d6 | 626 | *q = 1000; |
76f4c610 | 627 | else if (!strcmp(s, "s")) |
76e107d6 | 628 | *q = 1; |
76f4c610 BV |
629 | else |
630 | /* Must have a time suffix. */ | |
631 | return SR_ERR_ARG; | |
632 | } | |
633 | ||
634 | return SR_OK; | |
635 | } | |
636 | ||
47117241 | 637 | /** @since 0.2.0 */ |
76e107d6 | 638 | SR_API int sr_parse_voltage(const char *voltstr, uint64_t *p, uint64_t *q) |
79afc8ca BV |
639 | { |
640 | char *s; | |
641 | ||
76e107d6 BV |
642 | *p = strtoull(voltstr, &s, 10); |
643 | if (*p == 0 && s == voltstr) | |
79afc8ca BV |
644 | /* No digits found. */ |
645 | return SR_ERR_ARG; | |
646 | ||
647 | if (s && *s) { | |
648 | while (*s == ' ') | |
649 | s++; | |
34577da6 | 650 | if (!g_ascii_strcasecmp(s, "mv")) |
76e107d6 | 651 | *q = 1000L; |
34577da6 | 652 | else if (!g_ascii_strcasecmp(s, "v")) |
76e107d6 | 653 | *q = 1; |
79afc8ca BV |
654 | else |
655 | /* Must have a base suffix. */ | |
656 | return SR_ERR_ARG; | |
657 | } | |
658 | ||
659 | return SR_OK; | |
660 | } | |
661 | ||
7b870c38 | 662 | /** @} */ |