From a856598c95e5820e855f2483a49db5aa5e97176f Mon Sep 17 00:00:00 2001 From: Johannes Löthberg Date: Mon, 10 Nov 2014 23:50:09 +0100 Subject: Make non-negatable arguments unsigned, make parse_uint --- src/utils.c | 24 ++++++++++++++++++++++++ src/utils.h | 1 + src/yawa.c | 12 ++++++------ src/yawa.h | 12 ++++++------ 4 files changed, 37 insertions(+), 12 deletions(-) (limited to 'src') 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 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 diff --git a/src/yawa.c b/src/yawa.c index a7978cf..dce73d7 100644 --- a/src/yawa.c +++ b/src/yawa.c @@ -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': diff --git a/src/yawa.h b/src/yawa.h index 8c86fe0..60d3ee8 100644 --- a/src/yawa.h +++ b/src/yawa.h @@ -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; -- cgit v1.2.3-70-g09d2