Permalink
Browse files

Add helpers for inputting and demonstrating floats.

  • Loading branch information...
1 parent 21c2bc4 commit 8b927af926f80ecfa76b6d0a1bcb7159f38f76bf @ckolivas committed Sep 26, 2011
Showing with 20 additions and 0 deletions.
  1. +18 −0 ccan/opt/helpers.c
  2. +2 −0 ccan/opt/opt.h
View
@@ -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);
View
@@ -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);

0 comments on commit 8b927af

Please sign in to comment.