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