added table system
[NeonServV5.git] / tools.c
1 #include "tools.h"
2
3 /* copied from IRCU 2.10.12 match.c */
4 /*
5  * Compare if a given string (name) matches the given
6  * mask (which can contain wild cards: '*' - match any
7  * number of chars, '?' - match any single character.
8  *
9  * return  0, if match
10  *         1, if no match
11  *
12  *  Originally by Douglas A Lewis (dalewis@acsu.buffalo.edu)
13  *  Rewritten by Timothy Vogelsang (netski), net@astrolink.org
14  */
15 int match(const char *mask, const char *name)
16 {
17   const char *m = mask, *n = name;
18   const char *m_tmp = mask, *n_tmp = name;
19   int star_p;
20
21   for (;;) switch (*m) {
22   case '\0':
23     if (!*n)
24       return 0;
25   backtrack:
26     if (m_tmp == mask)
27       return 1;
28     m = m_tmp;
29     n = ++n_tmp;
30     if (*n == '\0')
31       return 1;
32     break;
33   case '\\':
34     m++;
35     /* allow escaping to force capitalization */
36     if (*m++ != *n++)
37       goto backtrack;
38     break;
39   case '*': case '?':
40     for (star_p = 0; ; m++) {
41       if (*m == '*')
42         star_p = 1;
43       else if (*m == '?') {
44         if (!*n++)
45           goto backtrack;
46       } else break;
47     }
48     if (star_p) {
49       if (!*m)
50         return 0;
51       else if (*m == '\\') {
52         m_tmp = ++m;
53         if (!*m)
54           return 1;
55         for (n_tmp = n; *n && *n != *m; n++) ;
56       } else {
57         m_tmp = m;
58         for (n_tmp = n; *n && tolower(*n) != tolower(*m); n++) ;
59       }
60     }
61     /* and fall through */
62   default:
63     if (!*n)
64       return *m != '\0';
65     if (tolower(*m) != tolower(*n))
66       goto backtrack;
67     m++;
68     n++;
69     break;
70   }
71 }
72
73
74 //TABLES
75 struct Table *table_init(int width, int length, int flags) {
76     int row;
77     struct Table *table = malloc(sizeof(*table));
78     table->contents = malloc(length * sizeof(*table->contents));
79     for(row = 0; row < length; row++) {
80         table->contents[row] = calloc(width, sizeof(*table->contents));
81     }
82     table->length = length;
83     table->width = width;
84     table->flags = flags;
85     table->col_flags = calloc(length, sizeof(int));
86     table->c_entry = 0;
87     table->maxwidth = calloc(length, sizeof(int));
88     table->table = NULL;
89 }
90
91 int table_add(struct Table *table, char **entry) {
92     int col;
93     for(col = 0; col < table->width; col++) {
94         table->contents[table->c_entry][col] = ((table->flags & TABLE_FLAG_USE_POINTER) ? entry[col] : strdup(entry[col]));
95         if(strlen(entry[col]) > table->maxwidth[col])
96             table->maxwidth[col] = strlen(entry[col]);
97     }
98     table->c_entry++;
99 }
100
101 int table_set_bold(struct Table *table, int collum, int bold) {
102     if(bold)
103         table->col_flags[collum] |= TABLE_FLAG_COL_BOLD;
104     else
105         table->col_flags[collum] &= ~TABLE_FLAG_COL_BOLD;
106 }
107
108 char **table_end(struct Table *table) {
109     int row, col, tablewidth, pos,i;
110     if(table->table) return table->table;
111     table->table = malloc(table->length * sizeof(*table->table));
112     for(col = 0; col < table->width; col++) {
113         tablewidth += table->maxwidth[col]+1;
114         if(table->col_flags[col] & TABLE_FLAG_COL_BOLD)
115             tablewidth += 2;
116     }
117     for(row = 0; row < table->row; row++) {
118         table->table[row] = malloc(tablewidth * sizeof(*table->table[row]))
119         pos = 0;
120         for(col = 0; col < table->width; col++) {
121             if(table->col_flags[col] & TABLE_FLAG_COL_BOLD)
122                 table->table[row][pos++] = '\002';
123             for(i = 0; i < strlen(table->contents[row][col]); i++) {
124                 table->table[row][pos++] = table->contents[row][col][i];
125             }
126             for(;i < table->maxwidth[col]; i++) {
127                 table->table[row][pos++] = ' ';
128             }
129             if(table->col_flags[col] & TABLE_FLAG_COL_BOLD)
130                 table->table[row][pos++] = '\002';
131             if(col < table->width-1)
132                 table->table[row][pos++] = ' ';
133             else
134                 table->table[row][pos++] = '\0';
135         }
136     }
137     return table->table;
138 }
139
140 void table_free(struct Table *table) {
141     int row, col;
142     for(row = 0; row < length; row++) {
143         if(!(table->flags & TABLE_FLAG_USE_POINTER)) {
144             for(col = 0; col < table->width; col++) {
145                 free(table->contents[row][col]);
146             }
147         }
148         free(table->contents[row]);
149     }
150     free(table->contents);
151     free(table->col_flags);
152     free(table->maxwidth);
153     if(table->table) {
154         for(row = 0; row < length; row++) {
155             free(table->table[row]);
156         }
157         free(table->table);
158     }
159     free(table);
160 }
161