]> git.pond.sub.org Git - empserver/blobdiff - src/lib/common/xundump.c
Clean up omit_ids() not to set an extra element in idgap[]
[empserver] / src / lib / common / xundump.c
index fbf1b9c2e2406c1c35c431ef676a433500ebd5d5..0059ee2a9b7373832f2d9444924e13fbaec3b8b3 100644 (file)
@@ -43,8 +43,6 @@
  * - 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:
 
 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 int partno;             /* Counts from 0..#parts-1 */
 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 old_nelem;
+static unsigned char *idgap;   /* idgap && idgap[ID] iff part#0 lacks ID */
+static int idgap_len;          /* #elements in idgap[] */
+
+static int human;              /* Reading human-readable syntax? */
+static int ellipsis;           /* Header ended with ...? */
 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 may_omit_id;                /* Okay to omit IDs? */
+static int may_trunc;          /* Okay to truncate? */
 
 static int gripe(char *, ...) ATTRIBUTE((format (printf, 1, 2)));
 static int deffld(int, char *, int);
@@ -110,6 +114,182 @@ gripe(char *fmt, ...)
     return -1;
 }
 
+/* Make TYPE the current table.  */
+static void
+tbl_start(int type)
+{
+    cur_type = type;
+    partno = 0;
+    cur_id = -1;
+    cur_obj = NULL;
+    old_nelem = type == EF_BAD ? 0 : ef_nelem(type);
+    idgap = NULL;
+    idgap_len = 0;
+}
+
+/* End the current table.  */
+static void
+tbl_end(void)
+{
+    free(idgap);
+    tbl_start(EF_BAD);
+}
+
+/*
+ * Seek to current table's ID-th record.
+ * ID must be acceptable.
+ * Store it in cur_obj, and set cur_id accordingly.
+ * Return 0 on success, -1 on failure.
+ */
+static int
+tbl_seek(int id)
+{
+    struct empfile *ep = &empfile[cur_type];
+
+    if (id >= ef_nelem(cur_type)) {
+       if (!ef_ensure_space(cur_type, id, 1))
+           return gripe("Can't put ID %d into table %s", id, ep->name);
+    }
+
+    cur_obj = ef_ptr(cur_type, id);
+    if (CANT_HAPPEN(!cur_obj))
+       return -1;
+    cur_id = id;
+    return 0;
+}
+
+/*
+ * Get the next object.
+ * Must not have a record index.
+ * Store it in cur_obj, and set cur_id accordingly.
+ * Return 0 on success, -1 on failure.
+ */
+static int
+tbl_next_obj(void)
+{
+    int max_id = ef_id_limit(cur_type);
+
+    if (cur_id >= max_id)
+       return gripe("Too many rows");
+    return tbl_seek(cur_id + 1);
+}
+
+/*
+ * Omit ID1..ID2-1.
+ * Reset the omitted objects to default state.
+ */
+static void
+omit_ids(int id1, int id2)
+{
+    int i;
+
+    if (id1 >= id2)
+       return;
+
+    idgap = realloc(idgap, id2 * sizeof(*idgap));
+    for (i = idgap_len; i < id1; i++)
+       idgap[i] = 0;
+    for (i = id1; i < id2; i++) {
+       ef_blank(cur_type, i, ef_ptr(cur_type, i));
+       idgap[i] = 1;
+    }
+    idgap_len = id2;
+}
+
+/*
+ * Return the smallest non-omitted ID in ID1..ID2-1 if any, else -1.
+ */
+static int
+expected_id(int id1, int id2)
+{
+    int i;
+
+    for (i = id1; i < id2; i++) {
+       if (i >= idgap_len || !idgap[i])
+           return i;
+    }
+    return -1;
+}
+
+/*
+ * Get the next object, it has record index ID.
+ * Store it in cur_obj, and set cur_id accordingly.
+ * Ensure we're omitting the same objects as the previous parts.
+ * Reset any omitted objects to default state.
+ * Return 0 on success, -1 on failure.
+ */
+static int
+tbl_skip_to_obj(int id)
+{
+    struct empfile *ep = &empfile[cur_type];
+    int prev_id = cur_id;
+    int max_id, exp_id;
+
+    if (partno == 0) {
+       if (!may_omit_id && id != cur_id + 1)
+           return gripe("Expected %d in field %d", cur_id + 1, 1);
+       if (id <= cur_id)
+           return gripe("Field %d must be > %d", 1, cur_id);
+       max_id = ef_id_limit(cur_type);
+       if (id > max_id)
+           return gripe("Field %d must be <= %d", 1, max_id);
+    } else {
+       exp_id = expected_id(cur_id + 1, ep->fids);
+       if (exp_id < 0)
+           return gripe("Table's first part doesn't have this row");
+       else if (id != exp_id)
+           return gripe("Expected %d in field %d,"
+                        " like in table's first part",
+                        exp_id, 1);
+    }
+
+    if (tbl_seek(id) < 0)
+       return -1;
+
+    if (partno == 0)
+       omit_ids(prev_id + 1, id);
+    return 0;
+}
+
+/*
+ * Finish table part.
+ * If the table has variable length, truncate it.
+ * Else ensure we're omitting the same objects as the previous parts.
+ * Reset any omitted objects to default state.
+ * Return 0 on success, -1 on failure.
+ */
+static int
+tbl_part_done(void)
+{
+    struct empfile *ep = &empfile[cur_type];
+    int exp_id;
+
+    if (cur_id + 1 < ep->fids) {
+       if (partno == 0) {
+           if (may_trunc) {
+               if (!ef_truncate(cur_type, cur_id + 1))
+                   return -1;
+           } else {
+               if (!may_omit_id)
+                   return gripe("Expected %d more rows",
+                                ep->fids - (cur_id + 1));
+               omit_ids(cur_id + 1, ep->fids);
+           }
+       } else {
+           exp_id = expected_id(cur_id + 1, ep->fids);
+           if (exp_id >= 0)
+               return gripe("Expected row with %d in field %d,"
+                            " like in table's first part",
+                            exp_id, 1);
+       }
+    }
+
+    partno++;
+    cur_id = -1;
+    cur_obj = NULL;
+    return 0;
+}
+
 /*
  * Read and ignore field separators from FP.
  * Return first character that is not a field separator.
@@ -352,7 +532,7 @@ 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 || CANT_HAPPEN(ca[res].ca_get))
+    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)
@@ -392,12 +572,19 @@ defellipsis(void)
 {
     struct castr *ca = ef_cadef(cur_type);
 
-    if (ca[0].ca_table != cur_type)
+    if (ca[0].ca_table != cur_type || (ca[0].ca_flags & NSC_EXTRA))
        return gripe("Table %s doesn't support ...", ef_nameof(cur_type));
-    ellipsis = is_partial = 1;
+    ellipsis = 1;
     return 0;
 }
 
+/* Is table split into parts? */
+static int
+is_partial(void)
+{
+    return ellipsis || partno;
+}
+
 /*
  * Check fields in xdump are sane.
  * Return 0 on success, -1 on error.
@@ -412,7 +599,7 @@ chkflds(void)
     if (ca[0].ca_table == cur_type && caflds[0] && fldca[0] != &ca[0])
        res = gripe("Header field %s must come first", ca[0].ca_name);
 
-    if (is_partial) {
+    if (is_partial()) {
        /* Need a join field, use 0-th selector */
        if (!caflds[0])
            res = gripe("Header field %s required in each table part",
@@ -474,36 +661,10 @@ fldval_must_match(int fldno)
      * 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))
