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.
67 enum enum_value v_type;
79 fprintf(stderr, "%s:%d: ", fname, lineno);
81 vfprintf(stderr, fmt, ap);
95 } while (ch == ' ' || ch == '\t');
100 } while (ch != EOF && ch != '\n');
116 if (sscanf(++src, "%3o%n", &octal_chr, &n) != 1 || n != 3)
118 *dst++ = (char)octal_chr;
128 xuflds(FILE *fp, struct value values[])
134 values[i].v_type = VAL_NOTUSED;
135 if (i >= MAX_NUM_COLUMNS)
136 return gripe("Too many columns");
141 return gripe("Unexpected EOF");
144 case '+': case '-': case '.':
145 case '0': case '1': case '2': case '3': case '4':
146 case '5': case '6': case '7': case '8': case '9':
148 if (fscanf(fp, "%lg", &values[i].v_field.v_double) != 1)
149 return gripe("Malformed number in field %d", i + 1);
150 values[i].v_type = VAL_DOUBLE;
153 if (fscanf(fp, "%1023[^ \t#\n]", buf) != 1
154 || buf[strlen(buf)-1] != '"')
155 return gripe("Malformed string in field %d", i + 1);
156 buf[strlen(buf)-1] = '\0';
158 return gripe("Invalid escape sequence in field %d",
160 values[i].v_type = VAL_STRING;
161 values[i].v_field.v_string = strdup(buf);
165 if (fscanf(fp, "%1023[^ \t#\n]", buf) != 1 || !isalpha(buf[0])) {
166 return gripe("Junk in field %d", i + 1);
168 if (!strcmp(buf, "nil")) {
169 values[i].v_type = VAL_STRING;
170 values[i].v_field.v_string = NULL;
173 values[i].v_type = VAL_SYMBOL;
174 values[i].v_field.v_string = strdup(buf);
180 else if (ch != ' ' && ch != '\t')
181 return gripe("Bad field separator after field %d", i + 1);
186 freeflds(struct value values[])
190 for (vp = values; vp->v_type != VAL_NOTUSED; vp++) {
191 if (vp->v_type != VAL_DOUBLE)
192 free(vp->v_field.v_string);
197 xunsymbol(struct castr *ca, char *buf)
199 struct symbol *symbol = (struct symbol *)empfile[ca->ca_table].cache;
204 if (ca->ca_flags & NSC_BITS)
205 token = strtok( buf, "|");
209 if (!token || token[0] == '\0')
210 return gripe("Empty symbol value for field %s", ca->ca_name);
213 if ((i = stmtch(token, symbol, offsetof(struct symbol, name),
214 sizeof(struct symbol))) != M_NOTFOUND) {
215 if (!(ca->ca_flags & NSC_BITS))
216 return(symbol[i].value);
217 value |= symbol[i].value;
220 return gripe("Symbol %s was not found for field %s", token,
222 token = strtok(NULL, "|");
228 has_const(struct castr ca[])
232 for (i = 0; ca[i].ca_name; i++) {
233 if (ca[i].ca_flags & NSC_CONST)
240 xuinitrow(int type, int row)
242 struct empfile *ep = &empfile[type];
243 char *ptr = ep->cache + ep->size * row;
245 memset(ptr, 0, ep->size);
252 xuloadrow(int type, int row, struct value values[])
255 struct empfile *ep = &empfile[type];
256 char *ptr = ep->cache + ep->size * row;
257 struct castr *ca = ep->cadef;
262 while (ca[i].ca_type != NSC_NOTYPE &&
263 values[j].v_type != VAL_NOTUSED) {
264 if (ca[i].ca_flags & NSC_EXTRA) {
268 row_ref = (char *)ptr + ca[i].ca_off;
273 * factor out NSC_CONST comparsion
275 switch (values[j].v_type) {
277 if (ca[i].ca_table == EF_BAD)
278 return(gripe("Found symbol string %s, but column %s "
279 "is not symbol or symbol sets",
280 values[j].v_field.v_string, ca[i].ca_name));
281 values[j].v_field.v_double =
282 (double)xunsymbol(&ca[i], values[j].v_field.v_string);
283 if (values[j].v_field.v_double < 0.0)
289 switch (ca[i].ca_type) {
291 if (ca[i].ca_flags & NSC_CONST) {
292 if (((int *)row_ref)[k] != (int)
293 values[j].v_field.v_double)
294 gripe("Field %s must be same, "
295 "read %d != expected %d",
298 (int)values[j].v_field.v_double);
301 ((int *)row_ref)[k] =
302 (int)values[j].v_field.v_double;
305 if (ca[i].ca_flags & NSC_CONST) {
306 if (((long *)row_ref)[k] != (long)
307 values[j].v_field.v_double)
308 gripe("Field %s must be same, "
309 "read %ld != expected %ld",
311 ((long *)row_ref)[k],
312 (long)values[j].v_field.v_double);
314 ((long *)row_ref)[k] = (long)
315 values[j].v_field.v_double;
318 if (ca[i].ca_flags & NSC_CONST) {
319 if (((unsigned short *)row_ref)[k] !=
320 (unsigned short)values[j].v_field.v_double)
321 gripe("Field %s must be same, "
322 "read %d != expected %d",
324 ((unsigned short *)row_ref)[k],
325 (unsigned short)values[j].v_field.v_double);
327 ((unsigned short *)row_ref)[k] = (unsigned short)
328 values[j].v_field.v_double;
331 if (ca[i].ca_flags & NSC_CONST) {
332 if (((unsigned char *)row_ref)[k] != (unsigned char)
333 values[j].v_field.v_double)
334 gripe("Field %s must be same, "
335 "read %d != expected %d",
337 ((unsigned char *)row_ref)[k],
338 (unsigned char)values[j].v_field.v_double);
340 ((unsigned char *)row_ref)[k] = (unsigned char)
341 values[j].v_field.v_double;
344 if (ca[i].ca_flags & NSC_CONST) {
345 if (((float *)row_ref)[k] != (float)
346 values[j].v_field.v_double)
347 gripe("Field %s must be same, "
348 "read %g != expected %g",
350 ((float *)row_ref)[k],
351 (float)values[j].v_field.v_double);
353 ((float *)row_ref)[k] = (float)
354 values[j].v_field.v_double;
357 return gripe("Field %s is a string type, "
358 "but %lg was read which is a number",
359 ca[i].ca_name, values[j].v_field.v_double);
361 return gripe("Field %s's type %d is not supported",
362 ca[i].ca_name, ca[i].ca_type);
366 switch(ca[i].ca_type) {
368 if (ca[i].ca_flags & NSC_CONST) {
369 if (strcmp(((char **)row_ref)[k],
370 values[j].v_field.v_string) != 0)
371 gripe("Field %s must be same, "
372 "read %s != expected %s",
374 ((char **)row_ref)[k],
375 values[j].v_field.v_string);
377 ((char **)row_ref)[k]
378 = strdup(values[j].v_field.v_string);
385 return gripe("Field %s is a number type %d, "
386 "but %s was read which is a string",
387 ca[i].ca_name, ca[i].ca_type,
388 values[j].v_field.v_string);
390 return gripe("Field %s's type %d is not supported",
391 ca[i].ca_name, ca[i].ca_type);
395 return gripe("Missing column %s in file", ca[i].ca_name);
397 return gripe("Unknown value type %d", values[j].v_type);
401 } while (k < ca[i].ca_len);
404 if (ca[i].ca_type != NSC_NOTYPE)
405 return gripe("Missing column %s in file", ca[i].ca_name);
406 switch (values[j].v_type) {
411 return gripe("Extra junk after the last column, read %s",
412 values[j].v_field.v_string);
414 return gripe("Extra junk after the last column, read %lg",
415 values[j].v_field.v_double);
417 return gripe("Extra junk after the last column, "
418 "unknown value type %d", values[j].v_type);
424 xundump(FILE *fp, char *file, int expected_table)
428 int row, res, rows, ch;
429 struct value values[MAX_NUM_COLUMNS + 1];
431 int fixed_rows, need_sentinel;
433 if (strcmp(fname, file) != 0) {
439 while ((ch = skipfs(fp)) == '\n')
441 if (ch == EOF && expected_table == EF_BAD)
445 if (fscanf(fp, "XDUMP%*[ \t]%63[^ \t#\n]%*[ \t]%*[^ \t#\n]", name) != 1)
446 return gripe("Expected XDUMP header");
447 if (skipfs(fp) != '\n')
448 return gripe("Junk after XDUMP header");
450 type = ef_byname(name);
452 return gripe("Unknown table `%s'", name);
454 if (CANT_HAPPEN(!(ep->flags & EFF_MEM)))
455 return -1; /* not implemented */
457 if (expected_table != EF_BAD && expected_table != type)
458 return gripe("Expected table `%s', not `%s'",
459 ef_nameof(expected_table), name);
461 fixed_rows = has_const(ef_cadef(type));
462 need_sentinel = !fixed_rows; /* FIXME only approximation */
464 for (row = 0; ; row++) {
472 * Add column count check to the return value of xuflds()
474 res = xuflds(fp, values);
475 if (res > 0 && row >= ep->csize - 1) {
476 /* TODO grow cache unless EFF_STATIC */
477 gripe("Too many rows for table %s", name);
482 xuinitrow(type, row);
483 res = xuloadrow(type, row, values);
492 if (!isdigit(ch) || fscanf(fp, "%d", &rows) != 1)
493 return gripe("Malformed table footer");
494 if (skipfs(fp) != '\n')
495 return gripe("Junk after table footer");
497 return gripe("Read %d rows, which doesn't match footer",
499 if (fixed_rows && row != ep->csize -1)
500 return gripe("Table %s requires %d rows, got %d",
501 name, ep->csize - 1, row);
503 xuinitrow(type, row);
505 ep->fids = ep->cids = row;