X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fstrutil.c;h=93e424ee1f047d7ba96a23c4b657450597a2c94c;hb=5e7377f4c75ed3fdfc391be86b83a2266934e20a;hp=b6c910f70ca1b70ccdad881251b4de1178b56fb3;hpb=d87549636609d2a01722eb6ad185cd4666d04ebb;p=libsigrok.git diff --git a/src/strutil.c b/src/strutil.c index b6c910f7..93e424ee 100644 --- a/src/strutil.c +++ b/src/strutil.c @@ -33,7 +33,6 @@ #include #include #include -#include #include #include "libsigrok-internal.h" @@ -591,6 +590,42 @@ 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 @ref 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. + */ +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 @ref sr_hexdump_new(). + * + * @param[in] s Pointer to the GString to release. + */ +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 +649,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 +660,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';