2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
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.
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.
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
22 * See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * xundump.c: Text loading functions based on xdump output
30 * Known contributors to this file:
31 * Ron Koenderink, 2005
43 #include "prototypes.h"
48 #define MAX_NUM_COLUMNS 256
50 static char *fname = "";
51 static int lineno = 0;
55 * This structure could be replaced with struct valstr.
65 enum enum_value v_type;
77 fprintf(stderr, "%s:%d: ", fname, lineno);
79 vfprintf(stderr, fmt, ap);
96 if (sscanf(++src, "%3o%n", &octal_chr, &n) != 1 || n != 3)
98 *dst++ = (char)octal_chr;
108 xuflds(FILE *fp, struct value values[])
114 for (i = 0; i < MAX_NUM_COLUMNS; i++) {
115 values[i].v_type = VAL_NOTUSED;
121 return gripe("Unexpected EOF");
122 case '+': case '-': case '.':
123 case '0': case '1': case '2': case '3': case '4':
124 case '5': case '6': case '7': case '8': case '9':
125 if (fscanf(fp, "%lg%c", &values[i].v_field.v_double, &sep) != 2)
126 return gripe("Malformed number in field %d", i + 1);
127 values[i].v_type = VAL_DOUBLE;
130 if (fscanf(fp, "\"%1023[^ \n]%c", buf, &sep) != 2
131 || buf[strlen(buf)-1] != '"')
132 return gripe("Malformed string in field %d", i + 1);
133 buf[strlen(buf)-1] = '\0';
135 return gripe("Invalid escape sequence in field %d",
137 values[i].v_type = VAL_STRING;
138 values[i].v_field.v_string = strdup(buf);
141 if (fscanf(fp, "%1023[^ \n]%c", buf, &sep) != 2) {
142 return gripe("Junk in field %d", i + 1);
144 if (!strcmp(buf, "nil")) {
145 values[i].v_type = VAL_STRING;
146 values[i].v_field.v_string = NULL;
149 values[i].v_type = VAL_SYMBOL;
150 values[i].v_field.v_string = strdup(buf);
157 "Expected space or newline as field separator found %c",
160 if (i >= MAX_NUM_COLUMNS)
161 return gripe("Too many columns");
163 return gripe("No columns read");
164 values[++i].v_type = VAL_NOTUSED;
169 freeflds(struct value values[])
173 for (vp = values; vp->v_type != VAL_NOTUSED; vp++) {
174 if (vp->v_type != VAL_DOUBLE)
175 free(vp->v_field.v_string);
180 xunsymbol(struct castr *ca, char *buf)
182 struct symbol *symbol = (struct symbol *)empfile[ca->ca_table].cache;
187 if (ca->ca_flags & NSC_BITS)
188 token = strtok( buf, "|");
192 if (!token || token[0] == '\0')
193 return gripe("Empty symbol value for field %s", ca->ca_name);
196 if ((i = stmtch(token, symbol, offsetof(struct symbol, name),
197 sizeof(struct symbol))) != M_NOTFOUND) {
198 if (!(ca->ca_flags & NSC_BITS))
199 return(symbol[i].value);
200 value |= symbol[i].value;
203 return gripe("Symbol %s was not found for field %s", token,
205 token = strtok(NULL, "|");
211 has_const(struct castr ca[])
215 for (i = 0; ca[i].ca_name; i++) {
216 if (ca[i].ca_flags & NSC_CONST)
223 xuinitrow(int type, int row)
225 struct empfile *ep = &empfile[type];
226 char *ptr = ep->cache + ep->size * row;
228 memset(ptr, 0, ep->size);
235 xuloadrow(int type, int row, struct value values[])
238 struct empfile *ep = &empfile[type];
239 char *ptr = ep->cache + ep->size * row;
240 struct castr *ca = ep->cadef;
245 while (ca[i].ca_type != NSC_NOTYPE &&
246 values[j].v_type != VAL_NOTUSED) {
247 row_ref = (char *)ptr + ca[i].ca_off;
252 * factor out NSC_CONST comparsion
254 switch (values[j].v_type) {
256 if (ca[i].ca_table == EF_BAD)
257 return(gripe("Found symbol string %s, but column %s "
258 "is not symbol or symbol sets",
259 values[j].v_field.v_string, ca[i].ca_name));
260 values[j].v_field.v_double =
261 (double)xunsymbol(&ca[i], values[j].v_field.v_string);
262 if (values[j].v_field.v_double < 0.0)
268 switch (ca[i].ca_type) {
270 if (ca[i].ca_flags & NSC_CONST) {
271 if (((int *)row_ref)[k] != (int)
272 values[j].v_field.v_double)
273 gripe("Field %s must be same, "
274 "read %d != expected %d",
277 (int)values[j].v_field.v_double);
280 ((int *)row_ref)[k] =
281 (int)values[j].v_field.v_double;
284 if (ca[i].ca_flags & NSC_CONST) {
285 if (((long *)row_ref)[k] != (long)
286 values[j].v_field.v_double)
287 gripe("Field %s must be same, "
288 "read %ld != expected %ld",
290 ((long *)row_ref)[k],
291 (long)values[j].v_field.v_double);
293 ((long *)row_ref)[k] = (long)
294 values[j].v_field.v_double;
297 if (ca[i].ca_flags & NSC_CONST) {
298 if (((unsigned short *)row_ref)[k] !=
299 (unsigned short)values[j].v_field.v_double)
300 gripe("Field %s must be same, "
301 "read %d != expected %d",
303 ((unsigned short *)row_ref)[k],
304 (unsigned short)values[j].v_field.v_double);
306 ((unsigned short *)row_ref)[k] = (unsigned short)
307 values[j].v_field.v_double;
310 if (ca[i].ca_flags & NSC_CONST) {
311 if (((unsigned char *)row_ref)[k] != (unsigned char)
312 values[j].v_field.v_double)
313 gripe("Field %s must be same, "
314 "read %d != expected %d",
316 ((unsigned char *)row_ref)[k],
317 (unsigned char)values[j].v_field.v_double);
319 ((unsigned char *)row_ref)[k] = (unsigned char)
320 values[j].v_field.v_double;
323 if (ca[i].ca_flags & NSC_CONST) {
324 if (((float *)row_ref)[k] != (float)
325 values[j].v_field.v_double)
326 gripe("Field %s must be same, "
327 "read %g != expected %g",
329 ((float *)row_ref)[k],
330 (float)values[j].v_field.v_double);
332 ((float *)row_ref)[k] = (float)
333 values[j].v_field.v_double;
336 return gripe("Field %s is a string type, "
337 "but %lg was read which is a number",
338 ca[i].ca_name, values[j].v_field.v_double);
340 return gripe("Field %s's type %d is not supported",
341 ca[i].ca_name, ca[i].ca_type);
345 switch(ca[i].ca_type) {
347 return gripe("Field %s is of NSC_STRINGY type "
348 "which is not supported", ca[i].ca_name);
350 if (ca[i].ca_flags & NSC_CONST) {
351 if (strcmp(((char **)row_ref)[k],
352 values[j].v_field.v_string) != 0)
353 gripe("Field %s must be same, "
354 "read %s != expected %s",
356 *((char **)row_ref)[k],
357 *values[j].v_field.v_string);
359 ((char **)row_ref)[k] = values[j].v_field.v_string;
366 return gripe("Field %s is a number type %d, "
367 "but %s was read which is a string",
368 ca[i].ca_name, ca[i].ca_type,
369 values[j].v_field.v_string);
371 return gripe("Field %s's type %d is not supported",
372 ca[i].ca_name, ca[i].ca_type);
376 return gripe("Missing column %s in file", ca[i].ca_name);
378 return gripe("Unknown value type %d", values[j].v_type);
382 } while (k < ca[i].ca_len);
385 if (ca[i].ca_type != NSC_NOTYPE)
386 return gripe("Missing column %s in file", ca[i].ca_name);
387 switch (values[j].v_type) {
392 return gripe("Extra junk after the last column, read %s",
393 values[j].v_field.v_string);
395 return gripe("Extra junk after the last column, read %lg",
396 values[j].v_field.v_double);
398 return gripe("Extra junk after the last column, "
399 "unknown value type %d", values[j].v_type);
405 xundump(FILE *fp, char *file, int expected_table)
409 int row, res, rows, ch;
410 struct value values[MAX_NUM_COLUMNS + 1];
414 if (strcmp(fname, file) != 0) {
420 if (fscanf(fp, "XDUMP %63s %*d%c", name, &sep) != 2)
421 return gripe("Expected XDUMP header");
423 return gripe("Junk after XDUMP header");
425 type = ef_byname(name);
427 return gripe("Table not found %s", name);
429 if (expected_table != EF_BAD && expected_table != type)
430 return gripe("Incorrect Table expecting %s got %s",
431 ef_nameof(expected_table), name);
433 fixed_rows = has_const(ef_cadef(type));
435 for (row = 0; ; row++) {
440 return gripe("Unexpected EOF");
445 * Add column count check to the return value of xuflds()
447 res = xuflds(fp, values);
448 if (res > 0 && row >= empfile[type].csize - 1) {
449 gripe("Too many rows for table %s", name);
453 empfile[type].fids = row + 1;
455 xuinitrow(type, row);
456 res = xuloadrow(type, row, values);
463 if (fscanf(fp, "/%d%c", &rows, &sep) != 2)
464 return gripe("Failed to find number of rows trailer");
466 return gripe("Number of rows doesn't match between "
467 "the trailer and what was read");
468 if (fixed_rows && row != empfile[type].csize -1)
469 return gripe("Number of rows doesn't match, and "
470 "it must for table %s", name);
472 return gripe("Junk after number of rows trailer");
475 xuinitrow(type, row);