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++) {
115 values[i].v_type = VAL_NOTUSED;
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);
139 if (fscanf(fp, "%1023[^ \n]%c", buf, &sep) != 2) {
140 return gripe("Junk in field %d", i + 1);
142 if (!strcmp(buf, "nil")) {
143 values[i].v_type = VAL_STRING;
144 values[i].v_field.v_string = NULL;
147 values[i].v_type = VAL_SYMBOL;
148 values[i].v_field.v_string = strdup(buf);
155 "Expected space or newline as field separator found %c",
158 if (i >= MAX_NUM_COLUMNS)
159 return gripe("Too many columns");
161 return gripe("No columns read");
162 values[++i].v_type = VAL_NOTUSED;
167 freeflds(struct value values[])
171 for (vp = values; vp->v_type != VAL_NOTUSED; vp++) {
172 if (vp->v_type != VAL_DOUBLE)
173 free(vp->v_field.v_string);
178 xunsymbol(struct castr *ca, char *buf)
180 struct symbol *symbol = (struct symbol *)empfile[ca->ca_table].cache;
185 if (ca->ca_flags & NSC_BITS)
186 token = strtok( buf, "|");
190 if (!token || token[0] == '\0')
191 return gripe("Empty symbol value for field %s", ca->ca_name);
194 if ((i = stmtch(token, symbol, offsetof(struct symbol, name),
195 sizeof(struct symbol))) != M_NOTFOUND) {
196 if (!(ca->ca_flags & NSC_BITS))
197 return(symbol[i].value);
198 value |= symbol[i].value;
201 return gripe("Symbol %s was not found for field %s", token,
203 token = strtok(NULL, "|");
209 has_const(struct castr ca[])
213 for (i = 0; ca[i].ca_name; i++) {
214 if (ca[i].ca_flags & NSC_CONST)
221 xuinitrow(int type, int row)
223 struct empfile *ep = &empfile[type];
224 char *ptr = ep->cache + ep->size * row;
226 memset(ptr, 0, ep->size);
233 xuloadrow(int type, int row, struct value values[])
236 struct empfile *ep = &empfile[type];
237 char *ptr = ep->cache + ep->size * row;
238 struct castr *ca = ep->cadef;
243 while (ca[i].ca_type != NSC_NOTYPE &&
244 values[j].v_type != VAL_NOTUSED) {
245 row_ref = (char *)ptr + ca[i].ca_off;
250 * factor out NSC_CONST comparsion
252 switch (values[j].v_type) {
254 if (ca[i].ca_table == EF_BAD)
255 return(gripe("Found symbol string %s, but column %s "
256 "is not symbol or symbol sets",
257 values[j].v_field.v_string, ca[i].ca_name));
258 values[j].v_field.v_double =
259 (double)xunsymbol(&ca[i], values[j].v_field.v_string);
260 if (values[j].v_field.v_double < 0.0)
266 switch (ca[i].ca_type) {
268 if (ca[i].ca_flags & NSC_CONST) {
269 if (((int *)row_ref)[k] != (int)
270 values[j].v_field.v_double)
271 gripe("Field %s must be same, "
272 "read %d != expected %d",
275 (int)values[j].v_field.v_double);
278 ((int *)row_ref)[k] =
279 (int)values[j].v_field.v_double;
282 if (ca[i].ca_flags & NSC_CONST) {
283 if (((long *)row_ref)[k] != (long)
284 values[j].v_field.v_double)
285 gripe("Field %s must be same, "
286 "read %ld != expected %ld",
288 ((long *)row_ref)[k],
289 (long)values[j].v_field.v_double);
291 ((long *)row_ref)[k] = (long)
292 values[j].v_field.v_double;
295 if (ca[i].ca_flags & NSC_CONST) {
296 if (((unsigned short *)row_ref)[k] !=
297 (unsigned short)values[j].v_field.v_double)
298 gripe("Field %s must be same, "
299 "read %d != expected %d",
301 ((unsigned short *)row_ref)[k],
302 (unsigned short)values[j].v_field.v_double);
304 ((unsigned short *)row_ref)[k] = (unsigned short)
305 values[j].v_field.v_double;
308 if (ca[i].ca_flags & NSC_CONST) {
309 if (((unsigned char *)row_ref)[k] != (unsigned char)
310 values[j].v_field.v_double)
311 gripe("Field %s must be same, "
312 "read %d != expected %d",
314 ((unsigned char *)row_ref)[k],
315 (unsigned char)values[j].v_field.v_double);
317 ((unsigned char *)row_ref)[k] = (unsigned char)
318 values[j].v_field.v_double;
321 if (ca[i].ca_flags & NSC_CONST) {
322 if (((float *)row_ref)[k] != (float)
323 values[j].v_field.v_double)
324 gripe("Field %s must be same, "
325 "read %g != expected %g",
327 ((float *)row_ref)[k],
328 (float)values[j].v_field.v_double);
330 ((float *)row_ref)[k] = (float)
331 values[j].v_field.v_double;
334 return gripe("Field %s is a string type, "
335 "but %lg was read which is a number",
336 ca[i].ca_name, values[j].v_field.v_double);
338 return gripe("Field %s's type %d is not supported",
339 ca[i].ca_name, ca[i].ca_type);
343 switch(ca[i].ca_type) {
345 return gripe("Field %s is of NSC_STRINGY type "
346 "which is not supported", ca[i].ca_name);
348 if (ca[i].ca_flags & NSC_CONST) {
349 if (strcmp(((char **)row_ref)[k],
350 values[j].v_field.v_string) != 0)
351 gripe("Field %s must be same, "
352 "read %s != expected %s",
354 *((char **)row_ref)[k],
355 *values[j].v_field.v_string);
357 ((char **)row_ref)[k] = values[j].v_field.v_string;
364 return gripe("Field %s is a number type %d, "
365 "but %s was read which is a string",
366 ca[i].ca_name, ca[i].ca_type,
367 values[j].v_field.v_string);
369 return gripe("Field %s's type %d is not supported",
370 ca[i].ca_name, ca[i].ca_type);
374 return gripe("Missing column %s in file", ca[i].ca_name);
376 return gripe("Unknown value type %d", values[j].v_type);
380 } while (k < ca[i].ca_len);
383 if (ca[i].ca_type != NSC_NOTYPE)
384 return gripe("Missing column %s in file", ca[i].ca_name);
385 switch (values[j].v_type) {
390 return gripe("Extra junk after the last column, read %s",
391 values[j].v_field.v_string);
393 return gripe("Extra junk after the last column, read %lg",
394 values[j].v_field.v_double);
396 return gripe("Extra junk after the last column, "
397 "unknown value type %d", values[j].v_type);
403 xundump(FILE *fp, char *file, int expected_table)
407 int row, res, rows, ch;
408 struct value values[MAX_NUM_COLUMNS + 1];
412 if (strcmp(fname, file) != 0) {
418 if (fscanf(fp, "XDUMP %63[^0123456789]%*d%c", name, &sep) != 2)
419 return gripe("Expected XDUMP header");
421 return gripe("Junk after XDUMP header");
423 if (strlen(name) < 2)
424 return gripe("Invalid table name in header %s", name);
425 if (name[strlen(name) - 1] != ' ')
426 return gripe("Missing space after table name in header %s",
428 name[strlen(name) - 1] = '\0';
430 type = ef_byname(name);
432 return gripe("Table not found %s", name);
434 if (expected_table != EF_BAD && expected_table != type)
435 return gripe("Incorrect Table expecting %s got %s",
436 ef_nameof(expected_table), name);
438 fixed_rows = has_const(ef_cadef(type));
440 for (row = 0; ; row++) {
445 return gripe("Unexpected EOF");
450 * Add column count check to the return value of xuflds()
452 res = xuflds(fp, values);
453 if (res > 0 && row >= empfile[type].csize - 1) {
454 gripe("Too many rows for table %s", name);
458 empfile[type].fids = row + 1;
460 xuinitrow(type, row);
461 res = xuloadrow(type, row, values);
468 if (fscanf(fp, "/%d%c", &rows, &sep) != 2)
469 return gripe("Failed to find number of rows trailer");
471 return gripe("Number of rows doesn't match between "
472 "the trailer and what was read");
473 if (fixed_rows && row != empfile[type].csize -1)
474 return gripe("Number of rows doesn't match, and "
475 "it must for table %s", name);
477 return gripe("Junk after number of rows trailer");
480 xuinitrow(type, row);