]> git.pond.sub.org Git - empserver/blob - src/lib/common/xundump.c
(xuloadrow): Fix crash.
[empserver] / src / lib / common / xundump.c
1 /*
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
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  xundump.c: Load back xdump output
29  * 
30  *  Known contributors to this file:
31  *     Ron Koenderink, 2005
32  *     Markus Armbruster, 2005
33  */
34
35 /* FIXME normalize terminology: table/rows/columns or file/records/fields */
36
37 #include <stdio.h>
38 #include <stdlib.h>
39
40 #include <ctype.h>
41 #include <string.h>
42 #include <stdarg.h>
43 #include <time.h>
44
45 #include "prototypes.h"
46 #include "file.h"
47 #include "nsc.h"
48 #include "match.h"
49
50 #define MAX_NUM_COLUMNS 256
51
52 static char *fname = "";
53 static int lineno = 0;
54
55 /*
56  * TODO
57  * This structure could be replaced with struct valstr.
58  */
59 enum enum_value {
60     VAL_NOTUSED,
61     VAL_STRING,
62     VAL_SYMBOL,
63     VAL_DOUBLE
64 };
65
66 struct value {
67     enum enum_value v_type;
68     union {
69         char *v_string;
70         double v_double;
71     } v_field;
72 };
73
74 static int
75 gripe(char *fmt, ...)
76 {
77     va_list ap;
78
79     fprintf(stderr, "%s:%d: ", fname, lineno);
80     va_start(ap, fmt);
81     vfprintf(stderr, fmt, ap);
82     va_end(ap);
83     putc('\n', stderr);
84
85     return -1;
86 }
87
88 static int
89 skipfs(FILE *fp)
90 {
91     int ch;
92
93     do {
94         ch = getc(fp);
95     } while (ch == ' ' || ch == '\t');
96
97     if (ch == '#') {
98         do {
99             ch = getc(fp);
100         } while (ch != EOF && ch != '\n');
101     }
102
103     return ch;
104 }
105
106 static char *
107 xuesc(char *buf)
108 {
109     char *src, *dst;
110     int octal_chr, n;
111
112     dst = buf;
113     src = buf;
114     while (*src) {
115         if (*src == '\\') {
116             if (sscanf(++src, "%3o%n", &octal_chr, &n) != 1 || n != 3)
117                 return NULL;
118             *dst++ = (char)octal_chr;
119             src += 3;
120         } else
121             *dst++ = *src++;
122     }
123     *dst = '\0';
124     return buf;
125 }
126
127 static int
128 xuflds(FILE *fp, struct value values[])
129 {
130     int i, ch;
131     char buf[1024];
132
133     for (i = 0; ; i++) {
134         values[i].v_type = VAL_NOTUSED;
135         if (i >= MAX_NUM_COLUMNS)
136             return gripe("Too many columns");
137
138         ch = skipfs(fp);
139         switch (ch) {
140         case EOF:
141             return gripe("Unexpected EOF");
142         case '\n':
143             return i;
144         case '+': case '-': case '.':
145         case '0': case '1': case '2': case '3': case '4':
146         case '5': case '6': case '7': case '8': case '9':
147             ungetc(ch, fp);
148             if (fscanf(fp, "%lg", &values[i].v_field.v_double) != 1)
149                 return gripe("Malformed number in field %d", i + 1);
150             values[i].v_type = VAL_DOUBLE;
151             break;
152         case '"':
153             if (fscanf(fp, "%1023[^ \t#\n]", buf) != 1
154                 || buf[strlen(buf)-1] != '"')
155                 return gripe("Malformed string in field %d", i + 1);
156             buf[strlen(buf)-1] = '\0';
157             if (!xuesc(buf))
158                 return gripe("Invalid escape sequence in field %d",
159                     i + 1);
160             values[i].v_type = VAL_STRING;
161             values[i].v_field.v_string = strdup(buf);
162             break;
163         default:
164             ungetc(ch, fp);
165             if (fscanf(fp, "%1023[^ \t#\n]", buf) != 1 || !isalpha(buf[0])) {
166                 return gripe("Junk in field %d", i + 1);
167             }
168             if (!strcmp(buf, "nil")) {
169                 values[i].v_type = VAL_STRING;
170                 values[i].v_field.v_string = NULL;
171             }
172             else {
173                 values[i].v_type = VAL_SYMBOL;
174                 values[i].v_field.v_string = strdup(buf);
175             }
176         }
177         ch = getc(fp);
178         if (ch == '\n')
179             ungetc(ch, fp);
180         else if (ch != ' ' && ch != '\t')
181             return gripe("Bad field separator after field %d", i + 1);
182     }
183 }
184
185 static void
186 freeflds(struct value values[])
187 {
188     struct value *vp;
189
190     for (vp = values; vp->v_type != VAL_NOTUSED; vp++) {
191         if (vp->v_type != VAL_DOUBLE)
192             free(vp->v_field.v_string);
193     }
194 }
195
196 static int
197 xunsymbol(struct castr *ca, char *buf)
198 {
199     struct symbol *symbol = (struct symbol *)empfile[ca->ca_table].cache;
200     int i;
201     int value = 0;
202     char *token;
203
204     if (ca->ca_flags & NSC_BITS)
205         token = strtok( buf, "|");
206     else
207         token = buf;
208
209     if (!token || token[0] == '\0')
210         return gripe("Empty symbol value for field %s", ca->ca_name);
211
212     while (token) {
213         if ((i = stmtch(token, symbol, offsetof(struct symbol, name),
214                         sizeof(struct symbol))) != M_NOTFOUND) {
215             if (!(ca->ca_flags & NSC_BITS))
216                 return(symbol[i].value);
217             value |= symbol[i].value;
218         }
219         else
220             return gripe("Symbol %s was not found for field %s", token,
221                 ca->ca_name);
222         token = strtok(NULL, "|");
223     }
224     return(value);
225 }
226
227 static int
228 has_const(struct castr ca[])
229 {
230     int i;
231
232     for (i = 0; ca[i].ca_name; i++) {
233         if (ca[i].ca_flags & NSC_CONST)
234             return 1;
235     }
236     return 0;
237 }
238
239 static void
240 xuinitrow(int type, int row)
241 {
242     struct empfile *ep = &empfile[type];
243     char *ptr = ep->cache + ep->size * row;
244
245     memset(ptr, 0, ep->size);
246
247     if (ep->init)
248         ep->init(row, ptr);
249 }
250
251 static int
252 xuloadrow(int type, int row, struct value values[])
253 {
254     int i,j,k;
255     struct empfile *ep = &empfile[type];
256     char *ptr = ep->cache + ep->size * row;
257     struct castr *ca = ep->cadef;
258     void *row_ref;
259
260     i = 0;
261     j = 0;
262     while (ca[i].ca_type != NSC_NOTYPE &&
263            values[j].v_type != VAL_NOTUSED) {
264         if (ca[i].ca_flags & NSC_EXTRA) {
265             i++;
266             continue;
267         }
268         row_ref = (char *)ptr + ca[i].ca_off;
269         k = 0;
270         do {
271             /*
272              * TODO
273              * factor out NSC_CONST comparsion
274              */
275             switch (values[j].v_type) {
276             case VAL_SYMBOL:
277                 if (ca[i].ca_table == EF_BAD)
278                     return(gripe("Found symbol string %s, but column %s "
279                         "is not symbol or symbol sets",
280                         values[j].v_field.v_string, ca[i].ca_name));
281                 values[j].v_field.v_double =
282                     (double)xunsymbol(&ca[i], values[j].v_field.v_string);
283                 if (values[j].v_field.v_double < 0.0)
284                     return -1;
285                 /*
286                  * fall through
287                  */
288             case VAL_DOUBLE:
289                 switch (ca[i].ca_type) {
290                 case NSC_INT:
291                     if (ca[i].ca_flags & NSC_CONST) {
292                         if (((int *)row_ref)[k] != (int)
293                              values[j].v_field.v_double)
294                             gripe("Field %s must be same, "
295                                 "read %d != expected %d",
296                                 ca[i].ca_name,
297                                 ((int *)row_ref)[k],
298                                 (int)values[j].v_field.v_double);
299
300                     } else
301                         ((int *)row_ref)[k] =
302                             (int)values[j].v_field.v_double;
303                     break;
304                 case NSC_LONG:
305                     if (ca[i].ca_flags & NSC_CONST) {
306                         if (((long *)row_ref)[k] != (long)
307                              values[j].v_field.v_double)
308                             gripe("Field %s must be same, "
309                                 "read %ld != expected %ld",
310                                 ca[i].ca_name,
311                                 ((long *)row_ref)[k],
312                                 (long)values[j].v_field.v_double);
313                     } else
314                         ((long *)row_ref)[k] = (long)
315                             values[j].v_field.v_double;
316                     break;
317                 case NSC_USHORT:
318                     if (ca[i].ca_flags & NSC_CONST) {
319                         if (((unsigned short *)row_ref)[k] !=
320                              (unsigned short)values[j].v_field.v_double)
321                             gripe("Field %s must be same, "
322                                 "read %d != expected %d",
323                                 ca[i].ca_name,
324                                 ((unsigned short *)row_ref)[k],
325                                 (unsigned short)values[j].v_field.v_double);
326                     } else
327                         ((unsigned short *)row_ref)[k] = (unsigned short)
328                             values[j].v_field.v_double;
329                     break;
330                 case NSC_UCHAR:
331                     if (ca[i].ca_flags & NSC_CONST) {
332                         if (((unsigned char *)row_ref)[k] != (unsigned char)
333                              values[j].v_field.v_double)
334                             gripe("Field %s must be same, "
335                                 "read %d != expected %d",
336                                 ca[i].ca_name,
337                                 ((unsigned char *)row_ref)[k],
338                                 (unsigned char)values[j].v_field.v_double);
339                     } else
340                         ((unsigned char *)row_ref)[k] = (unsigned char)
341                             values[j].v_field.v_double;
342                     break;
343                 case NSC_FLOAT:
344                     if (ca[i].ca_flags & NSC_CONST) {
345                         if (((float *)row_ref)[k] != (float)
346                              values[j].v_field.v_double)
347                             gripe("Field %s must be same, "
348                                 "read %g != expected %g",
349                                 ca[i].ca_name,
350                                 ((float *)row_ref)[k],
351                                 (float)values[j].v_field.v_double);
352                     } else
353                         ((float *)row_ref)[k] = (float)
354                             values[j].v_field.v_double;
355                     break;
356                 case NSC_STRING:
357                     return gripe("Field %s is a string type, "
358                         "but %lg was read which is a number",
359                         ca[i].ca_name, values[j].v_field.v_double);
360                 default:
361                     return gripe("Field %s's type %d is not supported",
362                         ca[i].ca_name, ca[i].ca_type);
363                 }
364                 break;
365             case VAL_STRING:
366                 switch(ca[i].ca_type) {
367                 case NSC_STRING:
368                     if (ca[i].ca_flags & NSC_CONST) {
369                         if (strcmp(((char **)row_ref)[k],
370                                    values[j].v_field.v_string) != 0)
371                             gripe("Field %s must be same, "
372                                 "read %s != expected %s",
373                                 ca[i].ca_name,
374                                 ((char **)row_ref)[k],
375                                 values[j].v_field.v_string);
376                     } else
377                         ((char **)row_ref)[k]
378                             = strdup(values[j].v_field.v_string);
379                     break;
380                 case NSC_INT:
381                 case NSC_LONG:
382                 case NSC_USHORT:
383                 case NSC_UCHAR:
384                 case NSC_FLOAT:
385                     return gripe("Field %s is a number type %d, "
386                         "but %s was read which is a string",
387                         ca[i].ca_name, ca[i].ca_type,
388                         values[j].v_field.v_string);
389                 default:
390                     return gripe("Field %s's type %d is not supported",
391                             ca[i].ca_name, ca[i].ca_type);
392                 }
393                 break;
394             case VAL_NOTUSED:
395                 return gripe("Missing column %s in file", ca[i].ca_name);
396             default:
397                 return gripe("Unknown value type %d", values[j].v_type);
398             }
399             k++;
400             j++;
401         } while (k < ca[i].ca_len);
402         i++;
403     }
404     if (ca[i].ca_type != NSC_NOTYPE)
405         return gripe("Missing column %s in file", ca[i].ca_name);
406     switch  (values[j].v_type) {
407     case VAL_NOTUSED:
408         break;
409     case VAL_STRING:
410     case VAL_SYMBOL:
411         return gripe("Extra junk after the last column, read %s",
412             values[j].v_field.v_string);
413     case VAL_DOUBLE:
414         return gripe("Extra junk after the last column, read %lg",
415             values[j].v_field.v_double);
416     default:
417         return gripe("Extra junk after the last column, "
418             "unknown value type %d", values[j].v_type);
419     }
420     return 0;
421 }
422
423 int
424 xundump(FILE *fp, char *file, int expected_table)
425 {
426     char name[64];
427     struct empfile *ep;
428     int row, res, rows, ch;
429     struct value values[MAX_NUM_COLUMNS + 1];
430     int type;
431     int fixed_rows, need_sentinel;
432
433     if (strcmp(fname, file) != 0) {
434         fname = file;
435         lineno = 1;
436     } else
437         lineno++;
438
439     while ((ch = skipfs(fp)) == '\n')
440         lineno++;
441     if (ch == EOF && expected_table == EF_BAD)
442         return -1;
443     ungetc(ch, fp);
444
445     if (fscanf(fp, "XDUMP%*[ \t]%63[^ \t#\n]%*[ \t]%*[^ \t#\n]", name) != 1)
446         return gripe("Expected XDUMP header");
447     if (skipfs(fp) != '\n')
448         return gripe("Junk after XDUMP header");
449
450     type = ef_byname(name);
451     if (type < 0)
452         return gripe("Unknown table `%s'", name);
453     ep = &empfile[type];
454     if (CANT_HAPPEN(!(ep->flags & EFF_MEM)))
455         return -1;              /* not implemented */
456
457     if (expected_table != EF_BAD && expected_table != type)
458         return gripe("Expected table `%s', not `%s'",
459                      ef_nameof(expected_table), name);
460
461     fixed_rows = has_const(ef_cadef(type));
462     need_sentinel = !fixed_rows; /* FIXME only approximation */
463
464     for (row = 0; ; row++) {
465         lineno++;
466         ch = skipfs(fp);
467         if (ch == '/')
468             break;
469         ungetc(ch, fp);
470         /*
471          * TODO
472          * Add column count check to the return value of xuflds()
473          */
474         res = xuflds(fp, values);
475         if (res > 0 && row >= ep->csize - 1) {
476             /* TODO grow cache unless EFF_STATIC */
477             gripe("Too many rows for table %s", name);
478             res = -1;
479         }
480         if (res > 0) {
481             if (!fixed_rows)
482                 xuinitrow(type, row);
483             res = xuloadrow(type, row, values);
484         }
485         freeflds(values);
486         if (res < 0)
487             return -1;
488     }
489
490     ch = getc(fp);
491     ungetc(ch, fp);
492     if (!isdigit(ch) || fscanf(fp, "%d", &rows) != 1)
493         return gripe("Malformed table footer");
494     if (skipfs(fp) != '\n')
495         return gripe("Junk after table footer");
496     if (row != rows)
497         return gripe("Read %d rows, which doesn't match footer",
498                      row);
499     if (fixed_rows && row != ep->csize -1)
500         return gripe("Table %s requires %d rows, got %d",
501                      name, ep->csize - 1, row);
502     if (need_sentinel)
503         xuinitrow(type, row);
504
505     ep->fids = ep->cids = row;
506     return type;
507 }