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: Text loading functions based on xdump output
30 * Known contributors to this file:
31 * Ron Koenderink, 2005
43 #include "prototypes.h"
48 #define MAX_NUM_COLUMNS 256
50 static char *fname = "";
51 static int lineno = 0;
55 * This structure could be replaced with struct valstr.
66 enum enum_value v_type;
78 fprintf(stderr, "%s:%d: ", fname, lineno);
80 vfprintf(stderr, fmt, ap);
97 if (sscanf(++src, "%3o%n", &octal_chr, &n) != 1 || n != 3)
99 *dst++ = (char)octal_chr;
109 xuflds(FILE *fp, struct value values[])
115 for (i = 0; i < MAX_NUM_COLUMNS; i++) {
120 case '+': case '-': case '.':
121 case '0': case '1': case '2': case '3': case '4':
122 case '5': case '6': case '7': case '8': case '9':
123 if (fscanf(fp, "%lg%c", &values[i].v_field.v_double, &sep) != 2)
124 return gripe("Malformed number in field %d", i + 1);
125 values[i].v_type = VAL_DOUBLE;
128 if (fscanf(fp, "\"%1023[^ \n]%c", buf, &sep) != 2
129 || buf[strlen(buf)-1] != '"')
130 return gripe("Malformed string in field %d", i + 1);
131 buf[strlen(buf)-1] = '\0';
133 return gripe("Invalid escape sequence in field %d",
135 values[i].v_type = VAL_STRING;
136 values[i].v_field.v_string = strdup(buf);
142 return gripe("Unexpected end of file while reading a symbol set for field %d", i + 1);
144 values[i].v_field.v_double = 0.0;
145 values[i].v_type = VAL_DOUBLE;
148 return gripe("Unexpected end of file while reading a symbol set for field %d", i + 1);
153 if (fscanf(fp, "(%1023[^)\n])%c", buf, &sep) != 2)
154 return gripe("Malformed symbol set in field %d", i + 1);
155 values[i].v_type = VAL_SYMBOL_SET;
156 values[i].v_field.v_string = strdup(buf);
159 if (fscanf(fp, "%1023[^ \n]%c", buf, &sep) != 2) {
160 return gripe("Junk in field %d", i + 1);
162 if (!strcmp(buf, "nil")) {
163 values[i].v_field.v_string = NULL;
164 values[i].v_type = VAL_STRING;
167 values[i].v_field.v_string = strdup(buf);
168 values[i].v_type = VAL_SYMBOL;
175 "Expected space or newline as field separator found %c",
178 if (i >= MAX_NUM_COLUMNS)
179 return gripe("Too many columns");
181 return gripe("No columns read");
182 values[++i].v_type = VAL_NOTUSED;
187 xunsymbol(struct castr *ca, char *buf, int symbol_set)
189 struct symbol *symbol = (struct symbol *)empfile[ca->ca_table].cache;
194 if (symbol_set && !(ca->ca_flags & NSC_BITS))
195 return gripe("Symbol Set (%s) was found but the field does not have "
196 "NSC_BITS set for field %s", buf, ca->ca_name);
197 if (!symbol_set && (ca->ca_flags & NSC_BITS))
198 return gripe("Symbol (%s) was found but the field was expecting an "
199 "Symbol Set for field %s", buf, ca->ca_name);
201 token = strtok( buf, " ");
205 if (!token || token[0] == '\0')
206 return gripe("Empty symbol value for field %s", ca->ca_name);
209 if ((i = stmtch(token, symbol, offsetof(struct symbol, name),
210 sizeof(struct symbol))) != M_NOTFOUND) {
211 if (!(ca->ca_flags & NSC_BITS))
212 return(symbol[i].value);
213 value |= symbol[i].value;
216 return gripe("Symbol %s was not found for field %s", token,
218 token = strtok(NULL, " ");
224 has_const(struct castr ca[])
228 for (i = 0; ca[i].ca_name; i++) {
229 if (ca[i].ca_flags & NSC_CONST)
236 xuinitrow(int type, int row)
238 struct empfile *ep = &empfile[type];
239 char *ptr = ep->cache + ep->size * row;
241 memset(ptr, 0, ep->size);
248 xuloadrow(int type, int row, struct value values[])
251 struct empfile *ep = &empfile[type];
252 char *ptr = ep->cache + ep->size * row;
253 struct castr *ca = ep->cadef;
258 while (ca[i].ca_type != NSC_NOTYPE &&
259 values[j].v_type != VAL_NOTUSED) {
260 row_ref = (char *)ptr + ca[i].ca_off;
265 * factor out NSC_CONST comparsion
267 switch (values[j].v_type) {
270 if (ca[i].ca_table == EF_BAD)
271 return(gripe("Found symbol string %s, but column %s "
272 "is not symbol or symbol sets",
273 values[j].v_field.v_string, ca[i].ca_name));
274 values[j].v_field.v_double =
275 (double)xunsymbol(&ca[i], values[j].v_field.v_string,
276 values[j].v_type == VAL_SYMBOL_SET ? 1 : 0);
277 free(values[i].v_field.v_string);
278 if (values[j].v_field.v_double < 0.0)
284 switch (ca[i].ca_type) {
286 if (ca[i].ca_flags & NSC_CONST) {
287 if (((int *)row_ref)[k] != (int)
288 values[j].v_field.v_double)
289 gripe("Field %s must be same, "
290 "read %d != expected %d",
293 (int)values[j].v_field.v_double);
296 ((int *)row_ref)[k] =
297 (int)values[j].v_field.v_double;
300 if (ca[i].ca_flags & NSC_CONST) {
301 if (((long *)row_ref)[k] != (long)
302 values[j].v_field.v_double)
303 gripe("Field %s must be same, "
304 "read %ld != expected %ld",
306 ((long *)row_ref)[k],
307 (long)values[j].v_field.v_double);
309 ((long *)row_ref)[k] = (long)
310 values[j].v_field.v_double;
313 if (ca[i].ca_flags & NSC_CONST) {
314 if (((unsigned short *)row_ref)[k] !=
315 (unsigned short)values[j].v_field.v_double)
316 gripe("Field %s must be same, "
317 "read %d != expected %d",
319 ((unsigned short *)row_ref)[k],
320 (unsigned short)values[j].v_field.v_double);
322 ((unsigned short *)row_ref)[k] = (unsigned short)
323 values[j].v_field.v_double;
326 if (ca[i].ca_flags & NSC_CONST) {
327 if (((unsigned char *)row_ref)[k] != (unsigned char)
328 values[j].v_field.v_double)
329 gripe("Field %s must be same, "
330 "read %d != expected %d",
332 ((unsigned char *)row_ref)[k],
333 (unsigned char)values[j].v_field.v_double);
335 ((unsigned char *)row_ref)[k] = (unsigned char)
336 values[j].v_field.v_double;
339 if (ca[i].ca_flags & NSC_CONST) {
340 if (((float *)row_ref)[k] != (float)
341 values[j].v_field.v_double)
342 gripe("Field %s must be same, "
343 "read %g != expected %g",
345 ((float *)row_ref)[k],
346 (float)values[j].v_field.v_double);
348 ((float *)row_ref)[k] = (float)
349 values[j].v_field.v_double;
352 return gripe("Field %s is a string type, "
353 "but %lg was read which is a number",
354 ca[i].ca_name, values[j].v_field.v_double);
356 return gripe("Field %s's type %d is not supported",
357 ca[i].ca_name, ca[i].ca_type);
361 switch(ca[i].ca_type) {
363 return gripe("Field %s is of NSC_STRINGY type "
364 "which is not supported", ca[i].ca_name);
366 if (ca[i].ca_flags & NSC_CONST) {
367 if (strcmp(((char **)row_ref)[k],
368 values[j].v_field.v_string) != 0)
369 gripe("Field %s must be same, "
370 "read %s != expected %s",
372 *((char **)row_ref)[k],
373 *values[j].v_field.v_string);
375 ((char **)row_ref)[k] = values[j].v_field.v_string;
382 return gripe("Field %s is a number type %d, "
383 "but %s was read which is a string",
384 ca[i].ca_name, ca[i].ca_type,
385 values[j].v_field.v_string);
387 return gripe("Field %s's type %d is not supported",
388 ca[i].ca_name, ca[i].ca_type);
392 return gripe("Missing column %s in file", ca[i].ca_name);
394 return gripe("Unknown value type %d", values[j].v_type);
398 } while (k < ca[i].ca_len);
401 if (ca[i].ca_type != NSC_NOTYPE)
402 return gripe("Missing column %s in file", ca[i].ca_name);
403 switch (values[j].v_type) {
408 return gripe("Extra junk after the last column, read %s",
409 values[j].v_field.v_string);
411 return gripe("Extra junk after the last column, read %lg",
412 values[j].v_field.v_double);
414 return gripe("Extra junk after the last column, "
415 "unknown value type %d", values[j].v_type);
421 xuskipcommentlines(FILE *fp)
432 } while (ch != '\n' && ch != EOF);
442 xundump(FILE *fp, char *file, int expected_table)
447 struct value values[MAX_NUM_COLUMNS + 1];
451 if (strcmp(fname, file) != 0) {
457 xuskipcommentlines(fp);
458 if (fscanf(fp, "XDUMP %63[^0123456789]%*d%c", name, &sep) != 2)
459 return gripe("Expected XDUMP header");
461 return gripe("Junk after XDUMP header");
463 if (strlen(name) < 2)
464 return gripe("Invalid table name in header %s", name);
465 if (name[strlen(name) - 1] != ' ')
466 return gripe("Missing space after table name in header %s",
468 name[strlen(name) - 1] = '\0';
470 type = ef_byname(name);
472 return gripe("Table not found %s", name);
474 if (expected_table != EF_BAD && expected_table != type)
475 return gripe("Incorrect Table expecting %s got %s",
476 ef_nameof(expected_table), name);
478 fixed_rows = has_const(ef_cadef(type));
480 for (row = 0; ; row++) {
481 xuskipcommentlines(fp);
486 return gripe("Unexpected EOF");
491 * Add column count check to the return value of xuflds()
493 if (xuflds(fp, values) <= 0)
496 if (row >= empfile[type].csize - 1)
497 return gripe("Too many rows for table %s", name);
498 empfile[type].fids = row + 1;
500 xuinitrow(type, row);
501 if (xuloadrow(type, row, values) < 0)
506 if (fscanf(fp, "/%d%c", &rows, &sep) != 2)
507 return gripe("Failed to find number of rows trailer");
509 return gripe("Number of rows doesn't match between "
510 "the trailer and what was read");
511 if (fixed_rows && row != empfile[type].csize -1)
512 return gripe("Number of rows doesn't match, and "
513 "it must for table %s", name);
515 return gripe("Junk after number of rows trailer");
518 xuinitrow(type, row);
520 xuskipcommentlines(fp);