aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjluehrs2 <jluehrs2@uiuc.edu>2008-03-17 22:17:07 -0500
committerjluehrs2 <jluehrs2@uiuc.edu>2008-03-17 22:17:07 -0500
commitfe54d4f2cb2706879c596776200dfc7ad693e24b (patch)
tree811be0a1bc66c59afea769feb701ae8bd826da1d
parent62eb308c26ef29ade6ab338af9c007044126de75 (diff)
downloadluancurses-fe54d4f2cb2706879c596776200dfc7ad693e24b.tar.gz
luancurses-fe54d4f2cb2706879c596776200dfc7ad693e24b.zip
reorganize string <-> enum stuff, move it into its own file, separate it out more
-rw-r--r--Makefile11
-rw-r--r--src/curses.c154
-rw-r--r--src/strings.c160
-rw-r--r--src/strings.h18
4 files changed, 204 insertions, 139 deletions
diff --git a/Makefile b/Makefile
index bc64adb..1e6849c 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
include Make.config
BIN = src/curses.so
-OBJ = src/curses.o
+OBJ = src/curses.o src/strings.o
CC = gcc
INCLUDES =
DEFINES =
@@ -10,7 +10,7 @@ COMMONFLAGS = -Werror -Wall -pedantic -O0 -g -pipe -fpic
CFLAGS = -c $(INCLUDES) $(DEFINES) $(COMMONFLAGS)
LDFLAGS = $(LIBS) $(COMMONFLAGS) -shared
-SRC = src/curses.c
+SRC = src/curses.c src/strings.c src/strings.h
TEST_LUAS = test/rl.lua \
test/test.lua
TTT_TEST_DIR = tictactoe
@@ -36,7 +36,7 @@ clean :
rm -f $(OBJ) $(BIN)
dep :
- makedepend $(INCLUDES) $(DEFINES) -Y *.c *.h > /dev/null 2>&1
+ makedepend $(INCLUDES) $(DEFINES) -Y $(SRC) > /dev/null 2>&1
rm -f Makefile.bak
install :
@@ -57,3 +57,8 @@ $(VERSION).tar.gz : $(SRC) $(TEST_LUAS) $(OTHER_FILES)
@cp $(OTHER_FILES) $(VERSION)
@tar czf $(VERSION).tar.gz $(VERSION)
@rm -rf $(VERSION)
+
+# DO NOT DELETE
+
+src/curses.o: src/strings.h
+src/strings.o: src/strings.h
diff --git a/src/curses.c b/src/curses.c
index fea344b..df34972 100644
--- a/src/curses.c
+++ b/src/curses.c
@@ -1,3 +1,4 @@
+#include "strings.h"
#include <curses.h>
#include <lua.h>
#include <lauxlib.h>
@@ -12,116 +13,7 @@ typedef struct _pos {
int y;
} pos;
-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 int ncolors = 1, ncolor_pairs = 1;
-
-static int str2enum(const trans table[], int table_len, const char* str)
-{
- int i;
-
- for (i = 0; i < table_len; ++i) {
- if (!strcmp(str, table[i].str)) {
- return table[i].tag;
- }
- }
-
- return -1;
-}
-
-static const char* enum2str(const trans* table, int table_len, int tag)
-{
- int i;
-
- for (i = 0; i < table_len; ++i) {
- if (tag == table[i].tag) {
- return table[i].str;
- }
- }
-
- return NULL;
-}
-
-static int get_color_enum(const char* str)
-{
- return str2enum(colors, sizeof(colors) / sizeof(colors[0]), str);
-}
-
-static int get_mode_enum(const char* str)
-{
- int ret;
-
- ret = str2enum(modes, sizeof(modes) / sizeof(modes[0]), str);
-
- return ret == -1 ? A_NORMAL : ret;
-}
-
-static int get_key_enum(const char* str)
-{
- int ret;
-
- ret = str2enum(keys, sizeof(keys) / sizeof(keys[0]), str);
-
- return ret == -1 ? (int)str[0] : ret;
-}
-
-static const char* get_color_str(int tag)
-{
- return enum2str(colors, sizeof(colors) / sizeof(colors[0]), tag);
-}
-
-static const char* get_mode_str(int tag)
-{
- return enum2str(modes, sizeof(modes) / sizeof(modes[0]), tag);
-}
-
-static const char* get_key_str(int tag)
-{
- return enum2str(keys, sizeof(keys) / sizeof(keys[0]), tag);
-}
+static int ncolors = 0, ncolor_pairs = 0;
static int get_color_pair(lua_State* L, const char* str)
{
@@ -156,18 +48,18 @@ static void init_color_pairs(lua_State* L)
lua_pop(L, 1);
}
-static void init_colors(lua_State* L)
+static void register_color(const char* color_str, int color_tag, void* data)
{
- int i;
-
- ncolors = sizeof(colors) / sizeof(colors[0]);
+ lua_pushinteger((lua_State*)data, color_tag);
+ lua_setfield((lua_State*)data, -2, color_str);
+ ncolors++;
+}
+static void init_colors(lua_State* L)
+{
lua_getfield(L, LUA_REGISTRYINDEX, REG_TABLE);
lua_newtable(L);
- for (i = 0; i < ncolors; ++i) {
- lua_pushinteger(L, colors[i].tag);
- lua_setfield(L, -2, colors[i].str);
- }
+ each_color(register_color, L);
lua_setfield(L, -2, "colors");
lua_pop(L, 1);
}
@@ -333,7 +225,7 @@ static int l_init_pair(lua_State* L)
* and we want to leave that C color_pair value on top of the stack
* for consistency */
lua_pop(L, 1);
- lua_pushinteger(L, ncolor_pairs++);
+ lua_pushinteger(L, ++ncolor_pairs);
lua_pushvalue(L, -1);
lua_setfield(L, -3, name);
}
@@ -363,8 +255,9 @@ static int l_init_pair(lua_State* L)
static int l_getch(lua_State* L)
{
- int c, i, found = 0;
+ int c;
pos p;
+ const char* key_name;
if (get_pos(L, &p)) {
c = mvgetch(p.y, p.x);
@@ -379,24 +272,13 @@ static int l_getch(lua_State* L)
return 2;
}
- for (i = 0; i < sizeof(keys) / sizeof(keys[0]); ++i) {
- if (c == keys[i].tag) {
- lua_pushstring(L, keys[i].str);
- found = 1;
- break;
- }
- }
+ key_name = get_key_str(c);
- if (!found) {
- if (c >= KEY_F(1) && c <= KEY_F(64)) {
- lua_pushfstring(L, "F%d", c - KEY_F0);
- }
- else {
- char s[1];
+ if (key_name == NULL) {
+ char s;
- s[0] = c;
- lua_pushlstring(L, s, 1);
- }
+ s = c;
+ lua_pushlstring(L, &s, 1);
}
return 1;
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 <curses.h>
+#include <string.h>
+
+#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);
+}
diff --git a/src/strings.h b/src/strings.h
new file mode 100644
index 0000000..757c686
--- /dev/null
+++ b/src/strings.h
@@ -0,0 +1,18 @@
+#ifndef STRINGS_H
+#define STRINGS_H
+
+typedef void (*table_cb)(const char* str, int tag, void* data);
+
+int get_color_enum(const char* str);
+int get_mode_enum(const char* str);
+int get_key_enum(const char* str);
+
+const char* get_color_str(int tag);
+const char* get_mode_str(int tag);
+const char* get_key_str(int tag);
+
+void each_color(table_cb cb, void* data);
+void each_mode(table_cb cb, void* data);
+void each_key(table_cb cb, void* data);
+
+#endif