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 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.
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 */
51 #include "prototypes.h"
59 static struct castr **fldca;
63 static int gripe(char *, ...) ATTRIBUTE((format (printf, 1, 2)));
64 static int deffld(int, char *, int);
65 static int setnum(int, double);
66 static int setstr(int, char *);
67 static int xunsymbol1(char *, struct symbol *, struct castr *, int);
68 static int setsym(int, char *);
69 static int mtsymset(int, long *);
70 static int add2symset(int, long *, char *);
71 static struct symbol *get_symtab(struct castr *);
72 static int xundump1(FILE *, int, struct castr *);
79 fprintf(stderr, "%s:%d: ", fname, lineno);
81 vfprintf(stderr, fmt, ap);
95 } while (ch == ' ' || ch == '\t');
100 } while (ch != EOF && ch != '\n');
107 getid(FILE *fp, char *buf)
110 if (fscanf(fp, "%1023[^#() \t\n]%n", buf, &n) != 1 || !isalpha(buf[0]))
125 if (sscanf(++src, "%3o%n", &octal_chr, &n) != 1 || n != 3)
127 *dst++ = (char)octal_chr;
137 xufldname(FILE *fp, int i)
145 return gripe("Unexpected EOF");
148 return gripe("Header fields missing"); /* TODO which? */
153 if (getid(fp, buf) < 0)
154 return gripe("Junk in header field %d", i + 1);
158 return deffld(i, buf, -1);
162 if (isdigit(ch) || ch == '-' || ch == '+') {
163 if (fscanf(fp, "%d", &idx) != 1)
164 return gripe("Malformed number in index of header field %d",
167 return gripe("Index must not be negative in header field %d",
170 if (getid(fp, buf) < 0)
171 return gripe("Malformed index in header field %d", i + 1);
172 return gripe("Symbolic index in header field %d not yet implemented",
177 return gripe("Malformed index in header field %d", i + 1);
178 return deffld(i, buf, idx);
183 xufld(FILE *fp, int i)
193 return gripe("Unexpected EOF");
196 return gripe("Field %s missing", fldca[i]->ca_name);
199 case '+': case '-': case '.':
200 case '0': case '1': case '2': case '3': case '4':
201 case '5': case '6': case '7': case '8': case '9':
203 if (fscanf(fp, "%lg", &dbl) != 1)
204 return gripe("Malformed number in field %d", i + 1);
205 return setnum(i, dbl);
212 if (fscanf(fp, "%1023[^\"\n]", buf) != 1 || getc(fp) != '"')
213 return gripe("Malformed string in field %d", i + 1);
215 return gripe("Invalid escape sequence in field %d",
218 return setstr(i, buf);
220 if (mtsymset(i, &set) < 0)
224 if (ch == EOF || ch == '\n')
225 return gripe("Unmatched '(' in field %d", i + 1);
229 if (getid(fp, buf) < 0)
230 return gripe("Junk in field %d", i + 1);
231 if (add2symset(i, &set, buf) < 0)
234 return setnum(i, set);
237 if (getid(fp, buf) < 0)
238 return gripe("Junk in field %d", i + 1);
239 if (!strcmp(buf, "nil"))
240 return setstr(i, NULL);
242 return setsym(i, buf);
247 xuflds(FILE *fp, int (*parse)(FILE *, int))
260 else if (ch != ' ' && ch != '\t')
261 return gripe("Bad field separator after field %d", i + 1);
266 deffld(int fldno, char *name, int idx)
268 struct castr *ca = ef_cadef(cur_type);
271 res = stmtch(name, ca, offsetof(struct castr, ca_name),
272 sizeof(struct castr));
274 return gripe("Header %s of field %d is %s", name, fldno + 1,
275 res == M_NOTUNIQUE ? "ambiguous" : "unknown");
276 if (ca[res].ca_type != NSC_STRINGY && ca[res].ca_len != 0) {
278 return gripe("Header %s requires an index in field %d",
279 ca[res].ca_name, fldno + 1);
280 if (idx >= ca[res].ca_len)
281 return gripe("Header %s(%d) index out of bounds in field %d",
282 ca[res].ca_name, idx, fldno + 1);
283 if (idx < caflds[res])
284 return gripe("Duplicate header %s(%d) in field %d",
285 ca[res].ca_name, idx, fldno + 1);
286 if (idx > caflds[res])
287 return gripe("Expected header %s(%d) in field %d",
288 ca[res].ca_name, caflds[res], fldno + 1);
291 return gripe("Header %s doesn't take an index in field %d",
292 ca[res].ca_name, fldno + 1);
295 return gripe("Duplicate header %s in field %d",
296 ca[res].ca_name, fldno + 1);
298 fldca[fldno] = &ca[res];
304 static struct castr *
305 getfld(int fldno, int *idx)
307 if (fldno >= nflds) {
308 gripe("Too many fields, expected only %d", nflds);
311 if (CANT_HAPPEN(fldno < 0))
314 *idx = fldidx[fldno];
319 setnum(int fldno, double dbl)
326 ca = getfld(fldno, &idx);
331 memb_ptr += ca->ca_off;
332 switch (ca->ca_type) {
335 old = ((signed char *)memb_ptr)[idx];
336 ((signed char *)memb_ptr)[idx] = (signed char)dbl;
339 old = ((unsigned char *)memb_ptr)[idx];
340 ((unsigned char *)memb_ptr)[idx] = (unsigned char)dbl;
343 old = ((short *)memb_ptr)[idx];
344 ((short *)memb_ptr)[idx] = (short)dbl;
347 old = ((unsigned short *)memb_ptr)[idx];
348 ((unsigned short *)memb_ptr)[idx] = (unsigned short)dbl;
351 old = ((int *)memb_ptr)[idx];
352 ((int *)memb_ptr)[idx] = (int)dbl;
355 old = ((long *)memb_ptr)[idx];
356 ((long *)memb_ptr)[idx] = (long)dbl;
359 old = ((coord *)memb_ptr)[idx];
360 ((coord *)memb_ptr)[idx] = XNORM((coord)dbl);
363 old = ((coord *)memb_ptr)[idx];
364 ((coord *)memb_ptr)[idx] = YNORM((coord)dbl);
367 old = ((float *)memb_ptr)[idx];
368 ((float *)memb_ptr)[idx] = (float)dbl;
371 old = ((double *)memb_ptr)[idx];
372 ((double *)memb_ptr)[idx] = dbl;
375 old = ((time_t *)memb_ptr)[idx];
376 ((time_t *)memb_ptr)[idx] = (time_t)dbl;
379 return gripe("Field %d doesn't take numbers", fldno + 1);
382 if ((ca->ca_flags & NSC_CONST) && old != dbl)
383 return gripe("Value for field %d must be %g", fldno + 1, old);
389 setstr(int fldno, char *str)
393 char *memb_ptr, *old;
395 ca = getfld(fldno, &idx);
400 memb_ptr += ca->ca_off;
401 switch (ca->ca_type) {
403 old = ((char **)memb_ptr)[idx];
404 if (!(ca->ca_flags & NSC_CONST))
405 ((char **)memb_ptr)[idx] = str ? strdup(str) : NULL;
408 if (CANT_HAPPEN(idx))
411 return gripe("Field doesn't take nil");
412 if (strlen(str) > ca->ca_len)
413 return gripe("Field %d takes at most %d characters",
414 fldno + 1, ca->ca_len);
416 if (!(ca->ca_flags & NSC_CONST))
417 strncpy(memb_ptr, str, ca->ca_len);
420 return gripe("Field %d doesn't take strings", fldno + 1);
423 if (ca->ca_flags & NSC_CONST) {
424 if (old && (!str || strcmp(old, str)))
425 return gripe("Value for field %d must be \"%s\"", fldno + 1, old);
427 return gripe("Value for field %d must be nil", fldno + 1);
434 xunsymbol1(char *id, struct symbol *symtab, struct castr *ca, int n)
436 int i = stmtch(id, symtab, offsetof(struct symbol, name),
437 sizeof(struct symbol));
439 return gripe("%s %s symbol `%s' in field %d",
440 i == M_NOTUNIQUE ? "Ambiguous" : "Unknown",
446 setsym(int fldno, char *sym)
449 struct symbol *symtab;
452 ca = getfld(fldno, NULL);
456 symtab = get_symtab(ca);
457 if (!symtab || (ca->ca_flags & NSC_BITS))
458 return gripe("Field %d doesn't take symbols", fldno + 1);
460 i = xunsymbol1(sym, symtab, ca, fldno);
463 return setnum(fldno, symtab[i].value);
467 has_const(struct castr ca[])
471 for (i = 0; ca[i].ca_name; i++) {
472 if (ca[i].ca_flags & NSC_CONST)
479 mtsymset(int fldno, long *set)
482 struct symbol *symtab;
484 ca = getfld(fldno, NULL);
488 symtab = get_symtab(ca);
489 if (!symtab || !(ca->ca_flags & NSC_BITS)) {
490 return gripe("Field %d doesn't take symbol sets", fldno + 1);
497 add2symset(int fldno, long *set, char *sym)
500 struct symbol *symtab;
503 ca = getfld(fldno, NULL);
507 symtab = get_symtab(ca);
508 i = xunsymbol1(sym, symtab, ca, fldno);
511 *set |= symtab[i].value;
515 static struct symbol *
516 get_symtab(struct castr *ca)
518 int symtype = ca->ca_table;
519 struct symbol *symtab;
521 if (symtype == EF_BAD || ef_cadef(symtype) != symbol_ca)
524 symtab = ef_ptr(symtype, 0);
525 CANT_HAPPEN(!symtab);
530 xuheader(FILE *fp, int expected_table)
536 while ((ch = skipfs(fp)) == '\n')
538 if (ch == EOF && expected_table == EF_BAD)
545 ? fscanf(fp, "config%*[ \t]%63[^ \t#\n]%n", name, &res) != 1
546 : fscanf(fp, "XDUMP%*[ \t]%63[^ \t#\n]%*[ \t]%*[^ \t#\n]%n",
547 name, &res) != 1) || res < 0)
548 return gripe("Expected xdump header");
550 type = ef_byname(name);
552 return gripe("Unknown table `%s'", name);
553 if (expected_table != EF_BAD && expected_table != type)
554 return gripe("Expected table `%s', not `%s'",
555 ef_nameof(expected_table), name);
557 if (!ef_cadef(type) || !(ef_flags(type) & EFF_MEM)) {
558 CANT_HAPPEN(expected_table != EF_BAD);
559 return gripe("Undumping of table `%s' not implemented", name);
562 if (skipfs(fp) != '\n')
563 return gripe("Junk after xdump header");
570 xutrailer(FILE *fp, int type, int row)
576 if (fscanf(fp, "config%n", &res) != 0 || res < 0)
577 return gripe("Malformed table footer");
579 if (fscanf(fp, "%d", &rows) != 1)
580 return gripe("Malformed table footer");
582 return gripe("Read %d rows, which doesn't match footer "
583 "%d rows", row, rows);
585 if (skipfs(fp) != '\n')
586 return gripe("Junk after table footer");
593 xundump(FILE *fp, char *file, int expected_table)
596 int type, nca, i, ch;
603 if ((type = xuheader(fp, expected_table)) < 0)
607 if (CANT_HAPPEN(!ca))
611 for (i = 0; ca[i].ca_name; i++) {
613 if (!(ca[i].ca_flags & NSC_EXTRA))
614 nflds += MAX(1, ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0);
616 fldca = calloc(nflds, sizeof(*fldca));
617 fldidx = calloc(nflds, sizeof(*fldidx));
618 caflds = calloc(nca, sizeof(*caflds));
622 while ((ch = skipfs(fp)) == '\n')
625 if (xuflds(fp, xufldname) < 0)
628 struct castr **fca = fldca;
631 for (i = 0; ca[i].ca_name; i++) {
632 if ((ca[i].ca_flags & NSC_EXTRA))
634 n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
643 if (type >= 0 && xundump1(fp, type, ca) < 0)
650 /* Skip empty lines so that callers can easily check for EOF */
651 while ((ch = skipfs(fp)) == '\n')
659 xundump1(FILE *fp, int type, struct castr *ca)
661 struct empfile *ep = &empfile[type];
662 int fixed_rows = has_const(ca);
663 int need_sentinel = !EF_IS_GAME_STATE(type);
666 for (row = 0;; ++row) {
667 while ((ch = skipfs(fp)) == '\n')
672 /* TODO ability to skip records */
674 if (row >= ep->csize - !!need_sentinel)
675 /* TODO grow cache unless EFF_STATIC */
676 return gripe("Too many rows for table %s", ef_nameof(type));
679 ep->cids = ep->fids = row + 1;
681 cur_obj = ef_ptr(type, row);
683 return gripe("Too many rows for table %s", ef_nameof(type));
684 if (xuflds(fp, xufld) < 0)
687 if (row != ep->fids) {
689 return gripe("Table %s requires %d rows, got %d",
690 ef_nameof(type), ep->fids, row);
692 ep->cids = ep->fids = row;
693 if (EF_IS_GAME_STATE(type) && row != ep->csize)
694 /* TODO truncate file */
695 gripe("Warning: should resize table %s from %d to %d, not implemented",
696 ef_nameof(type), ep->csize, row);
701 if (CANT_HAPPEN(row >= ep->csize))
702 return gripe("No space for sentinel");
703 memset(ep->cache + ep->size * row, 0, ep->size);
706 if (xutrailer(fp, type, row) < 0)