X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fstrutil.c;h=0dff3c4b48c0bf8df4014f73bddde9dd279644c3;hb=1f8ef363cb87ff425dec2b81f13e06bb15ed6e9c;hp=b6c910f70ca1b70ccdad881251b4de1178b56fb3;hpb=d87549636609d2a01722eb6ad185cd4666d04ebb;p=libsigrok.git diff --git a/src/strutil.c b/src/strutil.c index b6c910f7..0dff3c4b 100644 --- a/src/strutil.c +++ b/src/strutil.c @@ -18,7 +18,9 @@ */ /* Needed for POSIX.1-2008 locale functions */ +/** @cond PRIVATE */ #define _XOPEN_SOURCE 700 +/** @endcond */ #include #include #include @@ -33,7 +35,6 @@ #include #include #include -#include #include #include "libsigrok-internal.h" @@ -56,8 +57,6 @@ */ /** - * @private - * * Convert a string representation of a numeric value (base 10) to a long integer. The * conversion is strict and will fail if the complete string does not represent * a valid long integer. The function sets errno according to the details of the @@ -68,6 +67,8 @@ * * @retval SR_OK Conversion successful. * @retval SR_ERR Failure. + * + * @private */ SR_PRIV int sr_atol(const char *str, long *ret) { @@ -91,8 +92,60 @@ SR_PRIV int sr_atol(const char *str, long *ret) } /** + * Convert a text to a number including support for non-decimal bases. + * Also optionally returns the position after the number, where callers + * can either error out, or support application specific suffixes. + * + * @param[in] str The input text to convert. + * @param[out] ret The conversion result. + * @param[out] end The position after the number. + * @param[in] base The number format's base, can be 0. + * + * @retval SR_OK Conversion successful. + * @retval SR_ERR Conversion failed. + * * @private * + * This routine is more general than @ref sr_atol(), which strictly + * expects the input text to contain just a decimal number, and nothing + * else in addition. The @ref sr_atol_base() routine accepts trailing + * text after the number, and supports non-decimal numbers (bin, hex), + * including automatic detection from prefix text. + */ +SR_PRIV int sr_atol_base(const char *str, long *ret, char **end, int base) +{ + long num; + char *endptr; + + /* Add "0b" prefix support which strtol(3) may be missing. */ + while (str && isspace(*str)) + str++; + if (!base && strncmp(str, "0b", strlen("0b")) == 0) { + str += strlen("0b"); + base = 2; + } + + /* Run the number conversion. Quick bail out if that fails. */ + errno = 0; + endptr = NULL; + num = strtol(str, &endptr, base); + if (!endptr || errno) { + if (!errno) + errno = EINVAL; + return SR_ERR; + } + *ret = num; + + /* Advance to optional non-space trailing suffix. */ + while (endptr && isspace(*endptr)) + endptr++; + if (end) + *end = endptr; + + return SR_OK; +} + +/** * Convert a string representation of a numeric value (base 10) to an integer. The * conversion is strict and will fail if the complete string does not represent * a valid integer. The function sets errno according to the details of the @@ -103,6 +156,8 @@ SR_PRIV int sr_atol(const char *str, long *ret) * * @retval SR_OK Conversion successful. * @retval SR_ERR Failure. + * + * @private */ SR_PRIV int sr_atoi(const char *str, int *ret) { @@ -121,8 +176,6 @@ SR_PRIV int sr_atoi(const char *str, int *ret) } /** - * @private - * * Convert a string representation of a numeric value to a double. The * conversion is strict and will fail if the complete string does not represent * a valid double. The function sets errno according to the details of the @@ -133,6 +186,8 @@ SR_PRIV int sr_atoi(const char *str, int *ret) * * @retval SR_OK Conversion successful. * @retval SR_ERR Failure. + * + * @private */ SR_PRIV int sr_atod(const char *str, double *ret) { @@ -156,8 +211,6 @@ SR_PRIV int sr_atod(const char *str, double *ret) } /** - * @private - * * Convert a string representation of a numeric value to a float. The * conversion is strict and will fail if the complete string does not represent * a valid float. The function sets errno according to the details of the @@ -168,6 +221,8 @@ SR_PRIV int sr_atod(const char *str, double *ret) * * @retval SR_OK Conversion successful. * @retval SR_ERR Failure. + * + * @private */ SR_PRIV int sr_atof(const char *str, float *ret) { @@ -186,8 +241,6 @@ SR_PRIV int sr_atof(const char *str, float *ret) } /** - * @private - * * Convert a string representation of a numeric value to a double. The * conversion is strict and will fail if the complete string does not represent * a valid double. The function sets errno according to the details of the @@ -198,6 +251,8 @@ SR_PRIV int sr_atof(const char *str, float *ret) * * @retval SR_OK Conversion successful. * @retval SR_ERR Failure. + * + * @private */ SR_PRIV int sr_atod_ascii(const char *str, double *ret) { @@ -218,8 +273,6 @@ SR_PRIV int sr_atod_ascii(const char *str, double *ret) } /** - * @private - * * Convert a string representation of a numeric value to a float. The * conversion is strict and will fail if the complete string does not represent * a valid float. The function sets errno according to the details of the @@ -230,6 +283,8 @@ SR_PRIV int sr_atod_ascii(const char *str, double *ret) * * @retval SR_OK Conversion successful. * @retval SR_ERR Failure. + * + * @private */ SR_PRIV int sr_atof_ascii(const char *str, float *ret) { @@ -591,6 +646,46 @@ SR_API int sr_vsnprintf_ascii(char *buf, size_t buf_size, #endif } +/** + * Convert a sequence of bytes to its textual representation ("hex dump"). + * + * Callers should free the allocated GString. See sr_hexdump_free(). + * + * @param[in] data Pointer to the byte sequence to print. + * @param[in] len Number of bytes to print. + * + * @return NULL upon error, newly allocated GString pointer otherwise. + * + * @private + */ +SR_PRIV GString *sr_hexdump_new(const uint8_t *data, const size_t len) +{ + GString *s; + size_t i; + + s = g_string_sized_new(3 * len); + for (i = 0; i < len; i++) { + if (i) + g_string_append_c(s, ' '); + g_string_append_printf(s, "%02x", data[i]); + } + + return s; +} + +/** + * Free a hex dump text that was created by sr_hexdump_new(). + * + * @param[in] s Pointer to the GString to release. + * + * @private + */ +SR_PRIV void sr_hexdump_free(GString *s) +{ + if (s) + g_string_free(s, TRUE); +} + /** * Convert a string representation of a numeric value to a sr_rational. * @@ -614,8 +709,8 @@ SR_API int sr_parse_rational(const char *str, struct sr_rational *ret) int64_t denominator = 1; int32_t fractional_len = 0; int32_t exponent = 0; - bool is_negative = false; - bool no_integer, no_fractional; + gboolean is_negative = FALSE; + gboolean no_integer, no_fractional; while (isspace(*str)) str++; @@ -625,21 +720,21 @@ SR_API int sr_parse_rational(const char *str, struct sr_rational *ret) if (str == endptr && (str[0] == '-' || str[0] == '+') && str[1] == '.') { endptr += 1; - no_integer = true; + no_integer = TRUE; } else if (str == endptr && str[0] == '.') { - no_integer = true; + no_integer = TRUE; } else if (errno) { return SR_ERR; } else { - no_integer = false; + no_integer = FALSE; } if (integral < 0 || str[0] == '-') - is_negative = true; + is_negative = TRUE; errno = 0; if (*endptr == '.') { - bool is_exp, is_eos; + gboolean is_exp, is_eos; const char *start = endptr + 1; fractional = g_ascii_strtoll(start, &endptr, 10); is_exp = *endptr == 'E' || *endptr == 'e';