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.
65 enum enum_value v_type;
77 fprintf(stderr, "%s:%d: ", fname, lineno);
79 vfprintf(stderr, fmt, ap);
96 if (sscanf(++src, "%3o%n", &octal_chr, &n) != 1 || n != 3)
98 *dst++ = (char)octal_chr;
108 xuflds(FILE *fp, struct value values[])
114 for (i = 0; i < MAX_NUM_COLUMNS; i++) {
119 case '+': case '-': case '.':
120 case '0': case '1': case '2': case '3': case '4':
121 case '5': case '6': case '7': case '8': case '9':
122 if (fscanf(fp, "%lg%c", &values[i].v_field.v_double, &sep) != 2)
123 return gripe("Malformed number in field %d", i + 1);
124 values[i].v_type = VAL_DOUBLE;
127 if (fscanf(fp, "\"%1023[^ \n]%c", buf, &sep) != 2
128 || buf[strlen(buf)-1] != '"')
129 return gripe("Malformed string in field %d", i + 1);
130 buf[strlen(buf)-1] = '\0';
132 return gripe("Invalid escape sequence in field %d",
134 values[i].v_type = VAL_STRING;
135 values[i].v_field.v_string = strdup(buf);
138 if (fscanf(fp, "%1023[^ \n]%c", buf, &sep) != 2) {
139 return gripe("Junk in field %d", i + 1);
141 if (!strcmp(buf, "nil")) {
142 values[i].v_field.v_string = NULL;
143 values[i].v_type = VAL_STRING;
146 values[i].v_field.v_string = strdup(buf);
147 values[i].v_type = VAL_SYMBOL;
154 "Expected space or newline as field separator found %c",
157 if (i >= MAX_NUM_COLUMNS)
158 return gripe("Too many columns");
160 return gripe("No columns read");
161 values[++i].v_type = VAL_NOTUSED;
166 xunsymbol(struct castr *ca, char *buf)
168 struct symbol *symbol = (struct symbol *)empfile[ca->ca_table].cache;
173 if (ca->ca_flags & NSC_BITS)
174 token = strtok( buf, "|");
178 if (!token || token[0] == '\0')
179 return gripe("Empty symbol value for field %s", ca->ca_name);
182 if ((i = stmtch(token, symbol, offsetof(struct symbol, name),
183 sizeof(struct symbol))) != M_NOTFOUND) {
184 if (!(ca->ca_flags & NSC_BITS))
185 return(symbol[i].value);
186 value |= symbol[i].value;
189 return gripe("Symbol %s was not found for field %s", token,
191 token = strtok(NULL, "|");
197 has_const(struct castr ca[])
201 for (i = 0; ca[i].ca_name; i++) {
202 if (ca[i].ca_flags & NSC_CONST)
209 xuinitrow(int type, int row)
211 struct empfile *ep = &empfile[type];
212 char *ptr = ep->cache + ep->size * row;
214 memset(ptr, 0, ep->size);
221 xuloadrow(int type, int row, struct value values[])
224 struct empfile *ep = &empfile[type];
225 char *ptr = ep->cache + ep->size * row;
226 struct castr *ca = ep->cadef;
231 while (ca[i].ca_type != NSC_NOTYPE &&
232 values[j].v_type != VAL_NOTUSED) {
233 row_ref = (char *)ptr + ca[i].ca_off;
238 * factor out NSC_CONST comparsion
240 switch (values[j].v_type) {
242 if (ca[i].ca_table == EF_BAD)
243 return(gripe("Found symbol string %s, but column %s "
244 "is not symbol or symbol sets",
245 values[j].v_field.v_string, ca[i].ca_name));
246 values[j].v_field.v_double =
247 (double)xunsymbol(&ca[i], values[j].v_field.v_string);
248 free(values[i].v_field.v_string);
249 if (values[j].v_field.v_double < 0.0)
255 switch (ca[i].ca_type) {
257 if (ca[i].ca_flags & NSC_CONST) {
258 if (((int *)row_ref)[k] != (int)
259 values[j].v_field.v_double)
260 gripe("Field %s must be same, "
261 "read %d != expected %d",
264 (int)values[j].v_field.v_double);
267 ((int *)row_ref)[k] =
268 (int)values[j].v_field.v_double;
271 if (ca[i].ca_flags & NSC_CONST) {
272 if (((long *)row_ref)[k] != (long)
273 values[j].v_field.v_double)
274 gripe("Field %s must be same, "
275 "read %ld != expected %ld",
277 ((long *)row_ref)[k],
278 (long)values[j].v_field.v_double);
280 ((long *)row_ref)[k] = (long)
281 values[j].v_field.v_double;
284 if (ca[i].ca_flags & NSC_CONST) {
285 if (((unsigned short *)row_ref)[k] !=
286 (unsigned short)values[j].v_field.v_double)
287 gripe("Field %s must be same, "
288 "read %d != expected %d",
290 ((unsigned short *)row_ref)[k],
291 (unsigned short)values[j].v_field.v_double);
293 ((unsigned short *)row_ref)[k] = (unsigned short)
294 values[j].v_field.v_double;
297 if (ca[i].ca_flags & NSC_CONST) {
298 if (((unsigned char *)row_ref)[k] != (unsigned char)
299 values[j].v_field.v_double)
300 gripe("Field %s must be same, "
301 "read %d != expected %d",
303 ((unsigned char *)row_ref)[k],
304 (unsigned char)values[j].v_field.v_double);
306 ((unsigned char *)row_ref)[k] = (unsigned char)
307 values[j].v_field.v_double;
310 if (ca[i].ca_flags & NSC_CONST) {
311 if (((float *)row_ref)[k] != (float)
312 values[j].v_field.v_double)
313 gripe("Field %s must be same, "
314 "read %g != expected %g",
316 ((float *)row_ref)[k],
317 (float)values[j].v_field.v_double);
319 ((float *)row_ref)[k] = (float)
320 values[j].v_field.v_double;
323 return gripe("Field %s is a string type, "
324 "but %lg was read which is a number",
325 ca[i].ca_name, values[j].v_field.v_double);
327 return gripe("Field %s's type %d is not supported",
328 ca[i].ca_name, ca[i].ca_type);
332 switch(ca[i].ca_type) {
334 return gripe("Field %s is of NSC_STRINGY type "
335 "which is not supported", ca[i].ca_name);
337 if (ca[i].ca_flags & NSC_CONST) {
338 if (strcmp(((char **)row_ref)[k],
339 values[j].v_field.v_string) != 0)
340 gripe("Field %s must be same, "
341 "read %s != expected %s",
343 *((char **)row_ref)[k],
344 *values[j].v_field.v_string);
346 ((char **)row_ref)[k] = values[j].v_field.v_string;
353 return gripe("Field %s is a number type %d, "
354 "but %s was read which is a string",
355 ca[i].ca_name, ca[i].ca_type,
356 values[j].v_field.v_string);
358 return gripe("Field %s's type %d is not supported",
359 ca[i].ca_name, ca[i].ca_type);
363 return gripe("Missing column %s in file", ca[i].ca_name);
365 return gripe("Unknown value type %d", values[j].v_type);
369 } while (k < ca[i].ca_len);
372 if (ca[i].ca_type != NSC_NOTYPE)
373 return gripe("Missing column %s in file", ca[i].ca_name);
374 switch (values[j].v_type) {
379 return gripe("Extra junk after the last column, read %s",
380 values[j].v_field.v_string);
382 return gripe("Extra junk after the last column, read %lg",
383 values[j].v_field.v_double);
385 return gripe("Extra junk after the last column, "
386 "unknown value type %d", values[j].v_type);
392 xuskipcommentlines(FILE *fp)
403 } while (ch != '\n' && ch != EOF);
413 xundump(FILE *fp, char *file, int expected_table)
418 struct value values[MAX_NUM_COLUMNS + 1];
422 if (strcmp(fname, file) != 0) {
428 xuskipcommentlines(fp);
429 if (fscanf(fp, "XDUMP %63[^0123456789]%*d%c", name, &sep) != 2)
430 return gripe("Expected XDUMP header");
432 return gripe("Junk after XDUMP header");
434 if (strlen(name) < 2)
435 return gripe("Invalid table name in header %s", name);
436 if (name[strlen(name) - 1] != ' ')
437 return gripe("Missing space after table name in header %s",
439 name[strlen(name) - 1] = '\0';
441 type = ef_byname(name);
443 return gripe("Table not found %s", name);
445 if (expected_table != EF_BAD && expected_table != type)
446 return gripe("Incorrect Table expecting %s got %s",
447 ef_nameof(expected_table), name);
449 fixed_rows = has_const(ef_cadef(type));
451 for (row = 0; ; row++) {
452 xuskipcommentlines(fp);
457 return gripe("Unexpected EOF");
462 * Add column count check to the return value of xuflds()
464 if (xuflds(fp, values) <= 0)
467 if (row >= empfile[type].csize - 1)
468 return gripe("Too many rows for table %s", name);
469 empfile[type].fids = row + 1;
471 xuinitrow(type, row);
472 if (xuloadrow(type, row, values) < 0)
477 if (fscanf(fp, "/%d%c", &rows, &sep) != 2)
478 return gripe("Failed to find number of rows trailer");
480 return gripe("Number of rows doesn't match between "
481 "the trailer and what was read");
482 if (fixed_rows && row != empfile[type].csize -1)
483 return gripe("Number of rows doesn't match, and "
484 "it must for table %s", name);
486 return gripe("Junk after number of rows trailer");
489 xuinitrow(type, row);
491 xuskipcommentlines(fp);