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.
This commit is contained in:
poljar (Damir Jelić) 2013-11-01 19:25:32 +01:00 committed by Uwe Hermann
parent 7b9d732031
commit aa1e3b400b
1 changed files with 40 additions and 0 deletions

View File

@ -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.
*