]> git.pond.sub.org Git - empserver/blob - src/lib/common/xundump.c
(xuloadrow): Fix rev. 1.11.
[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 char *
89 xuesc(char *buf)
90 {
91     char *src, *dst;
92     int octal_chr, n;
93
94     dst = buf;
95     src = buf;
96     while (*src) {
97         if (*src == '\\') {
98             if (sscanf(++src, "%3o%n", &octal_chr, &n) != 1 || n != 3)
99                 return NULL;
100             *dst++ = (char)octal_chr;
101             src += 3;
102         } else
103             *dst++ = *src++;
104     }
105     *dst = '\0';
106     return buf;
107 }
108
109 static int
110 xuflds(FILE *fp, struct value values[])
111 {
112     int i, ch;
113     char buf[1024];
114
115     for (i = 0; ; i++) {
116         values[i].v_type = VAL_NOTUSED;
117         if (i >= MAX_NUM_COLUMNS)
118             return gripe("Too many columns");
119
120         ch = getc(fp);
121         switch (ch) {
122         case EOF:
123             return gripe("Unexpected EOF");
124         case '\n':
125             return i;
126         case '+': case '-': case '.':
127         case '0': case '1': case '2': case '3': case '4':
128         case '5': case '6': case '7': case '8': case '9':
129             ungetc(ch, fp);
130             if (fscanf(fp, "%lg", &values[i].v_field.v_double) != 1)
131                 return gripe("Malformed number in field %d", i + 1);
132             values[i].v_type = VAL_DOUBLE;
133             break;
134         case '"':
135             if (fscanf(fp, "%1023[^ \n]", buf) != 1
136                 || buf[strlen(buf)-1] != '"')
137                 return gripe("Malformed string in field %d", i + 1);
138             buf[strlen(buf)-1] = '\0';
139             if (!xuesc(buf))
140                 return gripe("Invalid escape sequence in field %d",
141                     i + 1);
142             values[i].v_type = VAL_STRING;
143             values[i].v_field.v_string = strdup(buf);
144             break;
145         default:
146             ungetc(ch, fp);
147             if (fscanf(fp, "%1023[^ \n]", buf) != 1 || !isalpha(buf[0])) {
148                 return gripe("Junk in field %d", i + 1);
149             }
150             if (!strcmp(buf, "nil")) {
151                 values[i].v_type = VAL_STRING;
152                 values[i].v_field.v_string = NULL;
153             }
154             else {
155                 values[i].v_type = VAL_SYMBOL;
156                 values[i].v_field.v_string = strdup(buf);
157             }
158         }
159         ch = getc(fp);
160         if (ch == '\n')
161             ungetc(ch, fp);
162         else if (ch != ' ')
163             return gripe("Bad field separator after field %d", i + 1);
164     }
165 }
166
167 static void
168 freeflds(struct value values[])
169 {
170     struct value *vp;
171
172     for (vp = values; vp->v_type != VAL_NOTUSED; vp++) {
173         if (vp->v_type != VAL_DOUBLE)
174             free(vp->v_field.v_string);
175     }
176 }
177
178 static int
179 xunsymbol(struct castr *ca, char *buf)
180 {
181     struct symbol *symbol = (struct symbol *)empfile[ca->ca_table].cache;
182     int i;
183     int value = 0;
184     char *token;
185
186     if (ca->ca_flags & NSC_BITS)
187         token = strtok( buf, "|");
188     else
189         token = buf;
190
191     if (!token || token[0] == '\0')
192         return gripe("Empty symbol value for field %s", ca->ca_name);
193
194     while (token) {
195         if ((i = stmtch(token, symbol, offsetof(struct symbol, name),
196                         sizeof(struct symbol))) != M_NOTFOUND) {
197             if (!(ca->ca_flags & NSC_BITS))
198                 return(symbol[i].value);
199             value |= symbol[i].value;
200         }
201         else
202             return gripe("Symbol %s was not found for field %s", token,
203                 ca->ca_name);
204         token = strtok(NULL, "|");
205     }
206     return(value);
207 }
208
209 static int
210 has_const(struct castr ca[])
211 {
212     int i;
213
214     for (i = 0; ca[i].ca_name; i++) {
215         if (ca[i].ca_flags & NSC_CONST)
216             return 1;
217     }
218     return 0;
219 }
220
221 static void
222 xuinitrow(int type, int row)
223 {
224     struct empfile *ep = &empfile[type];
225     char *ptr = ep->cache + ep->size * row;
226
227     memset(ptr, 0, ep->size);
228
229     if (ep->init)
230         ep->init(row, ptr);
231 }
232
233 static int
234 xuloadrow(int type, int row, struct value values[])
235 {
236     int i,j,k;
237     struct empfile *ep = &empfile[type];
238     char *ptr = ep->cache + ep->size * row;
239     struct castr *ca = ep->cadef;
240     void *row_ref;
241
242     i = 0;
243     j = 0;
244     while (ca[i].ca_type != NSC_NOTYPE &&
245            values[j].v_type != VAL_NOTUSED) {
246         row_ref = (char *)ptr + ca[i].ca_off;
247         k = 0;
248         do {
249             /*
250              * TODO
251              * factor out NSC_CONST comparsion
252              */
253             switch (values[j].v_type) {
254             case VAL_SYMBOL:
255                 if (ca[i].ca_table == EF_BAD)
256                     return(gripe("Found symbol string %s, but column %s "
257                         "is not symbol or symbol sets",
258                         values[j].v_field.v_string, ca[i].ca_name));
259                 values[j].v_field.v_double =
260                     (double)xunsymbol(&ca[i], values[j].v_field.v_string);
261                 if (values[j].v_field.v_double < 0.0)
262                     return -1;
263                 /*
264                  * fall through
265                  */
266             case VAL_DOUBLE:
267                 switch (ca[i].ca_type) {
268                 case NSC_INT:
269                     if (ca[i].ca_flags & NSC_CONST) {
270                         if (((int *)row_ref)[k] != (int)
271                              values[j].v_field.v_double)
272                             gripe("Field %s must be same, "
273                                 "read %d != expected %d",
274                                 ca[i].ca_name,
275                                 ((int *)row_ref)[k],
276                                 (int)values[j].v_field.v_double);
277
278                     } else
279                         ((int *)row_ref)[k] =
280                             (int)values[j].v_field.v_double;
281                     break;
282                 case NSC_LONG:
283                     if (ca[i].ca_flags & NSC_CONST) {
284                         if (((long *)row_ref)[k] != (long)
285                              values[j].v_field.v_double)
286                             gripe("Field %s must be same, "
287                                 "read %ld != expected %ld",
288                                 ca[i].ca_name,
289                                 ((long *)row_ref)[k],
290                                 (long)values[j].v_field.v_double);
291                     } else
292                         ((long *)row_ref)[k] = (long)
293                             values[j].v_field.v_double;
294                     break;
295                 case NSC_USHORT:
296                     if (ca[i].ca_flags & NSC_CONST) {
297                         if (((unsigned short *)row_ref)[k] !=
298                              (unsigned short)values[j].v_field.v_double)
299                             gripe("Field %s must be same, "
300                                 "read %d != expected %d",
301                                 ca[i].ca_name,
302                                 ((unsigned short *)row_ref)[k],
303                                 (unsigned short)values[j].v_field.v_double);
304                     } else
305                         ((unsigned short *)row_ref)[k] = (unsigned short)
306                             values[j].v_field.v_double;
307                     break;
308                 case NSC_UCHAR:
309                     if (ca[i].ca_flags & NSC_CONST) {
310                         if (((unsigned char *)row_ref)[k] != (unsigned char)
311                              values[j].v_field.v_double)
312                             gripe("Field %s must be same, "
313                                 "read %d != expected %d",
314                                 ca[i].ca_name,
315                                 ((unsigned char *)row_ref)[k],
316                                 (unsigned char)values[j].v_field.v_double);
317                     } else
318                         ((unsigned char *)row_ref)[k] = (unsigned char)
319                             values[j].v_field.v_double;
320                     break;
321                 case NSC_FLOAT:
322                     if (ca[i].ca_flags & NSC_CONST) {
323                         if (((float *)row_ref)[k] != (float)
324                              values[j].v_field.v_double)
325                             gripe("Field %s must be same, "
326                                 "read %g != expected %g",
327                                 ca[i].ca_name,
328                                 ((float *)row_ref)[k],
329                                 (float)values[j].v_field.v_double);
330                     } else
331                         ((float *)row_ref)[k] = (float)
332                             values[j].v_field.v_double;
333                     break;
334                 case NSC_STRING:
335                     return gripe("Field %s is a string type, "
336                         "but %lg was read which is a number",
337                         ca[i].ca_name, values[j].v_field.v_double);
338                 default:
339                     return gripe("Field %s's type %d is not supported",
340                         ca[i].ca_name, ca[i].ca_type);
341                 }
342                 break;
343             case VAL_STRING:
344                 switch(ca[i].ca_type) {
345                 case NSC_STRINGY:
346                     return gripe("Field %s is of NSC_STRINGY type "
347                         "which is not supported", ca[i].ca_name);
348                 case NSC_STRING:
349                     if (ca[i].ca_flags & NSC_CONST) {
350                         if (strcmp(((char **)row_ref)[k],
351                                    values[j].v_field.v_string) != 0)
352                             gripe("Field %s must be same, "
353                                 "read %s != expected %s",
354                                 ca[i].ca_name,
355                                 *((char **)row_ref)[k],
356                                 *values[j].v_field.v_string);
357                     } else
358                         ((char **)row_ref)[k]
359                             = strdup(values[j].v_field.v_string);
360                     break;
361                 case NSC_INT:
362                 case NSC_LONG:
363                 case NSC_USHORT:
364                 case NSC_UCHAR:
365                 case NSC_FLOAT:
366                     return gripe("Field %s is a number type %d, "
367                         "but %s was read which is a string",
368                         ca[i].ca_name, ca[i].ca_type,
369                         values[j].v_field.v_string);
370                 default:
371                     return gripe("Field %s's type %d is not supported",
372                             ca[i].ca_name, ca[i].ca_type);
373                 }
374                 break;
375             case VAL_NOTUSED:
376                 return gripe("Missing column %s in file", ca[i].ca_name);
377             default:
378                 return gripe("Unknown value type %d", values[j].v_type);
379             }
380             k++;
381             j++;
382         } while (k < ca[i].ca_len);
383         i++;
384     }
385     if (ca[i].ca_type != NSC_NOTYPE)
386         return gripe("Missing column %s in file", ca[i].ca_name);
387     switch  (values[j].v_type) {
388     case VAL_NOTUSED:
389         break;
390     case VAL_STRING:
391     case VAL_SYMBOL:
392         return gripe("Extra junk after the last column, read %s",
393             values[j].v_field.v_string);
394     case VAL_DOUBLE:
395         return gripe("Extra junk after the last column, read %lg",
396             values[j].v_field.v_double);
397     default:
398         return gripe("Extra junk after the last column, "
399             "unknown value type %d", values[j].v_type);
400     }
401     return 0;
402 }
403
404 int
405 xundump(FILE *fp, char *file, int expected_table)
406 {
407     char name[64];
408     char sep;
409     int row, res, rows, ch;
410     struct value values[MAX_NUM_COLUMNS + 1];
411     int type;
412     int fixed_rows;
413
414     if (strcmp(fname, file) != 0) {
415         fname = file;
416         lineno = 1;
417     } else
418         lineno++;
419
420     if (fscanf(fp, "XDUMP%*1[ ]%63[^ \n]%*1[ ]%*[^ \n]%c", name, &sep) != 2)
421         return gripe("Expected XDUMP header");
422     if (sep != '\n')
423         return gripe("Junk after XDUMP header");
424
425     type = ef_byname(name);
426     if (type < 0)
427         return gripe("Unknown table `%s'", name);
428
429     if (expected_table != EF_BAD && expected_table != type)
430         return gripe("Expected table `%s', not `%s'",
431                      ef_nameof(expected_table), name);
432
433     fixed_rows = has_const(ef_cadef(type));
434
435     for (row = 0; ; row++) {
436         lineno++;
437         ch = getc(fp);
438         if (ch == '/')
439             break;
440         ungetc(ch, fp);
441         /*
442          * TODO
443          * Add column count check to the return value of xuflds()
444          */
445         res = xuflds(fp, values);
446         if (res > 0 && row >= empfile[type].csize - 1) {
447             gripe("Too many rows for table %s", name);
448             res = -1;
449         }
450         if (res > 0) {
451             empfile[type].fids = row + 1;
452             if (!fixed_rows)
453                 xuinitrow(type, row);
454             res = xuloadrow(type, row, values);
455         }
456         freeflds(values);
457         if (res < 0)
458             return -1;
459     }
460
461     ch = getc(fp);
462     ungetc(ch, fp);
463     if (!isdigit(ch) || fscanf(fp, "%d%c", &rows, &sep) != 2)
464         return gripe("Malformed table footer");
465     if (row != rows)
466         return gripe("Read %d rows, which doesn't match footer",
467                      row);
468     if (fixed_rows && row != empfile[type].csize -1)
469         return gripe("Table %s requires %d rows, got %d",
470                      name, empfile[type].csize - 1, row);
471     if (sep != '\n')
472         return gripe("Junk after table footer");
473
474     if (!fixed_rows)
475         xuinitrow(type, row);
476
477     return 0;
478 }