]> git.pond.sub.org Git - empserver/blob - src/lib/common/xundump.c
(init_server, ef_load, xundump): Add ability to customize game
[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 "file.h"
44 #include "nsc.h"
45 #include "match.h"
46
47 #define MAX_NUM_COLUMNS 256
48
49 static char *fname = "";
50 static int lineno = 0;
51
52 /*
53  * TODO
54  * This structure could be replaced with struct valstr.
55  */
56 enum enum_value {
57     VAL_NOTUSED,
58     VAL_STRING,
59     VAL_SYMBOL,
60     VAL_DOUBLE
61 };
62
63 struct value {
64     enum enum_value v_type;
65     union {
66         char *v_string;
67         double v_double;
68     } v_field;
69 };
70
71 static int
72 gripe(char *fmt, ...)
73 {
74     va_list ap;
75
76     fprintf(stderr, "%s:%d: ", fname, lineno);
77     va_start(ap, fmt);
78     vfprintf(stderr, fmt, ap);
79     va_end(ap);
80     putc('\n', stderr);
81
82     return -1;
83 }
84
85 static char *
86 xuesc(char *buf)
87 {
88     char *src, *dst;
89     int octal_chr, n;
90
91     dst = buf;
92     src = buf;
93     while (*src) {
94         if (*src == '\\') {
95             if (sscanf(++src, "%3o%n", &octal_chr, &n) != 1 || n != 3)
96                 return NULL;
97             *dst++ = (char)octal_chr;
98             src += 3;
99         } else
100             *dst++ = *src++;
101     }
102     *dst = '\0';
103     return buf;
104 }
105
106 static int
107 xuflds(FILE *fp, struct value values[])
108 {
109     int i, ch;
110     char sep;
111     char buf[1024];
112
113     for (i = 0; i < MAX_NUM_COLUMNS; i++) {
114         ch = getc(fp);
115         ungetc(ch, fp);
116
117         switch (ch) {
118         case '+': case '-': case '.':
119         case '0': case '1': case '2': case '3': case '4':
120         case '5': case '6': case '7': case '8': case '9':
121             if (fscanf(fp, "%lg%c", &values[i].v_field.v_double, &sep) != 2)
122                 return gripe("Malformed number in field %d", i + 1);
123             values[i].v_type = VAL_DOUBLE;
124             break;
125         case '"':
126             if (fscanf(fp, "\"%1023[^ \n]%c", buf, &sep) != 2
127                 || buf[strlen(buf)-1] != '"')
128                 return gripe("Malformed string in field %d", i + 1);
129             buf[strlen(buf)-1] = '\0';
130             if (!xuesc(buf))
131                 return gripe("Invalid escape sequence in field %d",
132                     i + 1);
133             values[i].v_type = VAL_STRING;
134             values[i].v_field.v_string = strdup(buf);
135             break;
136         default:
137             if (fscanf(fp, "%1023[^ \n]%c", buf, &sep) != 2) {
138                 return gripe("Junk in field %d", i + 1);
139             }
140             if (!strcmp(buf, "nil")) {
141                 values[i].v_field.v_string = NULL;
142                 values[i].v_type = VAL_STRING;
143             }
144             else {
145                 values[i].v_field.v_string = strdup(buf);
146                 values[i].v_type = VAL_SYMBOL;
147             }
148         }
149         if (sep == '\n')
150             break;
151         if (sep != ' ')
152             return gripe(
153                 "Expected space or newline as field separator found %c",
154                 sep);
155     }
156     if (i >= MAX_NUM_COLUMNS)
157         return gripe("Too many columns");
158     if (i == 0)
159         return gripe("No columns read");
160     values[++i].v_type = VAL_NOTUSED;
161     return i;
162 }
163
164 static int
165 xunsymbol(struct castr *ca, char *buf)
166 {
167     struct symbol *symbol = (struct symbol *)empfile[ca->ca_table].cache;
168     int i;
169     int value = 0;
170     char *token;
171
172     if (ca->ca_flags & NSC_BITS)
173         token = strtok( buf, "|");
174     else
175         token = buf;
176
177     if (!token || token[0] == '\0')
178         return gripe("Empty symbol value for field %s", ca->ca_name);
179
180     while (token) {
181         if ((i = stmtch(token, symbol, offsetof(struct symbol, name),
182                         sizeof(struct symbol))) != M_NOTFOUND) {
183             if (!(ca->ca_flags & NSC_BITS))
184                 return(symbol[i].value);
185             value |= symbol[i].value;
186             break;
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 int
392 xundump(FILE *fp, char *file, int expected_table)
393 {
394     char name[64];
395     char sep;
396     int row, rows, ch;
397     struct value values[MAX_NUM_COLUMNS + 1];
398     int type;
399     int fixed_rows;
400
401     if (strcmp(fname, file) != 0) {
402         fname = file;
403         lineno = 1;
404     } else
405         lineno++;
406
407     if (fscanf(fp, "XDUMP %63[^0123456789]%*d%c", name, &sep) != 2)
408         return gripe("Expected XDUMP header");
409     if (sep != '\n')
410         return gripe("Junk after XDUMP header");
411
412     if (strlen(name) < 2)
413         return gripe("Invalid table name in header %s", name);
414     if (name[strlen(name) - 1] != ' ')
415         return gripe("Missing space after table name in header %s",
416             name);
417     name[strlen(name) - 1] = '\0';
418     
419     type = ef_byname(name);
420     if (type < 0)
421         return gripe("Table not found %s", name);
422
423     if (expected_table != EF_BAD && expected_table != type)
424         return gripe("Incorrect Table expecting %s got %s",
425             ef_nameof(expected_table), name);
426
427     fixed_rows = has_const(ef_cadef(type));
428
429     for (row = 0; ; row++) {
430         lineno++;
431         ch = getc(fp);
432         ungetc(ch, fp);
433         if (ch == EOF)
434             return gripe("Unexpected EOF");
435         if (ch == '/')
436             break;
437         /*
438          * TODO
439          * Add column count check to the return value of xuflds()
440          */
441         if (xuflds(fp, values) <= 0)
442             return -1;
443         else {
444             if (row >= empfile[type].csize - 1)
445                 return gripe("Too many rows for table %s", name);
446             empfile[type].fids = row + 1;
447             if (!fixed_rows)
448                 xuinitrow(type, row);
449             if (xuloadrow(type, row, values) < 0)
450                 return -1;
451         }
452     }
453
454     if (fscanf(fp, "/%d%c", &rows, &sep) != 2)
455         return gripe("Failed to find number of rows trailer");
456     if (row != rows)
457         return gripe("Number of rows doesn't match between "
458             "the trailer and what was read");
459     if (fixed_rows && row != empfile[type].csize -1)
460         return gripe("Number of rows doesn't match, and "
461             "it must for table %s", name);
462     if (sep != '\n')
463         return gripe("Junk after number of rows trailer");
464
465     if (!fixed_rows)
466         xuinitrow(type, row);
467
468     return 0;
469 }