]> 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-2010, 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-2009
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 enum 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, ','))
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 /*
98  * translate #1 or lx:ly,hx:hy into a result range struct
99  * returns absolute coords
100  */
101 int
102 sarg_area(char *str, struct range *rp)
103 {
104     long rlm;
105     struct natstr *np;
106     struct realmstr realm;
107     char *end;
108
109     if (*str == '#') {
110         /*
111          * realm #X where (X > 0 && X < MAXNOR)
112          * Assumes realms are in abs coordinates
113          */
114         if (*++str) {
115             rlm = strtol(str, &end, 10);
116             if (end == str || (*end != 0 && !isspace(*end))
117                 || rlm < 0 || MAXNOR <= rlm)
118                 return 0;
119         } else
120             rlm = 0;
121         getrealm(rlm, player->cnum, &realm);
122         rp->lx = realm.r_xl;
123         rp->hx = realm.r_xh;
124         rp->ly = realm.r_yl;
125         rp->hy = realm.r_yh;
126     } else {
127         /*
128          * full map specification
129          * LX:LY,HX:HY where
130          * ly, hy are optional.
131          */
132         rp->lx = rp->hx = strtox(str, &str);
133         if (rp->lx < 0)
134             return 0;
135         if (*str == ':') {
136             rp->hx = strtox(str + 1, &str);
137             if (rp->hx < 0)
138                 return 0;
139         }
140         if (*str++ != ',')
141             return 0;
142         rp->ly = rp->hy = strtoy(str, &str);
143         if (rp->ly < 0)
144             return 0;
145         if (*str == ':') {
146             rp->hy = strtoy(str + 1, &str);
147             if (rp->hy < 0)
148                 return 0;
149         }
150         if (*str != 0 && !isspace(*str))
151             return 0;
152         np = getnatp(player->cnum);
153         rp->lx = xabs(np, rp->lx);
154         rp->hx = xabs(np, rp->hx);
155         rp->ly = yabs(np, rp->ly);
156         rp->hy = yabs(np, rp->hy);
157     }
158     xysize_range(rp);
159     return 1;
160 }
161
162 /*
163  * translate @x,y:int into
164  * result params
165  */
166 int
167 sarg_range(char *str, coord *xp, coord *yp, int *dist)
168 {
169     coord x, y;
170     long d;
171     char *end;
172     struct natstr *np;
173
174     if (*str++ != '@')
175         return 0;
176     x = strtox(str, &str);
177     if (x < 0 || *str++ != ',')
178         return 0;
179     y = strtoy(str, &str);
180     if (y < 0 || *str++ != ':')
181         return 0;
182     d = strtol(str, &end, 10);
183     if (end == str || (*end != 0 && !isspace(*end)) || d < 0)
184         return 0;
185     *dist = d;
186     np = getnatp(player->cnum);
187     *xp = xabs(np, x);
188     *yp = yabs(np, y);
189     return 1;
190 }
191
192 /*
193  * list of idents; id/id/id/id/id
194  */
195 int
196 sarg_list(char *str, int *list, int max)
197 {
198     int i, j;
199     long n;
200     char *end;
201
202     i = 0;
203     do {
204         n = strtol(str, &end, 10);
205         if (end == str || n < 0) {
206             pr("Illegal character '%c'\n", *str);
207             return 0;
208         }
209         for (j = 0; j < i; j++) {
210             if (list[j] == n)
211                 break;
212         }
213         if (j == i) {
214             if (i >= max) {
215                 pr("List too long (limit is %d)\n", max);
216                 return 0;
217             }
218             list[i++] = n;
219         }
220         str = end;
221     } while (*str++ == '/');
222
223     if (str[-1] != 0 && !isspace(str[-1])) {
224         pr("Expecting '/', got '%c'\n", str[-1]);
225         return 0;
226     }
227     return i;
228 }