[1855] in BarnOwl Developers
Re: [PATCH] Rewrite color name<->number mapping code
daemon@ATHENA.MIT.EDU (David A Benjamin)
Thu Oct 29 18:23:17 2009
Resent-From: nelhage@mit.edu
Resent-To: barnowl-dev-mtg@charon.mit.edu
X-Original-To: nelhage@lunatique.mit.edu
Date: Sat, 24 Oct 2009 14:19:57 -0400 (EDT)
From: David A Benjamin <davidben@MIT.EDU>
To: Karl Ramm <kcr@1ts.org>
cc: barnowl-dev@mit.edu
In-Reply-To: <1256407547-4168-2-git-send-email-kcr@1ts.org>
On Sat, 24 Oct 2009, Karl Ramm wrote:
> +static const struct {
> + const int number;
> + const char *name;
> +} color_map[] = {
> + {OWL_COLOR_INVALID, "invalid"},
> + {OWL_COLOR_DEFAULT, "default"},
> + {OWL_COLOR_BLACK, "black"},
> + {OWL_COLOR_RED, "red"},
> + {OWL_COLOR_GREEN, "green"},
> + {OWL_COLOR_YELLOW,"yellow"},
> + {OWL_COLOR_BLUE, "blue"},
> + {OWL_COLOR_MAGENTA, "magenta"},
> + {OWL_COLOR_CYAN, "cyan"},
> + {OWL_COLOR_WHITE, "white"},
> + {0, NULL}
> +};
>
Would C99 initializers (are we using them in barnowl?) work better
here? The code below seems to assume that that the elements are in
order which is not immediately obvious looking at the code.
> @@ -442,15 +444,8 @@ int owl_util_string_to_color(const char *color)
> /* Return a string name of the given owl color */
> const char *owl_util_color_to_string(int color)
> {
> - if (color==OWL_COLOR_BLACK) return("black");
> - if (color==OWL_COLOR_RED) return("red");
> - if (color==OWL_COLOR_GREEN) return("green");
> - if (color==OWL_COLOR_YELLOW) return("yellow");
> - if (color==OWL_COLOR_BLUE) return("blue");
> - if (color==OWL_COLOR_MAGENTA) return("magenta");
> - if (color==OWL_COLOR_CYAN) return("cyan");
> - if (color==OWL_COLOR_WHITE) return("white");
> - if (color==OWL_COLOR_DEFAULT) return("default");
> + if (color >= OWL_COLOR_INVALID && color <= OWL_COLOR_WHITE)
> + return color_map[color - OWL_COLOR_INVALID].name;
> return("Unknown color");
> }
Perhaps sizeof(color_map) / sizeof(color_map[0]) or a OWL_NUM_COLORS
or something along those lines?
David Benjamin