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