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[])
117 values[i].v_type = VAL_NOTUSED;
118 if (i >= MAX_NUM_COLUMNS)
119 return gripe("Too many columns");
126 return gripe("Unexpected EOF");
127 case '+': case '-': case '.':
128 case '0': case '1': case '2': case '3': case '4':
129 case '5': case '6': case '7': case '8': case '9':
130 if (fscanf(fp, "%lg%c", &values[i].v_field.v_double, &sep) != 2)
131 return gripe("Malformed number in field %d", i + 1);
132 values[i].v_type = VAL_DOUBLE;
135 if (fscanf(fp, "\"%1023[^ \n]%c", buf, &sep) != 2
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);
146 if (fscanf(fp, "%1023[^ \n]%c", buf, &sep) != 2) {
147 return gripe("Junk in field %d", i + 1);
149 if (!strcmp(buf, "nil")) {
150 values[i].v_type = VAL_STRING;
151 values[i].v_field.v_string = NULL;
154 values[i].v_type = VAL_SYMBOL;
155 values[i].v_field.v_string = strdup(buf);
162 "Expected space or newline as field separator found %c",
165 values[++i].v_type = VAL_NOTUSED;
170 freeflds(struct value values[])
174 for (vp = values; vp->v_type != VAL_NOTUSED; vp++) {
175 if (vp->v_type != VAL_DOUBLE)
176 free(vp->v_field.v_string);
181 xunsymbol(struct castr *ca, char *buf)
183 struct symbol *symbol = (struct symbol *)empfile[ca->ca_table].cache;
188 if (ca->ca_flags & NSC_BITS)
189 token = strtok( buf, "|");
193 if (!token || token[0] == '\0')
194 return gripe("Empty symbol value for field %s", ca->ca_name);
197 if ((i = stmtch(token, symbol, offsetof(struct symbol, name),
198 sizeof(struct symbol))) != M_NOTFOUND) {
199 if (!(ca->ca_flags & NSC_BITS))
200 return(symbol[i].value);
201 value |= symbol[i].value;
204 return gripe("Symbol %s was not found for field %s", token,
206 token = strtok(NULL, "|");
212 has_const(struct castr ca[])
216 for (i = 0; ca[i].ca_name; i++) {
217 if (ca[i].ca_flags & NSC_CONST)
224 xuinitrow(int type, int row)
226 struct empfile *ep = &empfile[type];
227 char *ptr = ep->cache + ep->size * row;
229 memset(ptr, 0, ep->size);
236 xuloadrow(int type, int row, struct value values[])
239 struct empfile *ep = &empfile[type];
240 char *ptr = ep->cache + ep->size * row;
241 struct castr *ca = ep->cadef;
246 while (ca[i].ca_type != NSC_NOTYPE &&
247 values[j].v_type != VAL_NOTUSED) {
248 row_ref = (char *)ptr + ca[i].ca_off;
253 * factor out NSC_CONST comparsion
255 switch (values[j].v_type) {
257 if (ca[i].ca_table == EF_BAD)
258 return(gripe("Found symbol string %s, but column %s "
259 "is not symbol or symbol sets",
260 values[j].v_field.v_string, ca[i].ca_name));
261 values[j].v_field.v_double =
262 (double)xunsymbol(&ca[i], values[j].v_field.v_string);
263 if (values[j].v_field.v_double < 0.0)
269 switch (ca[i].ca_type) {
271 if (ca[i].ca_flags & NSC_CONST) {
272 if (((int *)row_ref)[k] != (int)
273 values[j].v_field.v_double)
274 gripe("Field %s must be same, "
275 "read %d != expected %d",
278 (int)values[j].v_field.v_double);
281 ((int *)row_ref)[k] =
282 (int)values[j].v_field.v_double;
285 if (ca[i].ca_flags & NSC_CONST) {
286 if (((long *)row_ref)[k] != (long)
287 values[j].v_field.v_double)
288 gripe("Field %s must be same, "
289 "read %ld != expected %ld",
291 ((long *)row_ref)[k],
292 (long)values[j].v_field.v_double);
294 ((long *)row_ref)[k] = (long)
295 values[j].v_field.v_double;
298 if (ca[i].ca_flags & NSC_CONST) {
299 if (((unsigned short *)row_ref)[k] !=
300 (unsigned short)values[j].v_field.v_double)
301 gripe("Field %s must be same, "
302 "read %d != expected %d",
304 ((unsigned short *)row_ref)[k],
305 (unsigned short)values[j].v_field.v_double);
307 ((unsigned short *)row_ref)[k] = (unsigned short)
308 values[j].v_field.v_double;
311 if (ca[i].ca_flags & NSC_CONST) {
312 if (((unsigned char *)row_ref)[k] != (unsigned char)
313 values[j].v_field.v_double)
314 gripe("Field %s must be same, "
315 "read %d != expected %d",
317 ((unsigned char *)row_ref)[k],
318 (unsigned char)values[j].v_field.v_double);
320 ((unsigned char *)row_ref)[k] = (unsigned char)
321 values[j].v_field.v_double;
324 if (ca[i].ca_flags & NSC_CONST) {
325 if (((float *)row_ref)[k] != (float)
326 values[j].v_field.v_double)
327 gripe("Field %s must be same, "
328 "read %g != expected %g",
330 ((float *)row_ref)[k],
331 (float)values[j].v_field.v_double);
333 ((float *)row_ref)[k] = (float)
334 values[j].v_field.v_double;
337 return gripe("Field %s is a string type, "
338 "but %lg was read which is a number",
339 ca[i].ca_name, values[j].v_field.v_double);
341 return gripe("Field %s's type %d is not supported",
342 ca[i].ca_name, ca[i].ca_type);
346 switch(ca[i].ca_type) {
348 return gripe("Field %s is of NSC_STRINGY type "
349 "which is not supported", ca[i].ca_name);
351 if (ca[i].ca_flags & NSC_CONST) {
352 if (strcmp(((char **)row_ref)[k],
353 values[j].v_field.v_string) != 0)
354 gripe("Field %s must be same, "
355 "read %s != expected %s",
357 *((char **)row_ref)[k],
358 *values[j].v_field.v_string);
360 ((char **)row_ref)[k] = values[j].v_field.v_string;
367 return gripe("Field %s is a number type %d, "
368 "but %s was read which is a string",
369 ca[i].ca_name, ca[i].ca_type,
370 values[j].v_field.v_string);
372 return gripe("Field %s's type %d is not supported",
373 ca[i].ca_name, ca[i].ca_type);
377 return gripe("Missing column %s in file", ca[i].ca_name);
379 return gripe("Unknown value type %d", values[j].v_type);
383 } while (k < ca[i].ca_len);
386 if (ca[i].ca_type != NSC_NOTYPE)
387 return gripe("Missing column %s in file", ca[i].ca_name);
388 switch (values[j].v_type) {
393 return gripe("Extra junk after the last column, read %s",
394 values[j].v_field.v_string);
396 return gripe("Extra junk after the last column, read %lg",
397 values[j].v_field.v_double);
399 return gripe("Extra junk after the last column, "
400 "unknown value type %d", values[j].v_type);
406 xundump(FILE *fp, char *file, int expected_table)
410 int row, res, rows, ch;
411 struct value values[MAX_NUM_COLUMNS + 1];
415 if (strcmp(fname, file) != 0) {
421 if (fscanf(fp, "XDUMP %63s %*d%c", name, &sep) != 2)
422 return gripe("Expected XDUMP header");
424 return gripe("Junk after XDUMP header");
426 type = ef_byname(name);
428 return gripe("Unknown table `%s'", name);
430 if (expected_table != EF_BAD && expected_table != type)
431 return gripe("Expected table `%s', not `%s'",
432 ef_nameof(expected_table), name);
434 fixed_rows = has_const(ef_cadef(type));
436 for (row = 0; ; row++) {
444 * Add column count check to the return value of xuflds()
446 res = xuflds(fp, values);
447 if (res > 0 && row >= empfile[type].csize - 1) {
448 gripe("Too many rows for table %s", name);
452 empfile[type].fids = row + 1;
454 xuinitrow(type, row);
455 res = xuloadrow(type, row, values);
462 if (fscanf(fp, "/%d%c", &rows, &sep) != 2)
463 return gripe("Expected 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);