X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Fopt%2Fhelpers.c;h=c557f96d8488d01d3e1297ea235f6c2c8b76fb4f;hp=48f4fa45991ed1809438f55ae4f840485b0d3946;hb=63b0055673d2c55ff90f8b7bd8a7c5fe30f83269;hpb=2179b06e713677476a5a37acbf179b8d5be8693b;ds=sidebyside diff --git a/ccan/opt/helpers.c b/ccan/opt/helpers.c index 48f4fa45..c557f96d 100644 --- a/ccan/opt/helpers.c +++ b/ccan/opt/helpers.c @@ -1,4 +1,4 @@ -/* Licensed under GPLv3+ - see LICENSE file for details */ +/* Licensed under GPLv2+ - see LICENSE file for details */ #include #include #include @@ -116,12 +116,61 @@ char *opt_set_ulongval(const char *arg, unsigned long *ul) return NULL; } +char *opt_set_floatval(const char *arg, float *f) +{ + double d; + char *err; + + err = opt_set_doubleval(arg, &d); + if (err) + return err; + + *f = d; + if (*f != d) + return arg_bad("'%s' is out of range", arg); + + return NULL; +} + +void opt_show_floatval(char buf[OPT_SHOW_LEN], const float *f) +{ + double d = *f; + opt_show_doubleval(buf, &d); +} + +char *opt_set_doubleval(const char *arg, double *d) +{ + char *endp; + + /* This is how the manpage says to do it. Yech. */ + errno = 0; + /* Don't assume strtof */ + *d = strtod(arg, &endp); + if (*endp || !arg[0]) + return arg_bad("'%s' is not a number", arg); + if (errno) + return arg_bad("'%s' is out of range", arg); + + return NULL; +} + +void opt_show_doubleval(char buf[OPT_SHOW_LEN], const double *d) +{ + snprintf(buf, OPT_SHOW_LEN, "%f", *d); +} + char *opt_inc_intval(int *i) { (*i)++; return NULL; } +char *opt_dec_intval(int *i) +{ + (*i)--; + return NULL; +} + /* Display version string. */ char *opt_version_and_exit(const char *version) { @@ -195,9 +244,10 @@ static char *set_llong_with_suffix(const char *arg, long long *ll, const long long base) { char *endp; - if (!arg[0]) + if (!arg[0]){ + *ll = 0; return arg_bad("'%s' (an empty string) is not a number", arg); - + } errno = 0; *ll = strtoll(arg, &endp, 0); if (errno)