Changeset a3e61a2 for util.c


Ignore:
Timestamp:
Oct 25, 2009, 12:12:41 PM (12 years ago)
Author:
Karl Ramm <kcr@1ts.org>
Branches:
master, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
1b9d3cc
Parents:
2b6622a6
git-author:
Karl Ramm <kcr@1ts.org> (10/24/09 13:57:10)
git-committer:
Karl Ramm <kcr@1ts.org> (10/25/09 12:12:41)
Message:
Rewrite color name<->number mapping code

Make it data-driven, as opposed to a giant, ugly if-ladder.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • util.c

    r0697f09 ra3e61a2  
    407407}
    408408
     409/* These are in order of their value in owl.h */
     410static const struct {
     411  int number;
     412  const char *name;
     413} color_map[] = {
     414  {OWL_COLOR_INVALID, "invalid"},
     415  {OWL_COLOR_DEFAULT, "default"},
     416  {OWL_COLOR_BLACK, "black"},
     417  {OWL_COLOR_RED, "red"},
     418  {OWL_COLOR_GREEN, "green"},
     419  {OWL_COLOR_YELLOW,"yellow"},
     420  {OWL_COLOR_BLUE, "blue"},
     421  {OWL_COLOR_MAGENTA, "magenta"},
     422  {OWL_COLOR_CYAN, "cyan"},
     423  {OWL_COLOR_WHITE, "white"},
     424};
    409425
    410426/* Return the owl color associated with the named color.  Return -1
     
    413429int owl_util_string_to_color(const char *color)
    414430{
    415   int c;
    416   if (!strcasecmp(color, "black")) {
    417     return(OWL_COLOR_BLACK);
    418   } else if (!strcasecmp(color, "red")) {
    419     return(OWL_COLOR_RED);
    420   } else if (!strcasecmp(color, "green")) {
    421     return(OWL_COLOR_GREEN);
    422   } else if (!strcasecmp(color, "yellow")) {
    423     return(OWL_COLOR_YELLOW);
    424   } else if (!strcasecmp(color, "blue")) {
    425     return(OWL_COLOR_BLUE);
    426   } else if (!strcasecmp(color, "magenta")) {
    427     return(OWL_COLOR_MAGENTA);
    428   } else if (!strcasecmp(color, "cyan")) {
    429     return(OWL_COLOR_CYAN);
    430   } else if (!strcasecmp(color, "white")) {
    431     return(OWL_COLOR_WHITE);
    432   } else if (!strcasecmp(color, "default")) {
    433     return(OWL_COLOR_DEFAULT);
    434   }
     431  int c, i;
     432
     433  for (i = 0; i < (sizeof(color_map)/sizeof(color_map[0])); i++)
     434    if (strcasecmp(color, color_map[i].name) == 0)
     435      return color_map[i].number;
     436
    435437  c = atoi(color);
    436438  if (c >= -1 && c < COLORS) {
     
    443445const char *owl_util_color_to_string(int color)
    444446{
    445   if (color==OWL_COLOR_BLACK)   return("black");
    446   if (color==OWL_COLOR_RED)     return("red");
    447   if (color==OWL_COLOR_GREEN)   return("green");
    448   if (color==OWL_COLOR_YELLOW)  return("yellow");
    449   if (color==OWL_COLOR_BLUE)    return("blue");
    450   if (color==OWL_COLOR_MAGENTA) return("magenta");
    451   if (color==OWL_COLOR_CYAN)    return("cyan");
    452   if (color==OWL_COLOR_WHITE)   return("white");
    453   if (color==OWL_COLOR_DEFAULT) return("default");
     447  if (color >= OWL_COLOR_INVALID && color <= OWL_COLOR_WHITE)
     448    return color_map[color - OWL_COLOR_INVALID].name;
    454449  return("Unknown color");
    455450}
Note: See TracChangeset for help on using the changeset viewer.