From d28d3f4690ae1ac388612417da09cc049af1d963 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 14 Nov 2016 01:00:48 +0100 Subject: [PATCH] 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]);