2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2017, 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-2016
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 */
83 * If @cnum is NATID_BAD, this is an empdump export rather than an
85 * Translate dump for country @cnum, except when @cnum is NATID_BAD.
86 * Ignore CA_DUMP_ONLY selectors when @cnum is NATID_BAD.
87 * If @human, dump in human-readable format.
88 * If @sloppy, try to cope with invalid data (may result in invalid
90 * Dump is to be delivered through callback @pr.
94 xdinit(struct xdstr *xd, natid cnum, int human, int sloppy,
95 void (*pr)(char *fmt, ...))
98 xd->divine = cnum == NATID_BAD || getnatp(cnum)->nat_stat == STAT_GOD;
106 * Evaluate a attribute of an object into @val, return @val.
107 * @ca describes the attribute.
108 * @xd is the xdump descriptor.
109 * @ptr points to the context object.
110 * @idx is the index within the attribute.
112 static struct valstr *
113 xdeval(struct valstr *val, struct xdstr *xd,
114 struct castr *ca, void *ptr, int idx)
116 nstr_mksymval(val, ca, idx);
117 return nstr_eval(val, xd->cnum, ptr, NSC_NOTYPE);
121 * Dump string @str to @xd with funny characters escaped.
122 * Dump at most @n characters.
125 xdpresc(struct xdstr *xd, char *str, size_t n)
127 unsigned char *s, *e;
129 s = (unsigned char *)str;
132 n && *e != '"' && *e != '\\' && isgraph(*e);
135 xd->pr("%.*s", (int)(e-s), s);
138 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_dump == CA_DUMP_NONE)
249 if (ca[i].ca_dump == CA_DUMP_ONLY && xd->cnum == NATID_BAD)
251 n = CA_ARRAY_LEN(&ca[i]);
254 xdeval(&val, xd, &ca[i], ptr, j);
255 sep = xdprval_sym(xd, &val, &ca[i], sep);
261 * Dump header for dump @name to @xd.
262 * If @meta, it's for the meta-data dump rather than the data dump.
265 xdhdr(struct xdstr *xd, char *name, int meta)
268 xd->pr("config %s\n", name);
270 xd->pr("XDUMP %s%s %ld\n",
272 name, (long)time(NULL));
276 * Dump column header to @xd.
277 * @ca[] describes fields.
278 * Does nothing unless @xd is human-readable.
281 xdcolhdr(struct xdstr *xd, struct castr ca[])
289 for (i = 0; ca[i].ca_name; ++i) {
290 if (ca[i].ca_flags & NSC_DEITY && !xd->divine)
292 if (ca[i].ca_dump == CA_DUMP_NONE)
294 n = CA_ARRAY_LEN(&ca[i]);
296 for (j = 0; j < n; j++) {
297 xd->pr("%s%s(%d)", sep, ca[i].ca_name, j);
301 xd->pr("%s%s", sep, ca[i].ca_name);
308 /* Dump footer for a dump that dumped N objects to XD. */
310 xdftr(struct xdstr *xd, int n)