/*
* Empire - A multi-player, client/server Internet based war game.
- * Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ * Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* ---
*
* xundump.c: Load back xdump output
- *
+ *
* Known contributors to this file:
* Ron Koenderink, 2005
- * Markus Armbruster, 2005
+ * Markus Armbruster, 2005-2008
*/
-/* FIXME normalize terminology: table/rows/columns or file/records/fields */
+/*
+ * See doc/xdump! And keep it up-to-date.
+ *
+ * Parsing of machine-readable xdump is not precise: it recognizes
+ * comments, accepts whitespace in place of single space, and accepts
+ * the full human-readable field syntax instead of its machine-
+ * readable subset.
+ *
+ * FIXME:
+ * - Normalize terminology: table/rows/columns or file/records/fields
+ * - Loading tables with NSC_STRING elements more than once leaks memory
+ * TODO:
+ * - Check each partial table supplies the same rows
+ * - Check EFF_CFG tables are dense
+ * - Symbolic array indexes
+ * - Option to treat missing and unknown fields as warning, not error
+ * TODO, but hardly worth the effort:
+ * - Permit reordering of array elements
+ */
#include <config.h>
+#include <ctype.h>
+#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
-
-#include <ctype.h>
#include <string.h>
-#include <stdarg.h>
#include <time.h>
-
#include "file.h"
#include "match.h"
#include "nsc.h"
#include "optlist.h"
-#include "prototypes.h"
-
-static char *fname;
-static int lineno;
-static int human;
-static int cur_type;
-static void *cur_obj;
-static int nxt_sel, nxt_idx;
+#include "xdump.h"
+
+static char *fname; /* Name of file being read */
+static int lineno; /* Current line number */
+static int human; /* Reading human-readable syntax? */
+static int ellipsis; /* Header ended with ...? */
+static int is_partial; /* Is input split into parts? */
+static int cur_type; /* Current table's file type */
+static void *cur_obj; /* The object being read into */
+static int cur_id; /* and its index in the table */
+static int cur_obj_is_blank;
+static int nflds; /* #fields in input records */
+static struct castr **fldca; /* Map field number to selector */
+static int *fldidx; /* Map field number to index */
+static int *caflds; /* Map selector number to #fields seen */
+static int *cafldspp; /* ditto, in previous parts */
static int gripe(char *, ...) ATTRIBUTE((format (printf, 1, 2)));
static int deffld(int, char *, int);
-static void nxtfld(void);
+static int defellipsis(void);
+static int chkflds(void);
static int setnum(int, double);
static int setstr(int, char *);
-static int xunsymbol1(char *, struct symbol *, struct castr *, int);
+static int xunsymbol(char *, struct castr *, int);
static int setsym(int, char *);
static int mtsymset(int, long *);
static int add2symset(int, long *, char *);
-static struct symbol *get_symtab(struct castr *);
+static int xubody(FILE *);
+static int xutail(FILE *, struct castr *);
+/*
+ * Gripe about the current line to stderr, return -1.
+ */
static int
gripe(char *fmt, ...)
{
return -1;
}
+/*
+ * Read and ignore field separators from FP.
+ * Return first character that is not a field separator.
+ */
static int
skipfs(FILE *fp)
{
return ch;
}
-static int
-getid(FILE *fp, char *buf)
-{
- int n;
- if (fscanf(fp, "%1023[^#() \t\n]%n", buf, &n) != 1 || !isalpha(buf[0]))
- return -1;
- return n;
-}
-
+/*
+ * Decode escape sequences in BUF.
+ * Return BUF on success, null pointer on failure.
+ */
static char *
xuesc(char *buf)
{
return buf;
}
+/*
+ * Read an identifier from FP into BUF.
+ * BUF must have space for 1024 characters.
+ * Return number of characters read on success, -1 on failure.
+ */
+static int
+getid(FILE *fp, char *buf)
+{
+ int n;
+ if (fscanf(fp, "%1023[^\"#()<>= \t\n]%n", buf, &n) != 1
+ || !isalpha(buf[0]))
+ return -1;
+ xuesc(buf);
+ return n;
+}
+
+/*
+ * Try to read a field name from FP.
+ * I is the field number, counting from zero.
+ * If a name is read, set fldca[I] and fldidx[I] for it, and update
+ * caflds[].
+ * Return 1 if a name or ... was read, 0 on end of line, -1 on error.
+ */
static int
xufldname(FILE *fp, int i)
{
case EOF:
return gripe("Unexpected EOF");
case '\n':
- if (nxt_sel >= 0)
- return gripe("Fields missing");
+ if (chkflds() < 0)
+ return -1;
lineno++;
return 0;
+ case '.':
+ if (getc(fp) != '.' || getc(fp) != '.')
+ return gripe("Junk in header field %d", i + 1);
+ if (defellipsis() < 0)
+ return -1;
+ ch = skipfs(fp);
+ if (ch != EOF && ch != '\n')
+ return gripe("Junk after ...");
+ ungetc(ch, fp);
+ return 1;
default:
ungetc(ch, fp);
if (getid(fp, buf) < 0)
- return gripe("Junk in field %d", i + 1);
+ return gripe("Junk in header field %d", i + 1);
ch = getc(fp);
if (ch != '(') {
ungetc(ch, fp);
ch = getc(fp);
ungetc(ch, fp);
if (isdigit(ch) || ch == '-' || ch == '+') {
- if (fscanf(fp, "%d", &idx) != 1) {
- return gripe("Malformed number in index field %d", i + 1);
- }
+ if (fscanf(fp, "%d", &idx) != 1)
+ return gripe("Malformed number in index of header field %d",
+ i + 1);
+ if (idx < 0)
+ return gripe("Index must not be negative in header field %d",
+ i + 1);
} else {
if (getid(fp, buf) < 0)
- return gripe("Malformed string in index field %d", i + 1);
- return gripe("Symbolic index in field %d not yet implemented",
+ return gripe("Malformed index in header field %d", i + 1);
+ return gripe("Symbolic index in header field %d not yet implemented",
i + 1);
}
ch = getc(fp);
if (ch != ')')
- return gripe("Malformed index field %d", i + 1);
+ return gripe("Malformed index in header field %d", i + 1);
return deffld(i, buf, idx);
}
}
+/*
+ * Try to read a field value from FP.
+ * I is the field number, counting from zero.
+ * Return 1 if a value was read, 0 on end of line, -1 on error.
+ */
static int
xufld(FILE *fp, int i)
{
case EOF:
return gripe("Unexpected EOF");
case '\n':
- if (nxt_sel >= 0)
- return gripe("Fields missing");
+ CANT_HAPPEN(i > nflds);
+ if (i < nflds) {
+ if (fldca[i]->ca_type != NSC_STRINGY && fldca[i]->ca_len)
+ return gripe("Field %s(%d) missing",
+ fldca[i]->ca_name, fldidx[i]);
+ return gripe("Field %s missing", fldca[i]->ca_name);
+ }
lineno++;
return 0;
case '+': case '-': case '.':
}
}
+/*
+ * Read fields from FP.
+ * Use PARSE() to read each field.
+ * Return number of fields read on success, -1 on error.
+ */
static int
xuflds(FILE *fp, int (*parse)(FILE *, int))
{
}
}
+/*
+ * Define the FLDNO-th field.
+ * If IDX is negative, define as selector NAME, else as NAME(IDX).
+ * Set fldca[FLDNO] and fldidx[FLDNO] accordingly.
+ * Update caflds[].
+ * Return 1 on success, -1 on error.
+ */
static int
-deffld(int fldidx, char *name, int idx)
+deffld(int fldno, char *name, int idx)
{
struct castr *ca = ef_cadef(cur_type);
int res;
- if (nxt_sel < 0)
- return gripe("Too many fields, expected only %d", fldidx);
-
res = stmtch(name, ca, offsetof(struct castr, ca_name),
sizeof(struct castr));
- if (ca[nxt_sel].ca_type != NSC_STRINGY && ca[nxt_sel].ca_len != 0) {
- if (res != nxt_sel || idx != nxt_idx)
- return gripe("Expected %s(%d) in field %d",
- ca[nxt_sel].ca_name, nxt_idx, fldidx + 1);
+ if (res < 0)
+ return gripe("Header %s of field %d is %s", name, fldno + 1,
+ res == M_NOTUNIQUE ? "ambiguous" : "unknown");
+ if (ca[res].ca_flags == NSC_EXTRA || CANT_HAPPEN(ca[res].ca_get))
+ return gripe("Extraneous header %s in field %d", name, fldno + 1);
+ if (ca[res].ca_type != NSC_STRINGY && ca[res].ca_len != 0) {
+ if (idx < 0)
+ return gripe("Header %s requires an index in field %d",
+ ca[res].ca_name, fldno + 1);
+ if (idx >= ca[res].ca_len)
+ return gripe("Header %s(%d) index out of bounds in field %d",
+ ca[res].ca_name, idx, fldno + 1);
+ if (idx < caflds[res])
+ return gripe("Duplicate header %s(%d) in field %d",
+ ca[res].ca_name, idx, fldno + 1);
+ if (idx > caflds[res])
+ return gripe("Expected header %s(%d) in field %d",
+ ca[res].ca_name, caflds[res], fldno + 1);
} else {
- if (res != nxt_sel || idx >= 0)
- return gripe("Expected %s in field %d",
- ca[nxt_sel].ca_name, fldidx + 1);
+ if (idx >= 0)
+ return gripe("Header %s doesn't take an index in field %d",
+ ca[res].ca_name, fldno + 1);
+ idx = 0;
+ if (caflds[res])
+ return gripe("Duplicate header %s in field %d",
+ ca[res].ca_name, fldno + 1);
}
-
- nxtfld();
+ fldca[fldno] = &ca[res];
+ fldidx[fldno] = idx;
+ caflds[res]++;
return 1;
}
-static void
-nxtfld(void)
+/*
+ * Record that header ends with ...
+ * Set ellipsis and is_partial.
+ * Return 0 on success, -1 on error.
+ */
+static int
+defellipsis(void)
{
struct castr *ca = ef_cadef(cur_type);
- unsigned len = ca[nxt_sel].ca_type == NSC_STRINGY ? 0 : ca[nxt_sel].ca_len; /* FIXME ugly */
- nxt_idx++;
- if ((unsigned)nxt_idx >= len) {
- nxt_idx = 0;
- for (;;) {
- nxt_sel++;
- if (!ca[nxt_sel].ca_name) {
- nxt_sel = -1;
- break;
- }
- if (!(ca[nxt_sel].ca_flags & NSC_EXTRA))
- break;
- }
+ if (ca[0].ca_table != cur_type)
+ return gripe("Table %s doesn't support ...", ef_nameof(cur_type));
+ ellipsis = is_partial = 1;
+ return 0;
+}
+
+/*
+ * Check fields in xdump are sane.
+ * Return 0 on success, -1 on error.
+ */
+static int
+chkflds(void)
+{
+ struct castr *ca = ef_cadef(cur_type);
+ int i, len, cafldsmax, res = 0;
+
+ if (is_partial) {
+ /* Need a join field, use 0-th selector */
+ if (!caflds[0])
+ return gripe("Header field %s required with ...", ca[0].ca_name);
}
+
+ if (ellipsis)
+ return 0;
+
+ for (i = 0; ca[i].ca_name; i++) {
+ cafldsmax = MAX(caflds[i], cafldspp[i]);
+ if (ca[i].ca_flags & NSC_EXTRA)
+ continue;
+ len = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
+ if (!len && !cafldsmax)
+ res = gripe("Header field %s missing", ca[i].ca_name);
+ else if (len && cafldsmax == len - 1)
+ res = gripe("Header field %s(%d) missing",
+ ca[i].ca_name, len - 1);
+ else if (len && cafldsmax < len - 1)
+ res = gripe("Header fields %s(%d) ... %s(%d) missing",
+ ca[i].ca_name, cafldsmax, ca[i].ca_name, len - 1);
+ }
+
+ return res;
}
+/*
+ * Get selector for field FLDNO.
+ * Assign the field's selector index to *IDX, unless it is null.
+ * Return the selector on success, null pointer on error.
+ */
static struct castr *
-ca4fld(int fldidx)
+getfld(int fldno, int *idx)
+{
+ if (fldno >= nflds) {
+ gripe("Too many fields, expected only %d", nflds);
+ return NULL;
+ }
+ if (CANT_HAPPEN(fldno < 0))
+ return NULL;
+ if (idx)
+ *idx = fldidx[fldno];
+ return fldca[fldno];
+}
+
+/*
+ * Is a new value for field FLDNO required to match the old one?
+ */
+static int
+fldval_must_match(int fldno)
{
struct castr *ca = ef_cadef(cur_type);
- unsigned len = ca[nxt_sel].ca_type == NSC_STRINGY ? 0 : ca[nxt_sel].ca_len; /* FIXME ugly */
+ int i = fldca[fldno] - ca;
+
+ /*
+ * Value must match if:
+ * it's for a const selector, unless the object is still blank, or
+ * it was already given in a previous part of a split table.
+ */
+ return (!cur_obj_is_blank && (fldca[fldno]->ca_flags & NSC_CONST))
+ || fldidx[fldno] < cafldspp[i];
+}
- if (nxt_sel < 0 || CANT_HAPPEN(nxt_idx && (unsigned)nxt_idx >= len)) {
- gripe("Too many fields, expected only %d", fldidx);
- return NULL;
+/*
+ * Get the current object.
+ * Store it in cur_obj, and set cur_obj_is_blank accordingly.
+ * Return cur_obj, which is null on error.
+ */
+static void *
+getobj(void)
+{
+ struct empfile *ep = &empfile[cur_type];
+
+ if (!cur_obj) {
+ cur_obj_is_blank = cur_id >= ep->fids;
+ if (cur_obj_is_blank) {
+ if (ef_ensure_space(cur_type, cur_id, 1))
+ cur_obj = ef_ptr(cur_type, cur_id);
+ /* FIXME diagnose out of dynamic memory vs. static table full */
+ if (!cur_obj)
+ gripe("Can't put ID %d into table %s, it holds only 0..%d.",
+ cur_id, ep->name, ep->fids - 1);
+ } else
+ cur_obj = ef_ptr(cur_type, cur_id);
}
- return &ca[nxt_sel];
+
+ return cur_obj;
}
+/*
+ * Set value of field FLDNO in current object to DBL.
+ * Return 1 on success, -1 on error.
+ */
static int
-setnum(int fldidx, double dbl)
+setnum(int fldno, double dbl)
{
- struct castr *ca = ca4fld(fldidx);
+ struct castr *ca;
+ int idx;
char *memb_ptr;
double old;
+ ca = getfld(fldno, &idx);
if (!ca)
return -1;
- memb_ptr = cur_obj;
+ /*
+ * If this is the record index, put it into cur_id.
+ */
+ if (fldno == 0 && ca->ca_table == cur_type)
+ cur_id = (int)dbl;
+
+ memb_ptr = getobj();
+ if (!memb_ptr)
+ return -1;
memb_ptr += ca->ca_off;
+
+ /* FIXME check assignment preserves value */
switch (ca->ca_type) {
case NSC_CHAR:
- case NSC_TYPEID:
- old = ((signed char *)memb_ptr)[nxt_idx];
- ((signed char *)memb_ptr)[nxt_idx] = (signed char)dbl;
+ old = ((signed char *)memb_ptr)[idx];
+ ((signed char *)memb_ptr)[idx] = (signed char)dbl;
break;
case NSC_UCHAR:
- old = ((unsigned char *)memb_ptr)[nxt_idx];
- ((unsigned char *)memb_ptr)[nxt_idx] = (unsigned char)dbl;
+ case NSC_HIDDEN:
+ old = ((unsigned char *)memb_ptr)[idx];
+ ((unsigned char *)memb_ptr)[idx] = (unsigned char)dbl;
break;
case NSC_SHORT:
- old = ((short *)memb_ptr)[nxt_idx];
- ((short *)memb_ptr)[nxt_idx] = (short)dbl;
+ old = ((short *)memb_ptr)[idx];
+ ((short *)memb_ptr)[idx] = (short)dbl;
break;
case NSC_USHORT:
- old = ((unsigned short *)memb_ptr)[nxt_idx];
- ((unsigned short *)memb_ptr)[nxt_idx] = (unsigned short)dbl;
+ old = ((unsigned short *)memb_ptr)[idx];
+ ((unsigned short *)memb_ptr)[idx] = (unsigned short)dbl;
break;
case NSC_INT:
- old = ((int *)memb_ptr)[nxt_idx];
- ((int *)memb_ptr)[nxt_idx] = (int)dbl;
+ old = ((int *)memb_ptr)[idx];
+ ((int *)memb_ptr)[idx] = (int)dbl;
break;
case NSC_LONG:
- old = ((long *)memb_ptr)[nxt_idx];
- ((long *)memb_ptr)[nxt_idx] = (long)dbl;
+ old = ((long *)memb_ptr)[idx];
+ ((long *)memb_ptr)[idx] = (long)dbl;
break;
case NSC_XCOORD:
- old = ((coord *)memb_ptr)[nxt_idx];
- ((coord *)memb_ptr)[nxt_idx] = XNORM((coord)dbl);
+ old = ((coord *)memb_ptr)[idx];
+ /* FIXME use variant of xrel() that takes orig instead of nation */
+ if (old >= WORLD_X / 2)
+ old -= WORLD_X;
+ ((coord *)memb_ptr)[idx] = XNORM((coord)dbl);
break;
case NSC_YCOORD:
- old = ((coord *)memb_ptr)[nxt_idx];
- ((coord *)memb_ptr)[nxt_idx] = YNORM((coord)dbl);
+ old = ((coord *)memb_ptr)[idx];
+ /* FIXME use variant of yrel() that takes orig instead of nation */
+ if (old >= WORLD_Y / 2)
+ old -= WORLD_Y;
+ ((coord *)memb_ptr)[idx] = YNORM((coord)dbl);
break;
case NSC_FLOAT:
- old = ((float *)memb_ptr)[nxt_idx];
- ((float *)memb_ptr)[nxt_idx] = (float)dbl;
+ old = ((float *)memb_ptr)[idx];
+ ((float *)memb_ptr)[idx] = (float)dbl;
break;
case NSC_DOUBLE:
- old = ((double *)memb_ptr)[nxt_idx];
- ((double *)memb_ptr)[nxt_idx] = dbl;
+ old = ((double *)memb_ptr)[idx];
+ ((double *)memb_ptr)[idx] = dbl;
break;
case NSC_TIME:
- old = ((time_t *)memb_ptr)[nxt_idx];
- ((time_t *)memb_ptr)[nxt_idx] = (time_t)dbl;
+ old = ((time_t *)memb_ptr)[idx];
+ ((time_t *)memb_ptr)[idx] = (time_t)dbl;
break;
default:
- return gripe("Field %d doesn't take numbers", fldidx + 1);
+ return gripe("Field %d doesn't take numbers", fldno + 1);
}
- if ((ca->ca_flags & NSC_CONST) && old != dbl)
- return gripe("Value for field %d must be %g", fldidx + 1, old);
+ if (fldval_must_match(fldno) && old != dbl)
+ return gripe("Value for field %d must be %g", fldno + 1, old);
- nxtfld();
return 1;
}
+/*
+ * Set value of field FLDNO in current object to STR.
+ * Return 1 on success, -1 on error.
+ */
static int
-setstr(int fldidx, char *str)
+setstr(int fldno, char *str)
{
- struct castr *ca = ca4fld(fldidx);
+ struct castr *ca;
+ int must_match, idx;
+ size_t len;
char *memb_ptr, *old;
+ ca = getfld(fldno, &idx);
if (!ca)
return -1;
- memb_ptr = cur_obj;
+ memb_ptr = getobj();
+ if (!memb_ptr)
+ return -1;
memb_ptr += ca->ca_off;
+ must_match = fldval_must_match(fldno);
+
switch (ca->ca_type) {
case NSC_STRING:
- old = ((char **)memb_ptr)[nxt_idx];
- if (!(ca->ca_flags & NSC_CONST))
- ((char **)memb_ptr)[nxt_idx] = str ? strdup(str) : NULL;
+ old = ((char **)memb_ptr)[idx];
+ if (!must_match)
+ ((char **)memb_ptr)[idx] = str ? strdup(str) : NULL;
+ len = 65535; /* really SIZE_MAX, but that's C99 */
break;
case NSC_STRINGY:
- CANT_HAPPEN(nxt_idx);
+ if (CANT_HAPPEN(idx))
+ return -1;
if (!str)
- return gripe("Field doesn't take nil");
- if (strlen(str) > ca->ca_len)
+ return gripe("Field %d doesn't take nil", fldno + 1);
+ len = ca->ca_len;
+ if (strlen(str) > len)
return gripe("Field %d takes at most %d characters",
- fldidx + 1, ca->ca_len);
+ fldno + 1, (int)len);
old = memb_ptr;
- if (!(ca->ca_flags & NSC_CONST))
- strncpy(memb_ptr, str, ca->ca_len);
+ if (!must_match)
+ strncpy(memb_ptr, str, len);
break;
default:
- return gripe("Field %d doesn't take strings", fldidx + 1);
+ return gripe("Field %d doesn't take strings", fldno + 1);
}
- if ((ca->ca_flags & NSC_CONST) && strcmp(old, str))
- return gripe("Value for field %d must be %s", fldidx + 1, old);
+ if (must_match) {
+ if (old && (!str || strncmp(old, str, len)))
+ return gripe("Value for field %d must be \"%.*s\"",
+ fldno + 1, (int)len, old);
+ if (!old && str)
+ return gripe("Value for field %d must be nil", fldno + 1);
+ }
- nxtfld();
return 1;
}
+/*
+ * Resolve symbol name ID in table referred to by CA.
+ * Use field number N for error messages.
+ * Return index in referred table on success, -1 on failure.
+ */
static int
-xunsymbol1(char *id, struct symbol *symtab, struct castr *ca, int n)
+xunsymbol(char *id, struct castr *ca, int n)
{
- int i = stmtch(id, symtab, offsetof(struct symbol, name),
- sizeof(struct symbol));
+ int i = ef_elt_byname(ca->ca_table, id);
if (i < 0)
return gripe("%s %s symbol `%s' in field %d",
i == M_NOTUNIQUE ? "Ambiguous" : "Unknown",
- ca->ca_name, id, n);
+ ca->ca_name, id, n + 1);
return i;
}
+/*
+ * Map symbol index to symbol value.
+ * CA is the table, and I is the index in it.
+ */
static int
-setsym(int fldidx, char *sym)
+symval(struct castr *ca, int i)
{
- struct castr *ca = ca4fld(fldidx);
- struct symbol *symtab;
+ int type = ca->ca_table;
+
+ if (type != EF_BAD && ef_cadef(type) == symbol_ca)
+ /* symbol table, value is in the table */
+ return ((struct symbol *)ef_ptr(type, i))->value;
+ /* value is the table index */
+ return i;
+}
+
+/*
+ * Set value of field FLDNO in current object to value of symbol SYM.
+ * Return 1 on success, -1 on error.
+ */
+static int
+setsym(int fldno, char *sym)
+{
+ struct castr *ca;
int i;
+ ca = getfld(fldno, NULL);
if (!ca)
return -1;
- symtab = get_symtab(ca);
- if (!symtab || (ca->ca_flags & NSC_BITS))
- return gripe("Field %d doesn't take symbols", fldidx + 1);
+ if (ca->ca_table == EF_BAD || (ca->ca_flags & NSC_BITS))
+ return gripe("Field %d doesn't take symbols", fldno + 1);
- i = xunsymbol1(sym, symtab, ca, fldidx);
+ i = xunsymbol(sym, ca, fldno);
if (i < 0)
return -1;
- return setnum(fldidx, symtab[i].value);
-}
-
-static int
-has_const(struct castr ca[])
-{
- int i;
-
- for (i = 0; ca[i].ca_name; i++) {
- if (ca[i].ca_flags & NSC_CONST)
- return 1;
- }
- return 0;
+ return setnum(fldno, symval(ca, i));
}
+/*
+ * Create an empty symbol set for field FLDNO in *SET.
+ * Return 1 on success, -1 on error.
+ */
static int
-mtsymset(int fldidx, long *set)
+mtsymset(int fldno, long *set)
{
- struct castr *ca = ca4fld(fldidx);
- struct symbol *symtab;
- int i;
+ struct castr *ca;
+ ca = getfld(fldno, NULL);
if (!ca)
return -1;
- symtab = get_symtab(ca);
- if (!symtab || !(ca->ca_flags & NSC_BITS)) {
- return gripe("Field %d doesn't take symbol sets", fldidx + 1);
- }
+ if (ca->ca_table == EF_BAD || ef_cadef(ca->ca_table) != symbol_ca
+ || !(ca->ca_flags & NSC_BITS))
+ return gripe("Field %d doesn't take symbol sets", fldno + 1);
*set = 0;
return 0;
}
+/*
+ * Add a symbol to a symbol set for field FLDNO in *SET.
+ * SYM is the name of the symbol to add.
+ * Return 1 on success, -1 on error.
+ */
static int
-add2symset(int fldidx, long *set, char *sym)
+add2symset(int fldno, long *set, char *sym)
{
- struct castr *ca = ca4fld(fldidx);
- struct symbol *symtab;
+ struct castr *ca;
int i;
+ ca = getfld(fldno, NULL);
if (!ca)
return -1;
- symtab = get_symtab(ca);
- i = xunsymbol1(sym, symtab, ca, fldidx);
+ i = xunsymbol(sym, ca, fldno);
if (i < 0)
return -1;
- *set |= symtab[i].value;
+ *set |= symval(ca, i);
return 0;
}
-static struct symbol *
-get_symtab(struct castr *ca)
-{
- int symtype = ca->ca_table;
- struct symbol *symtab;
-
- if (symtype == EF_BAD || ef_cadef(symtype) != symbol_ca)
- return NULL;
-
- symtab = ef_ptr(symtype, 0);
- CANT_HAPPEN(!symtab);
- return symtab;
-}
-
+/*
+ * Read an xdump table header line from FP.
+ * Expect header for EXPECTED_TABLE, unless it is EF_BAD.
+ * Recognize header for machine- and human-readable syntax, and set
+ * human accordingly.
+ * Return table type on success, -2 on EOF before header, -1 on failure.
+ */
static int
xuheader(FILE *fp, int expected_table)
{
while ((ch = skipfs(fp)) == '\n')
lineno++;
if (ch == EOF && expected_table == EF_BAD)
- return -1;
+ return -2;
ungetc(ch, fp);
human = ch == 'c';
return gripe("Expected table `%s', not `%s'",
ef_nameof(expected_table), name);
- if (CANT_HAPPEN(!(ef_flags(type) & EFF_MEM)))
- return -1;
+ if (!ef_cadef(type) || !(ef_flags(type) & EFF_MEM)) {
+ CANT_HAPPEN(expected_table != EF_BAD);
+ return gripe("Table `%s' is not permitted here", name);
+ }
if (skipfs(fp) != '\n')
return gripe("Junk after xdump header");
return type;
}
+/*
+ * Find fields in this xdump.
+ * If reading human-readable syntax, read a field header line from FP.
+ * Else take fields from the table's selectors in CA[].
+ * Set ellipsis, nflds, fldca[], fldidx[] and caflds[] accordingly.
+ * Return 0 on success, -1 on failure.
+ */
+static int
+xufldhdr(FILE *fp, struct castr ca[])
+{
+ struct castr **fca;
+ int *fidx;
+ int ch, i, j, n;
+
+ for (i = 0; ca[i].ca_name; i++)
+ caflds[i] = 0;
+ ellipsis = 0;
+
+ if (human) {
+ while ((ch = skipfs(fp)) == '\n')
+ lineno++;
+ ungetc(ch, fp);
+ nflds = xuflds(fp, xufldname);
+ if (nflds < 0)
+ return -1;
+ nflds -= ellipsis != 0;
+ } else {
+ fca = fldca;
+ fidx = fldidx;
+
+ for (i = 0; ca[i].ca_name; i++) {
+ if ((ca[i].ca_flags & NSC_EXTRA))
+ continue;
+ n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
+ j = 0;
+ do {
+ *fca++ = &ca[i];
+ *fidx++ = j;
+ } while (++j < n);
+ }
+
+ nflds = fidx - fldidx;
+ }
+
+ return 0;
+}
+
+/*
+ * Read xdump footer from FP.
+ * CA[] contains the table's selectors.
+ * The body had RECS records.
+ * Update cafldspp[] from caflds[].
+ * Return 0 on success, -1 on failure.
+ */
static int
-xutrailer(FILE *fp, int type, int row)
+xufooter(FILE *fp, struct castr ca[], int recs)
{
- int rows, ch, res;
+ int res, n, i;
res = -1;
if (human) {
if (fscanf(fp, "config%n", &res) != 0 || res < 0)
return gripe("Malformed table footer");
} else {
- if (fscanf(fp, "%d", &rows) != 1)
+ if (fscanf(fp, "%d", &n) != 1)
return gripe("Malformed table footer");
- if (row != rows)
+ if (recs != n)
return gripe("Read %d rows, which doesn't match footer "
- "%d rows", row, rows);
+ "%d rows", recs, n);
}
if (skipfs(fp) != '\n')
return gripe("Junk after table footer");
lineno++;
+ for (i = 0; ca[i].ca_name; i++) {
+ if (cafldspp[i] < caflds[i])
+ cafldspp[i] = caflds[i];
+ }
+
return 0;
}
+/*
+ * Read an xdump table from FP.
+ * Both machine- and human-readable xdump syntax are recognized.
+ * Expect table EXPECTED_TABLE, unless it is EF_BAD.
+ * Report errors to stderr.
+ * Messages assume FP starts in the file FILE at line *PLNO.
+ * Update *PLNO to reflect lines read from FP.
+ * Return table type on success, -2 on EOF before header, -1 on failure.
+ */
int
-xundump(FILE *fp, char *file, int expected_table)
+xundump(FILE *fp, char *file, int *plno, int expected_table)
{
- struct empfile *ep;
- int type, fixed_rows, need_sentinel;
- int row, res, ch;
+ struct castr *ca;
+ int type, nca, nf, i, ch;
- if (fname != file) {
- fname = file;
- lineno = 1;
- }
+ fname = file;
+ lineno = *plno;
if ((type = xuheader(fp, expected_table)) < 0)
- return -1;
+ return type;
- ep = &empfile[type];
- fixed_rows = has_const(ef_cadef(type));
- need_sentinel = !EF_IS_GAME_STATE(type);
+ ca = ef_cadef(type);
+ if (CANT_HAPPEN(!ca))
+ return -1;
+ nca = nf = 0;
+ for (i = 0; ca[i].ca_name; i++) {
+ nca++;
+ if (!(ca[i].ca_flags & NSC_EXTRA))
+ nf += MAX(1, ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0);
+ }
+ fldca = malloc(nf * sizeof(*fldca));
+ fldidx = malloc(nf * sizeof(*fldidx));
+ caflds = malloc(nca * sizeof(*caflds));
+ cafldspp = calloc(nca, sizeof(*cafldspp));
cur_type = type;
- if (human) {
- nxt_sel = nxt_idx = 0;
- if (xuflds(fp, xufldname) < 0)
+
+ if (xutail(fp, ca) < 0)
+ type = EF_BAD;
+
+ free(cafldspp);
+ free(caflds);
+ free(fldidx);
+ free(fldca);
+
+ /* Skip empty lines so that callers can easily check for EOF */
+ while ((ch = skipfs(fp)) == '\n')
+ lineno++;
+ ungetc(ch, fp);
+
+ *plno = lineno;
+ return type;
+}
+
+/*
+ * Read the remainder of an xdump after the table header line from FP.
+ * CA[] contains the table's selectors.
+ * Return 0 on success, -1 on failure.
+ */
+static int
+xutail(FILE *fp, struct castr *ca)
+{
+ int recs;
+
+ is_partial = 0;
+ for (;;) {
+ if (xufldhdr(fp, ca) < 0)
+ return -1;
+ if ((recs = xubody(fp)) < 0)
+ return -1;
+ if (xufooter(fp, ca, recs) < 0)
+ return -1;
+ if (!ellipsis)
+ return 0;
+ if (xuheader(fp, cur_type) < 0)
return -1;
}
+}
- for (row = 0;; ++row) {
- ch = skipfs(fp);
+/*
+ * Read the body of an xdump table from FP.
+ * Return number of rows read on success, -1 on failure.
+ */
+static int
+xubody(FILE *fp)
+{
+ struct empfile *ep = &empfile[cur_type];
+ int i, maxid, ch;
+
+ maxid = 0;
+ for (i = 0;; ++i) {
+ while ((ch = skipfs(fp)) == '\n')
+ lineno++;
if (ch == '/')
break;
ungetc(ch, fp);
- /* TODO ability to skip records */
- if (!fixed_rows) {
- if (row >= ep->csize - !!need_sentinel)
- /* TODO grow cache unless EFF_STATIC */
- return gripe("Too many rows for table %s", ef_nameof(type));
- if (row >= ep->cids)
- /* TODO grow file */
- ep->cids = ep->fids = row + 1;
- }
- cur_obj = ef_ptr(type, row);
- if (!cur_obj)
- return gripe("Too many rows for table %s", ef_nameof(type));
- nxt_sel = nxt_idx = 0;
- res = xuflds(fp, xufld);
- if (res < 0)
+ cur_obj = NULL;
+ cur_id = i;
+ if (xuflds(fp, xufld) < 0)
return -1;
- }
- if (row != ep->fids) {
- if (fixed_rows)
- return gripe("Table %s requires %d rows, got %d",
- ef_nameof(type), ep->fids, row);
- else {
- ep->cids = ep->fids = row;
- if (EF_IS_GAME_STATE(type) && row != ep->csize)
- /* TODO truncate file */
- gripe("Warning: should resize table %s from %d to %d, not implemented",
- ef_nameof(type), ep->csize, row);
- }
+ maxid = MAX(maxid, cur_id + 1);
}
- if (need_sentinel) {
- if (CANT_HAPPEN(row >= ep->csize))
- return gripe("No space for sentinel");
- memset(ep->cache + ep->size * row, 0, ep->size);
+ if (CANT_HAPPEN(maxid > ep->fids))
+ maxid = ep->fids;
+ if (maxid < ep->fids) {
+ if (EF_IS_GAME_STATE(cur_type)
+ || (cur_type >= EF_SHIP_CHR && cur_type <= EF_NUKE_CHR))
+ ef_truncate(cur_type, maxid);
+ else
+ return gripe("Table %s requires %d rows, got %d",
+ ef_nameof(cur_type), ep->fids, maxid);
}
- if (xutrailer(fp, type, row) < 0)
- return -1;
-
- return type;
+ return i;
}