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 /* FIXME use variant of xrel() that takes orig instead of nation */
361 if (old >= WORLD_X / 2)
363 ((coord *)memb_ptr)[idx] = XNORM((coord)dbl);
366 old = ((coord *)memb_ptr)[idx];
367 /* FIXME use variant of yrel() that takes orig instead of nation */
368 if (old >= WORLD_Y / 2)
370 ((coord *)memb_ptr)[idx] = YNORM((coord)dbl);
373 old = ((float *)memb_ptr)[idx];
374 ((float *)memb_ptr)[idx] = (float)dbl;
377 old = ((double *)memb_ptr)[idx];
378 ((double *)memb_ptr)[idx] = dbl;
381 old = ((time_t *)memb_ptr)[idx];
382 ((time_t *)memb_ptr)[idx] = (time_t)dbl;
385 return gripe("Field %d doesn't take numbers", fldno + 1);
388 if ((ca->ca_flags & NSC_CONST) && old != dbl)
389 return gripe("Value for field %d must be %g", fldno + 1, old);
395 setstr(int fldno, char *str)
400 char *memb_ptr, *old;
402 ca = getfld(fldno, &idx);
407 memb_ptr += ca->ca_off;
408 switch (ca->ca_type) {
410 old = ((char **)memb_ptr)[idx];
411 if (!(ca->ca_flags & NSC_CONST))
412 ((char **)memb_ptr)[idx] = str ? strdup(str) : NULL;
413 len = 65535; /* really SIZE_MAX, but it's C99 */
416 if (CANT_HAPPEN(idx))
419 return gripe("Field doesn't take nil");
421 if (strlen(str) > len)
422 return gripe("Field %d takes at most %d characters",
425 if (!(ca->ca_flags & NSC_CONST))
426 strncpy(memb_ptr, str, len);
429 return gripe("Field %d doesn't take strings", fldno + 1);
432 if (ca->ca_flags & NSC_CONST) {
433 if (old && (!str || strncmp(old, str, len)))
434 return gripe("Value for field %d must be \"%.*s\"",
435 fldno + 1, len, old);
437 return gripe("Value for field %d must be nil", fldno + 1);
444 xunsymbol1(char *id, struct symbol *symtab, struct castr *ca, int n)
446 int i = stmtch(id, symtab, offsetof(struct symbol, name),
447 sizeof(struct symbol));
449 return gripe("%s %s symbol `%s' in field %d",
450 i == M_NOTUNIQUE ? "Ambiguous" : "Unknown",
456 setsym(int fldno, char *sym)
459 struct symbol *symtab;
462 ca = getfld(fldno, NULL);
466 symtab = get_symtab(ca);
467 if (!symtab || (ca->ca_flags & NSC_BITS))
468 return gripe("Field %d doesn't take symbols", fldno + 1);
470 i = xunsymbol1(sym, symtab, ca, fldno);
473 return setnum(fldno, symtab[i].value);
477 mtsymset(int fldno, long *set)
480 struct symbol *symtab;
482 ca = getfld(fldno, NULL);
486 symtab = get_symtab(ca);
487 if (!symtab || !(ca->ca_flags & NSC_BITS)) {
488 return gripe("Field %d doesn't take symbol sets", fldno + 1);
495 add2symset(int fldno, long *set, char *sym)
498 struct symbol *symtab;
501 ca = getfld(fldno, NULL);
505 symtab = get_symtab(ca);
506 i = xunsymbol1(sym, symtab, ca, fldno);
509 *set |= symtab[i].value;
513 static struct symbol *
514 get_symtab(struct castr *ca)
516 int symtype = ca->ca_table;
517 struct symbol *symtab;
519 if (symtype == EF_BAD || ef_cadef(symtype) != symbol_ca)
522 symtab = ef_ptr(symtype, 0);
523 CANT_HAPPEN(!symtab);
528 xuheader(FILE *fp, int expected_table)
534 while ((ch = skipfs(fp)) == '\n')
536 if (ch == EOF && expected_table == EF_BAD)
543 ? fscanf(fp, "config%*[ \t]%63[^ \t#\n]%n", name, &res) != 1
544 : fscanf(fp, "XDUMP%*[ \t]%63[^ \t#\n]%*[ \t]%*[^ \t#\n]%n",
545 name, &res) != 1) || res < 0)
546 return gripe("Expected xdump header");
548 type = ef_byname(name);
550 return gripe("Unknown table `%s'", name);
551 if (expected_table != EF_BAD && expected_table != type)
552 return gripe("Expected table `%s', not `%s'",
553 ef_nameof(expected_table), name);
555 if (!ef_cadef(type) || !(ef_flags(type) & EFF_MEM)) {
556 CANT_HAPPEN(expected_table != EF_BAD);
557 return gripe("Undumping of table `%s' not implemented", name);
560 if (skipfs(fp) != '\n')
561 return gripe("Junk after xdump header");
568 xuheader1(FILE *fp, int type, struct castr ca[])
577 while ((ch = skipfs(fp)) == '\n')
580 if (xuflds(fp, xufldname) < 0)
586 for (i = 0; ca[i].ca_name; i++) {
587 if ((ca[i].ca_flags & NSC_EXTRA))
589 n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
602 xutrailer(FILE *fp, int type, int row)
608 if (fscanf(fp, "config%n", &res) != 0 || res < 0)
609 return gripe("Malformed table footer");
611 if (fscanf(fp, "%d", &rows) != 1)
612 return gripe("Malformed table footer");
614 return gripe("Read %d rows, which doesn't match footer "
615 "%d rows", row, rows);
617 if (skipfs(fp) != '\n')
618 return gripe("Junk after table footer");
625 xundump(FILE *fp, char *file, int expected_table)
628 int type, nca, i, ch;
635 if ((type = xuheader(fp, expected_table)) < 0)
639 if (CANT_HAPPEN(!ca))
643 for (i = 0; ca[i].ca_name; i++) {
645 if (!(ca[i].ca_flags & NSC_EXTRA))
646 nflds += MAX(1, ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0);
648 fldca = calloc(nflds, sizeof(*fldca));
649 fldidx = calloc(nflds, sizeof(*fldidx));
650 caflds = calloc(nca, sizeof(*caflds));
652 if (xuheader1(fp, type, ca) < 0 || xundump1(fp, type, ca) < 0)
659 /* Skip empty lines so that callers can easily check for EOF */
660 while ((ch = skipfs(fp)) == '\n')
668 xundump1(FILE *fp, int type, struct castr *ca)
670 struct empfile *ep = &empfile[type];
671 int need_sentinel = !EF_IS_GAME_STATE(type);
676 for (row = 0;; ++row) {
677 while ((ch = skipfs(fp)) == '\n')
682 /* TODO ability to skip records */
683 if (row >= ep->fids) {
684 /* TODO grow cache (and posssibly file) unless EFF_STATIC */
685 if (row < ep->csize - !!need_sentinel)
686 ep->cids = ep->fids = row + 1;
687 /* else: ef_ptr() will fail */
689 cur_obj = ef_ptr(type, row);
691 return gripe("Too many rows for table %s", ef_nameof(type));
692 if (xuflds(fp, xufld) < 0)
696 if (CANT_HAPPEN(row > ep->fids))
698 if (row < ep->fids) {
699 if (EF_IS_GAME_STATE(type) && row != ep->csize)
700 /* TODO truncate file */
701 gripe("Warning: should resize table %s from %d to %d, not implemented",
702 ef_nameof(type), ep->csize, row);
703 else if (type >= EF_SHIP_CHR && type <= EF_NUKE_CHR)
704 ; /* shrinking these is okay */
706 return gripe("Table %s requires %d rows, got %d",
707 ef_nameof(type), ep->fids, row);
711 if (CANT_HAPPEN(row >= ep->csize))
712 return gripe("No space for sentinel");
713 memset(ep->cache + ep->size * row, 0, ep->size);
716 if (xutrailer(fp, type, row) < 0)