]> git.pond.sub.org Git - empserver/blob - src/lib/common/ef_verify.c
Update copyright notice.
[empserver] / src / lib / common / ef_verify.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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  *  ef_verify.c: Verify game configuration
29  * 
30  *  Known contributors to this file:
31  *     Ron Koenderink, 2005
32  *  
33  */
34
35 #include <config.h>
36
37 #include <stdio.h>
38 #include <time.h>
39
40 #include "prototypes.h"
41 #include "file.h"
42 #include "nsc.h"
43
44 static int
45 verify_row(int type, int row)
46 {
47     struct castr *ca = ef_cadef(type);
48     void *row_ref = malloc(empfile[type].size);
49     int i, j, k, n;
50     struct castr *ca_sym;
51     struct valstr val;
52     int ret_val = 0; 
53     int in_mem = (ef_flags(type) & EFF_MEM) != 0; 
54  
55  
56     if (!in_mem) { 
57         row_ref = malloc(empfile[type].size); 
58         ef_read(type, row, row_ref); 
59     } else 
60         row_ref = ef_ptr(type, row); 
61
62     for (i = 0; ca[i].ca_name; ++i) {
63         if (ca[i].ca_flags & NSC_EXTRA)
64             continue;
65         n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
66         j = 0;
67         do {
68             if (ca[i].ca_table != EF_BAD && ca[i].ca_table != type) {
69                 val.val_type = ca[i].ca_type;
70                 val.val_cat = NSC_OFF;
71                 val.val_as.sym.off = ca[i].ca_off;
72                 val.val_as.sym.idx = j;
73                 nstr_exec_val(&val, 0, row_ref, NSC_NOTYPE);
74                 if (val.val_type != NSC_LONG && val.val_type != NSC_TYPEID)
75                     continue;
76
77                 ca_sym = ef_cadef(ca[i].ca_table);
78                 if (ca[i].ca_flags & NSC_BITS) {
79                     if (ca_sym != symbol_ca) {
80                         fprintf(stderr,
81                             "Unable to verify symbol set as the "
82                             "table %s is not created as symbol table "
83                             "for table %s row %d field %s\n",
84                             ef_nameof(ca[i].ca_table), ef_nameof(type),
85                             row + 1, ca[i].ca_name
86                             );
87                         continue;
88                     }
89                     for (k = 0; k < (int)sizeof(long) * 8; k++) {
90                         if (val.val_as.lng & (1 << k))
91                             if (!symbol_by_value(1 << k, ef_ptr(ca[i].ca_table, 0))) {
92                                 fprintf(stderr,
93                                     "bit %d not found in symbol table %s "
94                                     "when verify table %s row %d field %s\n",
95                                     k, ef_nameof(ca[i].ca_table),
96                                     ef_nameof(type), row + 1, ca[i].ca_name);
97                                 ret_val = -1;
98                             }
99                     }
100                 } else {
101                     if (ca_sym != symbol_ca) {
102                         if (val.val_as.lng >= ef_nelem(ca[i].ca_table) ||
103                             val.val_as.lng < -2) {
104                             fprintf(stderr, "Table index %ld to table %s "
105                                 "out of range, nelements %d for table %s "
106                                 "row %d field %s\n",
107                                 val.val_as.lng, ef_nameof(ca[i].ca_table),
108                                 ef_nelem(ca[i].ca_table), ef_nameof(type), 
109                                 row + 1, ca[i].ca_name);
110                             ret_val = -1;
111                         }
112                     } else {
113                         if (val.val_as.lng > -1) {
114                             if (!symbol_by_value(val.val_as.lng,
115                                        ef_ptr(ca[i].ca_table,0))) {
116                                 fprintf(stderr, "value %ld not found in "
117                                     "symbol table %s when verify table %s "
118                                     "row %d field %s\n",
119                                     val.val_as.lng,
120                                     ef_nameof(ca[i].ca_table),
121                                     ef_nameof(type), row + 1,
122                                     ca[i].ca_name);
123                                 ret_val = -1;
124                             }
125                         }
126                     }
127                 }
128             }
129         } while (++j < n);
130     } 
131     if (!in_mem)
132         free(row_ref);
133     return ret_val;
134 }
135
136 int
137 ef_verify()
138 {
139     struct empfile *ep;
140     int retval = 0;
141     int i;
142
143     for (ep = empfile; ep->name; ep++) {
144         if (!ef_cadef(ep->uid))
145             continue;
146         for (i = 0; i < ef_nelem(ep->uid); i++) {
147             retval += verify_row(ep->uid, i);
148         }
149     }
150     return retval;
151 }