]> git.pond.sub.org Git - empserver/blob - src/lib/common/nstreval.c
Fix trailing whitespace
[empserver] / src / lib / common / nstreval.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  *  nstreval.c: evaluate compiled values
29  *
30  *  Known contributors to this file:
31  *     Dave Pare, 1989
32  *     Steve McClure, 1997
33  *     Markus Armbruster, 2004-2008
34  */
35
36 #include <config.h>
37
38 #include <limits.h>
39 #include <string.h>
40 #include "file.h"
41 #include "nat.h"
42 #include "nsc.h"
43 #include "optlist.h"
44
45 /*
46  * Initialize VAL to symbolic value for selector CA with index IDX.
47  * Return VAL.
48  */
49 struct valstr *
50 nstr_mksymval(struct valstr *val, struct castr *ca, int idx)
51 {
52     val->val_type = ca->ca_type;
53     val->val_cat = NSC_OFF;
54     val->val_as.sym.off = ca->ca_off;
55     val->val_as.sym.len = ca->ca_len;
56     val->val_as.sym.idx = idx;
57     val->val_as.sym.get = ca->ca_get;
58     return val;
59 }
60
61 /*
62  * Evaluate VAL.
63  * If VAL is symbolic, evaluate it into a promoted value type.
64  * Translate it for country CNUM (coordinate system and contact
65  * status), except when CNUM is NATID_BAD.
66  * PTR points to a context object of the type that was used to compile
67  * the value.
68  * Unless WANT is NSC_NOTYPE, coerce the value to promoted value type
69  * WANT.  VAL must be coercible.  That's the case if a previous
70  * nstr_coerce_val(VAL, WANT, STR) succeeded.
71  */
72 struct valstr *
73 nstr_exec_val(struct valstr *val, natid cnum, void *ptr, enum nsc_type want)
74 {
75     char *memb_ptr;
76     enum nsc_type valtype;
77     int idx;
78     coord c;
79     struct natstr *natp;
80
81     if (CANT_HAPPEN(want != NSC_NOTYPE && !NSC_IS_PROMOTED(want)))
82         want = nstr_promote(want);
83
84     switch (val->val_cat) {
85     case NSC_VAL:
86         valtype = val->val_type;
87         break;
88     case NSC_OFF:
89         if (val->val_as.sym.get) {
90             natp = getnatp(cnum);
91             do {
92                 ptr = val->val_as.sym.get(val, natp, ptr);
93             } while (ptr && val->val_as.sym.get);
94             if (!ptr) {
95                 valtype = val->val_type;
96                 val->val_cat = NSC_VAL;
97                 break;
98             }
99         }
100
101         valtype = NSC_LONG;
102         memb_ptr = ptr;
103         memb_ptr += val->val_as.sym.off;
104         idx = val->val_as.sym.idx;
105         switch (val->val_type) {
106         case NSC_CHAR:
107             val->val_as.lng = ((signed char *)memb_ptr)[idx];
108             break;
109         case NSC_UCHAR:
110             val->val_as.lng = ((unsigned char *)memb_ptr)[idx];
111             break;
112         case NSC_SHORT:
113             val->val_as.lng = ((short *)memb_ptr)[idx];
114             break;
115         case NSC_USHORT:
116             val->val_as.lng = ((unsigned short *)memb_ptr)[idx];
117             break;
118         case NSC_INT:
119             val->val_as.lng = ((int *)memb_ptr)[idx];
120             break;
121         case NSC_LONG:
122             val->val_as.lng = ((long *)memb_ptr)[idx];
123             break;
124         case NSC_XCOORD:
125             c = ((short *)memb_ptr)[idx];
126             if (cnum == NATID_BAD) {
127                 /* FIXME use variant of xrel() that takes orig instead of nation */
128                 if (c >= WORLD_X / 2)
129                     c -= WORLD_X;
130             } else
131                 c = xrel(getnatp(cnum), c);
132             val->val_as.lng = c;
133             break;
134         case NSC_YCOORD:
135             c = ((short *)memb_ptr)[idx];
136             if (cnum == NATID_BAD) {
137                 /* FIXME use variant of yrel() that takes orig instead of nation */
138                 if (c >= WORLD_Y / 2)
139                     c -= WORLD_Y;
140             } else
141                 c = yrel(getnatp(cnum), c);
142             val->val_as.lng = c;
143             break;
144         case NSC_HIDDEN:
145             val->val_as.lng = -1;
146             if (CANT_HAPPEN(((struct natstr *)ptr)->ef_type != EF_NATION))
147                 break;
148             if (opt_HIDDEN && cnum != NATID_BAD) {
149                 natp = getnatp(cnum);
150                 if (natp->nat_stat != STAT_GOD
151                     && !(getcontact(natp, idx) && getcontact(ptr, idx)))
152                     break;
153             }
154             val->val_as.lng = ((unsigned char *)memb_ptr)[idx];
155             break;
156         case NSC_FLOAT:
157             val->val_as.dbl = ((float *)memb_ptr)[idx];
158             valtype = NSC_DOUBLE;
159             break;
160         case NSC_DOUBLE:
161             val->val_as.dbl = ((double *)memb_ptr)[idx];
162             valtype = NSC_DOUBLE;
163             break;
164         case NSC_STRINGY:
165             CANT_HAPPEN(idx);
166             val->val_as.str.maxsz = val->val_as.sym.len;
167             val->val_as.str.base = (char *)memb_ptr;
168             valtype = NSC_STRING;
169             break;
170         case NSC_STRING:
171             val->val_as.str.base = ((char **)memb_ptr)[idx];
172             val->val_as.str.maxsz = INT_MAX;
173             valtype = NSC_STRING;
174             break;
175         case NSC_TIME:
176             val->val_as.lng = ((time_t *)memb_ptr)[idx];
177             break;
178         default:
179             CANT_REACH();
180             val->val_as.lng = 0;
181         }
182         val->val_cat = NSC_VAL;
183         break;
184     default:
185         CANT_REACH();
186         valtype = NSC_NOTYPE;
187     }
188
189     if (valtype == want)
190         ;
191     else if (want == NSC_DOUBLE) {
192         if (valtype == NSC_LONG) {
193             valtype = want;
194             val->val_as.dbl = val->val_as.lng;
195         }
196     } else if (want == NSC_STRING)
197         CANT_REACH();           /* FIXME implement */
198
199     if (CANT_HAPPEN(valtype != want && want != NSC_NOTYPE)) {
200         /* make up an error value */
201         valtype = want;
202         memset(&val->val_as, 0, sizeof(val->val_as));
203     }
204
205     val->val_type = valtype;
206     return val;
207 }
208
209 /*
210  * Promote VALTYPE.
211  * If VALTYPE is an integer type, return NSC_LONG.
212  * If VALTYPE is a floating-point type, return NSC_DOUBLE.
213  * If VALTYPE is a string type, return NSC_STRING.
214  */
215 int
216 nstr_promote(int valtype)
217 {
218     switch (valtype) {
219     case NSC_LONG:
220     case NSC_DOUBLE:
221     case NSC_STRING:
222         break;
223     case NSC_CHAR:
224     case NSC_UCHAR:
225     case NSC_SHORT:
226     case NSC_USHORT:
227     case NSC_INT:
228     case NSC_XCOORD:
229     case NSC_YCOORD:
230     case NSC_HIDDEN:
231     case NSC_TIME:
232         valtype = NSC_LONG;
233         break;
234     case NSC_FLOAT:
235         valtype = NSC_DOUBLE;
236         break;
237     case NSC_STRINGY:
238         valtype = NSC_STRING;
239         break;
240     default:
241         CANT_REACH();
242         valtype = NSC_NOTYPE;
243     }
244     return valtype;
245 }
246
247 char *
248 symbol_by_value(int key, struct symbol *table)
249 {
250     int i;
251
252     for (i = 0; table[i].name; i++)
253         if (key == table[i].value)
254             return table[i].name;
255
256     return NULL;
257 }