feat: add cli_parse_int64 and fix parse_float for negative values

This commit is contained in:
lohhiiccc 2026-03-29 03:27:25 +02:00
parent 4589912088
commit 3336ac272c
5 changed files with 31 additions and 6 deletions

View file

@ -4,6 +4,7 @@
#include <stdint.h>
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);
#endif

View file

@ -2,6 +2,8 @@ lib_LTLIBRARIES = libcli.la
libcli_la_SOURCES = \
parse.c \
help.c \
parse_utils/parse_uint.c \
parse_utils/parse_int.c \
parse_utils/parse_float.c

View file

@ -7,7 +7,7 @@ cli_parse_float(const char *s, float *out)
{
char *end;
if (NULL == s || '\0' == *s || '-' == *s)
if (NULL == s || '\0' == *s)
return 1;
errno = 0;

View file

@ -1,21 +1,22 @@
#include <errno.h>
#include <inttypes.h>
#include <stddef.h>
#include <stdint.h>
int
cli_parse_uint64(const char *s, uint64_t *out)
cli_parse_int64(const char *s, int64_t *out)
{
char *end;
if ( NULL == s || '\0' == *s || '-' == *s)
if (NULL == s || '\0' == *s)
return 1;
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;
*out = (uint64_t)v;
*out = (int64_t)v;
return 0;
}

View 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;
}