2 * This file is part of the libsigrok project.
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
27 #include <libsigrok/libsigrok.h>
28 #include "libsigrok-internal.h"
31 #define LOG_PREFIX "strutil"
37 * Helper functions for handling or converting libsigrok-related strings.
41 * @defgroup grp_strutil String utilities
43 * Helper functions for handling or converting libsigrok-related strings.
51 * Convert a string representation of a numeric value (base 10) to a long integer. The
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
56 * @param str The string representation to convert.
57 * @param ret Pointer to long where the result of the conversion will be stored.
59 * @retval SR_OK Conversion successful.
60 * @retval SR_ERR Failure.
64 SR_PRIV int sr_atol(const char *str, long *ret)
70 tmp = strtol(str, &endptr, 10);
72 if (!endptr || *endptr || errno) {
85 * Convert a string representation of a numeric value (base 10) to an integer. The
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
90 * @param str The string representation to convert.
91 * @param ret Pointer to int where the result of the conversion will be stored.
93 * @retval SR_OK Conversion successful.
94 * @retval SR_ERR Failure.
98 SR_PRIV int sr_atoi(const char *str, int *ret)
102 if (sr_atol(str, &tmp) != SR_OK)
105 if ((int) tmp != tmp) {
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
122 * @param str The string representation to convert.
123 * @param ret Pointer to double where the result of the conversion will be stored.
125 * @retval SR_OK Conversion successful.
126 * @retval SR_ERR Failure.
130 SR_PRIV int sr_atod(const char *str, double *ret)
136 tmp = strtof(str, &endptr);
138 if (!endptr || *endptr || errno) {
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
156 * @param str The string representation to convert.
157 * @param ret Pointer to float where the result of the conversion will be stored.
159 * @retval SR_OK Conversion successful.
160 * @retval SR_ERR Failure.
164 SR_PRIV int sr_atof(const char *str, float *ret)
168 if (sr_atod(str, &tmp) != SR_OK)
171 if ((float) tmp != tmp) {
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.
188 * @param str The string representation to convert.
189 * @param ret Pointer to float where the result of the conversion will be stored.
191 * @retval SR_OK Conversion successful.
192 * @retval SR_ERR Failure.
196 SR_PRIV int sr_atof_ascii(const char *str, float *ret)
202 tmp = g_ascii_strtod(str, &endptr);
204 if (!endptr || *endptr || errno) {
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. */
213 if ((float) tmp != tmp) {
215 sr_dbg("ERANGEEEE %e != %e", (float) tmp, tmp);
225 * Convert a numeric value value to its "natural" string representation
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".
231 * @param x The value to convert.
232 * @param unit The unit to append to the string, or NULL if the string
235 * @return A newly allocated string representation of the samplerate value,
236 * or NULL upon errors. The caller is responsible to g_free() the
241 SR_API char *sr_si_string_u64(uint64_t x, const char *unit)
244 uint64_t quot, divisor[] = {
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),
248 const char *p, prefix[] = "\0kMGTPE";
249 char fmt[16], fract[20] = "", *f;
254 for (i = 0; (quot = x / divisor[i]) >= 1000; i++);
257 sprintf(fmt, ".%%0%d"PRIu64, i * 3);
258 f = fract + sprintf(fract, fmt, x % divisor[i]) - 1;
260 while (f >= fract && strchr("0.", *f))
266 return g_strdup_printf("%" PRIu64 "%s %.1s%s", quot, fract, p, unit);
270 * Convert a numeric samplerate value to its "natural" string representation.
272 * E.g. a value of 3000000 would be converted to "3 MHz", 20000 to "20 kHz",
273 * 31500 would become "31.5 kHz".
275 * @param samplerate The samplerate in Hz.
277 * @return A newly allocated string representation of the samplerate value,
278 * or NULL upon errors. The caller is responsible to g_free() the
283 SR_API char *sr_samplerate_string(uint64_t samplerate)
285 return sr_si_string_u64(samplerate, "Hz");
289 * Convert a numeric frequency value to the "natural" string representation
292 * E.g. a value of 3000000 would be converted to "3 us", 20000 to "50 ms".
294 * @param frequency The frequency in Hz.
296 * @return A newly allocated string representation of the frequency value,
297 * or NULL upon errors. The caller is responsible to g_free() the
302 SR_API char *sr_period_string(uint64_t frequency)
307 /* Allocate enough for a uint64_t as string + " ms". */
308 o = g_malloc0(30 + 1);
310 if (frequency >= SR_GHZ(1))
311 r = snprintf(o, 30, "%" PRIu64 " ns", frequency / 1000000000);
312 else if (frequency >= SR_MHZ(1))
313 r = snprintf(o, 30, "%" PRIu64 " us", frequency / 1000000);
314 else if (frequency >= SR_KHZ(1))
315 r = snprintf(o, 30, "%" PRIu64 " ms", frequency / 1000);
317 r = snprintf(o, 30, "%" PRIu64 " s", frequency);
320 /* Something went wrong... */
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.
333 * E.g. a value of 300000 would be converted to "300mV", 2 to "2V".
335 * @param v_p The voltage numerator.
336 * @param v_q The voltage denominator.
338 * @return A newly allocated string representation of the voltage value,
339 * or NULL upon errors. The caller is responsible to g_free() the
344 SR_API char *sr_voltage_string(uint64_t v_p, uint64_t v_q)
349 o = g_malloc0(30 + 1);
352 r = snprintf(o, 30, "%" PRIu64 "mV", v_p);
354 r = snprintf(o, 30, "%" PRIu64 "V", v_p);
356 r = snprintf(o, 30, "%gV", (float)v_p / (float)v_q);
359 /* Something went wrong... */
368 * Convert a "natural" string representation of a size value to uint64_t.
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.
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.
377 * @param sizestring A string containing a (decimal) size value.
378 * @param size Pointer to uint64_t which will contain the string's size value.
380 * @return SR_OK upon success, SR_ERR upon errors.
384 SR_API int sr_parse_sizestring(const char *sizestring, uint64_t *size)
386 int multiplier, done;
390 *size = strtoull(sizestring, &s, 10);
394 while (s && *s && multiplier == 0 && !done) {
399 frac_part = g_ascii_strtod(s, &s);
403 multiplier = SR_KHZ(1);
407 multiplier = SR_MHZ(1);
411 multiplier = SR_GHZ(1);
419 if (multiplier > 0) {
421 *size += frac_part * multiplier;
425 if (s && *s && g_ascii_strcasecmp(s, "Hz"))
432 * Convert a "natural" string representation of a time value to an
433 * uint64_t value in milliseconds.
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.
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.
442 * @param timestring A string containing a (decimal) time value.
443 * @return The string's time value as uint64_t, in milliseconds.
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.
451 SR_API uint64_t sr_parse_timestring(const char *timestring)
456 /* TODO: Error handling, logging. */
458 time_msec = strtoull(timestring, &s, 10);
459 if (time_msec == 0 && s == timestring)
467 else if (!strcmp(s, "ms"))
477 SR_API gboolean sr_parse_boolstring(const char *boolstr)
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))
492 SR_API int sr_parse_period(const char *periodstr, uint64_t *p, uint64_t *q)
496 *p = strtoull(periodstr, &s, 10);
497 if (*p == 0 && s == periodstr)
498 /* No digits found. */
504 if (!strcmp(s, "fs"))
505 *q = 1000000000000000ULL;
506 else if (!strcmp(s, "ps"))
507 *q = 1000000000000ULL;
508 else if (!strcmp(s, "ns"))
510 else if (!strcmp(s, "us"))
512 else if (!strcmp(s, "ms"))
514 else if (!strcmp(s, "s"))
517 /* Must have a time suffix. */
525 SR_API int sr_parse_voltage(const char *voltstr, uint64_t *p, uint64_t *q)
529 *p = strtoull(voltstr, &s, 10);
530 if (*p == 0 && s == voltstr)
531 /* No digits found. */
537 if (!g_ascii_strcasecmp(s, "mv"))
539 else if (!g_ascii_strcasecmp(s, "v"))
542 /* Must have a base suffix. */