]> git.pond.sub.org Git - empserver/blob - src/lib/common/xundump.c
(xundump): Table names no longer contain whitespace. Simplify.
[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: Text loading functions based on xdump output
29  * 
30  *  Known contributors to this file:
31  *     Ron Koenderink, 2005
32  *  
33  */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37
38 #include <ctype.h>
39 #include <string.h>
40 #include <stdarg.h>
41 #include <time.h>
42
43 #include "prototypes.h"
44 #include "file.h"
45 #include "nsc.h"
46 #include "match.h"
47
48 #define MAX_NUM_COLUMNS 256
49
50 static char *fname = "";
51 static int lineno = 0;
52
53 /*
54  * TODO
55  * This structure could be replaced with struct valstr.
56  */
57 enum enum_value {
58     VAL_NOTUSED,
59     VAL_STRING,
60     VAL_SYMBOL,
61     VAL_DOUBLE
62 };
63
64 struct value {
65     enum enum_value v_type;
66     union {
67         char *v_string;
68         double v_double;
69     } v_field;
70 };
71
72 static int
73 gripe(char *fmt, ...)
74 {
75     va_list ap;
76
77     fprintf(stderr, "%s:%d: ", fname, lineno);
78     va_start(ap, fmt);
79     vfprintf(stderr, fmt, ap);
80     va_end(ap);
81     putc('\n', stderr);
82
83     return -1;
84 }
85
86 static char *
87 xuesc(char *buf)
88 {
89     char *src, *dst;
90     int octal_chr, n;
91
92     dst = buf;
93     src = buf;
94     while (*src) {
95         if (*src == '\\') {
96             if (sscanf(++src, "%3o%n", &octal_chr, &n) != 1 || n != 3)
97                 return NULL;
98             *dst++ = (char)octal_chr;
99             src += 3;
100         } else
101             *dst++ = *src++;
102     }
103     *dst = '\0';
104     return buf;
105 }
106
107 static int
108 xuflds(FILE *fp, struct value values[])
109 {
110     int i, ch;
111     char sep;
112     char buf[1024];
113
114     for (i = 0; i < MAX_NUM_COLUMNS; i++) {
115         values[i].v_type = VAL_NOTUSED;
116         ch = getc(fp);
117         ungetc(ch, fp);
118
119         switch (ch) {
120         case EOF:
121             return gripe("Unexpected EOF");
122         case '+': case '-': case '.':
123         case '0': case '1': case '2': case '3': case '4':
124         case '5': case '6': case '7': case '8': case '9':
125             if (fscanf(fp, "%lg%c", &values[i].v_field.v_double, &sep) != 2)
126                 return gripe("Malformed number in field %d", i + 1);
127             values[i].v_type = VAL_DOUBLE;
128             break;
129         case '"':
130             if (fscanf(fp, "\"%1023[^ \n]%c", buf, &sep) != 2
131                 || buf[strlen(buf)-1] != '"')
132                 return gripe("Malformed string in field %d", i + 1);
133             buf[strlen(buf)-1] = '\0';
134             if (!xuesc(buf))
135                 return gripe("Invalid escape sequence in field %d",
136                     i + 1);
137             values[i].v_type = VAL_STRING;
138             values[i].v_field.v_string = strdup(buf);
139             break;
140         default:
141             if (fscanf(fp, "%1023[^ \n]%c", buf, &sep) != 2) {
142                 return gripe("Junk in field %d", i + 1);
143             }
144             if (!strcmp(buf, "nil")) {
145                 values[i].v_type = VAL_STRING;
146                 values[i].v_field.v_string = NULL;
147             }
148             else {
149                 values[i].v_type = VAL_SYMBOL;
150                 values[i].v_field.v_string = strdup(buf);
151             }
152         }
153         if (sep == '\n')
154             break;
155         if (sep != ' ')
156             return gripe(
157                 "Expected space or newline as field separator found %c",
158                 sep);
159     }
160     if (i >= MAX_NUM_COLUMNS)
161         return gripe("Too many columns");
162     if (i == 0)
163         return gripe("No columns read");
164     values[++i].v_type = VAL_NOTUSED;
165     return i;
166 }
167
168 static void
169 freeflds(struct value values[])
170 {
171     struct value *vp;
172
173     for (vp = values; vp->v_type != VAL_NOTUSED; vp++) {
174         if (vp->v_type != VAL_DOUBLE)
175             free(vp->v_field.v_string);
176     }
177 }
178
179 static int
180 xunsymbol(struct castr *ca, char *buf)
181 {
182     struct symbol *symbol = (struct symbol *)empfile[ca->ca_table].cache;
183     int i;
184     int value = 0;
185     char *token;
186
187     if (ca->ca_flags & NSC_BITS)
188         token = strtok( buf, "|");
189     else
190         token = buf;
191
192     if (!token || token[0] == '\0')
193         return gripe("Empty symbol value for field %s", ca->ca_name);
194
195     while (token) {
196         if ((i = stmtch(token, symbol, offsetof(struct symbol, name),
197                         sizeof(struct symbol))) != M_NOTFOUND) {
198             if (!(ca->ca_flags & NSC_BITS))
199                 return(symbol[i].value);
200             value |= symbol[i].value;
201         }
202         else
203             return gripe("Symbol %s was not found for field %s", token,
204                 ca->ca_name);
205         token = strtok(NULL, "|");
206     }
207     return(value);
208 }
209
210 static int
211 has_const(struct castr ca[])
212 {
213     int i;
214
215     for (i = 0; ca[i].ca_name; i++) {
216         if (ca[i].ca_flags & NSC_CONST)
217             return 1;
218     }
219     return 0;
220 }
221
222 static void
223 xuinitrow(int type, int row)
224 {
225     struct empfile *ep = &empfile[type];
226     char *ptr = ep->cache + ep->size * row;
227
228     memset(ptr, 0, ep->size);
229
230     if (ep->init)
231         ep->init(row, ptr);
232 }
233
234 static int
235 xuloadrow(int type, int row, struct value values[])
236 {
237     int i,j,k;
238     struct empfile *ep = &empfile[type];
239     char *ptr = ep->cache + ep->size * row;
240     struct castr *ca = ep->cadef;
241     void *row_ref;
242
243     i = 0;
244     j = 0;
245     while (ca[i].ca_type != NSC_NOTYPE &&
246            values[j].v_type != VAL_NOTUSED) {
247         row_ref = (char *)ptr + ca[i].ca_off;
248         k = 0;
249         do {
250             /*
251              * TODO
252              * factor out NSC_CONST comparsion
253              */
254             switch (values[j].v_type) {
255             case VAL_SYMBOL:
256                 if (ca[i].ca_table == EF_BAD)
257                     return(gripe("Found symbol string %s, but column %s "
258                         "is not symbol or symbol sets",
259                         values[j].v_field.v_string, ca[i].ca_name));
260                 values[j].v_field.v_double =
261                     (double)xunsymbol(&ca[i], values[j].v_field.v_string);
262                 if (values[j].v_field.v_double < 0.0)
263                     return -1;
264                 /*
265                  * fall through
266                  */
267             case VAL_DOUBLE:
268                 switch (ca[i].ca_type) {
269                 case NSC_INT:
270                     if (ca[i].ca_flags & NSC_CONST) {
271                         if (((int *)row_ref)[k] != (int)
272                              values[j].v_field.v_double)
273                             gripe("Field %s must be same, "
274                                 "read %d != expected %d",
275                                 ca[i].ca_name,
276                                 ((int *)row_ref)[k],
277                                 (int)values[j].v_field.v_double);
278
279                     } else
280                         ((int *)row_ref)[k] =
281                             (int)values[j].v_field.v_double;
282                     break;
283                 case NSC_LONG:
284                     if (ca[i].ca_flags & NSC_CONST) {
285                         if (((long *)row_ref)[k] != (long)
286                              values[j].v_field.v_double)
287                             gripe("Field %s must be same, "
288                                 "read %ld != expected %ld",
289                                 ca[i].ca_name,
290                                 ((long *)row_ref)[k],
291                                 (long)values[j].v_field.v_double);
292                     } else
293                         ((long *)row_ref)[k] = (long)
294                             values[j].v_field.v_double;
295                     break;
296                 case NSC_USHORT:
297                     if (ca[i].ca_flags & NSC_CONST) {
298                         if (((unsigned short *)row_ref)[k] !=
299                              (unsigned short)values[j].v_field.v_double)
300                             gripe("Field %s must be same, "
301                                 "read %d != expected %d",
302                                 ca[i].ca_name,
303                                 ((unsigned short *)row_ref)[k],
304                                 (unsigned short)values[j].v_field.v_double);
305                     } else
306                         ((unsigned short *)row_ref)[k] = (unsigned short)
307                             values[j].v_field.v_double;
308                     break;
309                 case NSC_UCHAR:
310                     if (ca[i].ca_flags & NSC_CONST) {
311                         if (((unsigned char *)row_ref)[k] != (unsigned char)
312                              values[j].v_field.v_double)
313                             gripe("Field %s must be same, "
314                                 "read %d != expected %d",
315                                 ca[i].ca_name,
316                                 ((unsigned char *)row_ref)[k],
317                                 (unsigned char)values[j].v_field.v_double);
318                     } else
319                         ((unsigned char *)row_ref)[k] = (unsigned char)
320                             values[j].v_field.v_double;
321                     break;
322                 case NSC_FLOAT:
323                     if (ca[i].ca_flags & NSC_CONST) {
324                         if (((float *)row_ref)[k] != (float)
325                              values[j].v_field.v_double)
326                             gripe("Field %s must be same, "
327                                 "read %g != expected %g",
328                                 ca[i].ca_name,
329                                 ((float *)row_ref)[k],
330                                 (float)values[j].v_field.v_double);
331                     } else
332                         ((float *)row_ref)[k] = (float)
333                             values[j].v_field.v_double;
334                     break;
335                 case NSC_STRING:
336                     return gripe("Field %s is a string type, "
337                         "but %lg was read which is a number",
338                         ca[i].ca_name, values[j].v_field.v_double);
339                 default:
340                     return gripe("Field %s's type %d is not supported",
341                         ca[i].ca_name, ca[i].ca_type);
342                 }
343                 break;
344             case VAL_STRING:
345                 switch(ca[i].ca_type) {
346                 case NSC_STRINGY:
347                     return gripe("Field %s is of NSC_STRINGY type "
348                         "which is not supported", ca[i].ca_name);
349                 case NSC_STRING:
350                     if (ca[i].ca_flags & NSC_CONST) {
351                         if (strcmp(((char **)row_ref)[k],
352                                    values[j].v_field.v_string) != 0)
353                             gripe("Field %s must be same, "
354                                 "read %s != expected %s",
355                                 ca[i].ca_name,
356                                 *((char **)row_ref)[k],
357                                 *values[j].v_field.v_string);
358                     } else
359                         ((char **)row_ref)[k] = 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 %63s %*d%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("Table not found %s", name);
428
429     if (expected_table != EF_BAD && expected_table != type)
430         return gripe("Incorrect Table expecting %s got %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         ungetc(ch, fp);
439         if (ch == EOF)
440             return gripe("Unexpected EOF");
441         if (ch == '/')
442             break;
443         /*
444          * TODO
445          * Add column count check to the return value of xuflds()
446          */
447         res = xuflds(fp, values);
448         if (res > 0 && row >= empfile[type].csize - 1) {
449             gripe("Too many rows for table %s", name);
450             res = -1;
451         }
452         if (res > 0) {
453             empfile[type].fids = row + 1;
454             if (!fixed_rows)
455                 xuinitrow(type, row);
456             res = xuloadrow(type, row, values);
457         }
458         freeflds(values);
459         if (res < 0)
460             return -1;
461     }
462
463     if (fscanf(fp, "/%d%c", &rows, &sep) != 2)
464         return gripe("Failed to find number of rows trailer");
465     if (row != rows)
466         return gripe("Number of rows doesn't match between "
467             "the trailer and what was read");
468     if (fixed_rows && row != empfile[type].csize -1)
469         return gripe("Number of rows doesn't match, and "
470             "it must for table %s", name);
471     if (sep != '\n')
472         return gripe("Junk after number of rows trailer");
473
474     if (!fixed_rows)
475         xuinitrow(type, row);
476
477     return 0;
478 }