]>
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 | ||
6ec6c43b | 21 | #include <config.h> |
25e7d9b1 UH |
22 | #include <stdint.h> |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
ba464a12 | 25 | #include <strings.h> |
9e4f8cf9 | 26 | #include <errno.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 | ||
9806c2d5 DJ |
172 | /** |
173 | * @private | |
174 | * | |
175 | * Convert a string representation of a numeric value to a float. The | |
176 | * conversion is strict and will fail if the complete string does not represent | |
177 | * a valid float. 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 float where the result of the conversion will be stored. | |
182 | * | |
1ba4a1cf MH |
183 | * @retval SR_OK Conversion successful. |
184 | * @retval SR_ERR Failure. | |
9806c2d5 DJ |
185 | */ |
186 | SR_PRIV int sr_atof_ascii(const char *str, float *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 | /* FIXME This fails unexpectedly. Some other method to safel downcast | |
201 | * needs to be found. Checking against FLT_MAX doesn't work as well. */ | |
202 | /* | |
203 | if ((float) tmp != tmp) { | |
204 | errno = ERANGE; | |
205 | sr_dbg("ERANGEEEE %e != %e", (float) tmp, tmp); | |
206 | return SR_ERR; | |
207 | } | |
208 | */ | |
209 | ||
210 | *ret = (float) tmp; | |
211 | return SR_OK; | |
212 | } | |
213 | ||
25e7d9b1 | 214 | /** |
b07b42f3 UH |
215 | * Convert a numeric value value to its "natural" string representation |
216 | * in SI units. | |
25e7d9b1 | 217 | * |
4cc9aea1 JH |
218 | * E.g. a value of 3000000, with units set to "W", would be converted |
219 | * to "3 MW", 20000 to "20 kW", 31500 would become "31.5 kW". | |
25e7d9b1 | 220 | * |
4cc9aea1 JH |
221 | * @param x The value to convert. |
222 | * @param unit The unit to append to the string, or NULL if the string | |
223 | * has no units. | |
44dae539 | 224 | * |
91219afc | 225 | * @return A newly allocated string representation of the samplerate value, |
133a37bf UH |
226 | * or NULL upon errors. The caller is responsible to g_free() the |
227 | * memory. | |
47117241 UH |
228 | * |
229 | * @since 0.2.0 | |
25e7d9b1 | 230 | */ |
4cc9aea1 | 231 | SR_API char *sr_si_string_u64(uint64_t x, const char *unit) |
25e7d9b1 | 232 | { |
094e6b81 PS |
233 | uint8_t i; |
234 | uint64_t quot, divisor[] = { | |
b07b42f3 UH |
235 | SR_HZ(1), SR_KHZ(1), SR_MHZ(1), SR_GHZ(1), |
236 | SR_GHZ(1000), SR_GHZ(1000 * 1000), SR_GHZ(1000 * 1000 * 1000), | |
094e6b81 PS |
237 | }; |
238 | const char *p, prefix[] = "\0kMGTPE"; | |
69d83be9 | 239 | char fmt[16], fract[20] = "", *f; |
094e6b81 | 240 | |
98fec29e | 241 | if (!unit) |
4cc9aea1 | 242 | unit = ""; |
25e7d9b1 | 243 | |
094e6b81 PS |
244 | for (i = 0; (quot = x / divisor[i]) >= 1000; i++); |
245 | ||
246 | if (i) { | |
69d83be9 | 247 | sprintf(fmt, ".%%0%d"PRIu64, i * 3); |
094e6b81 PS |
248 | f = fract + sprintf(fract, fmt, x % divisor[i]) - 1; |
249 | ||
250 | while (f >= fract && strchr("0.", *f)) | |
251 | *f-- = 0; | |
133a37bf | 252 | } |
25e7d9b1 | 253 | |
094e6b81 PS |
254 | p = prefix + i; |
255 | ||
256 | return g_strdup_printf("%" PRIu64 "%s %.1s%s", quot, fract, p, unit); | |
4cc9aea1 | 257 | } |
25e7d9b1 | 258 | |
4cc9aea1 JH |
259 | /** |
260 | * Convert a numeric samplerate value to its "natural" string representation. | |
261 | * | |
262 | * E.g. a value of 3000000 would be converted to "3 MHz", 20000 to "20 kHz", | |
263 | * 31500 would become "31.5 kHz". | |
264 | * | |
265 | * @param samplerate The samplerate in Hz. | |
266 | * | |
91219afc | 267 | * @return A newly allocated string representation of the samplerate value, |
4cc9aea1 JH |
268 | * or NULL upon errors. The caller is responsible to g_free() the |
269 | * memory. | |
47117241 UH |
270 | * |
271 | * @since 0.1.0 | |
4cc9aea1 JH |
272 | */ |
273 | SR_API char *sr_samplerate_string(uint64_t samplerate) | |
274 | { | |
275 | return sr_si_string_u64(samplerate, "Hz"); | |
25e7d9b1 | 276 | } |
2a3f9541 | 277 | |
2a3f9541 | 278 | /** |
dfcc0bf9 | 279 | * Convert a numeric frequency value to the "natural" string representation |
2a3f9541 BV |
280 | * of its period. |
281 | * | |
2507648e | 282 | * E.g. a value of 3000000 would be converted to "3 us", 20000 to "50 ms". |
2a3f9541 BV |
283 | * |
284 | * @param frequency The frequency in Hz. | |
44dae539 | 285 | * |
91219afc | 286 | * @return A newly allocated string representation of the frequency value, |
133a37bf UH |
287 | * or NULL upon errors. The caller is responsible to g_free() the |
288 | * memory. | |
47117241 UH |
289 | * |
290 | * @since 0.1.0 | |
2a3f9541 | 291 | */ |
1a081ca6 | 292 | SR_API char *sr_period_string(uint64_t frequency) |
2a3f9541 BV |
293 | { |
294 | char *o; | |
295 | int r; | |
296 | ||
133a37bf | 297 | /* Allocate enough for a uint64_t as string + " ms". */ |
91219afc | 298 | o = g_malloc0(30 + 1); |
2a3f9541 | 299 | |
59df0c77 | 300 | if (frequency >= SR_GHZ(1)) |
2a3f9541 | 301 | r = snprintf(o, 30, "%" PRIu64 " ns", frequency / 1000000000); |
59df0c77 | 302 | else if (frequency >= SR_MHZ(1)) |
2507648e | 303 | r = snprintf(o, 30, "%" PRIu64 " us", frequency / 1000000); |
59df0c77 | 304 | else if (frequency >= SR_KHZ(1)) |
2a3f9541 BV |
305 | r = snprintf(o, 30, "%" PRIu64 " ms", frequency / 1000); |
306 | else | |
307 | r = snprintf(o, 30, "%" PRIu64 " s", frequency); | |
308 | ||
309 | if (r < 0) { | |
310 | /* Something went wrong... */ | |
133a37bf | 311 | g_free(o); |
2a3f9541 BV |
312 | return NULL; |
313 | } | |
314 | ||
315 | return o; | |
316 | } | |
40f5ddac | 317 | |
79afc8ca | 318 | /** |
e0e15067 BV |
319 | * Convert a numeric voltage value to the "natural" string representation |
320 | * of its voltage value. The voltage is specified as a rational number's | |
321 | * numerator and denominator. | |
79afc8ca BV |
322 | * |
323 | * E.g. a value of 300000 would be converted to "300mV", 2 to "2V". | |
324 | * | |
e0e15067 BV |
325 | * @param v_p The voltage numerator. |
326 | * @param v_q The voltage denominator. | |
79afc8ca | 327 | * |
91219afc | 328 | * @return A newly allocated string representation of the voltage value, |
79afc8ca BV |
329 | * or NULL upon errors. The caller is responsible to g_free() the |
330 | * memory. | |
47117241 UH |
331 | * |
332 | * @since 0.2.0 | |
79afc8ca | 333 | */ |
e0e15067 | 334 | SR_API char *sr_voltage_string(uint64_t v_p, uint64_t v_q) |
79afc8ca | 335 | { |
79afc8ca | 336 | int r; |
d5a669a9 | 337 | char *o; |
79afc8ca | 338 | |
91219afc | 339 | o = g_malloc0(30 + 1); |
79afc8ca | 340 | |
e0e15067 BV |
341 | if (v_q == 1000) |
342 | r = snprintf(o, 30, "%" PRIu64 "mV", v_p); | |
343 | else if (v_q == 1) | |
344 | r = snprintf(o, 30, "%" PRIu64 "V", v_p); | |
79afc8ca | 345 | else |
e0e15067 | 346 | r = snprintf(o, 30, "%gV", (float)v_p / (float)v_q); |
79afc8ca BV |
347 | |
348 | if (r < 0) { | |
349 | /* Something went wrong... */ | |
350 | g_free(o); | |
351 | return NULL; | |
352 | } | |
353 | ||
354 | return o; | |
355 | } | |
356 | ||
dfcc0bf9 UH |
357 | /** |
358 | * Convert a "natural" string representation of a size value to uint64_t. | |
359 | * | |
360 | * E.g. a value of "3k" or "3 K" would be converted to 3000, a value | |
361 | * of "15M" would be converted to 15000000. | |
362 | * | |
363 | * Value representations other than decimal (such as hex or octal) are not | |
364 | * supported. Only 'k' (kilo), 'm' (mega), 'g' (giga) suffixes are supported. | |
365 | * Spaces (but not other whitespace) between value and suffix are allowed. | |
366 | * | |
367 | * @param sizestring A string containing a (decimal) size value. | |
f64c1414 | 368 | * @param size Pointer to uint64_t which will contain the string's size value. |
dfcc0bf9 | 369 | * |
44dae539 | 370 | * @return SR_OK upon success, SR_ERR upon errors. |
47117241 UH |
371 | * |
372 | * @since 0.1.0 | |
dfcc0bf9 | 373 | */ |
1a081ca6 | 374 | SR_API int sr_parse_sizestring(const char *sizestring, uint64_t *size) |
40f5ddac | 375 | { |
f64c1414 | 376 | int multiplier, done; |
580f3099 | 377 | double frac_part; |
40f5ddac BV |
378 | char *s; |
379 | ||
f64c1414 | 380 | *size = strtoull(sizestring, &s, 10); |
40f5ddac | 381 | multiplier = 0; |
580f3099 | 382 | frac_part = 0; |
f64c1414 BV |
383 | done = FALSE; |
384 | while (s && *s && multiplier == 0 && !done) { | |
40f5ddac BV |
385 | switch (*s) { |
386 | case ' ': | |
387 | break; | |
580f3099 DJ |
388 | case '.': |
389 | frac_part = g_ascii_strtod(s, &s); | |
390 | break; | |
40f5ddac BV |
391 | case 'k': |
392 | case 'K': | |
59df0c77 | 393 | multiplier = SR_KHZ(1); |
40f5ddac BV |
394 | break; |
395 | case 'm': | |
396 | case 'M': | |
59df0c77 | 397 | multiplier = SR_MHZ(1); |
40f5ddac BV |
398 | break; |
399 | case 'g': | |
400 | case 'G': | |
59df0c77 | 401 | multiplier = SR_GHZ(1); |
40f5ddac BV |
402 | break; |
403 | default: | |
f64c1414 BV |
404 | done = TRUE; |
405 | s--; | |
40f5ddac BV |
406 | } |
407 | s++; | |
408 | } | |
580f3099 | 409 | if (multiplier > 0) { |
f64c1414 | 410 | *size *= multiplier; |
580f3099 DJ |
411 | *size += frac_part * multiplier; |
412 | } else | |
413 | *size += frac_part; | |
40f5ddac | 414 | |
34577da6 | 415 | if (s && *s && g_ascii_strcasecmp(s, "Hz")) |
f64c1414 BV |
416 | return SR_ERR; |
417 | ||
418 | return SR_OK; | |
40f5ddac BV |
419 | } |
420 | ||
dfcc0bf9 UH |
421 | /** |
422 | * Convert a "natural" string representation of a time value to an | |
423 | * uint64_t value in milliseconds. | |
424 | * | |
425 | * E.g. a value of "3s" or "3 s" would be converted to 3000, a value | |
426 | * of "15ms" would be converted to 15. | |
427 | * | |
428 | * Value representations other than decimal (such as hex or octal) are not | |
429 | * supported. Only lower-case "s" and "ms" time suffixes are supported. | |
430 | * Spaces (but not other whitespace) between value and suffix are allowed. | |
431 | * | |
432 | * @param timestring A string containing a (decimal) time value. | |
433 | * @return The string's time value as uint64_t, in milliseconds. | |
434 | * | |
6b2d8d3e UH |
435 | * @todo Add support for "m" (minutes) and others. |
436 | * @todo Add support for picoseconds? | |
437 | * @todo Allow both lower-case and upper-case? If no, document it. | |
47117241 UH |
438 | * |
439 | * @since 0.1.0 | |
dfcc0bf9 | 440 | */ |
1a081ca6 | 441 | SR_API uint64_t sr_parse_timestring(const char *timestring) |
40f5ddac BV |
442 | { |
443 | uint64_t time_msec; | |
444 | char *s; | |
445 | ||
6b2d8d3e UH |
446 | /* TODO: Error handling, logging. */ |
447 | ||
40f5ddac BV |
448 | time_msec = strtoull(timestring, &s, 10); |
449 | if (time_msec == 0 && s == timestring) | |
450 | return 0; | |
451 | ||
452 | if (s && *s) { | |
453 | while (*s == ' ') | |
454 | s++; | |
455 | if (!strcmp(s, "s")) | |
456 | time_msec *= 1000; | |
457 | else if (!strcmp(s, "ms")) | |
458 | ; /* redundant */ | |
459 | else | |
460 | return 0; | |
461 | } | |
462 | ||
463 | return time_msec; | |
464 | } | |
4d436e71 | 465 | |
47117241 | 466 | /** @since 0.1.0 */ |
1a081ca6 | 467 | SR_API gboolean sr_parse_boolstring(const char *boolstr) |
4d436e71 GM |
468 | { |
469 | if (!boolstr) | |
470 | return FALSE; | |
471 | ||
993526f8 BV |
472 | if (!g_ascii_strncasecmp(boolstr, "true", 4) || |
473 | !g_ascii_strncasecmp(boolstr, "yes", 3) || | |
474 | !g_ascii_strncasecmp(boolstr, "on", 2) || | |
475 | !g_ascii_strncasecmp(boolstr, "1", 1)) | |
4d436e71 GM |
476 | return TRUE; |
477 | ||
478 | return FALSE; | |
479 | } | |
76f4c610 | 480 | |
47117241 | 481 | /** @since 0.2.0 */ |
76e107d6 | 482 | SR_API int sr_parse_period(const char *periodstr, uint64_t *p, uint64_t *q) |
76f4c610 BV |
483 | { |
484 | char *s; | |
485 | ||
76e107d6 BV |
486 | *p = strtoull(periodstr, &s, 10); |
487 | if (*p == 0 && s == periodstr) | |
76f4c610 BV |
488 | /* No digits found. */ |
489 | return SR_ERR_ARG; | |
490 | ||
491 | if (s && *s) { | |
492 | while (*s == ' ') | |
493 | s++; | |
8c012adb | 494 | if (!strcmp(s, "fs")) |
76e107d6 | 495 | *q = 1000000000000000ULL; |
8c012adb | 496 | else if (!strcmp(s, "ps")) |
76e107d6 | 497 | *q = 1000000000000ULL; |
8c012adb | 498 | else if (!strcmp(s, "ns")) |
76e107d6 | 499 | *q = 1000000000ULL; |
76f4c610 | 500 | else if (!strcmp(s, "us")) |
76e107d6 | 501 | *q = 1000000; |
76f4c610 | 502 | else if (!strcmp(s, "ms")) |
76e107d6 | 503 | *q = 1000; |
76f4c610 | 504 | else if (!strcmp(s, "s")) |
76e107d6 | 505 | *q = 1; |
76f4c610 BV |
506 | else |
507 | /* Must have a time suffix. */ | |
508 | return SR_ERR_ARG; | |
509 | } | |
510 | ||
511 | return SR_OK; | |
512 | } | |
513 | ||
47117241 | 514 | /** @since 0.2.0 */ |
76e107d6 | 515 | SR_API int sr_parse_voltage(const char *voltstr, uint64_t *p, uint64_t *q) |
79afc8ca BV |
516 | { |
517 | char *s; | |
518 | ||
76e107d6 BV |
519 | *p = strtoull(voltstr, &s, 10); |
520 | if (*p == 0 && s == voltstr) | |
79afc8ca BV |
521 | /* No digits found. */ |
522 | return SR_ERR_ARG; | |
523 | ||
524 | if (s && *s) { | |
525 | while (*s == ' ') | |
526 | s++; | |
34577da6 | 527 | if (!g_ascii_strcasecmp(s, "mv")) |
76e107d6 | 528 | *q = 1000L; |
34577da6 | 529 | else if (!g_ascii_strcasecmp(s, "v")) |
76e107d6 | 530 | *q = 1; |
79afc8ca BV |
531 | else |
532 | /* Must have a base suffix. */ | |
533 | return SR_ERR_ARG; | |
534 | } | |
535 | ||
536 | return SR_OK; | |
537 | } | |
538 | ||
7b870c38 | 539 | /** @} */ |