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)
394 char *memb_ptr, *old;
396 ca = getfld(fldno, &idx);
401 memb_ptr += ca->ca_off;
402 switch (ca->ca_type) {
404 old = ((char **)memb_ptr)[idx];
405 if (!(ca->ca_flags & NSC_CONST))
406 ((char **)memb_ptr)[idx] = str ? strdup(str) : NULL;
407 len = 65535; /* really SIZE_MAX, but it's C99 */
410 if (CANT_HAPPEN(idx))
413 return gripe("Field doesn't take nil");
415 if (strlen(str) > len)
416 return gripe("Field %d takes at most %d characters",
419 if (!(ca->ca_flags & NSC_CONST))
420 strncpy(memb_ptr, str, len);
423 return gripe("Field %d doesn't take strings", fldno + 1);
426 if (ca->ca_flags & NSC_CONST) {
427 if (old && (!str || strncmp(old, str, len)))
428 return gripe("Value for field %d must be \"%.*s\"",
429 fldno + 1, len, old);
431 return gripe("Value for field %d must be nil", fldno + 1);
438 xunsymbol1(char *id, struct symbol *symtab, struct castr *ca, int n)
440 int i = stmtch(id, symtab, offsetof(struct symbol, name),
441 sizeof(struct symbol));
443 return gripe("%s %s symbol `%s' in field %d",
444 i == M_NOTUNIQUE ? "Ambiguous" : "Unknown",
450 setsym(int fldno, char *sym)
453 struct symbol *symtab;
456 ca = getfld(fldno, NULL);
460 symtab = get_symtab(ca);
461 if (!symtab || (ca->ca_flags & NSC_BITS))
462 return gripe("Field %d doesn't take symbols", fldno + 1);
464 i = xunsymbol1(sym, symtab, ca, fldno);
467 return setnum(fldno, symtab[i].value);
471 has_const(struct castr ca[])
475 for (i = 0; ca[i].ca_name; i++) {
476 if (ca[i].ca_flags & NSC_CONST)
483 mtsymset(int fldno, long *set)
486 struct symbol *symtab;
488 ca = getfld(fldno, NULL);
492 symtab = get_symtab(ca);
493 if (!symtab || !(ca->ca_flags & NSC_BITS)) {
494 return gripe("Field %d doesn't take symbol sets", fldno + 1);
501 add2symset(int fldno, long *set, char *sym)
504 struct symbol *symtab;
507 ca = getfld(fldno, NULL);
511 symtab = get_symtab(ca);
512 i = xunsymbol1(sym, symtab, ca, fldno);
515 *set |= symtab[i].value;
519 static struct symbol *
520 get_symtab(struct castr *ca)
522 int symtype = ca->ca_table;
523 struct symbol *symtab;
525 if (symtype == EF_BAD || ef_cadef(symtype) != symbol_ca)
528 symtab = ef_ptr(symtype, 0);
529 CANT_HAPPEN(!symtab);
534 xuheader(FILE *fp, int expected_table)
540 while ((ch = skipfs(fp)) == '\n')
542 if (ch == EOF && expected_table == EF_BAD)
549 ? fscanf(fp, "config%*[ \t]%63[^ \t#\n]%n", name, &res) != 1
550 : fscanf(fp, "XDUMP%*[ \t]%63[^ \t#\n]%*[ \t]%*[^ \t#\n]%n",
551 name, &res) != 1) || res < 0)
552 return gripe("Expected xdump header");
554 type = ef_byname(name);
556 return gripe("Unknown table `%s'", name);
557 if (expected_table != EF_BAD && expected_table != type)
558 return gripe("Expected table `%s', not `%s'",
559 ef_nameof(expected_table), name);
561 if (!ef_cadef(type) || !(ef_flags(type) & EFF_MEM)) {
562 CANT_HAPPEN(expected_table != EF_BAD);
563 return gripe("Undumping of table `%s' not implemented", name);
566 if (skipfs(fp) != '\n')
567 return gripe("Junk after xdump header");
574 xuheader1(FILE *fp, int type, struct castr ca[])
583 while ((ch = skipfs(fp)) == '\n')
586 if (xuflds(fp, xufldname) < 0)
592 for (i = 0; ca[i].ca_name; i++) {
593 if ((ca[i].ca_flags & NSC_EXTRA))
595 n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
608 xutrailer(FILE *fp, int type, int row)
614 if (fscanf(fp, "config%n", &res) != 0 || res < 0)
615 return gripe("Malformed table footer");
617 if (fscanf(fp, "%d", &rows) != 1)
618 return gripe("Malformed table footer");
620 return gripe("Read %d rows, which doesn't match footer "
621 "%d rows", row, rows);
623 if (skipfs(fp) != '\n')
624 return gripe("Junk after table footer");
631 xundump(FILE *fp, char *file, int expected_table)
634 int type, nca, i, ch;
641 if ((type = xuheader(fp, expected_table)) < 0)
645 if (CANT_HAPPEN(!ca))
649 for (i = 0; ca[i].ca_name; i++) {
651 if (!(ca[i].ca_flags & NSC_EXTRA))
652 nflds += MAX(1, ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0);
654 fldca = calloc(nflds, sizeof(*fldca));
655 fldidx = calloc(nflds, sizeof(*fldidx));
656 caflds = calloc(nca, sizeof(*caflds));
658 if (xuheader1(fp, type, ca) < 0 || xundump1(fp, type, ca) < 0)
665 /* Skip empty lines so that callers can easily check for EOF */
666 while ((ch = skipfs(fp)) == '\n')
674 xundump1(FILE *fp, int type, struct castr *ca)
676 struct empfile *ep = &empfile[type];
677 int fixed_rows = has_const(ca);
678 int need_sentinel = !EF_IS_GAME_STATE(type);
683 for (row = 0;; ++row) {
684 while ((ch = skipfs(fp)) == '\n')
689 /* TODO ability to skip records */
691 if (row >= ep->csize - !!need_sentinel)
692 /* TODO grow cache unless EFF_STATIC */
693 return gripe("Too many rows for table %s", ef_nameof(type));
696 ep->cids = ep->fids = row + 1;
698 cur_obj = ef_ptr(type, row);
700 return gripe("Too many rows for table %s", ef_nameof(type));
701 if (xuflds(fp, xufld) < 0)
705 if (row != ep->fids) {
707 return gripe("Table %s requires %d rows, got %d",
708 ef_nameof(type), ep->fids, row);
710 ep->cids = ep->fids = row;
711 if (EF_IS_GAME_STATE(type) && row != ep->csize)
712 /* TODO truncate file */
713 gripe("Warning: should resize table %s from %d to %d, not implemented",
714 ef_nameof(type), ep->csize, row);
719 if (CANT_HAPPEN(row >= ep->csize))
720 return gripe("No space for sentinel");
721 memset(ep->cache + ep->size * row, 0, ep->size);
724 if (xutrailer(fp, type, row) < 0)