From fe54d4f2cb2706879c596776200dfc7ad693e24b Mon Sep 17 00:00:00 2001 From: jluehrs2 Date: Mon, 17 Mar 2008 22:17:07 -0500 Subject: reorganize string <-> enum stuff, move it into its own file, separate it out more --- src/strings.c | 160 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 src/strings.c (limited to 'src/strings.c') diff --git a/src/strings.c b/src/strings.c new file mode 100644 index 0000000..56bf4c6 --- /dev/null +++ b/src/strings.c @@ -0,0 +1,160 @@ +#include "strings.h" +#include +#include + +#define lengthof(x) (sizeof(x) / sizeof((x)[0])) + +typedef struct _trans { + const char* str; + int tag; +} trans; + +static trans colors[] = { + {"black", COLOR_BLACK}, + {"red", COLOR_RED}, + {"green", COLOR_GREEN}, + {"yellow", COLOR_YELLOW}, + {"blue", COLOR_BLUE}, + {"magenta", COLOR_MAGENTA}, + {"cyan", COLOR_CYAN}, + {"white", COLOR_WHITE}, +}; + +static trans modes[] = { + {"standout", A_STANDOUT}, + {"underline", A_UNDERLINE}, + {"reverse", A_REVERSE}, + {"blink", A_BLINK}, + {"dim", A_DIM}, + {"bold", A_BOLD}, + {"protect", A_PROTECT}, + {"invis", A_INVIS}, + {"altcharset", A_ALTCHARSET}, + {"chartext", A_CHARTEXT}, +}; + +static trans keys[] = { + {"left", KEY_LEFT}, + {"right", KEY_RIGHT}, + {"up", KEY_UP}, + {"down", KEY_DOWN}, + {"home", KEY_HOME}, + {"end", KEY_END}, + {"backspace", KEY_BACKSPACE}, + {"enter", KEY_ENTER}, + {"page down", KEY_NPAGE}, + {"page up", KEY_PPAGE}, + {"break", KEY_BREAK}, + {"delete", KEY_DC}, + {"insert", KEY_IC}, +}; + +static const char* fn_keys[] = { + "F0", "F1", "F2", "F3", "F4", "F5", "F6", "F7", + "F8", "F9", "F10", "F11", "F12", "F13", "F14", "F15", + "F16", "F17", "F18", "F19", "F20", "F21", "F22", "F23", + "F24", "F25", "F26", "F27", "F28", "F29", "F30", "F31", + "F32", "F33", "F34", "F35", "F36", "F37", "F38", "F39", + "F40", "F41", "F42", "F43", "F44", "F45", "F46", "F47", + "F48", "F49", "F50", "F51", "F52", "F53", "F54", "F55", + "F56", "F57", "F58", "F59", "F60", "F61", "F62", "F63", +}; + +static int str2enum(const trans table[], int len, const char* str) +{ + int i; + + for (i = 0; i < len; ++i) { + if (!strcmp(str, table[i].str)) { + return table[i].tag; + } + } + + return -1; +} + +static const char* enum2str(const trans* table, int len, int tag) +{ + int i; + + for (i = 0; i < len; ++i) { + if (tag == table[i].tag) { + return table[i].str; + } + } + + return NULL; +} + +static void each_item(const trans* table, int len, table_cb cb, void* data) +{ + int i; + + for (i = 0; i < len; ++i) { + cb(table[i].str, table[i].tag, data); + } +} + +int get_color_enum(const char* str) +{ + return str2enum(colors, lengthof(colors), str); +} + +int get_mode_enum(const char* str) +{ + int ret; + + ret = str2enum(modes, lengthof(modes), str); + + return ret == -1 ? A_NORMAL : ret; +} + +int get_key_enum(const char* str) +{ + int ret; + + ret = str2enum(keys, lengthof(keys), str); + + if (ret == -1) { + int fkey; + if (sscanf(str, "F%d", &fkey) == 1) { + return KEY_F(fkey); + } + } + + return ret == -1 ? (int)str[0] : ret; +} + +const char* get_color_str(int tag) +{ + return enum2str(colors, lengthof(colors), tag); +} + +const char* get_mode_str(int tag) +{ + return enum2str(modes, lengthof(modes), tag); +} + +const char* get_key_str(int tag) +{ + if (tag >= KEY_F(0) && tag <= KEY_F(63)) { + return fn_keys[tag - KEY_F0]; + } + + return enum2str(keys, lengthof(keys), tag); +} + +void each_color(table_cb cb, void* data) +{ + each_item(colors, lengthof(colors), cb, data); +} + +void each_mode(table_cb cb, void* data) +{ + each_item(modes, lengthof(modes), cb, data); +} + +void each_key(table_cb cb, void* data) +{ + each_item(keys, lengthof(keys), cb, data); +} -- cgit v1.2.3-54-g00ecf