]> git.pond.sub.org Git - empserver/blob - src/lib/common/xdump.c
5e7f5a0cb2c41c46ba0b6a301d5e5680d2c1dfcf
[empserver] / src / lib / common / xdump.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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 files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  xdump.c: Extended dumps
29  * 
30  *  Known contributors to this file:
31  *     Markus Armbruster, 2008
32  */
33
34 /*
35  * Dump everything under the sun
36  *
37  * Static game data (configuration):
38  * - Item characteristics: ichr[]
39  * - Land unit characteristics: lchr[]
40  * - Nuke characteristics: nchr[]
41  * - Plane characteristics: plchr[]
42  * - Product characteristics: pchr[]
43  * - Sector designation characteristics: dchr[]
44  * - Sector infrastructure characteristics: intrchr[]
45  * - Ship characteristics: mchr[]
46  * - News item characteristics: rpt[]
47  * - News page headings: page_headings[]
48  * - Commands: player_coms[] (TODO)
49  * - Update schedule: update_time[] (not really static)
50  * - Configuration: configkeys[]
51  *
52  * Dynamic game data:
53  * - Sectors: EF_SECTOR (superseding dump)
54  * - Land units: EF_LAND (superseding ldump)
55  * - Lost: EF_LOST (superseding lost)
56  * - Nukes: EF_NUKE (superseding ndump)
57  * - Planes: EF_PLANE (superseding pdump)
58  * - Ships: EF_SHIP (superseding sdump)
59  * - News: EF_NEWS
60  * - Treaties: EF_TREATY
61  * - Power: EF_POWER (TODO)
62  * - Nations: EF_NATION
63  * - Loans: EF_LOAN
64  * - Map: EF_MAP (TODO)
65  * - Bmap: EF_BMAP (TODO)
66  * - Market: EF_COMM
67  */
68
69 /*
70  * See doc/xdump for motivation, syntax, semantics, and rationale.
71  * Make sure to keep it up-to-date!
72  */
73
74 /* TODO don't dump stuff that's useless due to options */
75
76 #include <config.h>
77
78 #include <ctype.h>
79 #include "file.h"
80 #include "nat.h"
81 #include "xdump.h"
82
83 /*
84  * Initialize XD to dump for country CNUM.
85  * Dump is to be delivered through callback PR.
86  * Return XD.
87  */
88 struct xdstr *
89 xdinit(struct xdstr *xd, natid cnum, void (*pr)(char *fmt, ...))
90 {
91     xd->cnum = cnum;
92     xd->divine = getnatp(cnum)->nat_stat == STAT_GOD;
93     xd->pr = pr;
94     return xd;
95 }
96
97 /*
98  * Evaluate a attribute of an object into VAL, return VAL.
99  * XD is the xdump descriptor.
100  * TYPE is the attribute's type.
101  * PTR points to the context object.
102  * The attribute is stored there at offset OFF + IDX * S, where S is
103  * its size.
104  * LEN is the #array elements if it is an array, else zero.
105  */
106 struct valstr *
107 xdeval(struct valstr *val, struct xdstr *xd,
108        nsc_type type, void *ptr, ptrdiff_t off, int idx, int len)
109 {
110     val->val_type = type;
111     val->val_cat = NSC_OFF;
112     val->val_as.sym.off = off;
113     val->val_as.sym.len = len;
114     val->val_as.sym.idx = idx;
115     nstr_exec_val(val, xd->cnum, ptr, NSC_NOTYPE);
116     return val;                 /* FIXME nstr_exec_val() should return VAL */
117 }
118
119 /* Dump VAL prefixed with SEP, return " ".  */
120 char *
121 xdprval(struct xdstr *xd, struct valstr *val, char *sep)
122 {
123     unsigned char *s, *e, *l;
124
125     switch (val->val_type) {
126     case NSC_LONG:
127         xd->pr("%s%ld", sep, val->val_as.lng);
128         break;
129     case NSC_DOUBLE:
130         xd->pr("%s%#g", sep, val->val_as.dbl);
131         break;
132     case NSC_STRING:
133         s = (unsigned char *)val->val_as.str.base;
134         if (s) {
135             xd->pr("%s\"", sep);
136             l = s + val->val_as.str.maxsz;
137             /* FIXME maxsz == INT_MAX ! */
138             for (;;) {
139                 for (e = s;
140                      e < l && *e != '"' && *e != '\\' && isgraph(*e);
141                      ++e)
142                     ;
143                 xd->pr("%.*s", (int)(e-s), s);
144                 if (e < l && *e)
145                     xd->pr("\\%03o", *e++);
146                 else
147                     break;
148                 s = e;
149             }
150             xd->pr("\"");
151         } else
152             xd->pr("%snil", sep);
153         break;
154     default:
155         CANT_REACH();
156         xd->pr("0");
157     }
158     return " ";
159 }
160
161 /*
162  * Dump field values of a context object to XD.
163  * CA[] describes fields.
164  * PTR points to context object.
165  */
166 void
167 xdflds(struct xdstr *xd, struct castr ca[], void *ptr)
168 {
169     int i, j, n;
170     struct valstr val;
171     char *sep = "";
172
173     for (i = 0; ca[i].ca_name; ++i) {
174         if (ca[i].ca_flags & NSC_DEITY && !xd->divine)
175             continue;
176         if (ca[i].ca_flags & NSC_EXTRA)
177             continue;
178         n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
179         j = 0;
180         do {
181             xdeval(&val, xd,
182                    ca[i].ca_type, ptr, ca[i].ca_off, j, ca[i].ca_len);
183             sep = xdprval(xd, &val, sep);
184         } while (++j < n);
185     }
186 }
187
188 /*
189  * Dump header for dump NAME.
190  * If META, it's for the meta-data dump rather than the data dump.
191  */
192 void
193 xdhdr(struct xdstr *xd, char *name, int meta)
194 {
195     xd->pr("XDUMP %s%s %ld\n", meta ? "meta " : "", name, (long)time(NULL));
196 }
197
198 /* Dump footer for a dump that dumped N objects.  */
199 void
200 xdftr(struct xdstr *xd, int n)
201 {
202     xd->pr("/%d\n", n);
203 }
204
205 /*
206  * Dump meta-data for items of type TYPE to XD.
207  * Return RET_OK.
208  */
209 int
210 xdmeta(struct xdstr *xd, int type)
211 {
212     struct castr *ca = ef_cadef(type);
213     int i;
214     int n = 0;
215
216     if (!ca)
217         return RET_SYN;
218
219     xdhdr(xd, ef_nameof(type), 1);
220
221     for (i = 0; ca[i].ca_name; i++) {
222         if (ca[i].ca_flags & NSC_DEITY && !xd->divine)
223             continue;
224         if (ca[i].ca_flags & NSC_EXTRA)
225             continue;
226         xdflds(xd, mdchr_ca, &ca[i]);
227         xd->pr("\n");
228         n++;
229     }
230
231     xdftr(xd, n);
232
233     return RET_OK;
234 }