2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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: Load back xdump output
30 * Known contributors to this file:
31 * Ron Koenderink, 2005
32 * Markus Armbruster, 2005
35 /* FIXME normalize terminology: table/rows/columns or file/records/fields */
47 #include "prototypes.h"
52 #define MAX_NUM_COLUMNS 256
54 static char *fname = "";
55 static int lineno = 0;
60 * This structure could be replaced with struct valstr.
71 enum enum_value v_type;
78 static int gripe(char *fmt, ...) ATTRIBUTE((format (printf, 1, 2)));
85 fprintf(stderr, "%s:%d: ", fname, lineno);
87 vfprintf(stderr, fmt, ap);
101 } while (ch == ' ' || ch == '\t');
106 } while (ch != EOF && ch != '\n');
113 getid(FILE *fp, char *buf)
116 if (fscanf(fp, "%1023[^#() \t\n]%n", buf, &n) != 1 || !isalpha(buf[0]))
131 if (sscanf(++src, "%3o%n", &octal_chr, &n) != 1 || n != 3)
133 *dst++ = (char)octal_chr;
143 xuflds(FILE *fp, struct value values[])
151 values[i].v_type = VAL_NOTUSED;
152 if (i >= MAX_NUM_COLUMNS)
153 return gripe("Too many columns");
158 return gripe("Unexpected EOF");
161 case '+': case '-': case '.':
162 case '0': case '1': case '2': case '3': case '4':
163 case '5': case '6': case '7': case '8': case '9':
165 if (fscanf(fp, "%lg", &values[i].v_field.v_double) != 1)
166 return gripe("Malformed number in field %d", i + 1);
167 values[i].v_type = VAL_DOUBLE;
175 if (fscanf(fp, "%1023[^\"\n]", buf) != 1 || getc(fp) != '"')
176 return gripe("Malformed string in field %d", i + 1);
178 return gripe("Invalid escape sequence in field %d",
181 values[i].v_type = VAL_STRING;
182 values[i].v_field.v_string = strdup(buf);
189 if (ch == EOF || ch == '\n')
190 return gripe("Unmatched '(' in field %d", i + 1);
196 return gripe("Junk in field %d", i + 1);
197 p = realloc(p, len + l1 + 2);
198 strcpy(p + len, buf);
199 strcpy(p + len + l1, " ");
204 values[i].v_type = VAL_SYMBOL_SET;
205 values[i].v_field.v_string = p;
209 if (getid(fp, buf) < 0) {
210 return gripe("Junk in field %d", i + 1);
212 if (!strcmp(buf, "nil")) {
213 values[i].v_type = VAL_STRING;
214 values[i].v_field.v_string = NULL;
217 values[i].v_type = VAL_SYMBOL;
218 values[i].v_field.v_string = strdup(buf);
224 else if (ch != ' ' && ch != '\t')
225 return gripe("Bad field separator after field %d", i + 1);
230 freeflds(struct value values[])
234 for (vp = values; vp->v_type != VAL_NOTUSED; vp++) {
235 if (vp->v_type != VAL_DOUBLE)
236 free(vp->v_field.v_string);
241 xunsymbol1(char *id, struct symbol *symtab, struct castr *ca, int n)
243 int i = stmtch(id, symtab, offsetof(struct symbol, name),
244 sizeof(struct symbol));
246 return gripe("%s %s symbol `%s' in field %d",
247 i == M_NOTUNIQUE ? "Ambiguous" : "Unknown",
253 xunsymbol(struct castr *ca, struct value *vp, int n)
255 struct symbol *symtab = (struct symbol *)empfile[ca->ca_table].cache;
256 char *buf = vp->v_field.v_string;
261 if (vp->v_type == VAL_SYMBOL_SET) {
262 if (!(ca->ca_flags & NSC_BITS) || ca->ca_table == EF_BAD)
263 return gripe("%s doesn't take a symbol set in field %d",
266 for (token = strtok(buf, " "); token; token = strtok(NULL, " ")) {
267 i = xunsymbol1(token, symtab, ca, n);
270 value |= symtab[i].value;
272 } else if (vp->v_type == VAL_SYMBOL) {
273 if ((ca->ca_flags & NSC_BITS) || ca->ca_table == EF_BAD)
274 return gripe("%s doesn't take a symbol in field %d",
276 i = xunsymbol1(buf, symtab, ca, n);
279 value = symtab[i].value;
283 vp->v_type = VAL_DOUBLE;
284 vp->v_field.v_double = value;
289 has_const(struct castr ca[])
293 for (i = 0; ca[i].ca_name; i++) {
294 if (ca[i].ca_flags & NSC_CONST)
301 xuinitrow(int type, int row)
303 struct empfile *ep = &empfile[type];
304 char *ptr = ep->cache + ep->size * row;
306 memset(ptr, 0, ep->size);
313 xuloadrow(int type, int row, struct value values[])
316 struct empfile *ep = &empfile[type];
317 char *ptr = ep->cache + ep->size * row;
318 struct castr *ca = ep->cadef;
323 while (ca[i].ca_type != NSC_NOTYPE &&
324 values[j].v_type != VAL_NOTUSED) {
325 if (ca[i].ca_flags & NSC_EXTRA) {
329 row_ref = (char *)ptr + ca[i].ca_off;
334 * factor out NSC_CONST comparsion
336 switch (values[j].v_type) {
339 if (xunsymbol(&ca[i], &values[j], j) < 0)
343 switch (ca[i].ca_type) {
345 if (ca[i].ca_flags & NSC_CONST) {
346 if (((int *)row_ref)[k] != (int)
347 values[j].v_field.v_double)
348 gripe("Field %s must be same, "
349 "read %d != expected %d",
352 (int)values[j].v_field.v_double);
355 ((int *)row_ref)[k] =
356 (int)values[j].v_field.v_double;
359 if (ca[i].ca_flags & NSC_CONST) {
360 if (((long *)row_ref)[k] != (long)
361 values[j].v_field.v_double)
362 gripe("Field %s must be same, "
363 "read %ld != expected %ld",
365 ((long *)row_ref)[k],
366 (long)values[j].v_field.v_double);
368 ((long *)row_ref)[k] = (long)
369 values[j].v_field.v_double;
372 if (ca[i].ca_flags & NSC_CONST) {
373 if (((unsigned short *)row_ref)[k] !=
374 (unsigned short)values[j].v_field.v_double)
375 gripe("Field %s must be same, "
376 "read %d != expected %d",
378 ((unsigned short *)row_ref)[k],
379 (unsigned short)values[j].v_field.v_double);
381 ((unsigned short *)row_ref)[k] = (unsigned short)
382 values[j].v_field.v_double;
385 if (ca[i].ca_flags & NSC_CONST) {
386 if (((unsigned char *)row_ref)[k] != (unsigned char)
387 values[j].v_field.v_double)
388 gripe("Field %s must be same, "
389 "read %d != expected %d",
391 ((unsigned char *)row_ref)[k],
392 (unsigned char)values[j].v_field.v_double);
394 ((unsigned char *)row_ref)[k] = (unsigned char)
395 values[j].v_field.v_double;
398 if (ca[i].ca_flags & NSC_CONST) {
399 if (((float *)row_ref)[k] != (float)
400 values[j].v_field.v_double)
401 gripe("Field %s must be same, "
402 "read %g != expected %g",
404 ((float *)row_ref)[k],
405 (float)values[j].v_field.v_double);
407 ((float *)row_ref)[k] = (float)
408 values[j].v_field.v_double;
411 return gripe("Field %s is a string type, "
412 "but %lg was read which is a number",
413 ca[i].ca_name, values[j].v_field.v_double);
415 return gripe("Field %s's type %d is not supported",
416 ca[i].ca_name, ca[i].ca_type);
420 switch(ca[i].ca_type) {
422 if (ca[i].ca_flags & NSC_CONST) {
423 if (strcmp(((char **)row_ref)[k],
424 values[j].v_field.v_string) != 0)
425 gripe("Field %s must be same, "
426 "read %s != expected %s",
428 ((char **)row_ref)[k],
429 values[j].v_field.v_string);
431 ((char **)row_ref)[k]
432 = strdup(values[j].v_field.v_string);
439 return gripe("Field %s is a number type %d, "
440 "but %s was read which is a string",
441 ca[i].ca_name, ca[i].ca_type,
442 values[j].v_field.v_string);
444 return gripe("Field %s's type %d is not supported",
445 ca[i].ca_name, ca[i].ca_type);
449 return gripe("Missing column %s in file", ca[i].ca_name);
451 return gripe("Unknown value type %d", values[j].v_type);
455 } while (k < ca[i].ca_len);
458 if (ca[i].ca_type != NSC_NOTYPE)
459 return gripe("Missing column %s in file", ca[i].ca_name);
460 switch (values[j].v_type) {
466 return gripe("Extra junk after the last column, read %s",
467 values[j].v_field.v_string);
469 return gripe("Extra junk after the last column, read %lg",
470 values[j].v_field.v_double);
472 return gripe("Extra junk after the last column, "
473 "unknown value type %d", values[j].v_type);
479 xuheader(FILE *fp, int expected_table, struct value values[])
486 while ((ch = skipfs(fp)) == '\n')
488 if (ch == EOF && expected_table == EF_BAD)
495 ? fscanf(fp, "config%*[ \t]%63[^ \t#\n]%n", name, &res) != 1
496 : fscanf(fp, "XDUMP%*[ \t]%63[^ \t#\n]%*[ \t]%*[^ \t#\n]%n",
497 name, &res) != 1) || res < 0)
498 return gripe("Expected xdump header");
500 if (skipfs(fp) != '\n')
501 return gripe("Junk after xdump header");
503 type = ef_byname(name);
505 return gripe("Unknown table `%s'", name);
507 if (CANT_HAPPEN(!(ep->flags & EFF_MEM)))
508 return -1; /* not implemented */
510 if (expected_table != EF_BAD && expected_table != type)
511 return gripe("Expected table `%s', not `%s'",
512 ef_nameof(expected_table), name);
517 xucolumnheader(FILE *fp, int type)
524 while ((ch = skipfs(fp)) == '\n');
527 /* FIXME parse column header */
528 if (fscanf(fp, "%*[^\n]\n") == -1)
529 return gripe("Invalid Column Header for table %s",
537 xutrailer(FILE *fp, int type, int row)
543 if (fscanf(fp, "config%n", &res) != 0) {
544 return gripe("Malformed table footer");
549 if (ch != '\n' || !human)
550 return gripe("Malformed table footer");
553 if (fscanf(fp, "%d", &rows) != 1)
554 return gripe("Malformed table footer");
556 return gripe("Read %d rows, which doesn't match footer "
557 "%d rows", row, rows);
559 if (skipfs(fp) != '\n')
560 return gripe("Junk after table footer");
562 while ((ch = skipfs(fp)) == '\n') ;
569 xundump(FILE *fp, char *file, int expected_table)
574 struct value values[MAX_NUM_COLUMNS + 1];
578 if (strcmp(fname, file) != 0) {
584 if ((type = xuheader(fp, expected_table, values)) == -1)
588 fixed_rows = has_const(ef_cadef(type));
589 need_sentinel = !fixed_rows; /* FIXME only approximation */
591 if (xucolumnheader(fp, type) == -1)
603 * Add column count check to the return value of xuflds()
605 res = xuflds(fp, values);
606 if (res > 0 && row >= ep->csize - 1) {
607 /* TODO grow cache unless EFF_STATIC */
608 gripe("Too many rows for table %s", ef_nameof(type));
613 xuinitrow(type, row);
614 res = xuloadrow(type, row, values);
621 if (fixed_rows && row != ep->csize -1)
622 return gripe("Table %s requires %d rows, got %d",
623 ef_nameof(type), ep->csize - 1, row);
626 xuinitrow(type, row);
628 if (xutrailer(fp, type, row) == -1)
631 ep->fids = ep->cids = row;