aboutsummaryrefslogtreecommitdiffstats
path: root/src/strings.c
blob: 56bf4c661961a710db37aab39516e693139b0ca6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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);
}