2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2014, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * xundump.c: Load back xdump output
29 * Known contributors to this file:
30 * Ron Koenderink, 2005
31 * Markus Armbruster, 2005-2014
35 * See doc/xdump! And keep it up-to-date.
37 * Parsing of machine-readable xdump is not precise: it recognizes
38 * comments, accepts whitespace in place of single space, and accepts
39 * the full human-readable field syntax instead of its machine-
43 * - Normalize terminology: table/rows/columns or file/records/fields
44 * - Loading tables with NSC_STRING elements more than once leaks memory
46 * - Symbolic array indexes
47 * - Option to treat missing and unknown fields as warning, not error
48 * TODO, but hardly worth the effort:
49 * - Permit reordering of array elements
67 static char *fname; /* Name of file being read */
68 static int lineno; /* Current line number */
70 static int cur_type; /* Current table's file type */
71 static int partno; /* Counts from 0..#parts-1 */
72 static void *cur_obj; /* The object being read into */
73 static int cur_id; /* and its index in the table */
75 static unsigned char *idgap; /* idgap && idgap[ID] iff part#0 lacks ID */
76 static int idgap_len; /* #elements in idgap[] */
78 static int human; /* Reading human-readable syntax? */
79 static int ellipsis; /* Header ended with ...? */
80 static int nflds; /* #fields in input records */
81 static struct castr **fldca; /* Map field number to selector */
82 static int *fldidx; /* Map field number to index */
83 static struct valstr *fldval; /* Map field number to value */
84 static int *caflds; /* Map selector number to #fields seen */
85 static int *cafldspp; /* ditto, in previous parts */
87 static int gripe(char *, ...) ATTRIBUTE((format (printf, 1, 2)));
88 static int deffld(int, char *, int);
89 static int chkflds(void);
90 static int setnum(int, double);
91 static int setstr(int, char *);
92 static int setsym(int, char *);
93 static int mtsymset(int, long *);
94 static int add2symset(int, long *, char *);
95 static int xubody(FILE *);
96 static int xutail(FILE *, struct castr *);
99 * Does the code hardcode indexes for table TYPE?
102 have_hardcoded_indexes(int type)
104 return type == EF_ITEM || type == EF_SECTOR_CHR
105 || type == EF_INFRASTRUCTURE;
109 * Okay to truncate table TYPE?
112 may_truncate(int type)
114 return empfile[type].nent < 0 && !have_hardcoded_indexes(type);
118 * Is TYPE's 0-th selector a usable ID?
123 struct castr *ca = ef_cadef(type);
125 return ca[0].ca_table == type && !(ca[0].ca_flags & NSC_EXTRA);
129 * Can we fill in gaps in table TYPE?
132 can_fill_gaps(int type)
134 return ca0_is_id(type) && !have_hardcoded_indexes(type);
138 * Gripe about the current line to stderr, return -1.
141 gripe(char *fmt, ...)
145 fprintf(stderr, "%s:%d: ", fname, lineno);
147 vfprintf(stderr, fmt, ap);
154 /* Make TYPE the current table. */
162 old_nelem = type == EF_BAD ? 0 : ef_nelem(type);
167 /* End the current table. */
176 * Seek to current table's ID-th record.
177 * ID must be acceptable.
178 * Store it in cur_obj, and set cur_id accordingly.
179 * Return 0 on success, -1 on failure.
184 struct empfile *ep = &empfile[cur_type];
186 if (id >= ef_nelem(cur_type)) {
187 if (!ef_ensure_space(cur_type, id, 1))
188 return gripe("Can't put ID %d into table %s", id, ep->name);
191 cur_obj = ef_ptr(cur_type, id);
192 if (CANT_HAPPEN(!cur_obj))
200 * Reset the omitted objects to default state.
203 omit_ids(int id1, int id2)
210 idgap = realloc(idgap, id2 * sizeof(*idgap));
211 for (i = idgap_len; i < id1; i++)
213 for (i = id1; i < id2; i++) {
214 ef_blank(cur_type, i, ef_ptr(cur_type, i));
221 * Return the smallest non-omitted ID in ID1..ID2-1 if any, else -1.
224 expected_id(int id1, int id2)
228 for (i = id1; i < id2; i++) {
229 if (i >= idgap_len || !idgap[i])
237 * If the table has variable length, truncate it.
238 * Else ensure we're omitting the same objects as the previous parts.
239 * Reset any omitted objects to default state.
240 * Return 0 on success, -1 on failure.
245 struct empfile *ep = &empfile[cur_type];
247 if (cur_id + 1 < ep->fids) {
249 if (may_truncate(cur_type)) {
250 if (!ef_truncate(cur_type, cur_id + 1))
253 if (!can_fill_gaps(cur_type))
254 return gripe("Expected %d more rows",
255 ep->fids - (cur_id + 1));
256 omit_ids(cur_id + 1, ep->fids);
259 if (expected_id(cur_id + 1, ep->fids) >= 0)
260 return gripe("Table's first part has more rows");
271 * Get selector for field FLDNO.
272 * Assign the field's selector index to *IDX, unless it is null.
273 * Return the selector on success, null pointer on error.
275 static struct castr *
276 getfld(int fldno, int *idx)
278 if (fldno >= nflds) {
279 gripe("Too many fields, expected only %d", nflds);
282 if (CANT_HAPPEN(fldno < 0))
285 *idx = fldidx[fldno];
290 * Find the field for selector CA with index IDX.
291 * Return the field number if it exists, else -1.
294 fld_find(struct castr *ca, int idx)
298 for (i = 0; i < nflds; i++) {
299 if (fldca[i] == ca && fldidx[i] == idx)
306 * Get the current row's ID.
307 * Current table's 0-th selector must be a usable ID.
308 * Return ID on success, -1 on failure.
313 struct castr *ca = ef_cadef(cur_type);
314 int fldno, id, max_id;
316 if (CANT_HAPPEN(partno != 0 || !ca0_is_id(cur_type)))
319 fldno = fld_find(ca, 0);
321 return cur_id + 1; /* ID not specified */
323 * Field values not representable as int will be rejected by
324 * putnum() or putstr(). Leave the error reporting to them, and
325 * simply pick the next ID here.
327 if (fldval[fldno].val_type != NSC_DOUBLE)
329 id = fldval[fldno].val_as.dbl;
330 if (id != fldval[fldno].val_as.dbl)
333 if (id != cur_id + 1 && !can_fill_gaps(cur_type))
334 return gripe("Expected %d in field %d",
335 cur_id + 1, fldno + 1);
337 return gripe("Field %d must be > %d", fldno + 1, cur_id);
338 max_id = ef_id_limit(cur_type);
340 return gripe("Field %d must be <= %d", fldno + 1, max_id);
346 * Get the current row's object.
347 * Store it in cur_obj, and set cur_id accordingly.
348 * Return 0 on success, -1 on failure.
353 int last_id = cur_id;
357 id = expected_id(cur_id + 1, empfile[cur_type].fids);
359 return gripe("Table's first part doesn't have this row");
360 } else if (ca0_is_id(cur_type)) {
366 if (id > ef_id_limit(cur_type))
367 return gripe("Too many rows");
368 if (tbl_seek(id) < 0)
372 omit_ids(last_id + 1, id);
377 * Is a new value for field FLDNO required to match the old one?
380 fldval_must_match(int fldno)
382 struct castr *ca = ef_cadef(cur_type);
383 int i = fldca[fldno] - ca;
386 * Value must match if:
387 * it's for a const selector, unless the object is still blank, or
388 * it was already given in a previous part of a split table.
390 return (cur_id < old_nelem && (fldca[fldno]->ca_flags & NSC_CONST))
391 || fldidx[fldno] < cafldspp[i];
395 * Set OBJ's field FLDNO to DBL.
396 * Return 0 on success, -1 on error.
399 putnum(void *obj, int fldno, double dbl)
401 struct castr *ca = fldca[fldno];
402 int idx = fldidx[fldno];
406 memb_ptr = (char *)obj + ca->ca_off;
408 switch (ca->ca_type) {
410 old = ((signed char *)memb_ptr)[idx];
411 new = ((signed char *)memb_ptr)[idx] = (signed char)dbl;
414 old = ((unsigned char *)memb_ptr)[idx];
415 new = ((unsigned char *)memb_ptr)[idx] = (unsigned char)dbl;
418 old = ((short *)memb_ptr)[idx];
419 new = ((short *)memb_ptr)[idx] = (short)dbl;
422 old = ((unsigned short *)memb_ptr)[idx];
423 new = ((unsigned short *)memb_ptr)[idx] = (unsigned short)dbl;
426 old = ((int *)memb_ptr)[idx];
427 new = ((int *)memb_ptr)[idx] = (int)dbl;
430 old = ((long *)memb_ptr)[idx];
431 new = ((long *)memb_ptr)[idx] = (long)dbl;
434 old = ((coord *)memb_ptr)[idx];
435 /* FIXME use variant of xrel() that takes orig instead of nation */
436 if (old >= WORLD_X / 2)
438 new = ((coord *)memb_ptr)[idx] = XNORM((coord)dbl);
439 if (new >= WORLD_X / 2)
443 old = ((coord *)memb_ptr)[idx];
444 /* FIXME use variant of yrel() that takes orig instead of nation */
445 if (old >= WORLD_Y / 2)
447 new = ((coord *)memb_ptr)[idx] = YNORM((coord)dbl);
448 if (new >= WORLD_Y / 2)
452 old = ((float *)memb_ptr)[idx];
453 ((float *)memb_ptr)[idx] = (float)dbl;
454 new = dbl; /* suppress new != dbl check */
457 old = ((double *)memb_ptr)[idx];
458 ((double *)memb_ptr)[idx] = dbl;
459 new = dbl; /* suppress new != dbl check */
462 old = ((time_t *)memb_ptr)[idx];
463 new = ((time_t *)memb_ptr)[idx] = (time_t)dbl;
466 return gripe("Field %d doesn't take numbers", fldno + 1);
469 if (fldval_must_match(fldno) && old != dbl)
470 return gripe("Value for field %d must be %g", fldno + 1, old);
472 return gripe("Field %d can't hold this value", fldno + 1);
478 * Set obj's field FLDNO to STR.
479 * Return 0 on success, -1 on error.
482 putstr(void *obj, int fldno, char *str)
484 struct castr *ca = fldca[fldno];
485 int idx = fldidx[fldno];
486 int must_match, mismatch;
488 char *memb_ptr, *old;
490 memb_ptr = (char *)obj + ca->ca_off;
491 must_match = fldval_must_match(fldno);
494 switch (ca->ca_type) {
496 old = ((char **)memb_ptr)[idx];
498 mismatch = old ? !str || strcmp(old, str) : !!str;
500 /* FIXME may leak old value */
501 ((char **)memb_ptr)[idx] = str ? strdup(str) : NULL;
502 len = -1; /* unlimited */
505 if (CANT_HAPPEN(idx))
508 return gripe("Field %d doesn't take nil", fldno + 1);
509 /* Wart: if ca_len <= 1, the terminating null may be omitted */
511 len = sz > 1 ? sz - 1 : sz;
512 if (strlen(str) > len)
513 return gripe("Field %d takes at most %d characters",
514 fldno + 1, (int)len);
517 mismatch = !str || strncmp(old, str, len);
519 strncpy(memb_ptr, str, sz);
522 return gripe("Field %d doesn't take strings", fldno + 1);
527 return gripe("Value for field %d must be \"%.*s\"",
528 fldno + 1, (int)len, old);
530 return gripe("Value for field %d must be nil", fldno + 1);
537 * Save the current row's fields in its object.
538 * Return 0 on success, -1 on failure.
548 for (i = 0; i < nflds; i++) {
549 switch (fldval[i].val_type) {
551 ret |= putnum(cur_obj, i, fldval[i].val_as.dbl);
554 ret |= putstr(cur_obj, i, fldval[i].val_as.str.base);
555 free(fldval[i].val_as.str.base);
567 * Read and ignore field separators from FP.
568 * Return first character that is not a field separator.
577 } while (ch == ' ' || ch == '\t');
582 } while (ch != EOF && ch != '\n');
589 * Decode escape sequences in BUF.
590 * Return BUF on success, null pointer on failure.
602 if (sscanf(++src, "%3o%n", &octal_chr, &n) != 1 || n != 3)
604 *dst++ = (char)octal_chr;
614 * Read an identifier from FP into BUF.
615 * BUF must have space for 1024 characters.
616 * Return number of characters read on success, -1 on failure.
619 getid(FILE *fp, char *buf)
622 if (fscanf(fp, "%1023[^\"#()<>= \t\n]%n", buf, &n) != 1
630 * Try to read a field name from FP.
631 * I is the field number, counting from zero.
632 * If a name is read, set fldca[I] and fldidx[I] for it, and update
634 * Return 1 if a name or ... was read, 0 on end of line, -1 on error.
637 xufldname(FILE *fp, int i)
645 return gripe("Unexpected EOF");
647 nflds = i - (ellipsis != 0);
653 if (getc(fp) != '.' || getc(fp) != '.')
654 return gripe("Junk in header field %d", i + 1);
656 return gripe("Header fields expected");
659 if (ch != EOF && ch != '\n')
660 return gripe("Junk after ...");
665 if (getid(fp, buf) < 0)
666 return gripe("Junk in header field %d", i + 1);
670 return deffld(i, buf, -1);
674 if (isdigit(ch) || ch == '-' || ch == '+') {
675 if (fscanf(fp, "%d", &idx) != 1)
676 return gripe("Malformed number in index of header field %d",
679 return gripe("Index must not be negative in header field %d",
682 if (getid(fp, buf) < 0)
683 return gripe("Malformed index in header field %d", i + 1);
684 return gripe("Symbolic index in header field %d not yet implemented",
689 return gripe("Malformed index in header field %d", i + 1);
690 return deffld(i, buf, idx);
695 * Try to read a field value from FP.
696 * I is the field number, counting from zero.
697 * Return 1 if a value was read, 0 on end of line, -1 on error.
700 xufld(FILE *fp, int i)
710 return gripe("Unexpected EOF");
712 CANT_HAPPEN(i > nflds);
713 for (j = i; j < nflds; j++) {
714 if (CA_IS_ARRAY(fldca[j]))
715 gripe("Field %s(%d) missing",
716 fldca[j]->ca_name, fldidx[j]);
718 gripe("Field %s missing", fldca[j]->ca_name);
720 if (i != nflds || putrow() < 0)
723 return i < nflds ? -1 : 0;
724 case '+': case '-': case '.':
725 case '0': case '1': case '2': case '3': case '4':
726 case '5': case '6': case '7': case '8': case '9':
728 if (fscanf(fp, "%lg", &dbl) != 1)
729 return gripe("Malformed number in field %d", i + 1);
730 return setnum(i, dbl);
737 if (fscanf(fp, "%1023[^\"\n]", buf) != 1 || getc(fp) != '"')
738 return gripe("Malformed string in field %d", i + 1);
740 return gripe("Invalid escape sequence in field %d",
743 return setstr(i, buf);
745 if (mtsymset(i, &set) < 0)
749 if (ch == EOF || ch == '\n')
750 return gripe("Unmatched '(' in field %d", i + 1);
754 if (getid(fp, buf) < 0)
755 return gripe("Junk in field %d", i + 1);
756 if (add2symset(i, &set, buf) < 0)
759 return setnum(i, set);
762 if (getid(fp, buf) < 0)
763 return gripe("Junk in field %d", i + 1);
764 if (!strcmp(buf, "nil"))
765 return setstr(i, NULL);
767 return setsym(i, buf);
772 * Read fields from FP.
773 * Use PARSE() to read each field.
774 * Return number of fields read on success, -1 on error.
777 xuflds(FILE *fp, int (*parse)(FILE *, int))
790 else if (ch != ' ' && ch != '\t')
791 return gripe("Bad field separator after field %d", i + 1);
796 * Define the FLDNO-th field.
797 * If IDX is negative, define as selector NAME, else as NAME(IDX).
798 * Set fldca[FLDNO] and fldidx[FLDNO] accordingly.
800 * Return 1 on success, -1 on error.
803 deffld(int fldno, char *name, int idx)
805 struct castr *ca = ef_cadef(cur_type);
808 res = stmtch(name, ca, offsetof(struct castr, ca_name),
809 sizeof(struct castr));
811 return gripe("Header %s of field %d is %s", name, fldno + 1,
812 res == M_NOTUNIQUE ? "ambiguous" : "unknown");
813 if ((ca[res].ca_flags & NSC_EXTRA) || CANT_HAPPEN(ca[res].ca_get))
814 return gripe("Extraneous header %s in field %d", name, fldno + 1);
815 if (CA_IS_ARRAY(&ca[res])) {
817 return gripe("Header %s requires an index in field %d",
818 ca[res].ca_name, fldno + 1);
819 if (idx >= ca[res].ca_len)
820 return gripe("Header %s(%d) index out of bounds in field %d",
821 ca[res].ca_name, idx, fldno + 1);
822 if (idx < caflds[res])
823 return gripe("Duplicate header %s(%d) in field %d",
824 ca[res].ca_name, idx, fldno + 1);
825 if (idx > caflds[res])
826 return gripe("Expected header %s(%d) in field %d",
827 ca[res].ca_name, caflds[res], fldno + 1);
830 return gripe("Header %s doesn't take an index in field %d",
831 ca[res].ca_name, fldno + 1);
834 return gripe("Duplicate header %s in field %d",
835 ca[res].ca_name, fldno + 1);
837 fldca[fldno] = &ca[res];
844 * Check fields in xdump are sane.
845 * Return 0 on success, -1 on error.
850 struct castr *ca = ef_cadef(cur_type);
851 int i, len, cafldsmax, res = 0;
854 return res; /* table is split, another part expected */
856 /* Check for missing fields */
857 for (i = 0; ca[i].ca_name; i++) {
858 cafldsmax = MAX(caflds[i], cafldspp[i]);
859 if (ca[i].ca_flags & NSC_EXTRA)
861 len = CA_ARRAY_LEN(&ca[i]);
862 if (!len && !cafldsmax)
863 res = gripe("Header field %s missing", ca[i].ca_name);
864 else if (len && cafldsmax == len - 1)
865 res = gripe("Header field %s(%d) missing",
866 ca[i].ca_name, len - 1);
867 else if (len && cafldsmax < len - 1)
868 res = gripe("Header fields %s(%d) ... %s(%d) missing",
869 ca[i].ca_name, cafldsmax, ca[i].ca_name, len - 1);
876 * Set value of field FLDNO in current row to DBL.
877 * Return 1 on success, -1 on error.
880 setnum(int fldno, double dbl)
882 if (!getfld(fldno, NULL))
884 fldval[fldno].val_cat = NSC_VAL;
885 fldval[fldno].val_type = NSC_DOUBLE;
886 fldval[fldno].val_as.dbl = dbl;
891 * Set value of field FLDNO in current row to STR.
892 * Return 1 on success, -1 on error.
895 setstr(int fldno, char *str)
897 if (!getfld(fldno, NULL))
899 fldval[fldno].val_cat = NSC_VAL;
900 fldval[fldno].val_type = NSC_STRING;
901 fldval[fldno].val_as.str.base = str ? strdup(str) : NULL;
902 fldval[fldno].val_as.str.maxsz = INT_MAX;
903 /* really SIZE_MAX, but that's C99 */
908 * Resolve symbol name ID in table referred to by CA.
909 * Use field number N for error messages.
910 * Return index in referred table on success, -1 on failure.
913 xunsymbol(char *id, struct castr *ca, int n)
915 int i = ef_elt_byname(ca->ca_table, id);
917 return gripe("%s %s symbol `%s' in field %d",
918 i == M_NOTUNIQUE ? "Ambiguous" : "Unknown",
919 ca->ca_name, id, n + 1);
924 * Map symbol index to symbol value.
925 * CA is the table, and I is the index in it.
928 symval(struct castr *ca, int i)
930 int type = ca->ca_table;
932 if (type != EF_BAD && ef_cadef(type) == symbol_ca)
933 /* symbol table, value is in the table */
934 return ((struct symbol *)ef_ptr(type, i))->value;
935 /* value is the table index */
940 * Set value of field FLDNO in current object to value of symbol SYM.
941 * Return 1 on success, -1 on error.
944 setsym(int fldno, char *sym)
949 ca = getfld(fldno, NULL);
953 if (ca->ca_table == EF_BAD || (ca->ca_flags & NSC_BITS))
954 return gripe("Field %d doesn't take symbols", fldno + 1);
956 i = xunsymbol(sym, ca, fldno);
959 return setnum(fldno, symval(ca, i));
963 * Create an empty symbol set for field FLDNO in *SET.
964 * Return 1 on success, -1 on error.
967 mtsymset(int fldno, long *set)
971 ca = getfld(fldno, NULL);
975 if (ca->ca_table == EF_BAD || ef_cadef(ca->ca_table) != symbol_ca
976 || !(ca->ca_flags & NSC_BITS))
977 return gripe("Field %d doesn't take symbol sets", fldno + 1);
983 * Add a symbol to a symbol set for field FLDNO in *SET.
984 * SYM is the name of the symbol to add.
985 * Return 1 on success, -1 on error.
988 add2symset(int fldno, long *set, char *sym)
993 ca = getfld(fldno, NULL);
997 i = xunsymbol(sym, ca, fldno);
1000 *set |= symval(ca, i);
1005 * Read an xdump table header line from FP.
1006 * Expect header for EXPECTED_TABLE, unless it is EF_BAD.
1007 * Recognize header for machine- and human-readable syntax, and set
1008 * human accordingly.
1009 * Return table type on success, -2 on EOF before header, -1 on failure.
1012 xuheader(FILE *fp, int expected_table)
1018 while ((ch = skipfs(fp)) == '\n')
1020 if (ch == EOF && expected_table == EF_BAD)
1027 ? fscanf(fp, "config%*[ \t]%63[^ \t#\n]%n", name, &res) != 1
1028 : fscanf(fp, "XDUMP%*[ \t]%63[^ \t#\n]%*[ \t]%*[^ \t#\n]%n",
1029 name, &res) != 1) || res < 0)
1030 return gripe("Expected xdump header");
1032 type = ef_byname(name);
1034 return gripe("Unknown table `%s'", name);
1035 if (expected_table != EF_BAD && expected_table != type)
1036 return gripe("Expected table `%s', not `%s'",
1037 ef_nameof(expected_table), name);
1039 if (!empfile[type].file
1040 || !ef_cadef(type) || !(ef_flags(type) & EFF_MEM)) {
1041 CANT_HAPPEN(expected_table != EF_BAD);
1042 return gripe("Table `%s' is not permitted here", name);
1045 if (skipfs(fp) != '\n')
1046 return gripe("Junk after xdump header");
1053 * Find fields in this xdump.
1054 * If reading human-readable syntax, read a field header line from FP.
1055 * Else take fields from the table's selectors in CA[].
1056 * Set ellipsis, nflds, fldca[], fldidx[] and caflds[] accordingly.
1057 * Return 0 on success, -1 on failure.
1060 xufldhdr(FILE *fp, struct castr ca[])
1066 for (i = 0; ca[i].ca_name; i++)
1071 while ((ch = skipfs(fp)) == '\n')
1074 if (xuflds(fp, xufldname) < 0)
1080 for (i = 0; ca[i].ca_name; i++) {
1081 if ((ca[i].ca_flags & NSC_EXTRA))
1083 n = CA_ARRAY_LEN(&ca[i]);
1091 nflds = fidx - fldidx;
1098 * Read xdump footer from FP.
1099 * CA[] contains the table's selectors.
1100 * The body had RECS records.
1101 * Update cafldspp[] from caflds[].
1102 * Return 0 on success, -1 on failure.
1105 xufooter(FILE *fp, struct castr ca[], int recs)
1111 if (fscanf(fp, "config%n", &res) != 0 || res < 0)
1112 return gripe("Malformed table footer");
1114 if (fscanf(fp, "%d", &n) != 1)
1115 return gripe("Malformed table footer");
1117 return gripe("Read %d rows, which doesn't match footer "
1118 "%d rows", recs, n);
1120 if (skipfs(fp) != '\n')
1121 return gripe("Junk after table footer");
1122 if (tbl_part_done() < 0)
1126 for (i = 0; ca[i].ca_name; i++) {
1127 if (cafldspp[i] < caflds[i])
1128 cafldspp[i] = caflds[i];
1135 * Read an xdump table from FP.
1136 * Both machine- and human-readable xdump syntax are recognized.
1137 * Expect table EXPECTED_TABLE, unless it is EF_BAD.
1138 * Report errors to stderr.
1139 * Messages assume FP starts in the file FILE at line *PLNO.
1140 * Update *PLNO to reflect lines read from FP.
1141 * Return table type on success, -2 on EOF before header, -1 on failure.
1144 xundump(FILE *fp, char *file, int *plno, int expected_table)
1147 int type, nca, nf, i, ch;
1152 if ((type = xuheader(fp, expected_table)) < 0)
1155 ca = ef_cadef(type);
1156 if (CANT_HAPPEN(!ca))
1160 for (i = 0; ca[i].ca_name; i++) {
1162 if (!(ca[i].ca_flags & NSC_EXTRA))
1163 nf += MAX(1, CA_ARRAY_LEN(&ca[i]));
1165 fldca = malloc(nf * sizeof(*fldca));
1166 fldidx = malloc(nf * sizeof(*fldidx));
1167 fldval = malloc(nf * sizeof(*fldval));
1168 caflds = malloc(nca * sizeof(*caflds));
1169 cafldspp = calloc(nca, sizeof(*cafldspp));
1172 if (xutail(fp, ca) < 0)
1182 /* Skip empty lines so that callers can easily check for EOF */
1183 while ((ch = skipfs(fp)) == '\n')
1192 * Read the remainder of an xdump after the table header line from FP.
1193 * CA[] contains the table's selectors.
1194 * Return 0 on success, -1 on failure.
1197 xutail(FILE *fp, struct castr *ca)
1202 if (xufldhdr(fp, ca) < 0)
1204 if ((recs = xubody(fp)) < 0)
1206 if (xufooter(fp, ca, recs) < 0)
1210 if (xuheader(fp, cur_type) < 0)
1216 * Read the body of an xdump table from FP.
1217 * Return number of rows read on success, -1 on failure.
1225 while ((ch = skipfs(fp)) == '\n')
1230 if (xuflds(fp, xufld) < 0)