]> sigrok.org Git - libsigrok.git/blame - src/strutil.c
No need to check return value of libusb_get_device_descriptor().
[libsigrok.git] / src / strutil.c
CommitLineData
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
DJ
61 *
62 * @since 0.3.0
63 */
8d558c7a 64SR_PRIV int sr_atol(const char *str, long *ret)
9e4f8cf9
DJ
65{
66 long tmp;
67 char *endptr = NULL;
68
69 errno = 0;
1ba4a1cf 70 tmp = strtol(str, &endptr, 10);
9e4f8cf9
DJ
71
72 if (!endptr || *endptr || errno) {
73 if (!errno)
74 errno = EINVAL;
75 return SR_ERR;
76 }
77
78 *ret = tmp;
79 return SR_OK;
80}
81
82/**
df823ac4
UH
83 * @private
84 *
1ba4a1cf 85 * Convert a string representation of a numeric value (base 10) to an integer. The
9e4f8cf9
DJ
86 * conversion is strict and will fail if the complete string does not represent
87 * a valid integer. The function sets errno according to the details of the
88 * failure.
89 *
90 * @param str The string representation to convert.
91 * @param ret Pointer to int where the result of the conversion will be stored.
92 *
1ba4a1cf
MH
93 * @retval SR_OK Conversion successful.
94 * @retval SR_ERR Failure.
9e4f8cf9
DJ
95 *
96 * @since 0.3.0
97 */
8d558c7a 98SR_PRIV int sr_atoi(const char *str, int *ret)
9e4f8cf9
DJ
99{
100 long tmp;
101
102 if (sr_atol(str, &tmp) != SR_OK)
103 return SR_ERR;
104
105 if ((int) tmp != tmp) {
106 errno = ERANGE;
107 return SR_ERR;
108 }
109
110 *ret = (int) tmp;
111 return SR_OK;
112}
113
114/**
df823ac4
UH
115 * @private
116 *
9e4f8cf9
DJ
117 * Convert a string representation of a numeric value to a double. The
118 * conversion is strict and will fail if the complete string does not represent
119 * a valid double. The function sets errno according to the details of the
120 * failure.
121 *
122 * @param str The string representation to convert.
123 * @param ret Pointer to double where the result of the conversion will be stored.
124 *
1ba4a1cf
MH
125 * @retval SR_OK Conversion successful.
126 * @retval SR_ERR Failure.
9e4f8cf9
DJ
127 *
128 * @since 0.3.0
129 */
8d558c7a 130SR_PRIV int sr_atod(const char *str, double *ret)
9e4f8cf9
DJ
131{
132 double tmp;
133 char *endptr = NULL;
134
135 errno = 0;
136 tmp = strtof(str, &endptr);
137
138 if (!endptr || *endptr || errno) {
139 if (!errno)
140 errno = EINVAL;
141 return SR_ERR;
142 }
143
144 *ret = tmp;
145 return SR_OK;
146}
147
148/**
df823ac4
UH
149 * @private
150 *
9e4f8cf9
DJ
151 * Convert a string representation of a numeric value to a float. The
152 * conversion is strict and will fail if the complete string does not represent
153 * a valid float. The function sets errno according to the details of the
154 * failure.
155 *
156 * @param str The string representation to convert.
157 * @param ret Pointer to float where the result of the conversion will be stored.
158 *
1ba4a1cf
MH
159 * @retval SR_OK Conversion successful.
160 * @retval SR_ERR Failure.
9e4f8cf9
DJ
161 *
162 * @since 0.3.0
163 */
8d558c7a 164SR_PRIV int sr_atof(const char *str, float *ret)
9e4f8cf9
DJ
165{
166 double tmp;
167
168 if (sr_atod(str, &tmp) != SR_OK)
169 return SR_ERR;
170
171 if ((float) tmp != tmp) {
172 errno = ERANGE;
173 return SR_ERR;
174 }
175
176 *ret = (float) tmp;
177 return SR_OK;
178}
179
9806c2d5
DJ
180/**
181 * @private
182 *
183 * Convert a string representation of a numeric value to a float. The
184 * conversion is strict and will fail if the complete string does not represent
185 * a valid float. The function sets errno according to the details of the
186 * failure. This version ignores the locale.
187 *
188 * @param str The string representation to convert.
189 * @param ret Pointer to float where the result of the conversion will be stored.
190 *
1ba4a1cf
MH
191 * @retval SR_OK Conversion successful.
192 * @retval SR_ERR Failure.
9806c2d5
DJ
193 *
194 * @since 0.3.0
195 */
196SR_PRIV int sr_atof_ascii(const char *str, float *ret)
197{
198 double tmp;
199 char *endptr = NULL;
200
201 errno = 0;
202 tmp = g_ascii_strtod(str, &endptr);
203
204 if (!endptr || *endptr || errno) {
205 if (!errno)
206 errno = EINVAL;
207 return SR_ERR;
208 }
209
210 /* FIXME This fails unexpectedly. Some other method to safel downcast
211 * needs to be found. Checking against FLT_MAX doesn't work as well. */
212 /*
213 if ((float) tmp != tmp) {
214 errno = ERANGE;
215 sr_dbg("ERANGEEEE %e != %e", (float) tmp, tmp);
216 return SR_ERR;
217 }
218 */
219
220 *ret = (float) tmp;
221 return SR_OK;
222}
223
25e7d9b1 224/**
b07b42f3
UH
225 * Convert a numeric value value to its "natural" string representation
226 * in SI units.
25e7d9b1 227 *
4cc9aea1
JH
228 * E.g. a value of 3000000, with units set to "W", would be converted
229 * to "3 MW", 20000 to "20 kW", 31500 would become "31.5 kW".
25e7d9b1 230 *
4cc9aea1
JH
231 * @param x The value to convert.
232 * @param unit The unit to append to the string, or NULL if the string
233 * has no units.
44dae539 234 *
91219afc 235 * @return A newly allocated string representation of the samplerate value,
133a37bf
UH
236 * or NULL upon errors. The caller is responsible to g_free() the
237 * memory.
47117241
UH
238 *
239 * @since 0.2.0
25e7d9b1 240 */
4cc9aea1 241SR_API char *sr_si_string_u64(uint64_t x, const char *unit)
25e7d9b1 242{
094e6b81
PS
243 uint8_t i;
244 uint64_t quot, divisor[] = {
b07b42f3
UH
245 SR_HZ(1), SR_KHZ(1), SR_MHZ(1), SR_GHZ(1),
246 SR_GHZ(1000), SR_GHZ(1000 * 1000), SR_GHZ(1000 * 1000 * 1000),
094e6b81
PS
247 };
248 const char *p, prefix[] = "\0kMGTPE";
69d83be9 249 char fmt[16], fract[20] = "", *f;
094e6b81 250
98fec29e 251 if (!unit)
4cc9aea1 252 unit = "";
25e7d9b1 253
094e6b81
PS
254 for (i = 0; (quot = x / divisor[i]) >= 1000; i++);
255
256 if (i) {
69d83be9 257 sprintf(fmt, ".%%0%d"PRIu64, i * 3);
094e6b81
PS
258 f = fract + sprintf(fract, fmt, x % divisor[i]) - 1;
259
260 while (f >= fract && strchr("0.", *f))
261 *f-- = 0;
133a37bf 262 }
25e7d9b1 263
094e6b81
PS
264 p = prefix + i;
265
266 return g_strdup_printf("%" PRIu64 "%s %.1s%s", quot, fract, p, unit);
4cc9aea1 267}
25e7d9b1 268
4cc9aea1
JH
269/**
270 * Convert a numeric samplerate value to its "natural" string representation.
271 *
272 * E.g. a value of 3000000 would be converted to "3 MHz", 20000 to "20 kHz",
273 * 31500 would become "31.5 kHz".
274 *
275 * @param samplerate The samplerate in Hz.
276 *
91219afc 277 * @return A newly allocated string representation of the samplerate value,
4cc9aea1
JH
278 * or NULL upon errors. The caller is responsible to g_free() the
279 * memory.
47117241
UH
280 *
281 * @since 0.1.0
4cc9aea1
JH
282 */
283SR_API char *sr_samplerate_string(uint64_t samplerate)
284{
285 return sr_si_string_u64(samplerate, "Hz");
25e7d9b1 286}
2a3f9541 287
2a3f9541 288/**
dfcc0bf9 289 * Convert a numeric frequency value to the "natural" string representation
2a3f9541
BV
290 * of its period.
291 *
2507648e 292 * E.g. a value of 3000000 would be converted to "3 us", 20000 to "50 ms".
2a3f9541
BV
293 *
294 * @param frequency The frequency in Hz.
44dae539 295 *
91219afc 296 * @return A newly allocated string representation of the frequency value,
133a37bf
UH
297 * or NULL upon errors. The caller is responsible to g_free() the
298 * memory.
47117241
UH
299 *
300 * @since 0.1.0
2a3f9541 301 */
1a081ca6 302SR_API char *sr_period_string(uint64_t frequency)
2a3f9541
BV
303{
304 char *o;
305 int r;
306
133a37bf 307 /* Allocate enough for a uint64_t as string + " ms". */
91219afc 308 o = g_malloc0(30 + 1);
2a3f9541 309
59df0c77 310 if (frequency >= SR_GHZ(1))
2a3f9541 311 r = snprintf(o, 30, "%" PRIu64 " ns", frequency / 1000000000);
59df0c77 312 else if (frequency >= SR_MHZ(1))
2507648e 313 r = snprintf(o, 30, "%" PRIu64 " us", frequency / 1000000);
59df0c77 314 else if (frequency >= SR_KHZ(1))
2a3f9541
BV
315 r = snprintf(o, 30, "%" PRIu64 " ms", frequency / 1000);
316 else
317 r = snprintf(o, 30, "%" PRIu64 " s", frequency);
318
319 if (r < 0) {
320 /* Something went wrong... */
133a37bf 321 g_free(o);
2a3f9541
BV
322 return NULL;
323 }
324
325 return o;
326}
40f5ddac 327
79afc8ca 328/**
e0e15067
BV
329 * Convert a numeric voltage value to the "natural" string representation
330 * of its voltage value. The voltage is specified as a rational number's
331 * numerator and denominator.
79afc8ca
BV
332 *
333 * E.g. a value of 300000 would be converted to "300mV", 2 to "2V".
334 *
e0e15067
BV
335 * @param v_p The voltage numerator.
336 * @param v_q The voltage denominator.
79afc8ca 337 *
91219afc 338 * @return A newly allocated string representation of the voltage value,
79afc8ca
BV
339 * or NULL upon errors. The caller is responsible to g_free() the
340 * memory.
47117241
UH
341 *
342 * @since 0.2.0
79afc8ca 343 */
e0e15067 344SR_API char *sr_voltage_string(uint64_t v_p, uint64_t v_q)
79afc8ca 345{
79afc8ca 346 int r;
d5a669a9 347 char *o;
79afc8ca 348
91219afc 349 o = g_malloc0(30 + 1);
79afc8ca 350
e0e15067
BV
351 if (v_q == 1000)
352 r = snprintf(o, 30, "%" PRIu64 "mV", v_p);
353 else if (v_q == 1)
354 r = snprintf(o, 30, "%" PRIu64 "V", v_p);
79afc8ca 355 else
e0e15067 356 r = snprintf(o, 30, "%gV", (float)v_p / (float)v_q);
79afc8ca
BV
357
358 if (r < 0) {
359 /* Something went wrong... */
360 g_free(o);
361 return NULL;
362 }
363
364 return o;
365}
366
dfcc0bf9
UH
367/**
368 * Convert a "natural" string representation of a size value to uint64_t.
369 *
370 * E.g. a value of "3k" or "3 K" would be converted to 3000, a value
371 * of "15M" would be converted to 15000000.
372 *
373 * Value representations other than decimal (such as hex or octal) are not
374 * supported. Only 'k' (kilo), 'm' (mega), 'g' (giga) suffixes are supported.
375 * Spaces (but not other whitespace) between value and suffix are allowed.
376 *
377 * @param sizestring A string containing a (decimal) size value.
f64c1414 378 * @param size Pointer to uint64_t which will contain the string's size value.
dfcc0bf9 379 *
44dae539 380 * @return SR_OK upon success, SR_ERR upon errors.
47117241
UH
381 *
382 * @since 0.1.0
dfcc0bf9 383 */
1a081ca6 384SR_API int sr_parse_sizestring(const char *sizestring, uint64_t *size)
40f5ddac 385{
f64c1414 386 int multiplier, done;
580f3099 387 double frac_part;
40f5ddac
BV
388 char *s;
389
f64c1414 390 *size = strtoull(sizestring, &s, 10);
40f5ddac 391 multiplier = 0;
580f3099 392 frac_part = 0;
f64c1414
BV
393 done = FALSE;
394 while (s && *s && multiplier == 0 && !done) {
40f5ddac
BV
395 switch (*s) {
396 case ' ':
397 break;
580f3099
DJ
398 case '.':
399 frac_part = g_ascii_strtod(s, &s);
400 break;
40f5ddac
BV
401 case 'k':
402 case 'K':
59df0c77 403 multiplier = SR_KHZ(1);
40f5ddac
BV
404 break;
405 case 'm':
406 case 'M':
59df0c77 407 multiplier = SR_MHZ(1);
40f5ddac
BV
408 break;
409 case 'g':
410 case 'G':
59df0c77 411 multiplier = SR_GHZ(1);
40f5ddac
BV
412 break;
413 default:
f64c1414
BV
414 done = TRUE;
415 s--;
40f5ddac
BV
416 }
417 s++;
418 }
580f3099 419 if (multiplier > 0) {
f64c1414 420 *size *= multiplier;
580f3099
DJ
421 *size += frac_part * multiplier;
422 } else
423 *size += frac_part;
40f5ddac 424
34577da6 425 if (s && *s && g_ascii_strcasecmp(s, "Hz"))
f64c1414
BV
426 return SR_ERR;
427
428 return SR_OK;
40f5ddac
BV
429}
430
dfcc0bf9
UH
431/**
432 * Convert a "natural" string representation of a time value to an
433 * uint64_t value in milliseconds.
434 *
435 * E.g. a value of "3s" or "3 s" would be converted to 3000, a value
436 * of "15ms" would be converted to 15.
437 *
438 * Value representations other than decimal (such as hex or octal) are not
439 * supported. Only lower-case "s" and "ms" time suffixes are supported.
440 * Spaces (but not other whitespace) between value and suffix are allowed.
441 *
442 * @param timestring A string containing a (decimal) time value.
443 * @return The string's time value as uint64_t, in milliseconds.
444 *
6b2d8d3e
UH
445 * @todo Add support for "m" (minutes) and others.
446 * @todo Add support for picoseconds?
447 * @todo Allow both lower-case and upper-case? If no, document it.
47117241
UH
448 *
449 * @since 0.1.0
dfcc0bf9 450 */
1a081ca6 451SR_API uint64_t sr_parse_timestring(const char *timestring)
40f5ddac
BV
452{
453 uint64_t time_msec;
454 char *s;
455
6b2d8d3e
UH
456 /* TODO: Error handling, logging. */
457
40f5ddac
BV
458 time_msec = strtoull(timestring, &s, 10);
459 if (time_msec == 0 && s == timestring)
460 return 0;
461
462 if (s && *s) {
463 while (*s == ' ')
464 s++;
465 if (!strcmp(s, "s"))
466 time_msec *= 1000;
467 else if (!strcmp(s, "ms"))
468 ; /* redundant */
469 else
470 return 0;
471 }
472
473 return time_msec;
474}
4d436e71 475
47117241 476/** @since 0.1.0 */
1a081ca6 477SR_API gboolean sr_parse_boolstring(const char *boolstr)
4d436e71
GM
478{
479 if (!boolstr)
480 return FALSE;
481
993526f8
BV
482 if (!g_ascii_strncasecmp(boolstr, "true", 4) ||
483 !g_ascii_strncasecmp(boolstr, "yes", 3) ||
484 !g_ascii_strncasecmp(boolstr, "on", 2) ||
485 !g_ascii_strncasecmp(boolstr, "1", 1))
4d436e71
GM
486 return TRUE;
487
488 return FALSE;
489}
76f4c610 490
47117241 491/** @since 0.2.0 */
76e107d6 492SR_API int sr_parse_period(const char *periodstr, uint64_t *p, uint64_t *q)
76f4c610
BV
493{
494 char *s;
495
76e107d6
BV
496 *p = strtoull(periodstr, &s, 10);
497 if (*p == 0 && s == periodstr)
76f4c610
BV
498 /* No digits found. */
499 return SR_ERR_ARG;
500
501 if (s && *s) {
502 while (*s == ' ')
503 s++;
8c012adb 504 if (!strcmp(s, "fs"))
76e107d6 505 *q = 1000000000000000ULL;
8c012adb 506 else if (!strcmp(s, "ps"))
76e107d6 507 *q = 1000000000000ULL;
8c012adb 508 else if (!strcmp(s, "ns"))
76e107d6 509 *q = 1000000000ULL;
76f4c610 510 else if (!strcmp(s, "us"))
76e107d6 511 *q = 1000000;
76f4c610 512 else if (!strcmp(s, "ms"))
76e107d6 513 *q = 1000;
76f4c610 514 else if (!strcmp(s, "s"))
76e107d6 515 *q = 1;
76f4c610
BV
516 else
517 /* Must have a time suffix. */
518 return SR_ERR_ARG;
519 }
520
521 return SR_OK;
522}
523
47117241 524/** @since 0.2.0 */
76e107d6 525SR_API int sr_parse_voltage(const char *voltstr, uint64_t *p, uint64_t *q)
79afc8ca
BV
526{
527 char *s;
528
76e107d6
BV
529 *p = strtoull(voltstr, &s, 10);
530 if (*p == 0 && s == voltstr)
79afc8ca
BV
531 /* No digits found. */
532 return SR_ERR_ARG;
533
534 if (s && *s) {
535 while (*s == ' ')
536 s++;
34577da6 537 if (!g_ascii_strcasecmp(s, "mv"))
76e107d6 538 *q = 1000L;
34577da6 539 else if (!g_ascii_strcasecmp(s, "v"))
76e107d6 540 *q = 1;
79afc8ca
BV
541 else
542 /* Must have a base suffix. */
543 return SR_ERR_ARG;
544 }
545
546 return SR_OK;
547}
548
7b870c38 549/** @} */