Changeset a3e61a29065fa8b947f06870ee2a0c52108398b3

Show
Ignore:
Timestamp:
10/25/09 12:12:41 (4 weeks ago)
Author:
Karl Ramm <kcr@1ts.org>
git-author:
Karl Ramm <kcr@1ts.org> / 2009-10-24T13:57:10Z-0400
Parents:
2b6622a602f1f8b0922ad96976b2abc20232db9c
Children:
1b9d3cc988eca748bb848a9e8e8c38c6b4022dfd
git-committer:
Karl Ramm <kcr@1ts.org> / 2009-10-25T12:12:41Z-0400
Message:
Rewrite color name<->number mapping code

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

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}