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);
98 if (sscanf(++src, "%3o%n", &octal_chr, &n) != 1 || n != 3)
100 *dst++ = (char)octal_chr;
110 xuflds(FILE *fp, struct value values[])
116 values[i].v_type = VAL_NOTUSED;
117 if (i >= MAX_NUM_COLUMNS)
118 return gripe("Too many columns");
123 return gripe("Unexpected EOF");
126 case '+': case '-': case '.':
127 case '0': case '1': case '2': case '3': case '4':
128 case '5': case '6': case '7': case '8': case '9':
130 if (fscanf(fp, "%lg", &values[i].v_field.v_double) != 1)
131 return gripe("Malformed number in field %d", i + 1);
132 values[i].v_type = VAL_DOUBLE;
135 if (fscanf(fp, "%1023[^ \n]", buf) != 1
136 || buf[strlen(buf)-1] != '"')
137 return gripe("Malformed string in field %d", i + 1);
138 buf[strlen(buf)-1] = '\0';
140 return gripe("Invalid escape sequence in field %d",
142 values[i].v_type = VAL_STRING;
143 values[i].v_field.v_string = strdup(buf);
147 if (fscanf(fp, "%1023[^ \n]", buf) != 1 || !isalpha(buf[0])) {
148 return gripe("Junk in field %d", i + 1);
150 if (!strcmp(buf, "nil")) {
151 values[i].v_type = VAL_STRING;
152 values[i].v_field.v_string = NULL;
155 values[i].v_type = VAL_SYMBOL;
156 values[i].v_field.v_string = strdup(buf);
163 return gripe("Bad field separator after field %d", i + 1);
168 freeflds(struct value values[])
172 for (vp = values; vp->v_type != VAL_NOTUSED; vp++) {
173 if (vp->v_type != VAL_DOUBLE)
174 free(vp->v_field.v_string);
179 xunsymbol(struct castr *ca, char *buf)
181 struct symbol *symbol = (struct symbol *)empfile[ca->ca_table].cache;
186 if (ca->ca_flags & NSC_BITS)
187 token = strtok( buf, "|");
191 if (!token || token[0] == '\0')
192 return gripe("Empty symbol value for field %s", ca->ca_name);
195 if ((i = stmtch(token, symbol, offsetof(struct symbol, name),
196 sizeof(struct symbol))) != M_NOTFOUND) {
197 if (!(ca->ca_flags & NSC_BITS))
198 return(symbol[i].value);
199 value |= symbol[i].value;
202 return gripe("Symbol %s was not found for field %s", token,
204 token = strtok(NULL, "|");
210 has_const(struct castr ca[])
214 for (i = 0; ca[i].ca_name; i++) {
215 if (ca[i].ca_flags & NSC_CONST)
222 xuinitrow(int type, int row)
224 struct empfile *ep = &empfile[type];
225 char *ptr = ep->cache + ep->size * row;
227 memset(ptr, 0, ep->size);
234 xuloadrow(int type, int row, struct value values[])
237 struct empfile *ep = &empfile[type];
238 char *ptr = ep->cache + ep->size * row;
239 struct castr *ca = ep->cadef;
244 while (ca[i].ca_type != NSC_NOTYPE &&
245 values[j].v_type != VAL_NOTUSED) {
246 row_ref = (char *)ptr + ca[i].ca_off;
251 * factor out NSC_CONST comparsion
253 switch (values[j].v_type) {
255 if (ca[i].ca_table == EF_BAD)
256 return(gripe("Found symbol string %s, but column %s "
257 "is not symbol or symbol sets",
258 values[j].v_field.v_string, ca[i].ca_name));
259 values[j].v_field.v_double =
260 (double)xunsymbol(&ca[i], values[j].v_field.v_string);
261 if (values[j].v_field.v_double < 0.0)
267 switch (ca[i].ca_type) {
269 if (ca[i].ca_flags & NSC_CONST) {
270 if (((int *)row_ref)[k] != (int)
271 values[j].v_field.v_double)
272 gripe("Field %s must be same, "
273 "read %d != expected %d",
276 (int)values[j].v_field.v_double);
279 ((int *)row_ref)[k] =
280 (int)values[j].v_field.v_double;
283 if (ca[i].ca_flags & NSC_CONST) {
284 if (((long *)row_ref)[k] != (long)
285 values[j].v_field.v_double)
286 gripe("Field %s must be same, "
287 "read %ld != expected %ld",
289 ((long *)row_ref)[k],
290 (long)values[j].v_field.v_double);
292 ((long *)row_ref)[k] = (long)
293 values[j].v_field.v_double;
296 if (ca[i].ca_flags & NSC_CONST) {
297 if (((unsigned short *)row_ref)[k] !=
298 (unsigned short)values[j].v_field.v_double)
299 gripe("Field %s must be same, "
300 "read %d != expected %d",
302 ((unsigned short *)row_ref)[k],
303 (unsigned short)values[j].v_field.v_double);
305 ((unsigned short *)row_ref)[k] = (unsigned short)
306 values[j].v_field.v_double;
309 if (ca[i].ca_flags & NSC_CONST) {
310 if (((unsigned char *)row_ref)[k] != (unsigned char)
311 values[j].v_field.v_double)
312 gripe("Field %s must be same, "
313 "read %d != expected %d",
315 ((unsigned char *)row_ref)[k],
316 (unsigned char)values[j].v_field.v_double);
318 ((unsigned char *)row_ref)[k] = (unsigned char)
319 values[j].v_field.v_double;
322 if (ca[i].ca_flags & NSC_CONST) {
323 if (((float *)row_ref)[k] != (float)
324 values[j].v_field.v_double)
325 gripe("Field %s must be same, "
326 "read %g != expected %g",
328 ((float *)row_ref)[k],
329 (float)values[j].v_field.v_double);
331 ((float *)row_ref)[k] = (float)
332 values[j].v_field.v_double;
335 return gripe("Field %s is a string type, "
336 "but %lg was read which is a number",
337 ca[i].ca_name, values[j].v_field.v_double);
339 return gripe("Field %s's type %d is not supported",
340 ca[i].ca_name, ca[i].ca_type);
344 switch(ca[i].ca_type) {
346 return gripe("Field %s is of NSC_STRINGY type "
347 "which is not supported", ca[i].ca_name);
349 if (ca[i].ca_flags & NSC_CONST) {
350 if (strcmp(((char **)row_ref)[k],
351 values[j].v_field.v_string) != 0)
352 gripe("Field %s must be same, "
353 "read %s != expected %s",
355 *((char **)row_ref)[k],
356 *values[j].v_field.v_string);
358 ((char **)row_ref)[k] = values[j].v_field.v_string;
365 return gripe("Field %s is a number type %d, "
366 "but %s was read which is a string",
367 ca[i].ca_name, ca[i].ca_type,
368 values[j].v_field.v_string);
370 return gripe("Field %s's type %d is not supported",
371 ca[i].ca_name, ca[i].ca_type);
375 return gripe("Missing column %s in file", ca[i].ca_name);
377 return gripe("Unknown value type %d", values[j].v_type);
381 } while (k < ca[i].ca_len);
384 if (ca[i].ca_type != NSC_NOTYPE)
385 return gripe("Missing column %s in file", ca[i].ca_name);
386 switch (values[j].v_type) {
391 return gripe("Extra junk after the last column, read %s",
392 values[j].v_field.v_string);
394 return gripe("Extra junk after the last column, read %lg",
395 values[j].v_field.v_double);
397 return gripe("Extra junk after the last column, "
398 "unknown value type %d", values[j].v_type);
404 xundump(FILE *fp, char *file, int expected_table)
408 int row, res, rows, ch;
409 struct value values[MAX_NUM_COLUMNS + 1];
413 if (strcmp(fname, file) != 0) {
419 if (fscanf(fp, "XDUMP%*1[ ]%63[^ \n]%*1[ ]%*[^ \n]%c", name, &sep) != 2)
420 return gripe("Expected XDUMP header");
422 return gripe("Junk after XDUMP header");
424 type = ef_byname(name);
426 return gripe("Unknown table `%s'", name);
428 if (expected_table != EF_BAD && expected_table != type)
429 return gripe("Expected table `%s', not `%s'",
430 ef_nameof(expected_table), name);
432 fixed_rows = has_const(ef_cadef(type));
434 for (row = 0; ; row++) {
442 * Add column count check to the return value of xuflds()
444 res = xuflds(fp, values);
445 if (res > 0 && row >= empfile[type].csize - 1) {
446 gripe("Too many rows for table %s", name);
450 empfile[type].fids = row + 1;
452 xuinitrow(type, row);
453 res = xuloadrow(type, row, values);
462 if (!isdigit(ch) || fscanf(fp, "%d%c", &rows, &sep) != 2)
463 return gripe("Malformed table footer");
465 return gripe("Read %d rows, which doesn't match footer",
467 if (fixed_rows && row != empfile[type].csize -1)
468 return gripe("Table %s requires %d rows, got %d",
469 name, empfile[type].csize - 1, row);
471 return gripe("Junk after table footer");
474 xuinitrow(type, row);