]> git.pond.sub.org Git - empserver/blob - src/lib/common/keyword.c
335a2af3a303f1630ed882264257b31ec200fe87
[empserver] / src / lib / common / keyword.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  keyword.c: Find keywords in a file
29  * 
30  *  Known contributors to this file:
31  *     
32  */
33
34 #include "misc.h"
35 #include "keyword.h"
36 #include "gen.h"
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <ctype.h>
41 #include "common.h"
42
43 struct kwtab {
44     struct kwtab *next;
45     s_char *name;
46     s_char *text;
47 };
48
49 struct kwtab *kw_list;
50
51 int
52 kw_read(FILE * fp)
53 {
54     register struct kwtab *kw;
55     register struct kwtab *next;
56     s_char buf[255];
57     s_char *p;
58     int n;
59
60     for (kw = kw_list; kw != 0; kw = next) {
61         next = kw->next;
62         free(kw->name);
63         free(kw->text);
64         free(kw);
65     }
66     kw_list = 0;
67     for (n = 0; fgets(buf, sizeof(buf), fp) != 0; n++) {
68         /* Allow for comments.. any line starting with # */
69         if (buf[0] == '#')
70             continue;
71         p = strrchr(buf, '\n');
72         if (p != 0)
73             *p = 0;
74         if ((p = strchr(buf, ':')) == 0) {
75             logerror("kw_read: Bad keyword line #%d\n", n);
76             return 0;
77         }
78         *p++ = 0;
79         while (*p && isspace(*p))
80             p++;
81         kw = (struct kwtab *)malloc(sizeof(*kw));
82         kw->name = strcpy(malloc(strlen(buf) + 1), buf);
83         kw->text = strcpy(malloc(strlen(p) + 1), p);
84         kw->next = kw_list;
85         kw_list = kw;
86     }
87     return n;
88 }
89
90 s_char *
91 kw_find(s_char *name)
92 {
93     register struct kwtab *kw;
94
95     for (kw = kw_list; kw != 0; kw = kw->next) {
96         if (strcmp(kw->name, name) == 0)
97             return kw->text;
98     }
99     return 0;
100 }
101
102 /*
103  * destructive parse
104  */
105 s_char *
106 kw_parse(int type, s_char *text, int *data)
107 {
108     s_char *next;
109
110     while (isspace(*text))
111         text++;
112     switch (type) {
113     case CF_VALUE:
114         *data = atoip(&text);
115         break;
116     case CF_TIME:
117         text = get_time(text, &data[0]);
118         break;
119     case CF_TIMERANGE:
120         if ((next = strchr(text, '-')) == 0)
121             return 0;
122         next++;
123         if ((text = get_time(text, &data[0])) == 0)
124             return 0;
125         text = get_time(next, &data[1]);
126         break;
127     case CF_WEEKDAY:
128         text = weekday(text, &data[0]);
129         break;
130     default:
131         text = 0;
132         break;
133     }
134     return text;
135 }
136
137 struct day {
138     s_char *string;
139     int day[7];
140 } day[] = {
141     {
142         "smtwtfs", {
143     -1, 0, -1, 2, -1, 4, -1}}, {
144         "uouehra", {
145     0, 1, 2, 3, 4, 5, 6}}
146 };
147
148 s_char *
149 weekday(s_char *ptr, int *data)
150 {
151     register s_char *string;
152     register int c;
153     register int n;
154
155     c = *ptr++;
156     if (isupper(c))
157         c = tolower(c);
158     string = day[0].string;
159     for (n = 0; n < 7; n++) {
160         if (string[n] != c)
161             continue;
162         if (day[0].day[n] >= 0)
163             break;
164         if (day[1].string[n] == *ptr)
165             break;
166     }
167     if (n == 7)
168         return 0;
169     *data = day[1].day[n];
170     while (*ptr && !isspace(*ptr))
171         ptr++;
172     return ptr;
173 }
174
175
176 s_char *
177 get_time(s_char *ptr, int *data)
178 {
179     int hour;
180     int minute;
181
182     if (!isdigit(*ptr))
183         return 0;
184     hour = atoip(&ptr);
185     minute = 0;
186     if (*ptr) {
187         if (*ptr != ':')
188             return 0;
189         ptr++;
190         if (!isdigit(*ptr))
191             return 0;
192         minute = atoip(&ptr);
193     }
194     *data = (hour * 60) + minute;
195     return ptr;
196 }