From: poljar (Damir Jelić) Date: Fri, 1 Nov 2013 18:25:32 +0000 (+0100) Subject: scpi: Add function to strictly parse bool strings. X-Git-Tag: libsigrok-0.3.0~513 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=aa1e3b400b95b7f16716bfd65a2ebbf870e42fa5;p=libsigrok.git scpi: Add function to strictly parse bool strings. This patch adds a function that is similar to sr_parse_boolstring but its matching rules are more strict. --- diff --git a/hardware/common/scpi.c b/hardware/common/scpi.c index d8f5a2b1..f932854f 100644 --- a/hardware/common/scpi.c +++ b/hardware/common/scpi.c @@ -34,6 +34,46 @@ #define SCPI_READ_RETRIES 100 #define SCPI_READ_RETRY_TIMEOUT 10000 +/** + * Parse a string representation of a boolean-like value into a gboolean. + * Similar to sr_parse_boolstring but rejects strings which do not represent + * a boolean-like value. + * + * @param str String to convert. + * @param ret Pointer to a gboolean where the result of the conversion will be + * stored. + * + * @return SR_OK on success, SR_ERR on failure. + */ +static int sr_parse_strict_bool(const char *str, gboolean *ret) +{ + if (!str) + return SR_ERR_ARG; + + if (!g_strcmp0(str, "1") || + !g_ascii_strncasecmp(str, "y", 1) || + !g_ascii_strncasecmp(str, "t", 1) || + !g_ascii_strncasecmp(str, "yes", 3) || + !g_ascii_strncasecmp(str, "true", 4) || + !g_ascii_strncasecmp(str, "on", 2)) { + + *ret = TRUE; + return SR_OK; + + } else if (!g_strcmp0(str, "0") || + !g_ascii_strncasecmp(str, "n", 1) || + !g_ascii_strncasecmp(str, "f", 1) || + !g_ascii_strncasecmp(str, "no", 2) || + !g_ascii_strncasecmp(str, "false", 5) || + !g_ascii_strncasecmp(str, "off", 3)) { + + *ret = FALSE; + return SR_OK; + } + + return SR_ERR; +} + /** * Send a SCPI command. *