3 ms·
Often one can ignore the errno case, as the input is (by then) semi-constrained, and its ambiguity resolution is not needed. e.g. so an idomatic pattern from s
by dfawcus 5y ago
Often one can ignore the errno case, as the input is (by then) semi-constrained, and its ambiguity resolution is not needed.
e.g. so an idomatic pattern from some real code would be something like:
static bool
parse_thing (char *value, struct thing *th)
{
char *endp = NULL;
unsigned long firstport = strtoul(value, &endp, 10);
if (endp == value || *endp || firstport > 0xffff) {
/* Do something with error, like log it */
return false;
}
th->firstport = firstport;
return true;
}
But granted, for the general case I'd prefer to use some helper like either of:
bool parse_decimal_uint32(struct string const *str, uint32_t *outval);
bool parse_decimal_uint32(char const *str, unsigned slen, uint32_t *outval);
- kazinator 5y agoIf you can ignore the errno case due to a "semi-constrainted" input, and don't care about trailing junk or having a pointer to more string material after the number is scanned, you can just call atol(str).