better handling of float cli arguments
This commit is contained in:
22
funcs/t.c
22
funcs/t.c
@@ -8,20 +8,28 @@
|
||||
|
||||
#include "../floatimg.h"
|
||||
|
||||
int fimg_pnm_infos(char *);
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int foo;
|
||||
char *infile = "foo.pnm";
|
||||
double dval;
|
||||
char *str;
|
||||
|
||||
pnm_init(&argc, argv);
|
||||
str = "12.34"; dval = 0.0;
|
||||
foo = parse_double(str, &dval);
|
||||
printf("%-10s -> %3d %g\n", str, foo, dval);
|
||||
|
||||
if (2 == argc) infile = argv[1];
|
||||
str = "12e4"; dval = 0.0;
|
||||
foo = parse_double(str, &dval);
|
||||
printf("%-10s -> %3d %g\n", str, foo, dval);
|
||||
|
||||
foo = fimg_pnm_infos(infile);
|
||||
fprintf(stderr, "got %d\n", foo);
|
||||
str = "5s"; dval = 0.0;
|
||||
foo = parse_double(str, &dval);
|
||||
printf("%-10s -> %3d %g\n", str, foo, dval);
|
||||
|
||||
str = "PORN"; dval = 0.0;
|
||||
foo = parse_double(str, &dval);
|
||||
printf("%-10s -> %3d %g\n", str, foo, dval);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -29,3 +29,16 @@ if (2 != foo) {
|
||||
return 2;
|
||||
}
|
||||
/* --------------------------------------------------------------------- */
|
||||
int parse_double(char *str, double *dptr)
|
||||
{
|
||||
double value;
|
||||
int foo;
|
||||
|
||||
foo = sscanf(str, "%lf", &value);
|
||||
if (1 == foo) {
|
||||
*dptr = value;
|
||||
return 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
Reference in New Issue
Block a user