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