feat: add cli_parse_ufloat for non-negative float parsing
This commit is contained in:
parent
960ed4584f
commit
a1bb371d3a
3 changed files with 24 additions and 1 deletions
|
|
@ -6,5 +6,6 @@
|
||||||
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_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);
|
||||||
|
int cli_parse_ufloat(const char *s, float *out);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,8 @@ libcli_la_SOURCES = \
|
||||||
help.c \
|
help.c \
|
||||||
parse_utils/parse_uint.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 \
|
||||||
|
parse_utils/parse_ufloat.c
|
||||||
|
|
||||||
libcli_la_CPPFLAGS = -I$(top_srcdir)/include
|
libcli_la_CPPFLAGS = -I$(top_srcdir)/include
|
||||||
libcli_la_CFLAGS = -std=c99 -Wall -Wextra
|
libcli_la_CFLAGS = -std=c99 -Wall -Wextra
|
||||||
|
|
|
||||||
21
src/parse_utils/parse_ufloat.c
Normal file
21
src/parse_utils/parse_ufloat.c
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
int
|
||||||
|
cli_parse_ufloat(const char *s, float *out)
|
||||||
|
{
|
||||||
|
char *end;
|
||||||
|
|
||||||
|
if (NULL == s || '\0' == *s)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
errno = 0;
|
||||||
|
float v = strtof(s, &end);
|
||||||
|
|
||||||
|
if (s == end || ERANGE == errno || '\0' != *end || 0 == isfinite(v) || v < 0.0f)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
*out = v;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue