2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program 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 2 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * xundump.c: Load back xdump output
30 * Known contributors to this file:
31 * Ron Koenderink, 2005
32 * Markus Armbruster, 2005
35 /* FIXME normalize terminology: table/rows/columns or file/records/fields */
45 #include "prototypes.h"
50 #define MAX_NUM_COLUMNS 256
52 static char *fname = "";
53 static int lineno = 0;
57 * This structure could be replaced with struct valstr.
68 enum enum_value v_type;
75 static int gripe(char *fmt, ...) ATTRIBUTE((format (printf, 1, 2)));
82 fprintf(stderr, "%s:%d: ", fname, lineno);
84 vfprintf(stderr, fmt, ap);
98 } while (ch == ' ' || ch == '\t');
103 } while (ch != EOF && ch != '\n');
110 getid(FILE *fp, char *buf)
113 if (fscanf(fp, "%1023[^#() \t\n]%n", buf, &n) != 1 || !isalpha(buf[0]))
128 if (sscanf(++src, "%3o%n", &octal_chr, &n) != 1 || n != 3)
130 *dst++ = (char)octal_chr;
140 xuflds(FILE *fp, struct value values[])
148 values[i].v_type = VAL_NOTUSED;
149 if (i >= MAX_NUM_COLUMNS)
150 return gripe("Too many columns");
155 return gripe("Unexpected EOF");
158 case '+': case '-': case '.':
159 case '0': case '1': case '2': case '3': case '4':
160 case '5': case '6': case '7': case '8': case '9':
162 if (fscanf(fp, "%lg", &values[i].v_field.v_double) != 1)
163 return gripe("Malformed number in field %d", i + 1);
164 values[i].v_type = VAL_DOUBLE;
172 if (fscanf(fp, "%1023[^\"\n]", buf) != 1 || getc(fp) != '"')
173 return gripe("Malformed string in field %d", i + 1);
175 return gripe("Invalid escape sequence in field %d",
178 values[i].v_type = VAL_STRING;
179 values[i].v_field.v_string = strdup(buf);
186 if (ch == EOF || ch == '\n')
187 return gripe("Unmatched '(' in field %d", i + 1);
193 return gripe("Junk in field %d", i + 1);
194 p = realloc(p, len + l1 + 2);
195 strcpy(p + len, buf);
196 strcpy(p + len + l1, " ");
201 values[i].v_type = VAL_SYMBOL_SET;
202 values[i].v_field.v_string = p;
206 if (getid(fp, buf) < 0) {
207 return gripe("Junk in field %d", i + 1);
209 if (!strcmp(buf, "nil")) {
210 values[i].v_type = VAL_STRING;
211 values[i].v_field.v_string = NULL;
214 values[i].v_type = VAL_SYMBOL;
215 values[i].v_field.v_string = strdup(buf);
221 else if (ch != ' ' && ch != '\t')
222 return gripe("Bad field separator after field %d", i + 1);
227 freeflds(struct value values[])
231 for (vp = values; vp->v_type != VAL_NOTUSED; vp++) {
232 if (vp->v_type != VAL_DOUBLE)
233 free(vp->v_field.v_string);
238 xunsymbol1(char *id, struct symbol *symtab, struct castr *ca, int n)
240 int i = stmtch(id, symtab, offsetof(struct symbol, name),
241 sizeof(struct symbol));
243 return gripe("%s %s symbol `%s' in field %d",
244 i == M_NOTUNIQUE ? "Ambiguous" : "Unknown",
250 xunsymbol(struct castr *ca, struct value *vp, int n)
252 struct symbol *symtab = (struct symbol *)empfile[ca->ca_table].cache;
253 char *buf = vp->v_field.v_string;
258 if (vp->v_type == VAL_SYMBOL_SET) {
259 if (!(ca->ca_flags & NSC_BITS) || ca->ca_table == EF_BAD)
260 return gripe("%s doesn't take a symbol set in field %d",
263 for (token = strtok(buf, " "); token; token = strtok(NULL, " ")) {
264 i = xunsymbol1(token, symtab, ca, n);
267 value |= symtab[i].value;
269 } else if (vp->v_type == VAL_SYMBOL) {
270 if ((ca->ca_flags & NSC_BITS) || ca->ca_table == EF_BAD)
271 return gripe("%s doesn't take a symbol in field %d",
273 i = xunsymbol1(buf, symtab, ca, n);
276 value = symtab[i].value;
280 vp->v_type = VAL_DOUBLE;
281 vp->v_field.v_double = value;
286 has_const(struct castr ca[])
290 for (i = 0; ca[i].ca_name; i++) {
291 if (ca[i].ca_flags & NSC_CONST)
298 xuinitrow(int type, int row)
300 struct empfile *ep = &empfile[type];
301 char *ptr = ep->cache + ep->size * row;
303 memset(ptr, 0, ep->size);
310 xuloadrow(int type, int row, struct value values[])
313 struct empfile *ep = &empfile[type];
314 char *ptr = ep->cache + ep->size * row;
315 struct castr *ca = ep->cadef;
320 while (ca[i].ca_type != NSC_NOTYPE &&
321 values[j].v_type != VAL_NOTUSED) {
322 if (ca[i].ca_flags & NSC_EXTRA) {
326 row_ref = (char *)ptr + ca[i].ca_off;
331 * factor out NSC_CONST comparsion
333 switch (values[j].v_type) {
336 if (xunsymbol(&ca[i], &values[j], j) < 0)
340 switch (ca[i].ca_type) {
342 if (ca[i].ca_flags & NSC_CONST) {
343 if (((int *)row_ref)[k] != (int)
344 values[j].v_field.v_double)
345 gripe("Field %s must be same, "
346 "read %d != expected %d",
349 (int)values[j].v_field.v_double);
352 ((int *)row_ref)[k] =
353 (int)values[j].v_field.v_double;
356 if (ca[i].ca_flags & NSC_CONST) {
357 if (((long *)row_ref)[k] != (long)
358 values[j].v_field.v_double)
359 gripe("Field %s must be same, "
360 "read %ld != expected %ld",
362 ((long *)row_ref)[k],
363 (long)values[j].v_field.v_double);
365 ((long *)row_ref)[k] = (long)
366 values[j].v_field.v_double;
369 if (ca[i].ca_flags & NSC_CONST) {
370 if (((unsigned short *)row_ref)[k] !=
371 (unsigned short)values[j].v_field.v_double)
372 gripe("Field %s must be same, "
373 "read %d != expected %d",
375 ((unsigned short *)row_ref)[k],
376 (unsigned short)values[j].v_field.v_double);
378 ((unsigned short *)row_ref)[k] = (unsigned short)
379 values[j].v_field.v_double;
382 if (ca[i].ca_flags & NSC_CONST) {
383 if (((unsigned char *)row_ref)[k] != (unsigned char)
384 values[j].v_field.v_double)
385 gripe("Field %s must be same, "
386 "read %d != expected %d",
388 ((unsigned char *)row_ref)[k],
389 (unsigned char)values[j].v_field.v_double);
391 ((unsigned char *)row_ref)[k] = (unsigned char)
392 values[j].v_field.v_double;
395 if (ca[i].ca_flags & NSC_CONST) {
396 if (((float *)row_ref)[k] != (float)
397 values[j].v_field.v_double)
398 gripe("Field %s must be same, "
399 "read %g != expected %g",
401 ((float *)row_ref)[k],
402 (float)values[j].v_field.v_double);
404 ((float *)row_ref)[k] = (float)
405 values[j].v_field.v_double;
408 return gripe("Field %s is a string type, "
409 "but %lg was read which is a number",
410 ca[i].ca_name, values[j].v_field.v_double);
412 return gripe("Field %s's type %d is not supported",
413 ca[i].ca_name, ca[i].ca_type);
417 switch(ca[i].ca_type) {
419 if (ca[i].ca_flags & NSC_CONST) {
420 if (strcmp(((char **)row_ref)[k],
421 values[j].v_field.v_string) != 0)
422 gripe("Field %s must be same, "
423 "read %s != expected %s",
425 ((char **)row_ref)[k],
426 values[j].v_field.v_string);
428 ((char **)row_ref)[k]
429 = strdup(values[j].v_field.v_string);
436 return gripe("Field %s is a number type %d, "
437 "but %s was read which is a string",
438 ca[i].ca_name, ca[i].ca_type,
439 values[j].v_field.v_string);
441 return gripe("Field %s's type %d is not supported",
442 ca[i].ca_name, ca[i].ca_type);
446 return gripe("Missing column %s in file", ca[i].ca_name);
448 return gripe("Unknown value type %d", values[j].v_type);
452 } while (k < ca[i].ca_len);
455 if (ca[i].ca_type != NSC_NOTYPE)
456 return gripe("Missing column %s in file", ca[i].ca_name);
457 switch (values[j].v_type) {
463 return gripe("Extra junk after the last column, read %s",
464 values[j].v_field.v_string);
466 return gripe("Extra junk after the last column, read %lg",
467 values[j].v_field.v_double);
469 return gripe("Extra junk after the last column, "
470 "unknown value type %d", values[j].v_type);
476 xundump(FILE *fp, char *file, int expected_table)
480 int row, res, rows, ch;
481 struct value values[MAX_NUM_COLUMNS + 1];
483 int fixed_rows, need_sentinel;
485 if (strcmp(fname, file) != 0) {
491 while ((ch = skipfs(fp)) == '\n')
493 if (ch == EOF && expected_table == EF_BAD)
498 if (fscanf(fp, "XDUMP%*[ \t]%63[^ \t#\n]%*[ \t]%*[^ \t#\n]%n",
501 return gripe("Expected XDUMP header");
502 if (skipfs(fp) != '\n')
503 return gripe("Junk after XDUMP header");
505 type = ef_byname(name);
507 return gripe("Unknown table `%s'", name);
509 if (CANT_HAPPEN(!(ep->flags & EFF_MEM)))
510 return -1; /* not implemented */
512 if (expected_table != EF_BAD && expected_table != type)
513 return gripe("Expected table `%s', not `%s'",
514 ef_nameof(expected_table), name);
516 fixed_rows = has_const(ef_cadef(type));
517 need_sentinel = !fixed_rows; /* FIXME only approximation */
528 * Add column count check to the return value of xuflds()
530 res = xuflds(fp, values);
531 if (res > 0 && row >= ep->csize - 1) {
532 /* TODO grow cache unless EFF_STATIC */
533 gripe("Too many rows for table %s", name);
538 xuinitrow(type, row);
539 res = xuloadrow(type, row, values);
549 if (!isdigit(ch) || fscanf(fp, "%d", &rows) != 1)
550 return gripe("Malformed table footer");
551 if (skipfs(fp) != '\n')
552 return gripe("Junk after table footer");
554 return gripe("Read %d rows, which doesn't match footer",
556 if (fixed_rows && row != ep->csize -1)
557 return gripe("Table %s requires %d rows, got %d",
558 name, ep->csize - 1, row);
560 xuinitrow(type, row);
562 while ((ch = skipfs(fp)) == '\n') ;
565 ep->fids = ep->cids = row;