Commit e0a3775d authored by Matteo Quintiliani's avatar Matteo Quintiliani

Add nmxptool_parse_long() and nmxptool_parse_int() as wrapper to strtol(). Part of issue #3

parent b1dcdce9
......@@ -599,6 +599,43 @@ int nmxptool_read_time(char *str_input, int32_t *pvalue) {
return ret_errors;
}
int nmxptool_parse_long(const char *str, long *val)
{
char *temp;
int rc = 1;
errno = 0;
*val = strtol(str, &temp, 0);
if (temp == str || *temp != '\0' ||
( (*val == LONG_MIN || *val == LONG_MAX) && errno == ERANGE)
) {
rc = 0;
}
return rc;
}
int nmxptool_parse_int(const char *str, int *val)
{
long v = 0;
int rc = 1;
rc = nmxptool_parse_long(str, &v);
if(rc) {
if(v >= INT_MIN && v <= INT_MAX) {
*val = v;
} else {
rc = 0;
}
}
return rc;
}
int nmxptool_getopt_long(int argc, char **argv, NMXPTOOL_PARAMS *params)
{
int ret_errors = 0;
......
......@@ -198,6 +198,28 @@ int nmxptool_read_time(char *str_input, int32_t *pvalue);
int nmxptool_getopt_long(int argc, char **argv, NMXPTOOL_PARAMS *params);
/*! \brief Wrapper to strtol()
*
* \param str
* \param[out] val
*
* \retval 1 on success.
* \retval 0 on failure.
*
*/
int nmxptool_parse_long(const char *str, long *val);
/*! \brief Wrapper to nmxptool_parse_long() to parse int value
*
* \param str
* \param[out] val
*
* \retval 1 on success.
* \retval 0 on failure.
*
*/
int nmxptool_parse_int(const char *str, int *val);
/*! \brief Print value of NMXPTOOL_PARAMS
*
* \param params Struct to validate.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment