2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2013, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 of the License, or
9 * (at your option) any later version.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * xdump.c: Extended dumps
29 * Known contributors to this file:
30 * Markus Armbruster, 2004-2013
34 * Dump everything under the sun
36 * Static game data (configuration):
37 * - Item characteristics: ichr[]
38 * - Land unit characteristics: lchr[]
39 * - Nuke characteristics: nchr[]
40 * - Plane characteristics: plchr[]
41 * - Product characteristics: pchr[]
42 * - Sector designation characteristics: dchr[]
43 * - Sector infrastructure characteristics: intrchr[]
44 * - Ship characteristics: mchr[]
45 * - News item characteristics: rpt[]
46 * - News page headings: page_headings[]
47 * - Commands: player_coms[] (TODO)
48 * - Update schedule: update_time[] (not really static)
49 * - Configuration: configkeys[]
52 * - Sectors: EF_SECTOR (superseding dump)
53 * - Land units: EF_LAND (superseding ldump)
54 * - Lost: EF_LOST (superseding lost)
55 * - Nukes: EF_NUKE (superseding ndump)
56 * - Planes: EF_PLANE (superseding pdump)
57 * - Ships: EF_SHIP (superseding sdump)
59 * - Treaties: EF_TREATY
60 * - Power: EF_POWER (TODO)
61 * - Nations: EF_NATION
63 * - Map: EF_MAP (TODO)
64 * - Bmap: EF_BMAP (TODO)
69 * See doc/xdump for motivation, syntax, semantics, and rationale.
70 * Make sure to keep it up-to-date!
73 /* TODO don't dump stuff that's useless due to options */
85 * Translate dump for country CNUM, except when CNUM is NATID_BAD.
86 * If HUMAN, dump in human-readable format.
87 * If SLOPPY, try to cope with invalid data (may result in invalid
89 * Dump is to be delivered through callback PR.
93 xdinit(struct xdstr *xd, natid cnum, int human, int sloppy,
94 void (*pr)(char *fmt, ...))
97 xd->divine = cnum == NATID_BAD || getnatp(cnum)->nat_stat == STAT_GOD;
105 * Evaluate a attribute of an object into VAL, return VAL.
106 * CA describes the attribute.
107 * XD is the xdump descriptor.
108 * PTR points to the context object.
109 * IDX is the index within the attribute.
111 static struct valstr *
112 xdeval(struct valstr *val, struct xdstr *xd,
113 struct castr *ca, void *ptr, int idx)
115 nstr_mksymval(val, ca, idx);
116 return nstr_exec_val(val, xd->cnum, ptr, NSC_NOTYPE);
120 * Dump string STR to XD with funny characters escaped.
121 * Dump at most N characters.
124 xdpresc(struct xdstr *xd, char *str, size_t n)
126 unsigned char *s, *e, *l;
128 s = (unsigned char *)str;
132 e < l && *e != '"' && *e != '\\' && isgraph(*e);
135 xd->pr("%.*s", (int)(e-s), s);
137 xd->pr("\\%03o", *e++);
145 * Dump VAL prefixed with SEP to XD, in machine readable format.
146 * VAL must be evaluated.
150 xdprval_nosym(struct xdstr *xd, struct valstr *val, char *sep)
152 if (CANT_HAPPEN(val->val_cat != NSC_VAL)) {
153 xd->pr("%snil", sep);
157 switch (val->val_type) {
159 xd->pr("%s%ld", sep, val->val_as.lng);
162 xd->pr("%s%#g", sep, val->val_as.dbl);
165 if (val->val_as.str.base) {
167 xdpresc(xd, val->val_as.str.base, val->val_as.str.maxsz);
170 xd->pr("%snil", sep);
174 xd->pr("%snil", sep);
180 * Dump symbol with value KEY from symbol table TYPE to XD.
181 * Prefix with SEP, return " ".
184 xdprsym(struct xdstr *xd, int key, int type, char *sep)
186 char *sym = symbol_by_value(key, ef_ptr(type, 0));
189 CANT_HAPPEN(!xd->sloppy);
190 xd->pr("%s%d", sep, key);
193 xdpresc(xd, sym, INT_MAX);
199 * Dump VAL prefixed with SEP to XD, return " ".
200 * VAL must be evaluated.
201 * CA describes the field from which the value was fetched.
204 xdprval_sym(struct xdstr *xd, struct valstr *val, struct castr *ca,
209 if (CANT_HAPPEN(val->val_cat != NSC_VAL)) {
210 xd->pr("%snil", sep);
214 if (!xd->human || val->val_type != NSC_LONG
215 || ca->ca_table == EF_BAD || ef_cadef(ca->ca_table) != symbol_ca)
216 return xdprval_nosym(xd, val, sep);
218 if (ca->ca_flags & NSC_BITS) {
221 for (bit = 1; bit; bit <<= 1) {
222 if (bit & val->val_as.lng)
223 sep = xdprsym(xd, bit, ca->ca_table, sep);
229 return xdprsym(xd, val->val_as.lng, ca->ca_table, sep);
233 * Dump field values of a context object to XD.
234 * CA[] describes fields.
235 * PTR points to context object.
238 xdflds(struct xdstr *xd, struct castr ca[], void *ptr)
244 for (i = 0; ca[i].ca_name; ++i) {
245 if (ca[i].ca_flags & NSC_DEITY && !xd->divine)
247 if (ca[i].ca_flags & NSC_EXTRA)
249 n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
252 xdeval(&val, xd, &ca[i], ptr, j);
253 sep = xdprval_sym(xd, &val, &ca[i], sep);
259 * Dump header for dump NAME to XD.
260 * If META, it's for the meta-data dump rather than the data dump.
263 xdhdr(struct xdstr *xd, char *name, int meta)
266 xd->pr("config %s\n", name);
268 xd->pr("XDUMP %s%s %ld\n",
270 name, (long)time(NULL));
274 * Dump column header to XD.
275 * CA[] describes fields.
276 * Does nothing unless XD is human-readable.
279 xdcolhdr(struct xdstr *xd, struct castr ca[])
287 for (i = 0; ca[i].ca_name; ++i) {
288 if (ca[i].ca_flags & NSC_DEITY && !xd->divine)
290 if (ca[i].ca_flags & NSC_EXTRA)
292 n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
294 for (j = 0; j < n; j++) {
295 xd->pr("%s%s(%d)", sep, ca[i].ca_name, j);
299 xd->pr("%s%s", sep, ca[i].ca_name);
306 /* Dump footer for a dump that dumped N objects to XD. */
308 xdftr(struct xdstr *xd, int n)