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, see <http://www.gnu.org/licenses/>.
20 /* Needed for POSIX.1-2008 locale functions */
22 #define _XOPEN_SOURCE 700
27 #if defined(__FreeBSD__) || defined(__APPLE__)
30 #if defined(__FreeBSD__)
31 #include <sys/param.h>
38 #include <libsigrok/libsigrok.h>
39 #include "libsigrok-internal.h"
42 #define LOG_PREFIX "strutil"
48 * Helper functions for handling or converting libsigrok-related strings.
52 * @defgroup grp_strutil String utilities
54 * Helper functions for handling or converting libsigrok-related strings.
60 * Convert a string representation of a numeric value (base 10) to a long integer. The
61 * conversion is strict and will fail if the complete string does not represent
62 * a valid long integer. The function sets errno according to the details of the
65 * @param str The string representation to convert.
66 * @param ret Pointer to long where the result of the conversion will be stored.
68 * @retval SR_OK Conversion successful.
69 * @retval SR_ERR Failure.
73 SR_PRIV int sr_atol(const char *str, long *ret)
79 tmp = strtol(str, &endptr, 10);
81 while (endptr && isspace(*endptr))
84 if (!endptr || *endptr || errno) {
95 * Convert a string representation of a numeric value (base 10) to an integer. The
96 * conversion is strict and will fail if the complete string does not represent
97 * a valid integer. The function sets errno according to the details of the
100 * @param str The string representation to convert.
101 * @param ret Pointer to int where the result of the conversion will be stored.
103 * @retval SR_OK Conversion successful.
104 * @retval SR_ERR Failure.
108 SR_PRIV int sr_atoi(const char *str, int *ret)
112 if (sr_atol(str, &tmp) != SR_OK)
115 if ((int) tmp != tmp) {
125 * Convert a string representation of a numeric value to a double. The
126 * conversion is strict and will fail if the complete string does not represent
127 * a valid double. The function sets errno according to the details of the
130 * @param str The string representation to convert.
131 * @param ret Pointer to double where the result of the conversion will be stored.
133 * @retval SR_OK Conversion successful.
134 * @retval SR_ERR Failure.
138 SR_PRIV int sr_atod(const char *str, double *ret)
144 tmp = strtof(str, &endptr);
146 while (endptr && isspace(*endptr))
149 if (!endptr || *endptr || errno) {
160 * Convert a string representation of a numeric value to a float. The
161 * conversion is strict and will fail if the complete string does not represent
162 * a valid float. The function sets errno according to the details of the
165 * @param str The string representation to convert.
166 * @param ret Pointer to float where the result of the conversion will be stored.
168 * @retval SR_OK Conversion successful.
169 * @retval SR_ERR Failure.
173 SR_PRIV int sr_atof(const char *str, float *ret)
177 if (sr_atod(str, &tmp) != SR_OK)
180 if ((float) tmp != tmp) {
190 * Convert a string representation of a numeric value to a double. The
191 * conversion is strict and will fail if the complete string does not represent
192 * a valid double. The function sets errno according to the details of the
193 * failure. This version ignores the locale.
195 * @param str The string representation to convert.
196 * @param ret Pointer to double where the result of the conversion will be stored.
198 * @retval SR_OK Conversion successful.
199 * @retval SR_ERR Failure.
203 SR_PRIV int sr_atod_ascii(const char *str, double *ret)
209 tmp = g_ascii_strtod(str, &endptr);
211 if (!endptr || *endptr || errno) {
222 * Convert a string representation of a numeric value to a float. The
223 * conversion is strict and will fail if the complete string does not represent
224 * a valid float. The function sets errno according to the details of the
225 * failure. This version ignores the locale.
227 * @param str The string representation to convert.
228 * @param ret Pointer to float where the result of the conversion will be stored.
230 * @retval SR_OK Conversion successful.
231 * @retval SR_ERR Failure.
235 SR_PRIV int sr_atof_ascii(const char *str, float *ret)
241 tmp = g_ascii_strtod(str, &endptr);
243 if (!endptr || *endptr || errno) {
249 /* FIXME This fails unexpectedly. Some other method to safel downcast
250 * needs to be found. Checking against FLT_MAX doesn't work as well. */
252 if ((float) tmp != tmp) {
254 sr_dbg("ERANGEEEE %e != %e", (float) tmp, tmp);
264 * Compose a string with a format string in the buffer pointed to by buf.
266 * It is up to the caller to ensure that the allocated buffer is large enough
267 * to hold the formatted result.
269 * A terminating NUL character is automatically appended after the content
272 * After the format parameter, the function expects at least as many additional
273 * arguments as needed for format.
275 * This version ignores the current locale and uses the locale "C" for Linux,
276 * FreeBSD, OSX and Android.
278 * @param buf Pointer to a buffer where the resulting C string is stored.
279 * @param format C string that contains a format string (see printf).
280 * @param ... A sequence of additional arguments, each containing a value to be
281 * used to replace a format specifier in the format string.
283 * @return On success, the number of characters that would have been written,
284 * not counting the terminating NUL character.
288 SR_API int sr_sprintf_ascii(char *buf, const char *format, ...)
293 va_start(args, format);
294 ret = sr_vsprintf_ascii(buf, format, args);
301 * Compose a string with a format string in the buffer pointed to by buf.
303 * It is up to the caller to ensure that the allocated buffer is large enough
304 * to hold the formatted result.
306 * Internally, the function retrieves arguments from the list identified by
307 * args as if va_arg was used on it, and thus the state of args is likely to
308 * be altered by the call.
310 * In any case, args should have been initialized by va_start at some point
311 * before the call, and it is expected to be released by va_end at some point
314 * This version ignores the current locale and uses the locale "C" for Linux,
315 * FreeBSD, OSX and Android.
317 * @param buf Pointer to a buffer where the resulting C string is stored.
318 * @param format C string that contains a format string (see printf).
319 * @param args A value identifying a variable arguments list initialized with
322 * @return On success, the number of characters that would have been written,
323 * not counting the terminating NUL character.
327 SR_API int sr_vsprintf_ascii(char *buf, const char *format, va_list args)
334 * TODO: This part compiles with mingw-w64 but doesn't run with Win7.
335 * Doesn't start because of "Procedure entry point _create_locale
336 * not found in msvcrt.dll".
337 * mingw-w64 should link to msvcr100.dll not msvcrt.dll!
338 * See: https://msdn.microsoft.com/en-us/en-en/library/1kt27hek.aspx
342 locale = _create_locale(LC_NUMERIC, "C");
343 ret = _vsprintf_l(buf, format, locale, args);
344 _free_locale(locale);
347 /* vsprintf() uses the current locale, may not work correctly for floats. */
348 ret = vsprintf(buf, format, args);
351 #elif defined(__APPLE__)
354 * https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man3/printf_l.3.html
355 * https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man3/xlocale.3.html
360 locale = newlocale(LC_NUMERIC_MASK, "C", NULL);
361 ret = vsprintf_l(buf, locale, format, args);
365 #elif defined(__FreeBSD__) && __FreeBSD_version >= 901000
368 * https://www.freebsd.org/cgi/man.cgi?query=printf_l&apropos=0&sektion=3&manpath=FreeBSD+9.1-RELEASE
369 * https://www.freebsd.org/cgi/man.cgi?query=xlocale&apropos=0&sektion=3&manpath=FreeBSD+9.1-RELEASE
374 locale = newlocale(LC_NUMERIC_MASK, "C", NULL);
375 ret = vsprintf_l(buf, locale, format, args);
379 #elif defined(__ANDROID__)
381 * The Bionic libc only has two locales ("C" aka "POSIX" and "C.UTF-8"
382 * aka "en_US.UTF-8"). The decimal point is hard coded as "."
383 * See: https://android.googlesource.com/platform/bionic/+/master/libc/bionic/locale.cpp
387 ret = vsprintf(buf, format, args);
390 #elif defined(__linux__)
392 locale_t old_locale, temp_locale;
394 /* Switch to C locale for proper float/double conversion. */
395 temp_locale = newlocale(LC_NUMERIC, "C", NULL);
396 old_locale = uselocale(temp_locale);
398 ret = vsprintf(buf, format, args);
400 /* Switch back to original locale. */
401 uselocale(old_locale);
402 freelocale(temp_locale);
405 #elif defined(__unix__) || defined(__unix)
407 * This is a fallback for all other BSDs, *nix and FreeBSD <= 9.0, by
408 * using the current locale for snprintf(). This may not work correctly
413 ret = vsprintf(buf, format, args);
417 /* No implementation for unknown systems! */
423 * Composes a string with a format string (like printf) in the buffer pointed
424 * by buf (taking buf_size as the maximum buffer capacity to fill).
425 * If the resulting string would be longer than n - 1 characters, the remaining
426 * characters are discarded and not stored, but counted for the value returned
428 * A terminating NUL character is automatically appended after the content
430 * After the format parameter, the function expects at least as many additional
431 * arguments as needed for format.
433 * This version ignores the current locale and uses the locale "C" for Linux,
434 * FreeBSD, OSX and Android.
436 * @param buf Pointer to a buffer where the resulting C string is stored.
437 * @param buf_size Maximum number of bytes to be used in the buffer. The
438 * generated string has a length of at most buf_size - 1, leaving space
439 * for the additional terminating NUL character.
440 * @param format C string that contains a format string (see printf).
441 * @param ... A sequence of additional arguments, each containing a value to be
442 * used to replace a format specifier in the format string.
444 * @return On success, the number of characters that would have been written if
445 * buf_size had been sufficiently large, not counting the terminating
446 * NUL character. On failure, a negative number is returned.
447 * Notice that only when this returned value is non-negative and less
448 * than buf_size, the string has been completely written.
452 SR_API int sr_snprintf_ascii(char *buf, size_t buf_size,
453 const char *format, ...)
458 va_start(args, format);
459 ret = sr_vsnprintf_ascii(buf, buf_size, format, args);
466 * Composes a string with a format string (like printf) in the buffer pointed
467 * by buf (taking buf_size as the maximum buffer capacity to fill).
468 * If the resulting string would be longer than n - 1 characters, the remaining
469 * characters are discarded and not stored, but counted for the value returned
471 * A terminating NUL character is automatically appended after the content
473 * Internally, the function retrieves arguments from the list identified by
474 * args as if va_arg was used on it, and thus the state of args is likely to
475 * be altered by the call.
476 * In any case, arg should have been initialized by va_start at some point
477 * before the call, and it is expected to be released by va_end at some point
480 * This version ignores the current locale and uses the locale "C" for Linux,
481 * FreeBSD, OSX and Android.
483 * @param buf Pointer to a buffer where the resulting C string is stored.
484 * @param buf_size Maximum number of bytes to be used in the buffer. The
485 * generated string has a length of at most buf_size - 1, leaving space
486 * for the additional terminating NUL character.
487 * @param format C string that contains a format string (see printf).
488 * @param args A value identifying a variable arguments list initialized with
491 * @return On success, the number of characters that would have been written if
492 * buf_size had been sufficiently large, not counting the terminating
493 * NUL character. On failure, a negative number is returned.
494 * Notice that only when this returned value is non-negative and less
495 * than buf_size, the string has been completely written.
499 SR_API int sr_vsnprintf_ascii(char *buf, size_t buf_size,
500 const char *format, va_list args)
507 * TODO: This part compiles with mingw-w64 but doesn't run with Win7.
508 * Doesn't start because of "Procedure entry point _create_locale
509 * not found in msvcrt.dll".
510 * mingw-w64 should link to msvcr100.dll not msvcrt.dll!.
511 * See: https://msdn.microsoft.com/en-us/en-en/library/1kt27hek.aspx
515 locale = _create_locale(LC_NUMERIC, "C");
516 ret = _vsnprintf_l(buf, buf_size, format, locale, args);
517 _free_locale(locale);
520 /* vsprintf uses the current locale, may cause issues for floats. */
521 ret = vsnprintf(buf, buf_size, format, args);
524 #elif defined(__APPLE__)
527 * https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man3/printf_l.3.html
528 * https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man3/xlocale.3.html
533 locale = newlocale(LC_NUMERIC_MASK, "C", NULL);
534 ret = vsnprintf_l(buf, buf_size, locale, format, args);
538 #elif defined(__FreeBSD__) && __FreeBSD_version >= 901000
541 * https://www.freebsd.org/cgi/man.cgi?query=printf_l&apropos=0&sektion=3&manpath=FreeBSD+9.1-RELEASE
542 * https://www.freebsd.org/cgi/man.cgi?query=xlocale&apropos=0&sektion=3&manpath=FreeBSD+9.1-RELEASE
547 locale = newlocale(LC_NUMERIC_MASK, "C", NULL);
548 ret = vsnprintf_l(buf, buf_size, locale, format, args);
552 #elif defined(__ANDROID__)
554 * The Bionic libc only has two locales ("C" aka "POSIX" and "C.UTF-8"
555 * aka "en_US.UTF-8"). The decimal point is hard coded as ".".
556 * See: https://android.googlesource.com/platform/bionic/+/master/libc/bionic/locale.cpp
560 ret = vsnprintf(buf, buf_size, format, args);
563 #elif defined(__linux__)
565 locale_t old_locale, temp_locale;
567 /* Switch to C locale for proper float/double conversion. */
568 temp_locale = newlocale(LC_NUMERIC, "C", NULL);
569 old_locale = uselocale(temp_locale);
571 ret = vsnprintf(buf, buf_size, format, args);
573 /* Switch back to original locale. */
574 uselocale(old_locale);
575 freelocale(temp_locale);
578 #elif defined(__unix__) || defined(__unix)
580 * This is a fallback for all other BSDs, *nix and FreeBSD <= 9.0, by
581 * using the current locale for snprintf(). This may not work correctly
586 ret = vsnprintf(buf, buf_size, format, args);
590 /* No implementation for unknown systems! */
596 * Convert a sequence of bytes to its textual representation ("hex dump").
598 * Callers should free the allocated GString. See sr_hexdump_free().
600 * @param[in] data Pointer to the byte sequence to print.
601 * @param[in] len Number of bytes to print.
603 * @return NULL upon error, newly allocated GString pointer otherwise.
607 SR_PRIV GString *sr_hexdump_new(const uint8_t *data, const size_t len)
612 s = g_string_sized_new(3 * len);
613 for (i = 0; i < len; i++) {
615 g_string_append_c(s, ' ');
616 g_string_append_printf(s, "%02x", data[i]);
623 * Free a hex dump text that was created by sr_hexdump_new().
625 * @param[in] s Pointer to the GString to release.
629 SR_PRIV void sr_hexdump_free(GString *s)
632 g_string_free(s, TRUE);
636 * Convert a string representation of a numeric value to a sr_rational.
638 * The conversion is strict and will fail if the complete string does not
639 * represent a valid number. The function sets errno according to the details
640 * of the failure. This version ignores the locale.
642 * @param str The string representation to convert.
643 * @param ret Pointer to sr_rational where the result of the conversion will be stored.
645 * @retval SR_OK Conversion successful.
646 * @retval SR_ERR Failure.
650 SR_API int sr_parse_rational(const char *str, struct sr_rational *ret)
654 int64_t fractional = 0;
655 int64_t denominator = 1;
656 int32_t fractional_len = 0;
657 int32_t exponent = 0;
658 gboolean is_negative = FALSE;
659 gboolean no_integer, no_fractional;
661 while (isspace(*str))
665 integral = g_ascii_strtoll(str, &endptr, 10);
667 if (str == endptr && (str[0] == '-' || str[0] == '+') && str[1] == '.') {
670 } else if (str == endptr && str[0] == '.') {
678 if (integral < 0 || str[0] == '-')
682 if (*endptr == '.') {
683 gboolean is_exp, is_eos;
684 const char *start = endptr + 1;
685 fractional = g_ascii_strtoll(start, &endptr, 10);
686 is_exp = *endptr == 'E' || *endptr == 'e';
687 is_eos = *endptr == '\0';
688 if (endptr == start && (is_exp || is_eos)) {
694 no_fractional = endptr == start;
695 if (no_integer && no_fractional)
697 fractional_len = endptr - start;
701 if ((*endptr == 'E') || (*endptr == 'e')) {
702 exponent = g_ascii_strtoll(endptr + 1, &endptr, 10);
710 for (int i = 0; i < fractional_len; i++)
712 exponent -= fractional_len;
715 integral += fractional;
717 integral -= fractional;
719 while (exponent > 0) {
724 while (exponent < 0) {
730 ret->q = denominator;
736 * Convert a numeric value value to its "natural" string representation
739 * E.g. a value of 3000000, with units set to "W", would be converted
740 * to "3 MW", 20000 to "20 kW", 31500 would become "31.5 kW".
742 * @param x The value to convert.
743 * @param unit The unit to append to the string, or NULL if the string
746 * @return A newly allocated string representation of the samplerate value,
747 * or NULL upon errors. The caller is responsible to g_free() the
752 SR_API char *sr_si_string_u64(uint64_t x, const char *unit)
755 uint64_t quot, divisor[] = {
756 SR_HZ(1), SR_KHZ(1), SR_MHZ(1), SR_GHZ(1),
757 SR_GHZ(1000), SR_GHZ(1000 * 1000), SR_GHZ(1000 * 1000 * 1000),
759 const char *p, prefix[] = "\0kMGTPE";
760 char fmt[16], fract[20] = "", *f;
765 for (i = 0; (quot = x / divisor[i]) >= 1000; i++);
768 sprintf(fmt, ".%%0%d"PRIu64, i * 3);
769 f = fract + sprintf(fract, fmt, x % divisor[i]) - 1;
771 while (f >= fract && strchr("0.", *f))
777 return g_strdup_printf("%" PRIu64 "%s %.1s%s", quot, fract, p, unit);
781 * Convert a numeric samplerate value to its "natural" string representation.
783 * E.g. a value of 3000000 would be converted to "3 MHz", 20000 to "20 kHz",
784 * 31500 would become "31.5 kHz".
786 * @param samplerate The samplerate in Hz.
788 * @return A newly allocated string representation of the samplerate value,
789 * or NULL upon errors. The caller is responsible to g_free() the
794 SR_API char *sr_samplerate_string(uint64_t samplerate)
796 return sr_si_string_u64(samplerate, "Hz");
800 * Convert a numeric period value to the "natural" string representation
801 * of its period value.
803 * The period is specified as a rational number's numerator and denominator.
805 * E.g. a pair of (1, 5) would be converted to "200 ms", (10, 100) to "100 ms".
807 * @param v_p The period numerator.
808 * @param v_q The period denominator.
810 * @return A newly allocated string representation of the period value,
811 * or NULL upon errors. The caller is responsible to g_free() the
816 SR_API char *sr_period_string(uint64_t v_p, uint64_t v_q)
821 freq = 1 / ((double)v_p / v_q);
823 if (freq > SR_GHZ(1)) {
824 v = (double)v_p / v_q * 1000000000000.0;
825 prec = ((v - (uint64_t)v) < FLT_MIN) ? 0 : 3;
826 return g_strdup_printf("%.*f ps", prec, v);
827 } else if (freq > SR_MHZ(1)) {
828 v = (double)v_p / v_q * 1000000000.0;
829 prec = ((v - (uint64_t)v) < FLT_MIN) ? 0 : 3;
830 return g_strdup_printf("%.*f ns", prec, v);
831 } else if (freq > SR_KHZ(1)) {
832 v = (double)v_p / v_q * 1000000.0;
833 prec = ((v - (uint64_t)v) < FLT_MIN) ? 0 : 3;
834 return g_strdup_printf("%.*f us", prec, v);
835 } else if (freq > 1) {
836 v = (double)v_p / v_q * 1000.0;
837 prec = ((v - (uint64_t)v) < FLT_MIN) ? 0 : 3;
838 return g_strdup_printf("%.*f ms", prec, v);
840 v = (double)v_p / v_q;
841 prec = ((v - (uint64_t)v) < FLT_MIN) ? 0 : 3;
842 return g_strdup_printf("%.*f s", prec, v);
847 * Convert a numeric voltage value to the "natural" string representation
848 * of its voltage value. The voltage is specified as a rational number's
849 * numerator and denominator.
851 * E.g. a value of 300000 would be converted to "300mV", 2 to "2V".
853 * @param v_p The voltage numerator.
854 * @param v_q The voltage denominator.
856 * @return A newly allocated string representation of the voltage value,
857 * or NULL upon errors. The caller is responsible to g_free() the
862 SR_API char *sr_voltage_string(uint64_t v_p, uint64_t v_q)
865 return g_strdup_printf("%" PRIu64 " mV", v_p);
867 return g_strdup_printf("%" PRIu64 " V", v_p);
869 return g_strdup_printf("%g V", (float)v_p / (float)v_q);
873 * Convert a "natural" string representation of a size value to uint64_t.
875 * E.g. a value of "3k" or "3 K" would be converted to 3000, a value
876 * of "15M" would be converted to 15000000.
878 * Value representations other than decimal (such as hex or octal) are not
879 * supported. Only 'k' (kilo), 'm' (mega), 'g' (giga) suffixes are supported.
880 * Spaces (but not other whitespace) between value and suffix are allowed.
882 * @param sizestring A string containing a (decimal) size value.
883 * @param size Pointer to uint64_t which will contain the string's size value.
885 * @return SR_OK upon success, SR_ERR upon errors.
889 SR_API int sr_parse_sizestring(const char *sizestring, uint64_t *size)
896 *size = strtoull(sizestring, &s, 10);
900 while (s && *s && multiplier == 0 && !done) {
905 frac_part = g_ascii_strtod(s, &s);
909 multiplier = SR_KHZ(1);
913 multiplier = SR_MHZ(1);
917 multiplier = SR_GHZ(1);
921 multiplier = SR_GHZ(1000);
925 multiplier = SR_GHZ(1000 * 1000);
929 multiplier = SR_GHZ(1000 * 1000 * 1000);
937 if (multiplier > 0) {
939 *size += frac_part * multiplier;
944 if (s && *s && g_ascii_strcasecmp(s, "Hz"))
951 * Convert a "natural" string representation of a time value to an
952 * uint64_t value in milliseconds.
954 * E.g. a value of "3s" or "3 s" would be converted to 3000, a value
955 * of "15ms" would be converted to 15.
957 * Value representations other than decimal (such as hex or octal) are not
958 * supported. Only lower-case "s" and "ms" time suffixes are supported.
959 * Spaces (but not other whitespace) between value and suffix are allowed.
961 * @param timestring A string containing a (decimal) time value.
962 * @return The string's time value as uint64_t, in milliseconds.
964 * @todo Add support for "m" (minutes) and others.
965 * @todo Add support for picoseconds?
966 * @todo Allow both lower-case and upper-case? If no, document it.
970 SR_API uint64_t sr_parse_timestring(const char *timestring)
975 /* TODO: Error handling, logging. */
977 time_msec = strtoull(timestring, &s, 10);
978 if (time_msec == 0 && s == timestring)
986 else if (!strcmp(s, "ms"))
996 SR_API gboolean sr_parse_boolstring(const char *boolstr)
999 * Complete absence of an input spec is assumed to mean TRUE,
1000 * as in command line option strings like this:
1001 * ...:samplerate=100k:header:numchannels=4:...
1003 if (!boolstr || !*boolstr)
1006 if (!g_ascii_strncasecmp(boolstr, "true", 4) ||
1007 !g_ascii_strncasecmp(boolstr, "yes", 3) ||
1008 !g_ascii_strncasecmp(boolstr, "on", 2) ||
1009 !g_ascii_strncasecmp(boolstr, "1", 1))
1016 SR_API int sr_parse_period(const char *periodstr, uint64_t *p, uint64_t *q)
1020 *p = strtoull(periodstr, &s, 10);
1021 if (*p == 0 && s == periodstr)
1022 /* No digits found. */
1028 if (!strcmp(s, "fs"))
1029 *q = UINT64_C(1000000000000000);
1030 else if (!strcmp(s, "ps"))
1031 *q = UINT64_C(1000000000000);
1032 else if (!strcmp(s, "ns"))
1033 *q = UINT64_C(1000000000);
1034 else if (!strcmp(s, "us"))
1036 else if (!strcmp(s, "ms"))
1038 else if (!strcmp(s, "s"))
1041 /* Must have a time suffix. */
1049 SR_API int sr_parse_voltage(const char *voltstr, uint64_t *p, uint64_t *q)
1053 *p = strtoull(voltstr, &s, 10);
1054 if (*p == 0 && s == voltstr)
1055 /* No digits found. */
1061 if (!g_ascii_strcasecmp(s, "mv"))
1063 else if (!g_ascii_strcasecmp(s, "v"))
1066 /* Must have a base suffix. */