feat: add cli_parse_int64 and fix parse_float for negative values
This commit is contained in:
parent
a0d41dc9b0
commit
64794a97a4
5 changed files with 31 additions and 6 deletions
|
|
@ -4,6 +4,7 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
int cli_parse_uint64(const char *s, uint64_t *out);
|
int cli_parse_uint64(const char *s, uint64_t *out);
|
||||||
|
int cli_parse_int64(const char *s, int64_t *out);
|
||||||
int cli_parse_float(const char *s, float *out);
|
int cli_parse_float(const char *s, float *out);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@ lib_LTLIBRARIES = libcli.la
|
||||||
|
|
||||||
libcli_la_SOURCES = \
|
libcli_la_SOURCES = \
|
||||||
parse.c \
|
parse.c \
|
||||||
|
help.c \
|
||||||
|
parse_utils/parse_uint.c \
|
||||||
parse_utils/parse_int.c \
|
parse_utils/parse_int.c \
|
||||||
parse_utils/parse_float.c
|
parse_utils/parse_float.c
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ cli_parse_float(const char *s, float *out)
|
||||||
{
|
{
|
||||||
char *end;
|
char *end;
|
||||||
|
|
||||||
if (NULL == s || '\0' == *s || '-' == *s)
|
if (NULL == s || '\0' == *s)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
|
|
|
||||||
|
|
@ -1,21 +1,22 @@
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
int
|
int
|
||||||
cli_parse_uint64(const char *s, uint64_t *out)
|
cli_parse_int64(const char *s, int64_t *out)
|
||||||
{
|
{
|
||||||
char *end;
|
char *end;
|
||||||
|
|
||||||
if ( NULL == s || '\0' == *s || '-' == *s)
|
if (NULL == s || '\0' == *s)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
const uintmax_t v = (uintmax_t)strtoumax(s, &end, 10);
|
const intmax_t v = strtoimax(s, &end, 10);
|
||||||
|
|
||||||
if (s == end || ERANGE == errno || '\0' != *end || v > UINT64_MAX)
|
if (s == end || ERANGE == errno || '\0' != *end || v < INT64_MIN || v > INT64_MAX)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
*out = (uint64_t)v;
|
*out = (int64_t)v;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
21
src/parse_utils/parse_uint.c
Normal file
21
src/parse_utils/parse_uint.c
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
#include <errno.h>
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
int
|
||||||
|
cli_parse_uint64(const char *s, uint64_t *out)
|
||||||
|
{
|
||||||
|
char *end;
|
||||||
|
|
||||||
|
if ( NULL == s || '\0' == *s || '-' == *s)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
errno = 0;
|
||||||
|
const uintmax_t v = (uintmax_t)strtoumax(s, &end, 10);
|
||||||
|
|
||||||
|
if (s == end || ERANGE == errno || '\0' != *end || v > UINT64_MAX)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
*out = (uint64_t)v;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue