]> git.pond.sub.org Git - empserver/blobdiff - src/lib/common/xundump.c
Make xundump catch extraneous fields
[empserver] / src / lib / common / xundump.c
index 739bddbc227dce011f06d2bbfa5ff932feafbbd3..d1d6f7e8178fd756a3afe39fa64acbfceb26dfb3 100644 (file)
 #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"
+#include "xdump.h"
 
 static char *fname;            /* Name of file being read */
 static int lineno;             /* Current line number */
@@ -130,21 +132,6 @@ skipfs(FILE *fp)
     return ch;
 }
 
-/*
- * 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;
-    return n;
-}
-
 /*
  * Decode escape sequences in BUF.
  * Return BUF on success, null pointer on failure.
@@ -170,6 +157,22 @@ 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.
@@ -349,6 +352,8 @@ 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",
@@ -479,17 +484,16 @@ 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 possibly 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;
@@ -958,8 +962,12 @@ xubody(FILE *fp)
 {
     struct empfile *ep = &empfile[cur_type];
     int need_sentinel = !EF_IS_GAME_STATE(cur_type);
+    int old_maxid = ep->fids;
     int i, maxid, ch;
 
+    if (old_maxid == 0 && need_sentinel)
+       ef_ensure_space(cur_type, 0, 1);
+
     maxid = 0;
     for (i = 0;; ++i) {
        while ((ch = skipfs(fp)) == '\n')
@@ -974,25 +982,22 @@ xubody(FILE *fp)
        maxid = MAX(maxid, cur_id + 1);
     }
 
+    if (maxid >= old_maxid && need_sentinel) {
+       /* appended a sentinel, strip it off */
+       ep->fids--;
+       ep->cids--;
+    }
+
     if (CANT_HAPPEN(maxid > ep->fids))
        maxid = ep->fids;
     if (maxid < ep->fids) {
-       if (EF_IS_GAME_STATE(cur_type) && maxid != ep->csize)
-           /* TODO truncate file */
-           gripe("Warning: should resize table %s from %d to %d, not implemented",
-                 ef_nameof(cur_type), ep->csize, maxid);
-       else if (cur_type >= EF_SHIP_CHR && cur_type <= EF_NUKE_CHR)
-           ep->cids = ep->fids = maxid;
+       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 (need_sentinel) {
-       if (CANT_HAPPEN(maxid >= ep->csize))
-           return gripe("No space for sentinel");
-       memset(ep->cache + ep->size * maxid, 0, ep->size);
-    }
-
     return i;
 }