/*
* Empire - A multi-player, client/server Internet based war game.
- * Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ * Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
*/
/*
+ * 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 references to non-symbol tables
* - Symbolic array indexes
* TODO, but hardly worth the effort:
* - Permit reordering of array elements
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
+#include <stdlib.h>
+#include <string.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 ellipsis, is_partial;
-static int cur_type, cur_id;
-static void *cur_obj;
+#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;
-static struct castr **fldca;
-static int *fldidx;
+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 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)
{
}
}
+/*
+ * 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)
{
}
}
+/*
+ * 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 fldno, char *name, int idx)
{
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)
+ 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",
return 1;
}
+/*
+ * Record that header ends with ...
+ * Set ellipsis and is_partial.
+ * Return 0 on success, -1 on error.
+ */
static int
defellipsis(void)
{
return 0;
}
+/*
+ * Check fields in xdump are sane.
+ * Return 0 on success, -1 on error.
+ */
static int
chkflds(void)
{
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 *
getfld(int fldno, int *idx)
{
return fldca[fldno];
}
+/*
+ * Is a new value for field FLDNO required to match the old one?
+ */
static int
fldval_must_match(int fldno)
{
|| fldidx[fldno] < cafldspp[i];
}
+/*
+ * 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)
{
int need_sentinel = !EF_IS_GAME_STATE(cur_type);
if (!cur_obj) {
- cur_obj_is_blank = cur_id >= ep->fids;
+ cur_obj_is_blank = cur_id >= ep->fids - !!need_sentinel;
if (cur_obj_is_blank) {
- /* TODO grow cache (and posssibly file) unless EFF_STATIC */
- if (cur_id < ep->csize - !!need_sentinel)
- ep->cids = ep->fids = cur_id + 1;
- /* else: ef_ptr() will fail */
- }
- cur_obj = ef_ptr(cur_type, cur_id);
- if (!cur_obj)
- gripe("Can't put ID %d into table %s, it holds only 0..%d.",
- cur_id, ep->name, ep->fids - 1);
+ if (ef_ensure_space(cur_type, cur_id + !!need_sentinel, 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 - !!need_sentinel - 1);
+ } else
+ cur_obj = ef_ptr(cur_type, cur_id);
}
return cur_obj;
}
+/*
+ * Set value of field FLDNO in current object to DBL.
+ * Return 1 on success, -1 on error.
+ */
static int
setnum(int fldno, double dbl)
{
return 1;
}
+/*
+ * Set value of field FLDNO in current object to STR.
+ * Return 1 on success, -1 on error.
+ */
static int
setstr(int fldno, char *str)
{
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
xunsymbol(char *id, struct castr *ca, int n)
{
return i;
}
+/*
+ * Map symbol index to symbol value.
+ * CA is the table, and I is the index in it.
+ */
static int
symval(struct castr *ca, int i)
{
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)
{
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 fldno, long *set)
{
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 fldno, long *set, char *sym)
{
return 0;
}
+/*
+ * 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)
{
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[])
{
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
-xufooter(FILE *fp, struct castr ca[], int row)
+xufooter(FILE *fp, struct castr ca[], int recs)
{
- int res, rows, i;
+ 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");
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 *plno, int expected_table)
{
if (!(ca[i].ca_flags & NSC_EXTRA))
nf += MAX(1, ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0);
}
- fldca = calloc(nf, sizeof(*fldca));
- fldidx = calloc(nf, sizeof(*fldidx));
+ fldca = malloc(nf * sizeof(*fldca));
+ fldidx = malloc(nf * sizeof(*fldidx));
caflds = malloc(nca * sizeof(*caflds));
cafldspp = calloc(nca, sizeof(*cafldspp));
cur_type = type;
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)
{
}
}
+/*
+ * 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 need_sentinel = !EF_IS_GAME_STATE(cur_type);
- int row, n, ch;
+ int old_maxid = ep->fids;
+ int i, maxid, ch;
+
+ if (old_maxid == 0 && need_sentinel)
+ ef_ensure_space(cur_type, 0, 1);
- n = 0;
- for (row = 0;; ++row) {
+ maxid = 0;
+ for (i = 0;; ++i) {
while ((ch = skipfs(fp)) == '\n')
lineno++;
if (ch == '/')
break;
ungetc(ch, fp);
cur_obj = NULL;
- cur_id = row;
+ cur_id = i;
if (xuflds(fp, xufld) < 0)
return -1;
- n = MAX(n, cur_id + 1);
+ maxid = MAX(maxid, cur_id + 1);
}
- if (CANT_HAPPEN(n > ep->fids))
- n = ep->fids;
- if (n < ep->fids) {
- if (EF_IS_GAME_STATE(cur_type) && n != ep->csize)
- /* TODO truncate file */
- gripe("Warning: should resize table %s from %d to %d, not implemented",
- ef_nameof(cur_type), ep->csize, n);
- else if (cur_type >= EF_SHIP_CHR && cur_type <= EF_NUKE_CHR)
- ep->cids = ep->fids = n;
- else
- return gripe("Table %s requires %d rows, got %d",
- ef_nameof(cur_type), ep->fids, n);
+ if (maxid >= old_maxid && need_sentinel) {
+ /* appended a sentinel, strip it off */
+ ep->fids--;
+ ep->cids--;
}
- if (need_sentinel) {
- if (CANT_HAPPEN(n >= ep->csize))
- return gripe("No space for sentinel");
- memset(ep->cache + ep->size * n, 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);
}
- return row;
+ return i;
}