From af0d7f5ab90341f353d3632ee25e70e08ded0d69 Mon Sep 17 00:00:00 2001 From: Johannes Löthberg Date: Tue, 11 Nov 2014 01:35:00 +0100 Subject: Add parse_double function to utils.c --- src/utils.c | 22 ++++++++++++++++++++++ src/utils.h | 3 +++ 2 files changed, 25 insertions(+) diff --git a/src/utils.c b/src/utils.c index d515b69..19d249d 100644 --- a/src/utils.c +++ b/src/utils.c @@ -53,6 +53,28 @@ parse_uint(char *string, char *arg) return (unsigned int)val; } +double +parse_double(char *string, char *arg) +{ + errno = 0; + char *endptr; + double val = strtod(string, &endptr); + if (errno != 0) { + char *errormsg; + asprintf(&errormsg, "parse_double: failed to parse %s", arg); + + perror(errormsg); + free(errormsg); + exit(-2); + } + + if (endptr == string) { + fprintf(stderr, "Valid %s not found\n", arg); + exit(-2); + } + return val; +} + bool parse_color(char *hex, Color *c, int a) { diff --git a/src/utils.h b/src/utils.h index 50703b0..b21c6d5 100644 --- a/src/utils.h +++ b/src/utils.h @@ -10,6 +10,9 @@ parse_int(char *string, char *arg); unsigned parse_uint(char *string, char *arg); +double +parse_double(char *string, char *arg); + bool parse_color(char *arg, Color *c, int a); -- cgit v1.2.3-70-g09d2