From 11b122048b4fca1b12a77bfb19a8499b7c05ab0d Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 13 Nov 2016 19:40:38 +0100 Subject: [PATCH 1/9] reduce dependency on save.h moving a_write* functions to attrib.h, which was most of the reason everyone and their mom include save.h --- src/attributes/attributes.c | 1 - src/attributes/dict.c | 1 - src/attributes/iceberg.c | 1 - src/attributes/key.c | 1 - src/attributes/movement.c | 1 - src/attributes/overrideroads.c | 3 +- src/attributes/racename.c | 1 - src/attributes/raceprefix.c | 1 - src/attributes/reduceproduction.c | 1 - src/attributes/stealth.c | 1 - src/attributes/targetregion.c | 1 - src/chaos.c | 1 - src/creport.c | 4 +- src/items/artrewards.c | 1 - src/kernel/ally.c | 1 - src/kernel/building.c | 1 - src/kernel/faction.c | 1 - src/kernel/item.c | 1 - src/kernel/region.c | 1 - src/kernel/save.c | 92 +------------------------------ src/kernel/save.h | 19 +------ src/kernel/unit.c | 1 - src/laws.c | 1 - src/modules/arena.c | 1 - src/move.c | 1 - src/report.c | 52 ++++++++--------- src/spells.c | 3 +- src/spells/borders.c | 1 - src/summary.c | 1 - src/triggers/changefaction.c | 1 - src/util/attrib.c | 82 +++++++++++++++++++++++++++ src/util/attrib.h | 14 +++++ src/util/attrib.test.c | 90 +++++++++++++++++++++++++++++- 33 files changed, 218 insertions(+), 165 deletions(-) diff --git a/src/attributes/attributes.c b/src/attributes/attributes.c index cae0c4742..8e9b22139 100644 --- a/src/attributes/attributes.c +++ b/src/attributes/attributes.c @@ -48,7 +48,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include #include -#include #include #include diff --git a/src/attributes/dict.c b/src/attributes/dict.c index cb24b148b..2bba9ea7b 100644 --- a/src/attributes/dict.c +++ b/src/attributes/dict.c @@ -24,7 +24,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include #include -#include #include #include diff --git a/src/attributes/iceberg.c b/src/attributes/iceberg.c index 661a2ec80..e36e6303a 100644 --- a/src/attributes/iceberg.c +++ b/src/attributes/iceberg.c @@ -20,7 +20,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include "iceberg.h" -#include #include attrib_type at_iceberg = { diff --git a/src/attributes/key.c b/src/attributes/key.c index 39c7dab66..0b252e0c0 100644 --- a/src/attributes/key.c +++ b/src/attributes/key.c @@ -20,7 +20,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include "key.h" -#include #include #include #include diff --git a/src/attributes/movement.c b/src/attributes/movement.c index 0ad08030e..874da5eb3 100644 --- a/src/attributes/movement.c +++ b/src/attributes/movement.c @@ -20,7 +20,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include "movement.h" -#include #include #include diff --git a/src/attributes/overrideroads.c b/src/attributes/overrideroads.c index 72d2260bc..ef466de2d 100644 --- a/src/attributes/overrideroads.c +++ b/src/attributes/overrideroads.c @@ -20,9 +20,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include "overrideroads.h" -#include #include attrib_type at_overrideroads = { - "roads_override", NULL, NULL, NULL, &a_writestring, &a_readstring + "roads_override", NULL, NULL, NULL, a_writestring, a_readstring }; diff --git a/src/attributes/racename.c b/src/attributes/racename.c index f60340b0c..b408cbf6a 100644 --- a/src/attributes/racename.c +++ b/src/attributes/racename.c @@ -20,7 +20,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include "racename.h" -#include #include /* libc includes */ diff --git a/src/attributes/raceprefix.c b/src/attributes/raceprefix.c index 308c75292..15f0036c1 100644 --- a/src/attributes/raceprefix.c +++ b/src/attributes/raceprefix.c @@ -19,7 +19,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include #include "raceprefix.h" -#include #include diff --git a/src/attributes/reduceproduction.c b/src/attributes/reduceproduction.c index 3f31938d3..01041380a 100644 --- a/src/attributes/reduceproduction.c +++ b/src/attributes/reduceproduction.c @@ -19,7 +19,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include #include "reduceproduction.h" -#include #include #include #include diff --git a/src/attributes/stealth.c b/src/attributes/stealth.c index b7336efc7..db2ec5304 100644 --- a/src/attributes/stealth.c +++ b/src/attributes/stealth.c @@ -1,7 +1,6 @@ #include #include #include -#include #include #include diff --git a/src/attributes/targetregion.c b/src/attributes/targetregion.c index 93ef08d0b..f61214dfc 100644 --- a/src/attributes/targetregion.c +++ b/src/attributes/targetregion.c @@ -21,7 +21,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include -#include #include #include diff --git a/src/chaos.c b/src/chaos.c index afa787b29..7a1be7e56 100644 --- a/src/chaos.c +++ b/src/chaos.c @@ -28,7 +28,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include #include -#include #include #include #include diff --git a/src/creport.c b/src/creport.c index 2cdc2d42f..becee2cb0 100644 --- a/src/creport.c +++ b/src/creport.c @@ -57,7 +57,6 @@ without prior permission by the authors of Eressea. #include #include #include -#include /* util includes */ #include @@ -70,6 +69,7 @@ without prior permission by the authors of Eressea. #include #include #include +#include /* libc includes */ #include @@ -177,7 +177,7 @@ static void print_items(FILE * F, item * items, const struct locale *lang) } static void -cr_output_curses(stream *out, const faction * viewer, const void *obj, objtype_t typ) +cr_output_curses(struct stream *out, const faction * viewer, const void *obj, objtype_t typ) { bool header = false; attrib *a = NULL; diff --git a/src/items/artrewards.c b/src/items/artrewards.c index 8b6288f61..0965125fd 100644 --- a/src/items/artrewards.c +++ b/src/items/artrewards.c @@ -26,7 +26,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include #include -#include #include #include #include diff --git a/src/kernel/ally.c b/src/kernel/ally.c index 2bb980dd0..5a5a5b1c3 100644 --- a/src/kernel/ally.c +++ b/src/kernel/ally.c @@ -3,7 +3,6 @@ #include "types.h" #include "ally.h" -#include "save.h" #include "unit.h" #include "region.h" #include "group.h" diff --git a/src/kernel/building.c b/src/kernel/building.c index 0056fba1e..8789e40b2 100644 --- a/src/kernel/building.c +++ b/src/kernel/building.c @@ -28,7 +28,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "race.h" #include "region.h" #include "skill.h" -#include "save.h" #include "lighthouse.h" /* util includes */ diff --git a/src/kernel/faction.c b/src/kernel/faction.c index 774a6aec8..0172ea835 100755 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -29,7 +29,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "plane.h" #include "race.h" #include "region.h" -#include "save.h" #include "spellbook.h" #include "terrain.h" #include "unit.h" diff --git a/src/kernel/item.c b/src/kernel/item.c index 66e361156..64d01bce0 100644 --- a/src/kernel/item.c +++ b/src/kernel/item.c @@ -30,7 +30,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "pool.h" #include "race.h" #include "region.h" -#include "save.h" #include "skill.h" #include "terrain.h" #include "unit.h" diff --git a/src/kernel/region.c b/src/kernel/region.c index a92a87291..e05bd76c1 100644 --- a/src/kernel/region.c +++ b/src/kernel/region.c @@ -32,7 +32,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "plane.h" #include "region.h" #include "resources.h" -#include "save.h" #include "ship.h" #include "terrain.h" #include "terrainid.h" diff --git a/src/kernel/save.c b/src/kernel/save.c index 80e9ca3a6..4a1b7897e 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -93,6 +93,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. /* exported symbols symbols */ int firstx = 0, firsty = 0; + +// TODO: is this still important? int enc_gamedata = ENCODING_UTF8; /* local symbols */ @@ -2074,93 +2076,3 @@ int write_game(gamedata *data) { return 0; } - -int a_readint(attrib * a, void *owner, struct gamedata *data) -{ - int n; - READ_INT(data->store, &n); - if (a) a->data.i = n; - return AT_READ_OK; -} - -void a_writeint(const attrib * a, const void *owner, struct storage *store) -{ - WRITE_INT(store, a->data.i); -} - -int a_readshorts(attrib * a, void *owner, struct gamedata *data) -{ - int n; - READ_INT(data->store, &n); - a->data.sa[0] = (short)n; - READ_INT(data->store, &n); - a->data.sa[1] = (short)n; - return AT_READ_OK; -} - -void a_writeshorts(const attrib * a, const void *owner, struct storage *store) -{ - WRITE_INT(store, a->data.sa[0]); - WRITE_INT(store, a->data.sa[1]); -} - -int a_readchars(attrib * a, void *owner, struct gamedata *data) -{ - int i; - for (i = 0; i != 4; ++i) { - int n; - READ_INT(data->store, &n); - a->data.ca[i] = (char)n; - } - return AT_READ_OK; -} - -void a_writechars(const attrib * a, const void *owner, struct storage *store) -{ - int i; - - for (i = 0; i != 4; ++i) { - WRITE_INT(store, a->data.ca[i]); - } -} - -int a_readvoid(attrib * a, void *owner, struct gamedata *data) -{ - return AT_READ_OK; -} - -void a_writevoid(const attrib * a, const void *owner, struct storage *store) -{ -} - -int a_readstring(attrib * a, void *owner, struct gamedata *data) -{ - char buf[DISPLAYSIZE]; - char * result = 0; - int e; - size_t len = 0; - do { - e = READ_STR(data->store, buf, sizeof(buf)); - if (result) { - result = realloc(result, len + DISPLAYSIZE - 1); - strcpy(result + len, buf); - len += DISPLAYSIZE - 1; - } - else { - result = _strdup(buf); - } - } while (e == ENOMEM); - a->data.v = result; - return AT_READ_OK; -} - -void a_writestring(const attrib * a, const void *owner, struct storage *store) -{ - assert(a->data.v); - WRITE_STR(store, (const char *)a->data.v); -} - -void a_finalizestring(attrib * a) -{ - free(a->data.v); -} diff --git a/src/kernel/save.h b/src/kernel/save.h index ffc9c2891..aa11ac1ad 100644 --- a/src/kernel/save.h +++ b/src/kernel/save.h @@ -39,6 +39,8 @@ extern "C" { * dass hier ein Fehler (fehlende ") vorliegt */ extern int data_version; + + // TODO: is this *really* still in use? extern int enc_gamedata; int readorders(const char *filename); @@ -72,23 +74,6 @@ extern "C" { void write_ship(struct gamedata *data, const struct ship *sh); struct ship *read_ship(struct gamedata *data); - int a_readint(struct attrib *a, void *owner, struct gamedata *); - void a_writeint(const struct attrib *a, const void *owner, - struct storage *store); - int a_readshorts(struct attrib *a, void *owner, struct gamedata *); - void a_writeshorts(const struct attrib *a, const void *owner, - struct storage *store); - int a_readchars(struct attrib *a, void *owner, struct gamedata *); - void a_writechars(const struct attrib *a, const void *owner, - struct storage *store); - int a_readvoid(struct attrib *a, void *owner, struct gamedata *); - void a_writevoid(const struct attrib *a, const void *owner, - struct storage *); - int a_readstring(struct attrib *a, void *owner, struct gamedata *); - void a_writestring(const struct attrib *a, const void *owner, - struct storage *); - void a_finalizestring(struct attrib *a); - void create_backup(char *file); int write_game(struct gamedata *data); diff --git a/src/kernel/unit.c b/src/kernel/unit.c index f43ef53bc..28e95b941 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -34,7 +34,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "region.h" #include "spell.h" #include "spellbook.h" -#include "save.h" #include "ship.h" #include "skill.h" #include "terrain.h" diff --git a/src/laws.c b/src/laws.c index de87afeb1..0838ba6a2 100644 --- a/src/laws.c +++ b/src/laws.c @@ -57,7 +57,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include #include -#include #include #include #include diff --git a/src/modules/arena.c b/src/modules/arena.c index d2b67d9af..a912d273a 100644 --- a/src/modules/arena.c +++ b/src/modules/arena.c @@ -38,7 +38,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include #include -#include #include #include #include diff --git a/src/move.c b/src/move.c index e7148a521..0888b6fde 100644 --- a/src/move.c +++ b/src/move.c @@ -44,7 +44,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include #include -#include #include #include #include diff --git a/src/report.c b/src/report.c index d41d567d7..23dcadb34 100644 --- a/src/report.c +++ b/src/report.c @@ -63,7 +63,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include #include -#include #include #include #include @@ -85,6 +84,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include #include +#include /* libc includes */ #include @@ -133,11 +133,11 @@ static char *gamedate_season(const struct locale *lang) return buf; } -void newline(stream *out) { +void newline(struct stream *out) { sputs("", out); } -void write_spaces(stream *out, size_t num) { +void write_spaces(struct stream *out, size_t num) { static const char spaces[REPORTWIDTH] = " "; while (num > 0) { size_t bytes = (num > REPORTWIDTH) ? REPORTWIDTH : num; @@ -146,7 +146,7 @@ void write_spaces(stream *out, size_t num) { } } -static void centre(stream *out, const char *s, bool breaking) +static void centre(struct stream *out, const char *s, bool breaking) { /* Bei Namen die genau 80 Zeichen lang sind, kann es hier Probleme * geben. Seltsamerweise wird i dann auf MAXINT oder aehnlich @@ -170,7 +170,7 @@ static void centre(stream *out, const char *s, bool breaking) } static void -paragraph(stream *out, const char *str, ptrdiff_t indent, int hanging_indent, +paragraph(struct stream *out, const char *str, ptrdiff_t indent, int hanging_indent, char marker) { size_t length = REPORTWIDTH; @@ -248,7 +248,7 @@ static size_t write_spell_modifier(spell * sp, int flag, const char * str, bool return 0; } -void nr_spell_syntax(stream *out, spellbook_entry * sbe, const struct locale *lang) +void nr_spell_syntax(struct stream *out, spellbook_entry * sbe, const struct locale *lang) { int bytes; char buf[4096]; @@ -436,7 +436,7 @@ void nr_spell_syntax(stream *out, spellbook_entry * sbe, const struct locale *la } -void nr_spell(stream *out, spellbook_entry * sbe, const struct locale *lang) +void nr_spell(struct stream *out, spellbook_entry * sbe, const struct locale *lang) { int bytes, k, itemanz, costtyp; char buf[4096]; @@ -554,7 +554,7 @@ void nr_spell(stream *out, spellbook_entry * sbe, const struct locale *lang) } static void -nr_curses_i(stream *out, int indent, const faction *viewer, objtype_t typ, const void *obj, attrib *a, int self) +nr_curses_i(struct stream *out, int indent, const faction *viewer, objtype_t typ, const void *obj, attrib *a, int self) { for (; a; a = a->next) { char buf[4096]; @@ -582,7 +582,7 @@ nr_curses_i(stream *out, int indent, const faction *viewer, objtype_t typ, const } } -static void nr_curses(stream *out, int indent, const faction *viewer, objtype_t typ, const void *obj) +static void nr_curses(struct stream *out, int indent, const faction *viewer, objtype_t typ, const void *obj) { int self = 0; attrib *a = NULL; @@ -652,7 +652,7 @@ static void nr_curses(stream *out, int indent, const faction *viewer, objtype_t nr_curses_i(out, indent, viewer, typ, obj, a, self); } -static void rps_nowrap(stream *out, const char *s) +static void rps_nowrap(struct stream *out, const char *s) { const char *x = s; size_t indent = 0; @@ -675,7 +675,7 @@ static void rps_nowrap(stream *out, const char *s) } static void -nr_unit(stream *out, const faction * f, const unit * u, int indent, seen_mode mode) +nr_unit(struct stream *out, const faction * f, const unit * u, int indent, seen_mode mode) { attrib *a_otherfaction; char marker; @@ -717,7 +717,7 @@ nr_unit(stream *out, const faction * f, const unit * u, int indent, seen_mode mo } static void -rp_messages(stream *out, message_list * msgs, faction * viewer, int indent, +rp_messages(struct stream *out, message_list * msgs, faction * viewer, int indent, bool categorized) { nrsection *section; @@ -753,7 +753,7 @@ rp_messages(stream *out, message_list * msgs, faction * viewer, int indent, } } -static void rp_battles(stream *out, faction * f) +static void rp_battles(struct stream *out, faction * f) { if (f->battles != NULL) { struct bmsg *bm = f->battles; @@ -773,7 +773,7 @@ static void rp_battles(stream *out, faction * f) } } -static void prices(stream *out, const region * r, const faction * f) +static void prices(struct stream *out, const region * r, const faction * f) { const luxury_type *sale = NULL; struct demand *dmd; @@ -875,7 +875,7 @@ bool see_border(const connection * b, const faction * f, const region * r) return cs; } -static void describe(stream *out, const region * r, faction * f) +static void describe(struct stream *out, const region * r, faction * f) { int n; bool dh; @@ -1268,7 +1268,7 @@ static void describe(stream *out, const region * r, faction * f) } } -static void statistics(stream *out, const region * r, const faction * f) +static void statistics(struct stream *out, const region * r, const faction * f) { const unit *u; int number = 0, p = rpeasants(r); @@ -1620,7 +1620,7 @@ show_allies(const faction * f, const ally * allies, char *buf, size_t size) *bufp = 0; } -static void allies(stream *out, const faction * f) +static void allies(struct stream *out, const faction * f) { const group *g = f->groups; char buf[16384]; @@ -1649,7 +1649,7 @@ static void allies(stream *out, const faction * f) } } -static void guards(stream *out, const region * r, const faction * see) +static void guards(struct stream *out, const region * r, const faction * see) { /* die Partei see sieht dies; wegen * "unbekannte Partei", wenn man es selbst ist... */ @@ -1722,7 +1722,7 @@ static void guards(stream *out, const region * r, const faction * see) } } -static void rpline(stream *out) +static void rpline(struct stream *out) { static char line[REPORTWIDTH + 1]; if (line[0] != '-') { @@ -1732,7 +1732,7 @@ static void rpline(stream *out) swrite(line, sizeof(char), sizeof(line), out); } -static void list_address(stream *out, const faction * uf, quicklist * seenfactions) +static void list_address(struct stream *out, const faction * uf, quicklist * seenfactions) { int qi = 0; quicklist *flist = seenfactions; @@ -1763,7 +1763,7 @@ static void list_address(stream *out, const faction * uf, quicklist * seenfactio } static void -nr_ship(stream *out, const region *r, const ship * sh, const faction * f, +nr_ship(struct stream *out, const region *r, const ship * sh, const faction * f, const unit * captain) { char buffer[8192], *bufp = buffer; @@ -1836,7 +1836,7 @@ nr_ship(stream *out, const region *r, const ship * sh, const faction * f, } static void -nr_building(stream *out, const region *r, const building *b, const faction *f) +nr_building(struct stream *out, const region *r, const building *b, const faction *f) { int i, bytes; const char *name, *bname, *billusion = NULL; @@ -1908,7 +1908,7 @@ nr_building(stream *out, const region *r, const building *b, const faction *f) } } -static void nr_paragraph(stream *out, message * m, faction * f) +static void nr_paragraph(struct stream *out, message * m, faction * f) { int bytes; char buf[4096], *bufp = buf; @@ -1924,14 +1924,14 @@ static void nr_paragraph(stream *out, message * m, faction * f) } typedef struct cb_data { - stream *out; + struct stream *out; char *start, *writep; size_t size; const faction *f; int maxtravel, counter; } cb_data; -static void init_cb(cb_data *data, stream *out, char *buffer, size_t size, const faction *f) { +static void init_cb(cb_data *data, struct stream *out, char *buffer, size_t size, const faction *f) { data->out = out; data->writep = buffer; data->start = buffer; @@ -1996,7 +1996,7 @@ static void cb_write_travelthru(region *r, unit *u, void *cbdata) { } } -void write_travelthru(stream *out, region *r, const faction *f) +void write_travelthru(struct stream *out, region *r, const faction *f) { int maxtravel; char buf[8192]; diff --git a/src/spells.c b/src/spells.c index 46eda80bd..44bd07dec 100644 --- a/src/spells.c +++ b/src/spells.c @@ -47,7 +47,6 @@ #include #include #include -#include #include #include #include @@ -109,7 +108,7 @@ static double zero_effect = 0.0; attrib_type at_wdwpyramid = { - "wdwpyramid", NULL, NULL, NULL, a_writevoid, a_readvoid + "wdwpyramid", NULL, NULL, NULL, NULL, NULL }; /* ----------------------------------------------------------------------- */ diff --git a/src/spells/borders.c b/src/spells/borders.c index 474d37796..04f7e15f4 100644 --- a/src/spells/borders.c +++ b/src/spells/borders.c @@ -8,7 +8,6 @@ #include #include #include -#include #include #include diff --git a/src/summary.c b/src/summary.c index d28b75202..8e9dea65d 100644 --- a/src/summary.c +++ b/src/summary.c @@ -22,7 +22,6 @@ #include #include #include -#include #include #include #include diff --git a/src/triggers/changefaction.c b/src/triggers/changefaction.c index 13785dfb9..218a8461f 100644 --- a/src/triggers/changefaction.c +++ b/src/triggers/changefaction.c @@ -22,7 +22,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. /* kernel includes */ #include -#include #include /* util includes */ diff --git a/src/util/attrib.c b/src/util/attrib.c index e6392aeeb..5e158f263 100644 --- a/src/util/attrib.c +++ b/src/util/attrib.c @@ -29,6 +29,88 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include +int a_readint(attrib * a, void *owner, struct gamedata *data) +{ + int n; + READ_INT(data->store, &n); + if (a) a->data.i = n; + return AT_READ_OK; +} + +void a_writeint(const attrib * a, const void *owner, struct storage *store) +{ + WRITE_INT(store, a->data.i); +} + +int a_readshorts(attrib * a, void *owner, struct gamedata *data) +{ + int n; + READ_INT(data->store, &n); + a->data.sa[0] = (short)n; + READ_INT(data->store, &n); + a->data.sa[1] = (short)n; + return AT_READ_OK; +} + +void a_writeshorts(const attrib * a, const void *owner, struct storage *store) +{ + WRITE_INT(store, a->data.sa[0]); + WRITE_INT(store, a->data.sa[1]); +} + +int a_readchars(attrib * a, void *owner, struct gamedata *data) +{ + int i; + for (i = 0; i != 4; ++i) { + int n; + READ_INT(data->store, &n); + a->data.ca[i] = (char)n; + } + return AT_READ_OK; +} + +void a_writechars(const attrib * a, const void *owner, struct storage *store) +{ + int i; + + for (i = 0; i != 4; ++i) { + WRITE_INT(store, a->data.ca[i]); + } +} + +#define DISPLAYSIZE 8192 +int a_readstring(attrib * a, void *owner, struct gamedata *data) +{ + char buf[DISPLAYSIZE]; + char * result = 0; + int e; + size_t len = 0; + do { + e = READ_STR(data->store, buf, sizeof(buf)); + if (result) { + result = realloc(result, len + DISPLAYSIZE - 1); + strcpy(result + len, buf); + len += DISPLAYSIZE - 1; + } + else { + result = _strdup(buf); + } + } while (e == ENOMEM); + a->data.v = result; + return AT_READ_OK; +} + +void a_writestring(const attrib * a, const void *owner, struct storage *store) +{ + assert(a->data.v); + WRITE_STR(store, (const char *)a->data.v); +} + +void a_finalizestring(attrib * a) +{ + free(a->data.v); +} + #define MAXATHASH 61 static attrib_type *at_hash[MAXATHASH]; diff --git a/src/util/attrib.h b/src/util/attrib.h index b9378ab55..8b7a00e6a 100644 --- a/src/util/attrib.h +++ b/src/util/attrib.h @@ -81,6 +81,20 @@ extern "C" { int a_read(struct gamedata *data, attrib ** attribs, void *owner); void a_write(struct storage *store, const attrib * attribs, const void *owner); + int a_readint(struct attrib *a, void *owner, struct gamedata *); + void a_writeint(const struct attrib *a, const void *owner, + struct storage *store); + int a_readshorts(struct attrib *a, void *owner, struct gamedata *); + void a_writeshorts(const struct attrib *a, const void *owner, + struct storage *store); + int a_readchars(struct attrib *a, void *owner, struct gamedata *); + void a_writechars(const struct attrib *a, const void *owner, + struct storage *store); + int a_readstring(struct attrib *a, void *owner, struct gamedata *); + void a_writestring(const struct attrib *a, const void *owner, + struct storage *); + void a_finalizestring(struct attrib *a); + void attrib_done(void); #define DEFAULT_AGE NULL diff --git a/src/util/attrib.test.c b/src/util/attrib.test.c index 0f189015a..1d79bd6b8 100644 --- a/src/util/attrib.test.c +++ b/src/util/attrib.test.c @@ -1,6 +1,10 @@ #include #include "attrib.h" +#include + +#include +#include #include #include @@ -16,7 +20,6 @@ static void test_attrib_new(CuTest * tc) CuAssertPtrEquals(tc, 0, a); } - static void test_attrib_add(CuTest * tc) { attrib_type at_foo = { "foo" }; @@ -52,7 +55,6 @@ static void test_attrib_remove_self(CuTest * tc) { a_removeall(&alist, NULL); } - static void test_attrib_removeall(CuTest * tc) { const attrib_type at_foo = { "foo" }; const attrib_type at_bar = { "bar" }; @@ -110,6 +112,86 @@ static void test_attrib_nexttype(CuTest * tc) a_removeall(&alist, &at_bar); } +static void test_attrib_rwstring(CuTest *tc) { + gamedata data; + storage store; + attrib a = { 0 }; + + test_setup(); + a.data.v = _strdup("Hello World"); + mstream_init(&data.strm); + gamedata_init(&data, &store, RELEASE_VERSION); + a_writestring(&a, NULL, &store); + a_finalizestring(&a); + data.strm.api->rewind(data.strm.handle); + a_readstring(&a, NULL, &data); + CuAssertStrEquals(tc, "Hello World", (const char *)a.data.v); + mstream_done(&data.strm); + gamedata_done(&data); + test_cleanup(); +} + +static void test_attrib_rwint(CuTest *tc) { + gamedata data; + storage store; + attrib a = { 0 }; + + test_setup(); + a.data.i = 42; + mstream_init(&data.strm); + gamedata_init(&data, &store, RELEASE_VERSION); + a_writeint(&a, NULL, &store); + a.data.i = 0; + data.strm.api->rewind(data.strm.handle); + a_readint(&a, NULL, &data); + CuAssertIntEquals(tc, 42, a.data.i); + mstream_done(&data.strm); + gamedata_done(&data); + test_cleanup(); +} + +static void test_attrib_rwchars(CuTest *tc) { + gamedata data; + storage store; + attrib a = { 0 }; + + test_setup(); + a.data.ca[0] = 1; + a.data.ca[3] = 42; + mstream_init(&data.strm); + gamedata_init(&data, &store, RELEASE_VERSION); + a_writeint(&a, NULL, &store); + memset(a.data.ca, 42, 0); + data.strm.api->rewind(data.strm.handle); + a_readint(&a, NULL, &data); + CuAssertIntEquals(tc, 1, a.data.ca[0]); + CuAssertIntEquals(tc, 42, a.data.ca[3]); + mstream_done(&data.strm); + gamedata_done(&data); + test_cleanup(); +} + +static void test_attrib_rwshorts(CuTest *tc) { + gamedata data; + storage store; + attrib a = { 0 }; + a.data.sa[0] = -4; + a.data.sa[1] = 42; + + test_setup(); + mstream_init(&data.strm); + gamedata_init(&data, &store, RELEASE_VERSION); + a_writeint(&a, NULL, &store); + memset(a.data.ca, 42, 0); + data.strm.api->rewind(data.strm.handle); + a_readint(&a, NULL, &data); + CuAssertIntEquals(tc, -4, a.data.sa[0]); + CuAssertIntEquals(tc, 42, a.data.sa[1]); + mstream_done(&data.strm); + gamedata_done(&data); + test_cleanup(); +} + CuSuite *get_attrib_suite(void) { CuSuite *suite = CuSuiteNew(); @@ -119,5 +201,9 @@ CuSuite *get_attrib_suite(void) SUITE_ADD_TEST(suite, test_attrib_removeall); SUITE_ADD_TEST(suite, test_attrib_remove_self); SUITE_ADD_TEST(suite, test_attrib_nexttype); + SUITE_ADD_TEST(suite, test_attrib_rwstring); + SUITE_ADD_TEST(suite, test_attrib_rwint); + SUITE_ADD_TEST(suite, test_attrib_rwchars); + SUITE_ADD_TEST(suite, test_attrib_rwshorts); return suite; } From 0eaa750ef4d6af7cd40d1bd1abb0e42f490082ab Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 13 Nov 2016 19:47:36 +0100 Subject: [PATCH 2/9] move read/write_attribs to attrib.h, too --- src/guard.c | 1 - src/kernel/save.c | 30 ------------------------------ src/kernel/save.h | 3 --- src/util/attrib.c | 30 ++++++++++++++++++++++++++++++ src/util/attrib.h | 3 +++ 5 files changed, 33 insertions(+), 34 deletions(-) diff --git a/src/guard.c b/src/guard.c index adacd1bf6..ee26e467b 100644 --- a/src/guard.c +++ b/src/guard.c @@ -24,7 +24,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "monster.h" #include -#include #include #include #include diff --git a/src/kernel/save.c b/src/kernel/save.c index 4a1b7897e..80b8becc9 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -670,36 +670,6 @@ writeorder(struct gamedata *data, const struct order *ord, WRITE_STR(data->store, obuf); } -int read_attribs(gamedata *data, attrib **alist, void *owner) { - int result; - if (data->version < ATHASH_VERSION) { - result = a_read_orig(data, alist, owner); - } - else { - result = a_read(data, alist, owner); - } - if (result == AT_READ_DEPR) { - /* handle deprecated attributes */ - attrib *a = *alist; - while (a) { - if (a->type->upgrade) { - a->type->upgrade(alist, a); - } - a = a->nexttype; - } - } - return result; -} - -void write_attribs(storage *store, attrib *alist, const void *owner) -{ -#if RELEASE_VERSION < ATHASH_VERSION - a_write_orig(store, alist, owner); -#else - a_write(store, alist, owner); -#endif -} - unit *read_unit(struct gamedata *data) { unit *u; diff --git a/src/kernel/save.h b/src/kernel/save.h index aa11ac1ad..883df6ea8 100644 --- a/src/kernel/save.h +++ b/src/kernel/save.h @@ -56,9 +56,6 @@ extern "C" { void read_spellbook(struct spellbook **bookp, struct gamedata *data, int(*get_level)(const struct spell * sp, void *), void * cbdata); void write_spellbook(const struct spellbook *book, struct storage *store); - void write_attribs(struct storage *store, struct attrib *alist, const void *owner); - int read_attribs(struct gamedata *store, struct attrib **alist, void *owner); - void write_unit(struct gamedata *data, const struct unit *u); struct unit *read_unit(struct gamedata *data); diff --git a/src/util/attrib.c b/src/util/attrib.c index 5e158f263..6b39b56ae 100644 --- a/src/util/attrib.c +++ b/src/util/attrib.c @@ -29,6 +29,36 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include +int read_attribs(gamedata *data, attrib **alist, void *owner) { + int result; + if (data->version < ATHASH_VERSION) { + result = a_read_orig(data, alist, owner); + } + else { + result = a_read(data, alist, owner); + } + if (result == AT_READ_DEPR) { + /* handle deprecated attributes */ + attrib *a = *alist; + while (a) { + if (a->type->upgrade) { + a->type->upgrade(alist, a); + } + a = a->nexttype; + } + } + return result; +} + +void write_attribs(storage *store, attrib *alist, const void *owner) +{ +#if RELEASE_VERSION < ATHASH_VERSION + a_write_orig(store, alist, owner); +#else + a_write(store, alist, owner); +#endif +} + int a_readint(attrib * a, void *owner, struct gamedata *data) { int n; diff --git a/src/util/attrib.h b/src/util/attrib.h index 8b7a00e6a..d93ce1240 100644 --- a/src/util/attrib.h +++ b/src/util/attrib.h @@ -66,6 +66,9 @@ extern "C" { extern void at_register(attrib_type * at); extern void at_deprecate(const char * name, int(*reader)(attrib *, void *, struct gamedata *)); + void write_attribs(struct storage *store, struct attrib *alist, const void *owner); + int read_attribs(struct gamedata *store, struct attrib **alist, void *owner); + extern attrib *a_select(attrib * a, const void *data, bool(*compare) (const attrib *, const void *)); extern attrib *a_find(attrib * a, const attrib_type * at); From 3cc719ba938a305cc44070427774551d4092d75a Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 13 Nov 2016 19:51:41 +0100 Subject: [PATCH 3/9] fix gcc build --- src/util/attrib.c | 1 + src/util/attrib.test.c | 1 + 2 files changed, 2 insertions(+) diff --git a/src/util/attrib.c b/src/util/attrib.c index 6b39b56ae..4bdf35bfa 100644 --- a/src/util/attrib.c +++ b/src/util/attrib.c @@ -28,6 +28,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include #include +#include int read_attribs(gamedata *data, attrib **alist, void *owner) { int result; diff --git a/src/util/attrib.test.c b/src/util/attrib.test.c index 1d79bd6b8..31c8a2d0d 100644 --- a/src/util/attrib.test.c +++ b/src/util/attrib.test.c @@ -7,6 +7,7 @@ #include #include #include +#include static void test_attrib_new(CuTest * tc) { From d28d3f4690ae1ac388612417da09cc049af1d963 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 14 Nov 2016 01:00:48 +0100 Subject: [PATCH 4/9] move read/write_items to item.c. fix bad memset in attrib.test.c. --- src/kernel/item.c | 42 ++++++++++++++++++++++++++++++++++++++++++ src/kernel/item.h | 5 +++++ src/kernel/save.c | 40 ---------------------------------------- src/kernel/save.h | 3 --- src/modules/museum.c | 1 - src/util/attrib.test.c | 4 ++-- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/kernel/item.c b/src/kernel/item.c index 64d01bce0..f9234368c 100644 --- a/src/kernel/item.c +++ b/src/kernel/item.c @@ -51,6 +51,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include +#include + /* libc includes */ #include #include @@ -1189,6 +1191,46 @@ static item *default_spoil(const struct race *rc, int size) return itm; } +void read_items(struct storage *store, item ** ilist) +{ + for (;;) { + char ibuf[32]; + const item_type *itype; + int i; + READ_STR(store, ibuf, sizeof(ibuf)); + if (!strcmp("end", ibuf)) { + break; + } + itype = it_find(ibuf); + READ_INT(store, &i); + if (i <= 0) { + log_error("data contains an entry with %d %s", i, resourcename(itype->rtype, NMF_PLURAL)); + } + else { + if (itype && itype->rtype) { + i_change(ilist, itype, i); + } + else { + log_error("data contains unknown item type %s.", ibuf); + } + assert(itype && itype->rtype); + } + } +} + +void write_items(struct storage *store, item * ilist) +{ + item *itm; + for (itm = ilist; itm; itm = itm->next) { + assert(itm->number >= 0); + if (itm->number) { + WRITE_TOK(store, resourcename(itm->type->rtype, 0)); + WRITE_INT(store, itm->number); + } + } + WRITE_TOK(store, "end"); +} + static void free_itype(item_type *itype) { assert(itype); free(itype->construction); diff --git a/src/kernel/item.h b/src/kernel/item.h index 26231fa2e..c32f38057 100644 --- a/src/kernel/item.h +++ b/src/kernel/item.h @@ -35,6 +35,8 @@ extern "C" { struct troop; struct item; struct order; + struct storage; + struct gamedata; typedef struct item { struct item *next; @@ -247,6 +249,9 @@ extern "C" { void i_freeall(item ** i); item *i_new(const item_type * it, int number); + void read_items(struct storage *store, struct item **it); + void write_items(struct storage *store, struct item *it); + /* convenience: */ item *i_change(item ** items, const item_type * it, int delta); int i_get(const item * i, const item_type * it); diff --git a/src/kernel/save.c b/src/kernel/save.c index 80b8becc9..cefbf3cc5 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -394,33 +394,6 @@ void create_backup(char *file) #endif } -void read_items(struct storage *store, item ** ilist) -{ - for (;;) { - char ibuf[32]; - const item_type *itype; - int i; - READ_STR(store, ibuf, sizeof(ibuf)); - if (!strcmp("end", ibuf)) { - break; - } - itype = it_find(ibuf); - READ_INT(store, &i); - if (i <= 0) { - log_error("data contains an entry with %d %s", i, resourcename(itype->rtype, NMF_PLURAL)); - } - else { - if (itype && itype->rtype) { - i_change(ilist, itype, i); - } - else { - log_error("data contains unknown item type %s.", ibuf); - } - assert(itype && itype->rtype); - } - } -} - static void read_alliances(struct gamedata *data) { storage *store = data->store; @@ -553,19 +526,6 @@ void write_alliances(struct gamedata *data) WRITE_SECTION(data->store); } -void write_items(struct storage *store, item * ilist) -{ - item *itm; - for (itm = ilist; itm; itm = itm->next) { - assert(itm->number >= 0); - if (itm->number) { - WRITE_TOK(store, resourcename(itm->type->rtype, 0)); - WRITE_INT(store, itm->number); - } - } - WRITE_TOK(store, "end"); -} - static int resolve_owner(variant id, void *address) { region_owner *owner = (region_owner *)address; diff --git a/src/kernel/save.h b/src/kernel/save.h index 883df6ea8..4d310310c 100644 --- a/src/kernel/save.h +++ b/src/kernel/save.h @@ -50,9 +50,6 @@ extern "C" { int current_turn(void); - void read_items(struct storage *store, struct item **it); - void write_items(struct storage *store, struct item *it); - void read_spellbook(struct spellbook **bookp, struct gamedata *data, int(*get_level)(const struct spell * sp, void *), void * cbdata); void write_spellbook(const struct spellbook *book, struct storage *store); diff --git a/src/modules/museum.c b/src/modules/museum.c index 5ad9092a4..e934a71a4 100644 --- a/src/modules/museum.c +++ b/src/modules/museum.c @@ -32,7 +32,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include #include -#include #include #include #include diff --git a/src/util/attrib.test.c b/src/util/attrib.test.c index 31c8a2d0d..7fa2ef82c 100644 --- a/src/util/attrib.test.c +++ b/src/util/attrib.test.c @@ -162,7 +162,7 @@ static void test_attrib_rwchars(CuTest *tc) { mstream_init(&data.strm); gamedata_init(&data, &store, RELEASE_VERSION); a_writeint(&a, NULL, &store); - memset(a.data.ca, 42, 0); + memset(a.data.ca, 0, 4); data.strm.api->rewind(data.strm.handle); a_readint(&a, NULL, &data); CuAssertIntEquals(tc, 1, a.data.ca[0]); @@ -183,7 +183,7 @@ static void test_attrib_rwshorts(CuTest *tc) { mstream_init(&data.strm); gamedata_init(&data, &store, RELEASE_VERSION); a_writeint(&a, NULL, &store); - memset(a.data.ca, 42, 0); + memset(a.data.ca, 0, 4); data.strm.api->rewind(data.strm.handle); a_readint(&a, NULL, &data); CuAssertIntEquals(tc, -4, a.data.sa[0]); From d0b4f162135906e20e9b4bbd2a82e91299892ca5 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 14 Nov 2016 01:05:10 +0100 Subject: [PATCH 5/9] reduce item.h include dependencies --- src/kernel/alliance.c | 1 - src/kernel/skills.c | 1 - 2 files changed, 2 deletions(-) diff --git a/src/kernel/alliance.c b/src/kernel/alliance.c index 7a65c4db5..36d054323 100644 --- a/src/kernel/alliance.c +++ b/src/kernel/alliance.c @@ -23,7 +23,6 @@ without prior permission by the authors of Eressea. #include #include #include -#include #include /* util includes */ diff --git a/src/kernel/skills.c b/src/kernel/skills.c index 17e1194c1..488800115 100644 --- a/src/kernel/skills.c +++ b/src/kernel/skills.c @@ -21,7 +21,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "skill.h" #include "curse.h" -#include "item.h" #include "race.h" #include "region.h" #include "terrain.h" From 0472ac761ea132c228da41b3bdecce4edc228b28 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 14 Nov 2016 01:22:50 +0100 Subject: [PATCH 6/9] declutter save.h (items, spellbooks) --- src/kernel/save.c | 50 --------------------------------------- src/kernel/save.h | 3 --- src/kernel/spellbook.c | 53 ++++++++++++++++++++++++++++++++++++++++++ src/kernel/spellbook.h | 5 ++++ 4 files changed, 58 insertions(+), 53 deletions(-) diff --git a/src/kernel/save.c b/src/kernel/save.c index cefbf3cc5..143912832 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -1194,56 +1194,6 @@ int get_spell_level_faction(const spell * sp, void * cbdata) return 0; } -void read_spellbook(spellbook **bookp, gamedata *data, int(*get_level)(const spell * sp, void *), void * cbdata) -{ - for (;;) { - spell *sp = 0; - char spname[64]; - int level = 0; - - READ_TOK(data->store, spname, sizeof(spname)); - if (strcmp(spname, "end") == 0) - break; - if (bookp) { - sp = find_spell(spname); - if (!sp) { - log_error("read_spells: could not find spell '%s'", spname); - } - } - if (data->version >= SPELLBOOK_VERSION) { - READ_INT(data->store, &level); - } - if (sp) { - spellbook * sb = *bookp; - if (level <= 0 && get_level) { - level = get_level(sp, cbdata); - } - if (!sb) { - *bookp = create_spellbook(0); - sb = *bookp; - } - if (level > 0 && (data->version >= SPELLBOOK_VERSION || !spellbook_get(sb, sp))) { - spellbook_add(sb, sp, level); - } - } - } -} - -void write_spellbook(const struct spellbook *book, struct storage *store) -{ - quicklist *ql; - int qi; - - if (book) { - for (ql = book->spells, qi = 0; ql; ql_advance(&ql, &qi, 1)) { - spellbook_entry *sbe = (spellbook_entry *)ql_get(ql, qi); - WRITE_TOK(store, sbe->sp->sname); - WRITE_INT(store, sbe->level); - } - } - WRITE_TOK(store, "end"); -} - static char * getpasswd(int fno) { const char *prefix = itoa36(fno); size_t len = strlen(prefix); diff --git a/src/kernel/save.h b/src/kernel/save.h index 4d310310c..acaf74a3b 100644 --- a/src/kernel/save.h +++ b/src/kernel/save.h @@ -50,9 +50,6 @@ extern "C" { int current_turn(void); - void read_spellbook(struct spellbook **bookp, struct gamedata *data, int(*get_level)(const struct spell * sp, void *), void * cbdata); - void write_spellbook(const struct spellbook *book, struct storage *store); - void write_unit(struct gamedata *data, const struct unit *u); struct unit *read_unit(struct gamedata *data); diff --git a/src/kernel/spellbook.c b/src/kernel/spellbook.c index 96410170b..da8fcd563 100644 --- a/src/kernel/spellbook.c +++ b/src/kernel/spellbook.c @@ -3,9 +3,12 @@ #include #include #include +#include #include "spellbook.h" +#include + #include #include #include @@ -18,6 +21,56 @@ spellbook * create_spellbook(const char * name) return result; } +void read_spellbook(spellbook **bookp, gamedata *data, int(*get_level)(const spell * sp, void *), void * cbdata) +{ + for (;;) { + spell *sp = 0; + char spname[64]; + int level = 0; + + READ_TOK(data->store, spname, sizeof(spname)); + if (strcmp(spname, "end") == 0) + break; + if (bookp) { + sp = find_spell(spname); + if (!sp) { + log_error("read_spells: could not find spell '%s'", spname); + } + } + if (data->version >= SPELLBOOK_VERSION) { + READ_INT(data->store, &level); + } + if (sp) { + spellbook * sb = *bookp; + if (level <= 0 && get_level) { + level = get_level(sp, cbdata); + } + if (!sb) { + *bookp = create_spellbook(0); + sb = *bookp; + } + if (level > 0 && (data->version >= SPELLBOOK_VERSION || !spellbook_get(sb, sp))) { + spellbook_add(sb, sp, level); + } + } + } +} + +void write_spellbook(const struct spellbook *book, struct storage *store) +{ + quicklist *ql; + int qi; + + if (book) { + for (ql = book->spells, qi = 0; ql; ql_advance(&ql, &qi, 1)) { + spellbook_entry *sbe = (spellbook_entry *)ql_get(ql, qi); + WRITE_TOK(store, sbe->sp->sname); + WRITE_INT(store, sbe->level); + } + } + WRITE_TOK(store, "end"); +} + void spellbook_add(spellbook *sb, struct spell * sp, int level) { spellbook_entry * sbe; diff --git a/src/kernel/spellbook.h b/src/kernel/spellbook.h index d0e25da23..84ec0ce5f 100644 --- a/src/kernel/spellbook.h +++ b/src/kernel/spellbook.h @@ -24,6 +24,8 @@ extern "C" { #endif struct spell; + struct storage; + struct gamedata; struct quicklist; typedef struct spellbook_entry { @@ -39,6 +41,9 @@ extern "C" { spellbook * create_spellbook(const char * name); + void read_spellbook(struct spellbook **bookp, struct gamedata *data, int(*get_level)(const struct spell * sp, void *), void * cbdata); + void write_spellbook(const struct spellbook *book, struct storage *store); + void spellbook_add(spellbook *sbp, struct spell * sp, int level); int spellbook_foreach(spellbook *sb, int(*callback)(spellbook_entry *, void *), void * data); void spellbook_clear(spellbook *sb); From 2b07ae810c570aca6ef0f9d7e7e4593268707944 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 14 Nov 2016 01:23:41 +0100 Subject: [PATCH 7/9] kil xmlreport files. remove unnecessary includes. --- src/kernel/connection.c | 1 - src/kernel/group.c | 1 - src/magic.c | 1 - src/modules/gmcmd.c | 1 - src/xmlreport.c | 800 ---------------------------------------- src/xmlreport.h | 26 -- 6 files changed, 830 deletions(-) delete mode 100644 src/xmlreport.c delete mode 100644 src/xmlreport.h diff --git a/src/kernel/connection.c b/src/kernel/connection.c index 9bbff5778..f10fbc53d 100644 --- a/src/kernel/connection.c +++ b/src/kernel/connection.c @@ -21,7 +21,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "connection.h" #include "region.h" -#include "save.h" #include "terrain.h" #include "unit.h" diff --git a/src/kernel/group.c b/src/kernel/group.c index 054cf44dc..8753531a3 100755 --- a/src/kernel/group.c +++ b/src/kernel/group.c @@ -23,7 +23,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. /* kernel includes */ #include "ally.h" #include "faction.h" -#include "save.h" #include "unit.h" /* attrib includes */ diff --git a/src/magic.c b/src/magic.c index 3f1008cc4..1ac2255c1 100644 --- a/src/magic.c +++ b/src/magic.c @@ -38,7 +38,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include #include -#include #include #include #include diff --git a/src/modules/gmcmd.c b/src/modules/gmcmd.c index b9e1bc342..810867a21 100644 --- a/src/modules/gmcmd.c +++ b/src/modules/gmcmd.c @@ -29,7 +29,6 @@ #include #include #include -#include #include #include #include diff --git a/src/xmlreport.c b/src/xmlreport.c deleted file mode 100644 index fd2b5e464..000000000 --- a/src/xmlreport.c +++ /dev/null @@ -1,800 +0,0 @@ -/* -+-------------------+ Enno Rehling -| Eressea PBEM host | Christian Schlittchen -| (c) 1998 - 2008 | Katja Zedel -+-------------------+ -This program may not be used, modified or distributed -without prior permission by the authors of Eressea. -*/ - -#include -#include -#include "xmlreport.h" - -#define XML_ATL_NAMESPACE (const xmlChar *) "http://www.eressea.de/XML/2008/atlantis" -#define XML_XML_LANG (const xmlChar *) "lang" - -/* modules include */ -#include - -/* attributes include */ -#include -#include -#include -#include -#include - -/* gamecode includes */ -#include "laws.h" -#include "economy.h" -#include "move.h" - -/* kernel includes */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -/* util includes */ -#include -#include -#include -#include -#include -#include -#include - -#ifdef USE_LIBXML2 -/* libxml2 includes */ -#include -#include -#ifdef USE_ICONV -#include -#endif -#endif - -/* libc includes */ -#include -#include -#include -#include -#include -#include - -#define L10N(x) x - -typedef struct xml_context { - xmlDocPtr doc; - xmlNsPtr ns_atl; - xmlNsPtr ns_xml; -} xml_context; - -static xmlNodePtr -xml_link(report_context * ctx, const xmlChar * rel, const xmlChar * ref) -{ - xml_context *xct = (xml_context *) ctx->userdata; - xmlNodePtr node = xmlNewNode(xct->ns_atl, BAD_CAST "link"); - - xmlNewNsProp(node, xct->ns_atl, BAD_CAST "rel", rel); - xmlNewNsProp(node, xct->ns_atl, BAD_CAST "ref", ref); - - return node; -} - -static const xmlChar *xml_ref_unit(const unit * u) -{ - static char idbuf[20]; - _snprintf(idbuf, sizeof(idbuf), "unit_%d", u->no); - return (const xmlChar *)idbuf; -} - -static const xmlChar *xml_ref_faction(const faction * f) -{ - static char idbuf[20]; - _snprintf(idbuf, sizeof(idbuf), "fctn_%d", f->no); - return (const xmlChar *)idbuf; -} - -static const xmlChar *xml_ref_group(const group * g) -{ - static char idbuf[20]; - _snprintf(idbuf, sizeof(idbuf), "grp_%d", g->gid); - return (const xmlChar *)idbuf; -} - -static const xmlChar *xml_ref_prefix(const char *str) -{ - static char idbuf[20]; - _snprintf(idbuf, sizeof(idbuf), "pref_%s", str); - return (const xmlChar *)idbuf; -} - -static const xmlChar *xml_ref_building(const building * b) -{ - static char idbuf[20]; - _snprintf(idbuf, sizeof(idbuf), "bldg_%d", b->no); - return (const xmlChar *)idbuf; -} - -static const xmlChar *xml_ref_ship(const ship * sh) -{ - static char idbuf[20]; - _snprintf(idbuf, sizeof(idbuf), "shp_%d", sh->no); - return (const xmlChar *)idbuf; -} - -static const xmlChar *xml_ref_region(const region * r) -{ - static char idbuf[20]; - _snprintf(idbuf, sizeof(idbuf), "rgn_%d", r->uid); - return (const xmlChar *)idbuf; -} - -static xmlNodePtr xml_inventory(report_context * ctx, item * items, unit * u) -{ - xml_context *xct = (xml_context *) ctx->userdata; - xmlNodePtr node = xmlNewNode(xct->ns_atl, BAD_CAST "items"); - item *itm; - - for (itm = items; itm; itm = itm->next) { - xmlNodePtr child; - const char *name; - int n; - - child = xmlAddChild(node, xmlNewNode(xct->ns_atl, BAD_CAST "item")); - report_item(u, itm, ctx->f, NULL, &name, &n, true); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "ref", (xmlChar *) name); - xmlNodeAddContent(child, (xmlChar *) itoab(n, 10)); - } - return node; -} - -#ifdef TODO /*spellbooks */ -static xmlNodePtr -xml_spells(report_context * ctx, quicklist * slist, int maxlevel) -{ - xml_context *xct = (xml_context *) ctx->userdata; - xmlNodePtr child, node = xmlNewNode(xct->ns_atl, BAD_CAST "spells"); - quicklist *ql; - int qi; - - for (ql = slist, qi = 0; ql; ql_advance(&ql, &qi, 1)) { - spell *sp = (spell *) ql_get(ql, qi); - - if (sp->level <= maxlevel) { - child = xmlAddChild(node, xmlNewNode(xct->ns_atl, BAD_CAST "spell")); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "name", BAD_CAST sp->sname); - } - } - return node; -} -#endif - -static xmlNodePtr xml_skills(report_context * ctx, unit * u) -{ - xml_context *xct = (xml_context *) ctx->userdata; - xmlNodePtr child, node = xmlNewNode(xct->ns_atl, BAD_CAST "skills"); - skill *sv; - - for (sv = u->skills; sv != u->skills + u->skill_size; ++sv) { - if (sv->level > 0) { - skill_t sk = sv->id; - int esk = eff_skill(u, sk, u->region); - - child = - xmlNewTextChild(node, xct->ns_atl, BAD_CAST "skill", BAD_CAST itoab(esk, - 10)); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "ref", BAD_CAST skillnames[sk]); - } - } - - return node; -} - -static xmlNodePtr xml_unit(report_context * ctx, unit * u, int mode) -{ - xml_context *xct = (xml_context *) ctx->userdata; - xmlNodePtr node = xmlNewNode(xct->ns_atl, BAD_CAST "unit"); - static const curse_type *itemcloak_ct = 0; - static bool init = false; - xmlNodePtr child; - const char *str, *rcname, *rcillusion; - bool disclosure = (ctx->f == u->faction || omniscient(ctx->f)); - - /* TODO: hitpoints, aura, combatspells, curses */ - - xmlNewNsProp(node, xct->ns_xml, XML_XML_ID, xml_ref_unit(u)); - xmlNewNsProp(node, xct->ns_atl, BAD_CAST "key", BAD_CAST itoa36(u->no)); - xmlNewTextChild(node, xct->ns_atl, BAD_CAST "name", (const xmlChar *)u->name); - xmlNewTextChild(node, xct->ns_atl, BAD_CAST "number", - (const xmlChar *)itoab(u->number, 10)); - - /* optional description */ - str = u_description(u, ctx->f->locale); - if (str) { - child = - xmlNewTextChild(node, xct->ns_atl, BAD_CAST "text", (const xmlChar *)str); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "rel", BAD_CAST "public"); - if (str != u->display) { - xmlNewNsProp(child, xct->ns_atl, XML_XML_LANG, - BAD_CAST locale_name(ctx->f->locale)); - } - } - - /* possible info */ - if (is_guard(u, GUARD_ALL) != 0) { - xmlAddChild(node, xmlNewNode(xct->ns_atl, BAD_CAST "guard")); - } - - /* siege */ - if (fval(u, UFL_SIEGE)) { - building *b = usiege(u); - if (b) { - xmlAddChild(node, xml_link(ctx, BAD_CAST "siege", xml_ref_building(b))); - } - } - - /* TODO: temp/alias */ - - /* race information */ - report_race(u, &rcname, &rcillusion); - if (disclosure) { - child = xmlAddChild(node, xmlNewNode(xct->ns_atl, BAD_CAST "race")); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "rel", BAD_CAST "true"); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "ref", (const xmlChar *)rcname); - if (rcillusion) { - child = xmlAddChild(node, xmlNewNode(xct->ns_atl, BAD_CAST "race")); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "rel", BAD_CAST "stealth"); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "ref", - (const xmlChar *)rcillusion); - } - } else { - child = xmlAddChild(node, xmlNewNode(xct->ns_atl, BAD_CAST "race")); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "ref", - (const xmlChar *)(rcillusion ? rcillusion : rcname)); - } - - /* group and prefix information. we only write the prefix if we really must */ - if (fval(u, UFL_GROUP)) { - attrib *a = a_find(u->attribs, &at_group); - if (a != NULL) { - const group *g = (const group *)a->data.v; - if (disclosure) { - child = xmlAddChild(node, xmlNewNode(xct->ns_atl, BAD_CAST "group")); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "ref", xml_ref_group(g)); - } else { - const char *prefix = get_prefix(g->attribs); - if (prefix) { - child = xmlAddChild(node, xmlNewNode(xct->ns_atl, BAD_CAST "prefix")); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "ref", - xml_ref_prefix(prefix)); - } - } - } - } - - if (disclosure) { - unit *mage; - - str = uprivate(u); - if (str) { - child = - xmlNewTextChild(node, xct->ns_atl, BAD_CAST "text", - (const xmlChar *)str); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "rel", BAD_CAST "private"); - } - - /* familiar info */ - mage = get_familiar_mage(u); - if (mage) - xmlAddChild(node, xml_link(ctx, BAD_CAST "familiar_of", - xml_ref_unit(mage))); - - /* combat status */ - child = xmlAddChild(node, xmlNewNode(xct->ns_atl, BAD_CAST "status")); - xmlSetNsProp(child, xct->ns_atl, BAD_CAST "rel", BAD_CAST "combat"); - xmlSetNsProp(child, xct->ns_atl, BAD_CAST "value", - BAD_CAST combatstatus[u->status]); - - if (fval(u, UFL_NOAID)) { - child = xmlAddChild(node, xmlNewNode(xct->ns_atl, BAD_CAST "status")); - xmlSetNsProp(child, xct->ns_atl, BAD_CAST "rel", BAD_CAST "aid"); - xmlSetNsProp(child, xct->ns_atl, BAD_CAST "value", BAD_CAST "false"); - } - - if (fval(u, UFL_STEALTH)) { - int i = u_geteffstealth(u); - if (i >= 0) { - child = xmlAddChild(node, xmlNewNode(xct->ns_atl, BAD_CAST "status")); - xmlSetNsProp(child, xct->ns_atl, BAD_CAST "rel", BAD_CAST "stealth"); - xmlSetNsProp(child, xct->ns_atl, BAD_CAST "value", BAD_CAST itoab(i, - 10)); - } - } - if (fval(u, UFL_HERO)) { - child = xmlAddChild(node, xmlNewNode(xct->ns_atl, BAD_CAST "status")); - xmlSetNsProp(child, xct->ns_atl, BAD_CAST "rel", BAD_CAST "hero"); - xmlSetNsProp(child, xct->ns_atl, BAD_CAST "value", BAD_CAST "true"); - } - - if (fval(u, UFL_HUNGER)) { - child = xmlAddChild(node, xmlNewNode(xct->ns_atl, BAD_CAST "status")); - xmlSetNsProp(child, xct->ns_atl, BAD_CAST "rel", BAD_CAST "hunger"); - xmlSetNsProp(child, xct->ns_atl, BAD_CAST "value", BAD_CAST "true"); - } - - /* skills */ - if (u->skill_size) { - xmlAddChild(node, xml_skills(ctx, u)); - } - -#ifdef TODO /*spellbooks */ - /* spells */ - if (is_mage(u)) { - sc_mage *mage = get_mage(u); - quicklist *slist = mage->spells; - if (slist) { - xmlAddChild(node, xml_spells(ctx, slist, effskill(u, SK_MAGIC))); - } - } -#endif - } - - /* faction information w/ visibiility */ - child = xmlAddChild(node, xmlNewNode(xct->ns_atl, BAD_CAST "faction")); - if (disclosure) { - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "rel", BAD_CAST "true"); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "ref", - xml_ref_faction(u->faction)); - - if (fval(u, UFL_ANON_FACTION)) { - const faction *sf = visible_faction(NULL, u); - child = xmlAddChild(node, xmlNewNode(xct->ns_atl, BAD_CAST "faction")); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "rel", BAD_CAST "stealth"); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "ref", xml_ref_faction(sf)); - } - } else { - const faction *sf = visible_faction(ctx->f, u); - if (sf == ctx->f) { - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "rel", BAD_CAST "stealth"); - } - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "ref", xml_ref_faction(sf)); - } - - /* the inventory */ - if (u->items) { - item result[MAX_INVENTORY]; - item *show = NULL; - - if (!init) { - init = true; - itemcloak_ct = ct_find("itemcloak"); - } - - if (disclosure) { - show = u->items; - } else { - bool see_items = (mode >= see_unit); - if (see_items) { - if (itemcloak_ct && curse_active(get_curse(u->attribs, itemcloak_ct))) { - see_items = false; - } else { - see_items = effskill(u, SK_STEALTH) < 3; - } - } - if (see_items) { - int n = report_items(u->items, result, MAX_INVENTORY, u, ctx->f); - assert(n >= 0); - if (n > 0) - show = result; - else - show = NULL; - } else { - show = NULL; - } - } - - if (show) { - xmlAddChild(node, xml_inventory(ctx, show, u)); - } - } - - return node; -} - -static xmlNodePtr xml_resources(report_context * ctx, const seen_region * sr) -{ - xml_context *xct = (xml_context *) ctx->userdata; - xmlNodePtr node = NULL; - resource_report result[MAX_RAWMATERIALS]; - int n, size = report_resources(sr, result, MAX_RAWMATERIALS, ctx->f); - - if (size) { - node = xmlNewNode(xct->ns_atl, BAD_CAST "resources"); - for (n = 0; n < size; ++n) { - if (result[n].number >= 0) { - xmlNodePtr child; - - child = xmlAddChild(node, xmlNewNode(xct->ns_atl, BAD_CAST "resource")); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "ref", - (xmlChar *) result[n].name); - if (result[n].level >= 0) { - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "level", - (xmlChar *) itoab(result[n].level, 10)); - } - xmlNodeAddContent(child, (xmlChar *) itoab(result[n].number, 10)); - } - } - } - return node; -} - -static xmlNodePtr xml_diplomacy(report_context * ctx, const struct ally *allies) -{ - xml_context *xct = (xml_context *) ctx->userdata; - xmlNodePtr child, node = xmlNewNode(xct->ns_atl, BAD_CAST "diplomacy"); - const struct ally *sf; - - for (sf = allies; sf; sf = sf->next) { - int i, status = sf->status; - for (i = 0; helpmodes[i].name; ++i) { - if (sf->faction && (status & helpmodes[i].status) == helpmodes[i].status) { - status -= helpmodes[i].status; - child = xmlAddChild(node, xmlNewNode(xct->ns_atl, BAD_CAST "status")); - xmlNewNsProp(child, xct->ns_xml, BAD_CAST "faction", - xml_ref_faction(sf->faction)); - xmlNewNsProp(child, xct->ns_xml, BAD_CAST "status", - (xmlChar *) helpmodes[i].name); - } - } - } - return node; -} - -static xmlNodePtr xml_groups(report_context * ctx, const group * groups) -{ - xml_context *xct = (xml_context *) ctx->userdata; - xmlNodePtr child, node = xmlNewNode(xct->ns_atl, BAD_CAST "faction"); - const group *g; - - for (g = groups; g; g = g->next) { - const char *prefix = get_prefix(g->attribs); - child = xmlAddChild(node, xmlNewNode(xct->ns_atl, BAD_CAST "group")); - xmlNewNsProp(child, xct->ns_xml, XML_XML_ID, xml_ref_group(g)); - xmlNewTextChild(child, xct->ns_atl, BAD_CAST "name", - (const xmlChar *)g->name); - - if (g->allies) - xmlAddChild(child, xml_diplomacy(ctx, g->allies)); - - if (prefix) { - child = xmlAddChild(child, xmlNewNode(xct->ns_atl, BAD_CAST "prefix")); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "ref", xml_ref_prefix(prefix)); - } - } - - return node; -} - -static xmlNodePtr xml_faction(report_context * ctx, faction * f) -{ - xml_context *xct = (xml_context *) ctx->userdata; - xmlNodePtr child, node = xmlNewNode(xct->ns_atl, BAD_CAST "faction"); - - /* TODO: alliance, locale */ - - xmlNewNsProp(node, xct->ns_xml, XML_XML_ID, xml_ref_faction(f)); - xmlNewNsProp(node, xct->ns_atl, BAD_CAST "key", BAD_CAST itoa36(f->no)); - xmlNewTextChild(node, xct->ns_atl, BAD_CAST "name", (const xmlChar *)f->name); - if (f->email) - xmlNewTextChild(node, xct->ns_atl, BAD_CAST "email", - (const xmlChar *)f->email); - if (f->banner) { - child = - xmlNewTextChild(node, xct->ns_atl, BAD_CAST "text", - (const xmlChar *)f->banner); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "rel", BAD_CAST "public"); - } - - if (ctx->f == f) { - xmlAddChild(node, xml_link(ctx, BAD_CAST "race", - BAD_CAST f->race->_name[0])); - - if (f->items) - xmlAddChild(node, xml_inventory(ctx, f->items, NULL)); - if (f->allies) - xmlAddChild(node, xml_diplomacy(ctx, f->allies)); - if (f->groups) - xmlAddChild(node, xml_groups(ctx, f->groups)); - - /* TODO: age, options, score, prefix, magic, immigrants, heroes, nmr, groups */ - } - return node; -} - -static xmlNodePtr -xml_building(report_context * ctx, seen_region * sr, const building * b, - const unit * owner) -{ - xml_context *xct = (xml_context *) ctx->userdata; - xmlNodePtr node = xmlNewNode(xct->ns_atl, BAD_CAST "building"); - xmlNodePtr child; - const char *bname, *billusion; - - xmlNewNsProp(node, xct->ns_xml, XML_XML_ID, xml_ref_building(b)); - xmlNewNsProp(node, xct->ns_atl, BAD_CAST "key", BAD_CAST itoa36(b->no)); - xmlNewTextChild(node, xct->ns_atl, BAD_CAST "name", (const xmlChar *)b->name); - xmlNewTextChild(node, xct->ns_atl, BAD_CAST "size", - (const xmlChar *)itoab(b->size, 10)); - if (b->display && b->display[0]) { - child = - xmlNewTextChild(node, xct->ns_atl, BAD_CAST "text", - (const xmlChar *)b->display); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "rel", BAD_CAST "public"); - } - if (b->besieged) { - xmlNewTextChild(node, xct->ns_atl, BAD_CAST "siege", - (const xmlChar *)itoab(b->besieged, 10)); - } - if (owner) - xmlAddChild(node, xml_link(ctx, BAD_CAST "owner", xml_ref_unit(owner))); - - report_building(b, &bname, &billusion); - if (owner && owner->faction == ctx->f) { - child = xmlAddChild(node, xmlNewNode(xct->ns_atl, BAD_CAST "type")); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "rel", BAD_CAST "true"); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "ref", (const xmlChar *)bname); - if (billusion) { - child = xmlAddChild(node, xmlNewNode(xct->ns_atl, BAD_CAST "type")); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "rel", BAD_CAST "illusion"); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "ref", - (const xmlChar *)billusion); - } - } else { - child = xmlAddChild(node, xmlNewNode(xct->ns_atl, BAD_CAST "type")); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "ref", - (const xmlChar *)(billusion ? billusion : bname)); - } - - return node; -} - -static xmlNodePtr -xml_ship(report_context * ctx, const seen_region * sr, const ship * sh, - const unit * owner) -{ - xml_context *xct = (xml_context *) ctx->userdata; - xmlNodePtr child, node = xmlNewNode(xct->ns_atl, BAD_CAST "ship"); - - xmlNewNsProp(node, xct->ns_xml, XML_XML_ID, xml_ref_ship(sh)); - xmlNewNsProp(node, xct->ns_atl, BAD_CAST "key", BAD_CAST itoa36(sh->no)); - xmlNewTextChild(node, xct->ns_atl, BAD_CAST "name", - (const xmlChar *)sh->name); - xmlNewTextChild(node, xct->ns_atl, BAD_CAST "size", - (const xmlChar *)itoab(sh->size, 10)); - - if (sh->damage) { - xmlNewTextChild(node, xct->ns_atl, BAD_CAST "damage", - (const xmlChar *)itoab(sh->damage, 10)); - } - - if (fval(sr->r->terrain, SEA_REGION) && sh->coast != NODIRECTION) { - xmlNewTextChild(node, xct->ns_atl, BAD_CAST "coast", - BAD_CAST directions[sh->coast]); - } - - child = xmlAddChild(node, xmlNewNode(xct->ns_atl, BAD_CAST "type")); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "ref", - (const xmlChar *)sh->type->name[0]); - - if (sh->display && sh->display[0]) { - child = - xmlNewTextChild(node, xct->ns_atl, BAD_CAST "text", - (const xmlChar *)sh->display); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "rel", BAD_CAST "public"); - } - - if (owner) - xmlAddChild(node, xml_link(ctx, BAD_CAST "owner", xml_ref_unit(owner))); - - if ((owner && owner->faction == ctx->f) || omniscient(ctx->f)) { - int n = 0, p = 0; - getshipweight(sh, &n, &p); - xmlNewTextChild(node, xct->ns_atl, BAD_CAST "cargo", - (const xmlChar *)itoab(n, 10)); - } - return node; -} - -static xmlNodePtr xml_region(report_context * ctx, seen_region * sr) -{ - xml_context *xct = (xml_context *) ctx->userdata; - const region *r = sr->r; - xmlNodePtr node = xmlNewNode(xct->ns_atl, BAD_CAST "region"); - xmlNodePtr child; - int stealthmod = stealth_modifier(sr->mode); - unit *u; - ship *sh = r->ships; - building *b = r->buildings; - plane *pl = rplane(r); - int nx = r->x, ny = r->y; - - pnormalize(&nx, &ny, pl); - adjust_coordinates(ctx->f, &nx, &ny, pl, r); - - /* TODO: entertain-quota, recruits, salary, prices, curses, borders, apparitions (Schemen), spells, travelthru, messages */ - xmlNewNsProp(node, xct->ns_xml, XML_XML_ID, xml_ref_region(r)); - - child = xmlAddChild(node, xmlNewNode(xct->ns_atl, BAD_CAST "coordinate")); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "x", xml_i(nx)); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "y", xml_i(ny)); - if (pl && pl->name) { - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "plane", (xmlChar *) pl->name); - } - - child = xmlAddChild(node, xmlNewNode(xct->ns_atl, BAD_CAST "terrain")); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "ref", (xmlChar *) terrain_name(r)); - - if (r->land != NULL) { - child = - xmlNewTextChild(node, xct->ns_atl, BAD_CAST "name", - (const xmlChar *)r->land->name); - if (r->land->items) { - xmlAddChild(node, xml_inventory(ctx, r->land->items, NULL)); - } - } - if (r->display && r->display[0]) { - child = - xmlNewTextChild(node, xct->ns_atl, BAD_CAST "text", - (const xmlChar *)r->display); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "rel", BAD_CAST "public"); - } - child = xml_resources(ctx, sr); - if (child) - xmlAddChild(node, child); - - child = xmlNewNode(xct->ns_atl, BAD_CAST "terrain"); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "ref", - (const xmlChar *)terrain_name(r)); - - if (sr->mode > see_neighbour) { - /* report all units. they are pre-sorted in an efficient manner */ - u = r->units; - while (b) { - while (b && (!u || u->building != b)) { - xmlAddChild(node, xml_building(ctx, sr, b, NULL)); - b = b->next; - } - if (b) { - child = xmlAddChild(node, xml_building(ctx, sr, b, u)); - while (u && u->building == b) { - xmlAddChild(child, xml_unit(ctx, u, sr->mode)); - u = u->next; - } - b = b->next; - } - } - while (u && !u->ship) { - if (stealthmod > INT_MIN) { - if (u->faction == ctx->f || cansee(ctx->f, r, u, stealthmod)) { - xmlAddChild(node, xml_unit(ctx, u, sr->mode)); - } - } - u = u->next; - } - while (sh) { - while (sh && (!u || u->ship != sh)) { - xmlAddChild(node, xml_ship(ctx, sr, sh, NULL)); - sh = sh->next; - } - if (sh) { - child = xmlAddChild(node, xml_ship(ctx, sr, sh, u)); - while (u && u->ship == sh) { - xmlAddChild(child, xml_unit(ctx, u, sr->mode)); - u = u->next; - } - sh = sh->next; - } - } - } - return node; -} - -static xmlNodePtr report_root(report_context * ctx) -{ - int qi; - quicklist *address; - region *r = ctx->first, *rend = ctx->last; - xml_context *xct = (xml_context *) ctx->userdata; - xmlNodePtr node, child, xmlReport = xmlNewNode(NULL, BAD_CAST "atlantis"); - const char *mailto = locale_string(ctx->f->locale, "mailto"); - const char *mailcmd = locale_string(ctx->f->locale, "mailcmd"); - char zText[128]; - /* TODO: locale, age, options, messages */ - - xct->ns_xml = xmlNewNs(xmlReport, XML_XML_NAMESPACE, BAD_CAST "xml"); - xct->ns_atl = xmlNewNs(xmlReport, XML_ATL_NAMESPACE, NULL); - xmlSetNs(xmlReport, xct->ns_atl); - - node = xmlAddChild(xmlReport, xmlNewNode(xct->ns_atl, BAD_CAST "server")); - if (mailto) { - _snprintf(zText, sizeof(zText), "mailto:%s?subject=%s", mailto, mailcmd); - child = xmlAddChild(node, xmlNewNode(xct->ns_atl, BAD_CAST "delivery")); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "method", BAD_CAST "mail"); - xmlNewNsProp(child, xct->ns_atl, BAD_CAST "href", BAD_CAST zText); - } - xmlNewTextChild(node, xct->ns_atl, BAD_CAST "game", - (xmlChar *) global.gamename); - strftime(zText, sizeof(zText), "%Y-%m-%dT%H:%M:%SZ", - gmtime(&ctx->report_time)); - xmlNewTextChild(node, xct->ns_atl, BAD_CAST "time", (xmlChar *) zText); - xmlNewTextChild(node, xct->ns_atl, BAD_CAST "turn", (xmlChar *) itoab(turn, - 10)); - - for (qi = 0, address = ctx->addresses; address; ql_advance(&address, &qi, 1)) { - faction *f = (faction *) ql_get(address, qi); - xmlAddChild(xmlReport, xml_faction(ctx, f)); - } - - for (; r != rend; r = r->next) { - seen_region *sr = find_seen(ctx->seen, r); - if (sr != NULL) - xmlAddChild(xmlReport, xml_region(ctx, sr)); - } - return xmlReport; -} - -/* main function of the xmlreport. creates the header and traverses all regions */ -static int -report_xml(const char *filename, report_context * ctx, const char *encoding) -{ - xml_context xct; - xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0"); - - xct.doc = doc; - assert(ctx->userdata == NULL); - ctx->userdata = &xct; - - xmlDocSetRootElement(doc, report_root(ctx)); - xmlKeepBlanksDefault(0); - xmlSaveFormatFileEnc(filename, doc, "utf-8", 1); - xmlFreeDoc(doc); - - ctx->userdata = NULL; - - return 0; -} - -void register_xr(void) -{ - register_reporttype("xml", &report_xml, 1 << O_XML); -#ifdef USE_ICONV - utf8 = iconv_open("UTF-8", ""); -#endif -} - -void xmlreport_cleanup(void) -{ -#ifdef USE_ICONV - iconv_close(utf8); -#endif -} diff --git a/src/xmlreport.h b/src/xmlreport.h deleted file mode 100644 index 676fbd1a2..000000000 --- a/src/xmlreport.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - +-------------------+ Christian Schlittchen - | Eressea PBEM host | Katja Zedel - | (c) 1998 - 2005 | Enno Rehling - +-------------------+ - - This program may not be used, modified or distributed - without prior permission by the authors of Eressea. -*/ -#ifndef H_GC_XMLREPORT -#define H_GC_XMLREPORT -#ifdef __cplusplus -extern "C" { -#endif - -#include - - extern void xmlreport_cleanup(void); - extern void register_xr(void); - - extern int crwritemap(const char *filename); - -#ifdef __cplusplus -} -#endif -#endif From 03a94d1264b0d18257e7e59d2958548a9852a61b Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 14 Nov 2016 01:35:45 +0100 Subject: [PATCH 8/9] remove unused and broken backup functionality --- src/bind_eressea.c | 2 +- src/gmtool.c | 4 +--- src/kernel/save.c | 22 +--------------------- src/kernel/save.h | 4 +--- src/kernel/save.test.c | 2 +- 5 files changed, 5 insertions(+), 29 deletions(-) diff --git a/src/bind_eressea.c b/src/bind_eressea.c index d7683ec18..279ce8f4e 100755 --- a/src/bind_eressea.c +++ b/src/bind_eressea.c @@ -19,7 +19,7 @@ void eressea_free_game(void) { } int eressea_read_game(const char * filename) { - return readgame(filename, false); + return readgame(filename); } int eressea_write_game(const char * filename) { diff --git a/src/gmtool.c b/src/gmtool.c index 608ad3c71..05997ed6e 100644 --- a/src/gmtool.c +++ b/src/gmtool.c @@ -823,8 +823,7 @@ void loaddata(state *st) { askstring(st->wnd_status->handle, "save as:", datafile, sizeof(datafile)); if (strlen(datafile) > 0) { - create_backup(datafile); - readgame(datafile, false); + readgame(datafile); st->modified = 0; } } @@ -834,7 +833,6 @@ void savedata(state *st) { askstring(st->wnd_status->handle, "save as:", datafile, sizeof(datafile)); if (strlen(datafile) > 0) { - create_backup(datafile); remove_empty_units(); writegame(datafile); st->modified = 0; diff --git a/src/kernel/save.c b/src/kernel/save.c index 143912832..fa2beabc6 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -378,22 +378,6 @@ race_t typus2race(unsigned char typus) return NORACE; } -void create_backup(char *file) -{ -#ifdef HAVE_LINK - char bfile[MAX_PATH]; - int c = 1; - - if (access(file, R_OK) == 0) - return; - do { - sprintf(bfile, "%s.backup%d", file, c); - c++; - } while (access(bfile, R_OK) == 0); - link(file, bfile); -#endif -} - static void read_alliances(struct gamedata *data) { storage *store = data->store; @@ -1476,7 +1460,7 @@ static int cb_sb_maxlevel(spellbook_entry *sbe, void *cbdata) { return 0; } -int readgame(const char *filename, bool backup) +int readgame(const char *filename) { int n; char path[MAX_PATH]; @@ -1490,10 +1474,6 @@ int readgame(const char *filename, bool backup) log_debug("- reading game data from %s", filename); join_path(datapath(), filename, path, sizeof(path)); - if (backup) { - create_backup(path); - } - F = fopen(path, "rb"); if (!F) { perror(path); diff --git a/src/kernel/save.h b/src/kernel/save.h index acaf74a3b..134fd8007 100644 --- a/src/kernel/save.h +++ b/src/kernel/save.h @@ -45,7 +45,7 @@ extern "C" { int readorders(const char *filename); int creategame(void); - int readgame(const char *filename, bool backup); + int readgame(const char *filename); int writegame(const char *filename); int current_turn(void); @@ -65,8 +65,6 @@ extern "C" { void write_ship(struct gamedata *data, const struct ship *sh); struct ship *read_ship(struct gamedata *data); - void create_backup(char *file); - int write_game(struct gamedata *data); int read_game(struct gamedata *data); diff --git a/src/kernel/save.test.c b/src/kernel/save.test.c index b4d79f4f8..6a2758a1d 100644 --- a/src/kernel/save.test.c +++ b/src/kernel/save.test.c @@ -38,7 +38,7 @@ static void test_readwrite_data(CuTest * tc) char path[MAX_PATH]; test_setup(); CuAssertIntEquals(tc, 0, writegame(filename)); - CuAssertIntEquals(tc, 0, readgame(filename, false)); + CuAssertIntEquals(tc, 0, readgame(filename)); join_path(datapath(), filename, path, sizeof(path)); CuAssertIntEquals(tc, 0, remove(path)); test_cleanup(); From 5e48eac26ac9db105c556ec53a2bc734c73c6383 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 14 Nov 2016 02:21:49 +0100 Subject: [PATCH 9/9] coverity warning: NEGATIVE_RETURNS --- src/kernel/types.h | 2 +- src/keyword.c | 3 +++ src/keyword.h | 2 +- src/keyword.test.c | 4 +++- 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/kernel/types.h b/src/kernel/types.h index e0521635e..e2a3923b5 100644 --- a/src/kernel/types.h +++ b/src/kernel/types.h @@ -144,7 +144,7 @@ typedef enum { P_TREES, P_ALLIANCE, MAXPARAMS, - NOPARAM = -1 + NOPARAM } param_t; typedef enum { /* Fehler und Meldungen im Report */ diff --git a/src/keyword.c b/src/keyword.c index 0bd699836..199811669 100644 --- a/src/keyword.c +++ b/src/keyword.c @@ -14,6 +14,9 @@ const char * keyword(keyword_t kwd) { static char result[32]; // FIXME: static return value + if (kwd==NOKEYWORD) { + return NULL; + } if (!result[0]) { strcpy(result, "keyword::"); } diff --git a/src/keyword.h b/src/keyword.h index 9d5e20f64..e60d0731f 100644 --- a/src/keyword.h +++ b/src/keyword.h @@ -70,7 +70,7 @@ extern "C" K_PAY, K_LOOT, MAXKEYWORDS, - NOKEYWORD = -1 + NOKEYWORD } keyword_t; extern const char *keywords[MAXKEYWORDS]; diff --git a/src/keyword.test.c b/src/keyword.test.c index 903eb0c13..75c31904a 100644 --- a/src/keyword.test.c +++ b/src/keyword.test.c @@ -11,11 +11,13 @@ static void test_init_keywords(CuTest *tc) { struct locale *lang; - test_cleanup(); + test_setup(); lang = get_or_create_locale("en"); locale_setstring(lang, "keyword::move", "MOVE"); init_keywords(lang); CuAssertIntEquals(tc, K_MOVE, get_keyword("move", lang)); + CuAssertStrEquals(tc, "keyword::move", keyword(K_MOVE)); + CuAssertPtrEquals(tc, NULL, (void *)keyword(NOKEYWORD)); test_cleanup(); }