]> git.pond.sub.org Git - empserver/blob - src/lib/common/keyword.c
70aa2778bca74ea89a3203b6f63d4faefaa56f13
[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 /*
44  * destructive parse
45  */
46 s_char *
47 kw_parse(int type, s_char *text, int *data)
48 {
49     s_char *next;
50
51     while (isspace(*text))
52         text++;
53     switch (type) {
54     case CF_VALUE:
55         *data = atoip(&text);
56         break;
57     case CF_TIME:
58         text = get_time(text, &data[0]);
59         break;
60     case CF_TIMERANGE:
61         if ((next = strchr(text, '-')) == 0)
62             return 0;
63         next++;
64         if ((text = get_time(text, &data[0])) == 0)
65             return 0;
66         text = get_time(next, &data[1]);
67         break;
68     case CF_WEEKDAY:
69         text = weekday(text, &data[0]);
70         break;
71     default:
72         text = 0;
73         break;
74     }
75     return text;
76 }
77
78 struct day {
79     s_char *string;
80     int day[7];
81 } day[] = {
82     {
83         "smtwtfs", {
84     -1, 0, -1, 2, -1, 4, -1}}, {
85         "uouehra", {
86     0, 1, 2, 3, 4, 5, 6}}
87 };
88
89 s_char *
90 weekday(s_char *ptr, int *data)
91 {
92     register s_char *string;
93     register int c;
94     register int n;
95
96     c = *ptr++;
97     if (isupper(c))
98         c = tolower(c);
99     string = day[0].string;
100     for (n = 0; n < 7; n++) {
101         if (string[n] != c)
102             continue;
103         if (day[0].day[n] >= 0)
104             break;
105         if (day[1].string[n] == *ptr)
106             break;
107     }
108     if (n == 7)
109         return 0;
110     *data = day[1].day[n];
111     while (*ptr && !isspace(*ptr))
112         ptr++;
113     return ptr;
114 }
115
116
117 s_char *
118 get_time(s_char *ptr, int *data)
119 {
120     int hour;
121     int minute;
122
123     if (!isdigit(*ptr))
124         return 0;
125     hour = atoip(&ptr);
126     minute = 0;
127     if (*ptr) {
128         if (*ptr != ':')
129             return 0;
130         ptr++;
131         if (!isdigit(*ptr))
132             return 0;
133         minute = atoip(&ptr);
134     }
135     *data = (hour * 60) + minute;
136     return ptr;
137 }