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; }