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