]> git.pond.sub.org Git - empserver/blob - src/lib/subs/sarg.c
Update known contributors comment.
[empserver] / src / lib / subs / sarg.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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 files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  sarg.c: Parse selection arguments
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1989
32  *     Markus Armbruster, 2004
33  */
34
35 #include <config.h>
36
37 #include <ctype.h>
38 #include <string.h>
39 #include "misc.h"
40 #include "player.h"
41 #include "xy.h"
42 #include "nsc.h"
43 #include "nat.h"
44 #include "file.h"
45 #include "prototypes.h"
46 #include "optlist.h"
47
48 /*
49  * returns one of
50  *
51  *  #1, lx:ly,hx:hy --> NS_AREA
52  *  @x,y:dist  --> NS_DIST
53  *  %d or %d/%d/%d --> NS_LIST
54  *  ~ or letter --> NS_GROUP
55  *  * --> NS_ALL
56  *
57  * or 0 for none of the above.
58  */
59 ns_seltype
60 sarg_type(char *str)
61 {
62     int c;
63
64     c = *str;
65     if (c == '@')
66         return NS_DIST;
67     if (c == '*')
68         return NS_ALL;
69     if (c == '#' || strchr(str, ',') != 0)
70         return NS_AREA;
71     if (isdigit(c))
72         return NS_LIST;
73     if (c == '~' || isalpha(c))
74         return NS_GROUP;
75     return NS_UNDEF;
76 }
77
78 int
79 sarg_xy(char *str, coord *xp, coord *yp)
80 {
81     coord x, y;
82     struct natstr *np;
83
84     x = strtox(str, &str);
85     if (x < 0 || *str++ != ',')
86         return 0;
87     y = strtoy(str, &str);
88     if (y < 0 || (*str != 0 && !isspace(*str)))
89       return 0;
90     if ((x ^ y) & 1)
91         return 0;
92     np = getnatp(player->cnum);
93     *xp = xabs(np, x);
94     *yp = yabs(np, y);
95     return 1;
96 }
97
98 /* returns absolute coords */
99 static int
100 sarg_getrange(char *str, struct range *rp)
101 {
102     long rlm;
103     struct natstr *np;
104     struct realmstr realm;
105     char *end;
106
107     if (*str == '#') {
108         /*
109          * realm #X where (X > 0 && X < MAXNOR)
110          * Assumes realms are in abs coordinates
111          */
112         if (*++str) {
113             rlm = strtol(str, &end, 10);
114             if (end == str || (*end != 0 && !isspace(*end))
115                 || rlm < 0 || MAXNOR <= rlm)
116                 return 0;
117         } else 
118             rlm = 0;
119         getrealm(rlm, player->cnum, &realm);
120         rp->lx = realm.r_xl;
121         rp->hx = realm.r_xh;
122         rp->ly = realm.r_yl;
123         rp->hy = realm.r_yh;
124     } else {
125         /*
126          * full map specification
127          * LX:LY,HX:HY where
128          * ly, hy are optional.
129          */
130         rp->lx = rp->hx = strtox(str, &str);
131         if (rp->lx < 0)
132             return 0;
133         if (*str == ':') {
134             rp->hx = strtox(str + 1, &str);
135             if (rp->hx < 0)
136                 return 0;
137         }
138         if (*str++ != ',')
139             return 0;
140         rp->ly = rp->hy = strtoy(str, &str);
141         if (rp->ly < 0)
142             return 0;
143         if (*str == ':') {
144             rp->hy = strtoy(str + 1, &str);
145             if (rp->hy < 0)
146                 return 0;
147         }
148         if (*str != 0 && !isspace(*str))
149             return 0;
150         np = getnatp(player->cnum);
151         rp->lx = xabs(np, rp->lx);
152         rp->hx = xabs(np, rp->hx);
153         rp->ly = yabs(np, rp->ly);
154         rp->hy = yabs(np, rp->hy);
155     }
156     xysize_range(rp);
157     return 1;
158 }
159
160 /*
161  * translate #1 or lx:ly,hx:hy into
162  * a result range struct
163  */
164 int
165 sarg_area(char *str, struct range *rp)
166 {
167     if (!sarg_getrange(str, rp))
168         return 0;
169     rp->hx += 1;
170     if (rp->hx >= WORLD_X)
171         rp->hx = 0;
172     rp->hy += 1;
173     if (rp->hy >= WORLD_Y)
174         rp->hy = 0;
175     xysize_range(rp);
176     return 1;
177 }
178
179 /*
180  * translate @x,y:int into
181  * result params
182  */
183 int
184 sarg_range(char *str, coord *xp, coord *yp, int *dist)
185 {
186     coord x, y;
187     long d;
188     char *end;
189     struct natstr *np;
190
191     if (*str++ != '@')
192         return 0;
193     x = strtox(str, &str);
194     if (x < 0 || *str++ != ',')
195         return 0;
196     y = strtoy(str, &str);
197     if (y < 0 || *str++ != ':')
198         return 0;
199     d = strtol(str, &end, 10);
200     if (end == str || (*end != 0 && !isspace(*end)) || d < 0)
201         return 0;
202     *dist = d;
203     np = getnatp(player->cnum);
204     *xp = xabs(np, x);
205     *yp = yabs(np, y);
206     return 1;
207 }
208
209 /*
210  * list of idents; id/id/id/id/id
211  */
212 int
213 sarg_list(char *str, int *list, int max)
214 {
215     int i, j;
216     long n;
217     char *end;
218
219     i = 0;
220     do {
221         n = strtol(str, &end, 10);
222         if (end == str || n < 0) {
223             pr("Illegal character '%c'\n", *str);
224             return 0;
225         }
226         for (j = 0; j < i; j++) {
227             if (list[j] == n)
228                 break;
229         }
230         if (j == i) {
231             if (i >= max) {
232                 pr("List too long (limit is %d)\n", max);
233                 return 0;
234             }
235             list[i++] = n;
236         }
237         str = end;
238     } while (*str++ == '/');
239
240     if (str[-1] != 0 && !isspace(str[-1])) {
241         pr("Expecting '/', got '%c'\n", str[-1]);
242         return 0;
243     }
244     return i;
245 }