From df6393be75b8b09184426269135e031bda0e8621 Mon Sep 17 00:00:00 2001 From: "Dmitrij D. Czarkoff" Date: Sun, 23 Oct 2016 03:32:58 +0200 Subject: Simplify hex color parser It is actually a trivial task for strtoul + some bit shifting, so there is no need for two separate functions. --- src/main.c | 11 +++++++---- src/util.c | 34 ---------------------------------- src/util.h | 4 ---- 3 files changed, 7 insertions(+), 42 deletions(-) diff --git a/src/main.c b/src/main.c index 2671a21..21fbc77 100644 --- a/src/main.c +++ b/src/main.c @@ -103,7 +103,7 @@ static void parse_args(int argc, char** argv) /* Do not print getopt errors */ opterr = 0; - char *argp, o; + char *argp, *ep = *argv, o; while((o = getopt(argc, argv, "firasSudxhln:b:e:t:")) != -1) { switch(o) { @@ -129,11 +129,15 @@ static void parse_args(int argc, char** argv) g_options.solid_bg = 0; } else { g_options.solid_bg = 1; - if(parse_hex_color(optarg, - &g_options.bg_r, &g_options.bg_g, &g_options.bg_b) != 0) { + argp = (*optarg == '#') ? optarg + 1 : optarg; + uint32_t n = strtoul(argp, &ep, 16); + if(*ep != '\0' || ep - argp != 6 || n > 0xFFFFFF) { fprintf(stderr, "Invalid hex color: '%s'\n", optarg); exit(1); } + g_options.bg_b = n & 0xFF; + g_options.bg_g = (n >> 8) & 0xFF; + g_options.bg_r = (n >> 16); } break; case 'e': @@ -143,7 +147,6 @@ static void parse_args(int argc, char** argv) g_options.delay = strtoul(optarg, &argp, 10); g_options.delay *= 1000; if (*argp == '.') { - char *ep; long delay = strtoul(++argp, &ep, 10); for (int i = 3 - (ep - argp); i; i--) { delay *= 10; diff --git a/src/util.c b/src/util.c index 9294f51..75d6910 100644 --- a/src/util.c +++ b/src/util.c @@ -124,40 +124,6 @@ SDL_Texture *create_chequered(SDL_Renderer *renderer) return ret; } -static int parse_hex_digit(char c) { - if(c >= '0' && c <= '9') { - return c - '0'; - } else if (c >= 'a' && c <= 'f') { - return c - 'a' + 10; - } else if (c >= 'A' && c <= 'F') { - return c - 'A' + 10; - } - return -1; -} - -int parse_hex_color(const char* str, - unsigned char *r, unsigned char *g, unsigned char *b) -{ - if(str[0] == '#') { - ++str; - } - - if(strlen(str) != 6) { - return 1; - } - - for(int i = 0; i < 6; ++i) { - if(!isxdigit(str[i])) { - return 1; - } - } - - *r = (parse_hex_digit(str[0]) << 4) + parse_hex_digit(str[1]); - *g = (parse_hex_digit(str[2]) << 4) + parse_hex_digit(str[3]); - *b = (parse_hex_digit(str[4]) << 4) + parse_hex_digit(str[5]); - return 0; -} - void imv_printf(SDL_Renderer *renderer, TTF_Font *font, int x, int y, SDL_Color *fg, SDL_Color *bg, const char *fmt, ...) { diff --git a/src/util.h b/src/util.h index 039d55f..652c5be 100644 --- a/src/util.h +++ b/src/util.h @@ -27,10 +27,6 @@ size_t read_from_stdin(void **buffer); /* Creates a new SDL_Texture* containing a chequeboard texture */ SDL_Texture *create_chequered(SDL_Renderer *renderer); -/* Parses a triplet of hexadecimal bytes. Writes values to r, g, and b. */ -int parse_hex_color(const char* str, - unsigned char *r, unsigned char *g, unsigned char *b); - /* Loads a font using SDL2_ttf given a spec in the format "name:size" */ TTF_Font *load_font(const char *font_spec); -- cgit v1.2.3