Add helpers for inputting and demonstrating floats.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
diff --git a/ccan/opt/helpers.c b/ccan/opt/helpers.c
index 315b7e4..8d0ac54 100644
--- a/ccan/opt/helpers.c
+++ b/ccan/opt/helpers.c
@@ -69,6 +69,19 @@ char *opt_set_intval(const char *arg, int *i)
return err;
}
+char *opt_set_floatval(const char *arg, float *f)
+{
+ char *endp;
+
+ errno = 0;
+ *f = strtof(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;
+}
+
char *opt_set_uintval(const char *arg, unsigned int *ui)
{
int i;
@@ -159,6 +172,11 @@ void opt_show_intval(char buf[OPT_SHOW_LEN], const int *i)
snprintf(buf, OPT_SHOW_LEN, "%i", *i);
}
+void opt_show_floatval(char buf[OPT_SHOW_LEN], const float *f)
+{
+ snprintf(buf, OPT_SHOW_LEN, "%.1f", *f);
+}
+
void opt_show_uintval(char buf[OPT_SHOW_LEN], const unsigned int *ui)
{
snprintf(buf, OPT_SHOW_LEN, "%u", *ui);
diff --git a/ccan/opt/opt.h b/ccan/opt/opt.h
index e06cd44..6fe2fb0 100644
--- a/ccan/opt/opt.h
+++ b/ccan/opt/opt.h
@@ -272,6 +272,8 @@ void opt_show_charp(char buf[OPT_SHOW_LEN], char *const *p);
/* Set an integer value, various forms. Sets to 1 on arg == NULL. */
char *opt_set_intval(const char *arg, int *i);
void opt_show_intval(char buf[OPT_SHOW_LEN], const int *i);
+char *opt_set_floatval(const char *arg, float *f);
+void opt_show_floatval(char buf[OPT_SHOW_LEN], const float *f);
char *opt_set_uintval(const char *arg, unsigned int *ui);
void opt_show_uintval(char buf[OPT_SHOW_LEN], const unsigned int *ui);
char *opt_set_longval(const char *arg, long *l);