diff options
author | Johannes Löthberg <johannes@kyriasis.com> | 2014-11-10 23:50:09 +0100 |
---|---|---|
committer | Johannes Löthberg <johannes@kyriasis.com> | 2014-11-10 23:50:09 +0100 |
commit | a856598c95e5820e855f2483a49db5aa5e97176f (patch) | |
tree | 25721736400c99f9d672776766dea56034a31f93 | |
parent | c9c31d8d15c2e7034a9a4dc1135a95aa60066b6d (diff) | |
download | yawa-a856598c95e5820e855f2483a49db5aa5e97176f.tar.xz |
Make non-negatable arguments unsigned, make parse_uint
-rw-r--r-- | src/utils.c | 24 | ||||
-rw-r--r-- | src/utils.h | 1 | ||||
-rw-r--r-- | src/yawa.c | 12 | ||||
-rw-r--r-- | src/yawa.h | 12 |
4 files changed, 37 insertions, 12 deletions
diff --git a/src/utils.c b/src/utils.c index 20cc6d4..0441ba2 100644 --- a/src/utils.c +++ b/src/utils.c @@ -33,6 +33,30 @@ parse_int(char *string, char *arg) return (int)val; } +unsigned int +parse_uint(char *string, char *arg) +{ + errno = 0; + char *endptr; + unsigned long val = strtoul(string, &endptr, 10); + if (errno != 0) + { + char *errormsg; + asprintf(&errormsg, "parse_uint: 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 (unsigned int)val; +} + bool parse_color(char *hex, Color *c, int a) { diff --git a/src/utils.h b/src/utils.h index 1e50975..ae9612e 100644 --- a/src/utils.h +++ b/src/utils.h @@ -3,6 +3,7 @@ #include <stdbool.h> int parse_int(char *string, char *arg); +unsigned int parse_uint(char *string, char *arg); bool parse_color(char *arg, Color *c, int a); #endif @@ -90,27 +90,27 @@ parse_opt(int key, char *arg, struct argp_state *state) case 'b': arguments->blur = true; - arguments->blur_radius = parse_int(arg, "blur radius"); + arguments->blur_radius = parse_uint(arg, "blur radius"); break; case 'S': arguments->sharpen = true; - arguments->sharpen_radius = parse_int(arg, "sharpen radius"); + arguments->sharpen_radius = parse_uint(arg, "sharpen radius"); break; case 'o': arguments->contrast = true; - arguments->contrast_amount = parse_int(arg, "contrast amount"); + arguments->contrast_amount = parse_uint(arg, "contrast amount"); break; case 'B': arguments->brightness = true; - arguments->brightness_amount = parse_int(arg, "brightness amount"); + arguments->brightness_amount = parse_uint(arg, "brightness amount"); break; case 'G': arguments->gamma = true; - arguments->gamma_amount = parse_int(arg, "gamma amount"); + arguments->gamma_amount = parse_uint(arg, "gamma amount"); break; case 'v': @@ -128,7 +128,7 @@ parse_opt(int key, char *arg, struct argp_state *state) case 'A': arguments->alpha = true; - arguments->alpha_amount = parse_int(arg, "alpha amount"); + arguments->alpha_amount = parse_uint(arg, "alpha amount"); break; case 'w': @@ -29,12 +29,12 @@ struct arguments { char *tint_color; int angle; - int blur_radius; - int sharpen_radius; - int contrast_amount; - int brightness_amount; - int gamma_amount; - int alpha_amount; + unsigned int blur_radius; + unsigned int sharpen_radius; + unsigned int contrast_amount; + unsigned int brightness_amount; + unsigned int gamma_amount; + unsigned int alpha_amount; bool add: 1; bool gradient: 1; |