]> git.pond.sub.org Git - empserver/blob - src/lib/common/xundump.c
(xuskipcommentlines): Include the lines skipped in the line count.
[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         ch = getc(fp);
116         ungetc(ch, fp);
117
118         switch (ch) {
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;
125             break;
126         case '"':
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';
131             if (!xuesc(buf))
132                 return gripe("Invalid escape sequence in field %d",
133                     i + 1);
134             values[i].v_type = VAL_STRING;
135             values[i].v_field.v_string = strdup(buf);
136             break;
137         default:
138             if (fscanf(fp, "%1023[^ \n]%c", buf, &sep) != 2) {
139                 return gripe("Junk in field %d", i + 1);
140             }
141             if (!strcmp(buf, "nil")) {
142                 values[i].v_field.v_string = NULL;
143                 values[i].v_type = VAL_STRING;
144             }
145             else {
146                 values[i].v_field.v_string = strdup(buf);
147                 values[i].v_type = VAL_SYMBOL;
148             }
149         }
150         if (sep == '\n')
151             break;
152         if (sep != ' ')
153             return gripe(
154                 "Expected space or newline as field separator found %c",
155                 sep);
156     }
157     if (i >= MAX_NUM_COLUMNS)
158         return gripe("Too many columns");
159     if (i == 0)
160         return gripe("No columns read");
161     values[++i].v_type = VAL_NOTUSED;
162     return i;
163 }
164
165 static int
166 xunsymbol(struct castr *ca, char *buf)
167 {
168     struct symbol *symbol = (struct symbol *)empfile[ca->ca_table].cache;
169     int i;
170     int value = 0;
171     char *token;
172
173     if (ca->ca_flags & NSC_BITS)
174         token = strtok( buf, "|");
175     else
176         token = buf;
177
178     if (!token || token[0] == '\0')
179         return gripe("Empty symbol value for field %s", ca->ca_name);
180
181     while (token) {
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;
187         }
188         else
189             return gripe("Symbol %s was not found for field %s", token,
190                 ca->ca_name);
191         token = strtok(NULL, "|");
192     }
193     return(value);
194 }
195
196 static int
197 has_const(struct castr ca[])
198 {
199     int i;
200
201     for (i = 0; ca[i].ca_name; i++) {
202         if (ca[i].ca_flags & NSC_CONST)
203             return 1;
204     }
205     return 0;
206 }
207
208 static void
209 xuinitrow(int type, int row)
210 {
211     struct empfile *ep = &empfile[type];
212     char *ptr = ep->cache + ep->size * row;
213
214     memset(ptr, 0, ep->size);
215
216     if (ep->init)
217         ep->init(row, ptr);
218 }
219
220 static int
221 xuloadrow(int type, int row, struct value values[])
222 {
223     int i,j,k;
224     struct empfile *ep = &empfile[type];
225     char *ptr = ep->cache + ep->size * row;
226     struct castr *ca = ep->cadef;
227     void *row_ref;
228
229     i = 0;
230     j = 0;
231     while (ca[i].ca_type != NSC_NOTYPE &&
232            values[j].v_type != VAL_NOTUSED) {
233         row_ref = (char *)ptr + ca[i].ca_off;
234         k = 0;
235         do {
236             /*
237              * TODO
238              * factor out NSC_CONST comparsion
239              */
240             switch (values[j].v_type) {
241             case VAL_SYMBOL:
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)
250                     return -1;
251                 /*
252                  * fall through
253                  */
254             case VAL_DOUBLE:
255                 switch (ca[i].ca_type) {
256                 case NSC_INT:
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",
262                                 ca[i].ca_name,
263                                 ((int *)row_ref)[k],
264                                 (int)values[j].v_field.v_double);
265
266                     } else
267                         ((int *)row_ref)[k] =
268                             (int)values[j].v_field.v_double;
269                     break;
270                 case NSC_LONG:
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",
276                                 ca[i].ca_name,
277                                 ((long *)row_ref)[k],
278                                 (long)values[j].v_field.v_double);
279                     } else
280                         ((long *)row_ref)[k] = (long)
281                             values[j].v_field.v_double;
282                     break;
283                 case NSC_USHORT:
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",
289                                 ca[i].ca_name,
290                                 ((unsigned short *)row_ref)[k],
291                                 (unsigned short)values[j].v_field.v_double);
292                     } else
293                         ((unsigned short *)row_ref)[k] = (unsigned short)
294                             values[j].v_field.v_double;
295                     break;
296                 case NSC_UCHAR:
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",
302                                 ca[i].ca_name,
303                                 ((unsigned char *)row_ref)[k],
304                                 (unsigned char)values[j].v_field.v_double);
305                     } else
306                         ((unsigned char *)row_ref)[k] = (unsigned char)
307                             values[j].v_field.v_double;
308                     break;
309                 case NSC_FLOAT:
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",
315                                 ca[i].ca_name,
316                                 ((float *)row_ref)[k],
317                                 (float)values[j].v_field.v_double);
318                     } else
319                         ((float *)row_ref)[k] = (float)
320                             values[j].v_field.v_double;
321                     break;
322                 case NSC_STRING:
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);
326                 default:
327                     return gripe("Field %s's type %d is not supported",
328                         ca[i].ca_name, ca[i].ca_type);
329                 }
330                 break;
331             case VAL_STRING:
332                 switch(ca[i].ca_type) {
333                 case NSC_STRINGY:
334                     return gripe("Field %s is of NSC_STRINGY type "
335                         "which is not supported", ca[i].ca_name);
336                 case NSC_STRING:
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",
342                                 ca[i].ca_name,
343                                 *((char **)row_ref)[k],
344                                 *values[j].v_field.v_string);
345                     } else
346                         ((char **)row_ref)[k] = values[j].v_field.v_string;
347                     break;
348                 case NSC_INT:
349                 case NSC_LONG:
350                 case NSC_USHORT:
351                 case NSC_UCHAR:
352                 case NSC_FLOAT:
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);
357                 default:
358                     return gripe("Field %s's type %d is not supported",
359                             ca[i].ca_name, ca[i].ca_type);
360                 }
361                 break;
362             case VAL_NOTUSED:
363                 return gripe("Missing column %s in file", ca[i].ca_name);
364             default:
365                 return gripe("Unknown value type %d", values[j].v_type);
366             }
367             k++;
368             j++;
369         } while (k < ca[i].ca_len);
370         i++;
371     }
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) {
375     case VAL_NOTUSED:
376         break;
377     case VAL_STRING:
378     case VAL_SYMBOL:
379         return gripe("Extra junk after the last column, read %s",
380             values[j].v_field.v_string);
381     case VAL_DOUBLE:
382         return gripe("Extra junk after the last column, read %lg",
383             values[j].v_field.v_double);
384     default:
385         return gripe("Extra junk after the last column, "
386             "unknown value type %d", values[j].v_type);
387     }
388     return 0;
389 }
390
391 void
392 xuskipcommentlines(FILE *fp)
393 {
394     int ch;
395
396     for (;;) {
397         ch = getc(fp);
398         if (ch == EOF)
399             return;
400         if (ch == '#') {
401             do {
402                 ch = getc(fp);
403             } while (ch != '\n' && ch != EOF);
404             lineno++;
405         } else {
406             ungetc(ch, fp);
407             return;
408         }
409     }
410 }
411
412 int
413 xundump(FILE *fp, char *file, int expected_table)
414 {
415     char name[64];
416     char sep;
417     int row, rows, ch;
418     struct value values[MAX_NUM_COLUMNS + 1];
419     int type;
420     int fixed_rows;
421
422     if (strcmp(fname, file) != 0) {
423         fname = file;
424         lineno = 1;
425     } else
426         lineno++;
427
428     xuskipcommentlines(fp);
429     if (fscanf(fp, "XDUMP %63[^0123456789]%*d%c", name, &sep) != 2)
430         return gripe("Expected XDUMP header");
431     if (sep != '\n')
432         return gripe("Junk after XDUMP header");
433
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",
438             name);
439     name[strlen(name) - 1] = '\0';
440     
441     type = ef_byname(name);
442     if (type < 0)
443         return gripe("Table not found %s", name);
444
445     if (expected_table != EF_BAD && expected_table != type)
446         return gripe("Incorrect Table expecting %s got %s",
447             ef_nameof(expected_table), name);
448
449     fixed_rows = has_const(ef_cadef(type));
450
451     for (row = 0; ; row++) {
452         xuskipcommentlines(fp);
453         lineno++;
454         ch = getc(fp);
455         ungetc(ch, fp);
456         if (ch == EOF)
457             return gripe("Unexpected EOF");
458         if (ch == '/')
459             break;
460         /*
461          * TODO
462          * Add column count check to the return value of xuflds()
463          */
464         if (xuflds(fp, values) <= 0)
465             return -1;
466         else {
467             if (row >= empfile[type].csize - 1)
468                 return gripe("Too many rows for table %s", name);
469             empfile[type].fids = row + 1;
470             if (!fixed_rows)
471                 xuinitrow(type, row);
472             if (xuloadrow(type, row, values) < 0)
473                 return -1;
474         }
475     }
476
477     if (fscanf(fp, "/%d%c", &rows, &sep) != 2)
478         return gripe("Failed to find number of rows trailer");
479     if (row != rows)
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);
485     if (sep != '\n')
486         return gripe("Junk after number of rows trailer");
487
488     if (!fixed_rows)
489         xuinitrow(type, row);
490
491     xuskipcommentlines(fp);
492     return 0;
493 }