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
26 #include <libsigrok/libsigrok.h>
27 #include "libsigrok-internal.h"
30 #define LOG_PREFIX "strutil"
36 * Helper functions for handling or converting libsigrok-related strings.
40 * @defgroup grp_strutil String utilities
42 * Helper functions for handling or converting libsigrok-related strings.
50 * Convert a string representation of a numeric value (base 10) to a long integer. The
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
55 * @param str The string representation to convert.
56 * @param ret Pointer to long where the result of the conversion will be stored.
58 * @retval SR_OK Conversion successful.
59 * @retval SR_ERR Failure.
63 SR_PRIV int sr_atol(const char *str, long *ret)
69 tmp = strtol(str, &endptr, 10);
71 if (!endptr || *endptr || errno) {
84 * Convert a string representation of a numeric value (base 10) to an integer. The
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
89 * @param str The string representation to convert.
90 * @param ret Pointer to int where the result of the conversion will be stored.
92 * @retval SR_OK Conversion successful.
93 * @retval SR_ERR Failure.
97 SR_PRIV int sr_atoi(const char *str, int *ret)
101 if (sr_atol(str, &tmp) != SR_OK)
104 if ((int) tmp != tmp) {
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
121 * @param str The string representation to convert.
122 * @param ret Pointer to double where the result of the conversion will be stored.
124 * @retval SR_OK Conversion successful.
125 * @retval SR_ERR Failure.
129 SR_PRIV int sr_atod(const char *str, double *ret)
135 tmp = strtof(str, &endptr);
137 if (!endptr || *endptr || errno) {
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
155 * @param str The string representation to convert.
156 * @param ret Pointer to float where the result of the conversion will be stored.
158 * @retval SR_OK Conversion successful.
159 * @retval SR_ERR Failure.
163 SR_PRIV int sr_atof(const char *str, float *ret)
167 if (sr_atod(str, &tmp) != SR_OK)
170 if ((float) tmp != tmp) {
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.
187 * @param str The string representation to convert.
188 * @param ret Pointer to float where the result of the conversion will be stored.
190 * @retval SR_OK Conversion successful.
191 * @retval SR_ERR Failure.
195 SR_PRIV int sr_atof_ascii(const char *str, float *ret)
201 tmp = g_ascii_strtod(str, &endptr);
203 if (!endptr || *endptr || errno) {
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. */
212 if ((float) tmp != tmp) {
214 sr_dbg("ERANGEEEE %e != %e", (float) tmp, tmp);
224 * Convert a numeric value value to its "natural" string representation
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".
230 * @param x The value to convert.
231 * @param unit The unit to append to the string, or NULL if the string
234 * @return A newly allocated string representation of the samplerate value,
235 * or NULL upon errors. The caller is responsible to g_free() the
240 SR_API char *sr_si_string_u64(uint64_t x, const char *unit)
243 uint64_t quot, divisor[] = {
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),
247 const char *p, prefix[] = "\0kMGTPE";
248 char fmt[16], fract[20] = "", *f;
253 for (i = 0; (quot = x / divisor[i]) >= 1000; i++);
256 sprintf(fmt, ".%%0%d"PRIu64, i * 3);
257 f = fract + sprintf(fract, fmt, x % divisor[i]) - 1;
259 while (f >= fract && strchr("0.", *f))
265 return g_strdup_printf("%" PRIu64 "%s %.1s%s", quot, fract, p, unit);
269 * Convert a numeric samplerate value to its "natural" string representation.
271 * E.g. a value of 3000000 would be converted to "3 MHz", 20000 to "20 kHz",
272 * 31500 would become "31.5 kHz".
274 * @param samplerate The samplerate in Hz.
276 * @return A newly allocated string representation of the samplerate value,
277 * or NULL upon errors. The caller is responsible to g_free() the
282 SR_API char *sr_samplerate_string(uint64_t samplerate)
284 return sr_si_string_u64(samplerate, "Hz");
288 * Convert a numeric frequency value to the "natural" string representation
291 * E.g. a value of 3000000 would be converted to "3 us", 20000 to "50 ms".
293 * @param frequency The frequency in Hz.
295 * @return A newly allocated string representation of the frequency value,
296 * or NULL upon errors. The caller is responsible to g_free() the
301 SR_API char *sr_period_string(uint64_t frequency)
306 /* Allocate enough for a uint64_t as string + " ms". */
307 o = g_malloc0(30 + 1);
309 if (frequency >= SR_GHZ(1))
310 r = snprintf(o, 30, "%" PRIu64 " ns", frequency / 1000000000);
311 else if (frequency >= SR_MHZ(1))
312 r = snprintf(o, 30, "%" PRIu64 " us", frequency / 1000000);
313 else if (frequency >= SR_KHZ(1))
314 r = snprintf(o, 30, "%" PRIu64 " ms", frequency / 1000);
316 r = snprintf(o, 30, "%" PRIu64 " s", frequency);
319 /* Something went wrong... */
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.
332 * E.g. a value of 300000 would be converted to "300mV", 2 to "2V".
334 * @param v_p The voltage numerator.
335 * @param v_q The voltage denominator.
337 * @return A newly allocated string representation of the voltage value,
338 * or NULL upon errors. The caller is responsible to g_free() the
343 SR_API char *sr_voltage_string(uint64_t v_p, uint64_t v_q)
348 o = g_malloc0(30 + 1);
351 r = snprintf(o, 30, "%" PRIu64 "mV", v_p);
353 r = snprintf(o, 30, "%" PRIu64 "V", v_p);
355 r = snprintf(o, 30, "%gV", (float)v_p / (float)v_q);
358 /* Something went wrong... */
367 * Convert a "natural" string representation of a size value to uint64_t.
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.
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.
376 * @param sizestring A string containing a (decimal) size value.
377 * @param size Pointer to uint64_t which will contain the string's size value.
379 * @return SR_OK upon success, SR_ERR upon errors.
383 SR_API int sr_parse_sizestring(const char *sizestring, uint64_t *size)
385 int multiplier, done;
389 *size = strtoull(sizestring, &s, 10);
393 while (s && *s && multiplier == 0 && !done) {
398 frac_part = g_ascii_strtod(s, &s);
402 multiplier = SR_KHZ(1);
406 multiplier = SR_MHZ(1);
410 multiplier = SR_GHZ(1);
418 if (multiplier > 0) {
420 *size += frac_part * multiplier;
424 if (s && *s && strcasecmp(s, "Hz"))
431 * Convert a "natural" string representation of a time value to an
432 * uint64_t value in milliseconds.
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.
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.
441 * @param timestring A string containing a (decimal) time value.
442 * @return The string's time value as uint64_t, in milliseconds.
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.
450 SR_API uint64_t sr_parse_timestring(const char *timestring)
455 /* TODO: Error handling, logging. */
457 time_msec = strtoull(timestring, &s, 10);
458 if (time_msec == 0 && s == timestring)
466 else if (!strcmp(s, "ms"))
476 SR_API gboolean sr_parse_boolstring(const char *boolstr)
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))
491 SR_API int sr_parse_period(const char *periodstr, uint64_t *p, uint64_t *q)
495 *p = strtoull(periodstr, &s, 10);
496 if (*p == 0 && s == periodstr)
497 /* No digits found. */
503 if (!strcmp(s, "fs"))
504 *q = 1000000000000000ULL;
505 else if (!strcmp(s, "ps"))
506 *q = 1000000000000ULL;
507 else if (!strcmp(s, "ns"))
509 else if (!strcmp(s, "us"))
511 else if (!strcmp(s, "ms"))
513 else if (!strcmp(s, "s"))
516 /* Must have a time suffix. */
524 SR_API int sr_parse_voltage(const char *voltstr, uint64_t *p, uint64_t *q)
528 *p = strtoull(voltstr, &s, 10);
529 if (*p == 0 && s == voltstr)
530 /* No digits found. */
536 if (!strcasecmp(s, "mv"))
538 else if (!strcasecmp(s, "v"))
541 /* Must have a base suffix. */