+    return (cur_id < old_nelem && (fldca[fldno]->ca_flags & NSC_CONST))
        || 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)
-{
-    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 cur_obj;
-}
-
 /*
  * Set value of field FLDNO in current object to DBL.
  * Return 1 on success, -1 on error.
@@ -512,77 +673,87 @@ static int
 setnum(int fldno, double dbl)
 {
     struct castr *ca;
-    int idx;
+    int next_id, idx;
     char *memb_ptr;
-    double old;
+    double old, new;
 
     ca = getfld(fldno, &idx);
     if (!ca)
        return -1;
 
-    /*
-     * 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;
+    if (fldno == 0) {
+       if (ca->ca_table == cur_type) {
+           /* Got record index */
+           next_id = (int)dbl;
+           if (next_id != dbl)
+               return gripe("Field %d can't hold this value", fldno + 1);
+           if (tbl_skip_to_obj(next_id) < 0)
+               return -1;
+       } else {
+           if (tbl_next_obj() < 0)
+               return -1;
+       }
+    }
+    memb_ptr = cur_obj;
     memb_ptr += ca->ca_off;
 
-    /* FIXME check assignment preserves value */
     switch (ca->ca_type) {
     case NSC_CHAR:
        old = ((signed char *)memb_ptr)[idx];
-       ((signed char *)memb_ptr)[idx] = (signed char)dbl;
+       new = ((signed char *)memb_ptr)[idx] = (signed char)dbl;
        break;
     case NSC_UCHAR:
     case NSC_HIDDEN:
        old = ((unsigned char *)memb_ptr)[idx];
-       ((unsigned char *)memb_ptr)[idx] = (unsigned char)dbl;
+       new = ((unsigned char *)memb_ptr)[idx] = (unsigned char)dbl;
        break;
     case NSC_SHORT:
        old = ((short *)memb_ptr)[idx];
-       ((short *)memb_ptr)[idx] = (short)dbl;
+       new = ((short *)memb_ptr)[idx] = (short)dbl;
        break;
     case NSC_USHORT:
        old = ((unsigned short *)memb_ptr)[idx];
-       ((unsigned short *)memb_ptr)[idx] = (unsigned short)dbl;
+       new = ((unsigned short *)memb_ptr)[idx] = (unsigned short)dbl;
        break;
     case NSC_INT:
        old = ((int *)memb_ptr)[idx];
-       ((int *)memb_ptr)[idx] = (int)dbl;
+       new = ((int *)memb_ptr)[idx] = (int)dbl;
        break;
     case NSC_LONG:
        old = ((long *)memb_ptr)[idx];
-       ((long *)memb_ptr)[idx] = (long)dbl;
+       new = ((long *)memb_ptr)[idx] = (long)dbl;
        break;
     case NSC_XCOORD:
        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);
