2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2015, 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-2014
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 * - Power: EF_POWER (TODO)
60 * - Nations: EF_NATION
62 * - Map: EF_MAP (TODO)
63 * - Bmap: EF_BMAP (TODO)
68 * See doc/xdump for motivation, syntax, semantics, and rationale.
69 * Make sure to keep it up-to-date!
72 /* TODO don't dump stuff that's useless due to options */
84 * Translate dump for country @cnum, except when @cnum is NATID_BAD.
85 * If @human, dump in human-readable format.
86 * If @sloppy, try to cope with invalid data (may result in invalid
88 * Dump is to be delivered through callback @pr.
92 xdinit(struct xdstr *xd, natid cnum, int human, int sloppy,
93 void (*pr)(char *fmt, ...))
96 xd->divine = cnum == NATID_BAD || getnatp(cnum)->nat_stat == STAT_GOD;
104 * Evaluate a attribute of an object into @val, return @val.
105 * @ca describes the attribute.
106 * @xd is the xdump descriptor.
107 * @ptr points to the context object.
108 * @idx is the index within the attribute.
110 static struct valstr *
111 xdeval(struct valstr *val, struct xdstr *xd,
112 struct castr *ca, void *ptr, int idx)
114 nstr_mksymval(val, ca, idx);
115 return nstr_eval(val, xd->cnum, ptr, NSC_NOTYPE);
119 * Dump string @str to @xd with funny characters escaped.
120 * Dump at most @n characters.
123 xdpresc(struct xdstr *xd, char *str, size_t n)
125 unsigned char *s, *e;
127 s = (unsigned char *)str;
130 n && *e != '"' && *e != '\\' && isgraph(*e);
133 xd->pr("%.*s", (int)(e-s), s);
136 xd->pr("\\%03o", *e++);
143 * Dump @val prefixed with @sep to @xd, in machine readable format.
144 * @val must be evaluated.
148 xdprval_nosym(struct xdstr *xd, struct valstr *val, char *sep)
150 if (CANT_HAPPEN(val->val_cat != NSC_VAL)) {
151 xd->pr("%snil", sep);
155 switch (val->val_type) {
157 xd->pr("%s%ld", sep, val->val_as.lng);
160 xd->pr("%s%#g", sep, val->val_as.dbl);
163 if (val->val_as.str.base) {
165 xdpresc(xd, val->val_as.str.base, val->val_as.str.maxsz);
168 xd->pr("%snil", sep);
172 xd->pr("%snil", sep);
178 * Dump symbol with value @key from symbol table @type to @xd.
179 * Prefix with @sep, return " ".
182 xdprsym(struct xdstr *xd, int key, int type, char *sep)
184 char *sym = symbol_by_value(key, ef_ptr(type, 0));
187 CANT_HAPPEN(!xd->sloppy);
188 xd->pr("%s%d", sep, key);
191 xdpresc(xd, sym, INT_MAX);
197 * Dump @val prefixed with @sep to @xd, return " ".
198 * @val must be evaluated.
199 * @ca describes the field from which the value was fetched.
202 xdprval_sym(struct xdstr *xd, struct valstr *val, struct castr *ca,
207 if (CANT_HAPPEN(val->val_cat != NSC_VAL)) {
208 xd->pr("%snil", sep);
212 if (!xd->human || val->val_type != NSC_LONG
213 || ca->ca_table == EF_BAD || ef_cadef(ca->ca_table) != symbol_ca)
214 return xdprval_nosym(xd, val, sep);
216 if (ca->ca_flags & NSC_BITS) {
219 for (bit = 1; bit; bit <<= 1) {
220 if (bit & val->val_as.lng)
221 sep = xdprsym(xd, bit, ca->ca_table, sep);
227 return xdprsym(xd, val->val_as.lng, ca->ca_table, sep);
231 * Dump field values of a context object to @xd.
232 * @ca[] describes fields.
233 * @ptr points to context object.
236 xdflds(struct xdstr *xd, struct castr ca[], void *ptr)
242 for (i = 0; ca[i].ca_name; ++i) {
243 if (ca[i].ca_flags & NSC_DEITY && !xd->divine)
245 if (ca[i].ca_flags & NSC_EXTRA)
247 n = CA_ARRAY_LEN(&ca[i]);
250 xdeval(&val, xd, &ca[i], ptr, j);
251 sep = xdprval_sym(xd, &val, &ca[i], sep);
257 * Dump header for dump @name to @xd.
258 * If @meta, it's for the meta-data dump rather than the data dump.
261 xdhdr(struct xdstr *xd, char *name, int meta)
264 xd->pr("config %s\n", name);
266 xd->pr("XDUMP %s%s %ld\n",
268 name, (long)time(NULL));
272 * Dump column header to @xd.
273 * @ca[] describes fields.
274 * Does nothing unless @xd is human-readable.
277 xdcolhdr(struct xdstr *xd, struct castr ca[])
285 for (i = 0; ca[i].ca_name; ++i) {
286 if (ca[i].ca_flags & NSC_DEITY && !xd->divine)
288 if (ca[i].ca_flags & NSC_EXTRA)
290 n = CA_ARRAY_LEN(&ca[i]);
292 for (j = 0; j < n; j++) {
293 xd->pr("%s%s(%d)", sep, ca[i].ca_name, j);
297 xd->pr("%s%s", sep, ca[i].ca_name);
304 /* Dump footer for a dump that dumped N objects to XD. */
306 xdftr(struct xdstr *xd, int n)