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.
62 SR_PRIV int sr_atol(const char *str, long *ret)
68 tmp = strtol(str, &endptr, 10);
70 if (!endptr || *endptr || errno) {
83 * Convert a string representation of a numeric value (base 10) to an integer. The
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
88 * @param str The string representation to convert.
89 * @param ret Pointer to int where the result of the conversion will be stored.
91 * @retval SR_OK Conversion successful.
92 * @retval SR_ERR Failure.
94 SR_PRIV int sr_atoi(const char *str, int *ret)
98 if (sr_atol(str, &tmp) != SR_OK)
101 if ((int) tmp != tmp) {
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
118 * @param str The string representation to convert.
119 * @param ret Pointer to double where the result of the conversion will be stored.
121 * @retval SR_OK Conversion successful.
122 * @retval SR_ERR Failure.
124 SR_PRIV int sr_atod(const char *str, double *ret)
130 tmp = strtof(str, &endptr);
132 if (!endptr || *endptr || errno) {
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
150 * @param str The string representation to convert.
151 * @param ret Pointer to float where the result of the conversion will be stored.
153 * @retval SR_OK Conversion successful.
154 * @retval SR_ERR Failure.
156 SR_PRIV int sr_atof(const char *str, float *ret)
160 if (sr_atod(str, &tmp) != SR_OK)
163 if ((float) tmp != tmp) {
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.
180 * @param str The string representation to convert.
181 * @param ret Pointer to float where the result of the conversion will be stored.
183 * @retval SR_OK Conversion successful.
184 * @retval SR_ERR Failure.
186 SR_PRIV int sr_atof_ascii(const char *str, float *ret)
192 tmp = g_ascii_strtod(str, &endptr);
194 if (!endptr || *endptr || errno) {
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. */
203 if ((float) tmp != tmp) {
205 sr_dbg("ERANGEEEE %e != %e", (float) tmp, tmp);
215 * Convert a numeric value value to its "natural" string representation
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".
221 * @param x The value to convert.
222 * @param unit The unit to append to the string, or NULL if the string
225 * @return A newly allocated string representation of the samplerate value,
226 * or NULL upon errors. The caller is responsible to g_free() the
231 SR_API char *sr_si_string_u64(uint64_t x, const char *unit)
234 uint64_t quot, divisor[] = {
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),
238 const char *p, prefix[] = "\0kMGTPE";
239 char fmt[16], fract[20] = "", *f;
244 for (i = 0; (quot = x / divisor[i]) >= 1000; i++);
247 sprintf(fmt, ".%%0%d"PRIu64, i * 3);
248 f = fract + sprintf(fract, fmt, x % divisor[i]) - 1;
250 while (f >= fract && strchr("0.", *f))
256 return g_strdup_printf("%" PRIu64 "%s %.1s%s", quot, fract, p, unit);
260 * Convert a numeric samplerate value to its "natural" string representation.
262 * E.g. a value of 3000000 would be converted to "3 MHz", 20000 to "20 kHz",
263 * 31500 would become "31.5 kHz".
265 * @param samplerate The samplerate in Hz.
267 * @return A newly allocated string representation of the samplerate value,
268 * or NULL upon errors. The caller is responsible to g_free() the
273 SR_API char *sr_samplerate_string(uint64_t samplerate)
275 return sr_si_string_u64(samplerate, "Hz");
279 * Convert a numeric frequency value to the "natural" string representation
282 * E.g. a value of 3000000 would be converted to "3 us", 20000 to "50 ms".
284 * @param frequency The frequency in Hz.
286 * @return A newly allocated string representation of the frequency value,
287 * or NULL upon errors. The caller is responsible to g_free() the
292 SR_API char *sr_period_string(uint64_t frequency)
297 /* Allocate enough for a uint64_t as string + " ms". */
298 o = g_malloc0(30 + 1);
300 if (frequency >= SR_GHZ(1))
301 r = snprintf(o, 30, "%" PRIu64 " ns", frequency / 1000000000);
302 else if (frequency >= SR_MHZ(1))
303 r = snprintf(o, 30, "%" PRIu64 " us", frequency / 1000000);
304 else if (frequency >= SR_KHZ(1))
305 r = snprintf(o, 30, "%" PRIu64 " ms", frequency / 1000);
307 r = snprintf(o, 30, "%" PRIu64 " s", frequency);
310 /* Something went wrong... */
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.
323 * E.g. a value of 300000 would be converted to "300mV", 2 to "2V".
325 * @param v_p The voltage numerator.
326 * @param v_q The voltage denominator.
328 * @return A newly allocated string representation of the voltage value,
329 * or NULL upon errors. The caller is responsible to g_free() the
334 SR_API char *sr_voltage_string(uint64_t v_p, uint64_t v_q)
339 o = g_malloc0(30 + 1);
342 r = snprintf(o, 30, "%" PRIu64 "mV", v_p);
344 r = snprintf(o, 30, "%" PRIu64 "V", v_p);
346 r = snprintf(o, 30, "%gV", (float)v_p / (float)v_q);
349 /* Something went wrong... */
358 * Convert a "natural" string representation of a size value to uint64_t.
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.
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.
367 * @param sizestring A string containing a (decimal) size value.
368 * @param size Pointer to uint64_t which will contain the string's size value.
370 * @return SR_OK upon success, SR_ERR upon errors.
374 SR_API int sr_parse_sizestring(const char *sizestring, uint64_t *size)
376 int multiplier, done;
380 *size = strtoull(sizestring, &s, 10);
384 while (s && *s && multiplier == 0 && !done) {
389 frac_part = g_ascii_strtod(s, &s);
393 multiplier = SR_KHZ(1);
397 multiplier = SR_MHZ(1);
401 multiplier = SR_GHZ(1);
409 if (multiplier > 0) {
411 *size += frac_part * multiplier;
415 if (s && *s && g_ascii_strcasecmp(s, "Hz"))
422 * Convert a "natural" string representation of a time value to an
423 * uint64_t value in milliseconds.
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.
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.
432 * @param timestring A string containing a (decimal) time value.
433 * @return The string's time value as uint64_t, in milliseconds.
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.
441 SR_API uint64_t sr_parse_timestring(const char *timestring)
446 /* TODO: Error handling, logging. */
448 time_msec = strtoull(timestring, &s, 10);
449 if (time_msec == 0 && s == timestring)
457 else if (!strcmp(s, "ms"))
467 SR_API gboolean sr_parse_boolstring(const char *boolstr)
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))
482 SR_API int sr_parse_period(const char *periodstr, uint64_t *p, uint64_t *q)
486 *p = strtoull(periodstr, &s, 10);
487 if (*p == 0 && s == periodstr)
488 /* No digits found. */
494 if (!strcmp(s, "fs"))
495 *q = 1000000000000000ULL;
496 else if (!strcmp(s, "ps"))
497 *q = 1000000000000ULL;
498 else if (!strcmp(s, "ns"))
500 else if (!strcmp(s, "us"))
502 else if (!strcmp(s, "ms"))
504 else if (!strcmp(s, "s"))
507 /* Must have a time suffix. */
515 SR_API int sr_parse_voltage(const char *voltstr, uint64_t *p, uint64_t *q)
519 *p = strtoull(voltstr, &s, 10);
520 if (*p == 0 && s == voltstr)
521 /* No digits found. */
527 if (!g_ascii_strcasecmp(s, "mv"))
529 else if (!g_ascii_strcasecmp(s, "v"))
532 /* Must have a base suffix. */