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