+       new = ((coord *)memb_ptr)[idx] = XNORM((coord)dbl);
+       if (new >= WORLD_X / 2)
+           new -= WORLD_X;
        break;
     case NSC_YCOORD:
        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);
+       new = ((coord *)memb_ptr)[idx] = YNORM((coord)dbl);
+       if (new >= WORLD_Y / 2)
+           new -= WORLD_Y;
        break;
     case NSC_FLOAT:
        old = ((float *)memb_ptr)[idx];
        ((float *)memb_ptr)[idx] = (float)dbl;
+       new = dbl;              /* suppress new != dbl check */
        break;
     case NSC_DOUBLE:
        old = ((double *)memb_ptr)[idx];
        ((double *)memb_ptr)[idx] = dbl;
+       new = dbl;              /* suppress new != dbl check */
        break;
     case NSC_TIME:
        old = ((time_t *)memb_ptr)[idx];
-       ((time_t *)memb_ptr)[idx] = (time_t)dbl;
+       new = ((time_t *)memb_ptr)[idx] = (time_t)dbl;
        break;
     default:
        return gripe("Field %d doesn't take numbers", fldno + 1);
@@ -590,6 +761,8 @@ setnum(int fldno, double dbl)
 
     if (fldval_must_match(fldno) && old != dbl)
        return gripe("Value for field %d must be %g", fldno + 1, old);
+    if (new != dbl)
+       return gripe("Field %d can't hold this value", fldno + 1);
 
     return 1;
 }
@@ -610,9 +783,11 @@ setstr(int fldno, char *str)
     if (!ca)
        return -1;
 
-    memb_ptr = getobj();
-    if (!memb_ptr)
-       return -1;
+    if (fldno == 0) {
+       if (tbl_next_obj() < 0)
+           return -1;
+    }
+    memb_ptr = cur_obj;
     memb_ptr += ca->ca_off;
     must_match = fldval_must_match(fldno);
 
@@ -784,7 +959,8 @@ xuheader(FILE *fp, int expected_table)
        return gripe("Expected table `%s', not `%s'",
                     ef_nameof(expected_table), name);
 
-    if (!ef_cadef(type) || !(ef_flags(type) & EFF_MEM)) {
+    if (!empfile[type].file
+       || !ef_cadef(type) || !(ef_flags(type) & EFF_MEM)) {
        CANT_HAPPEN(expected_table != EF_BAD);
        return gripe("Table `%s' is not permitted here", name);
     }
@@ -868,6 +1044,8 @@ xufooter(FILE *fp, struct castr ca[], int recs)
     }
     if (skipfs(fp) != '\n')
        return gripe("Junk after table footer");
+    if (tbl_part_done() < 0)
+       return -1;
     lineno++;
 
     for (i = 0; ca[i].ca_name; i++) {
@@ -904,19 +1082,25 @@ xundump(FILE *fp, char *file, int *plno, int expected_table)
        return -1;
 
     nca = nf = 0;
+    may_omit_id = 1;
+    may_trunc = empfile[type].nent < 0;
     for (i = 0; ca[i].ca_name; i++) {
        nca++;
-       if (!(ca[i].ca_flags & NSC_EXTRA))
+       if (!(ca[i].ca_flags & NSC_EXTRA)) {
            nf += MAX(1, ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0);
+           if (ca[i].ca_flags & NSC_CONST)
+               may_omit_id = may_trunc = 0;
+       }
     }
     fldca = malloc(nf * sizeof(*fldca));
     fldidx = malloc(nf * sizeof(*fldidx));
     caflds = malloc(nca * sizeof(*caflds));
     cafldspp = calloc(nca, sizeof(*cafldspp));
-    cur_type = type;
 
+    tbl_start(type);
     if (xutail(fp, ca) < 0)
        type = EF_BAD;
+    tbl_end();
 
     free(cafldspp);
     free(caflds);
@@ -942,7 +1126,6 @@ xutail(FILE *fp, struct castr *ca)
 {
     int recs;
 
-    is_partial = 0;
     for (;;) {
        if (xufldhdr(fp, ca) < 0)
            return -1;
@@ -964,33 +1147,16 @@ xutail(FILE *fp, struct castr *ca)
 static int
 xubody(FILE *fp)
 {
-    struct empfile *ep = &empfile[cur_type];
-    int i, maxid, ch;
+    int i, ch;
 
-    maxid = 0;
     for (i = 0;; ++i) {
        while ((ch = skipfs(fp)) == '\n')
            lineno++;
        if (ch == '/')
            break;
        ungetc(ch, fp);
-       cur_obj = NULL;
-       cur_id = i;
        if (xuflds(fp, xufld) < 0)
            return -1;
-       maxid = MAX(maxid, cur_id + 1);
     }
-
-    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 i;
 }