move read/write_items to item.c.

fix bad memset in attrib.test.c.
This commit is contained in:
Enno Rehling 2016-11-14 01:00:48 +01:00
parent 3cc719ba93
commit d28d3f4690
6 changed files with 49 additions and 46 deletions

View File

@ -51,6 +51,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/umlaut.h> #include <util/umlaut.h>
#include <util/rng.h> #include <util/rng.h>
#include <storage.h>
/* libc includes */ /* libc includes */
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
@ -1189,6 +1191,46 @@ static item *default_spoil(const struct race *rc, int size)
return itm; 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) { static void free_itype(item_type *itype) {
assert(itype); assert(itype);
free(itype->construction); free(itype->construction);

View File

@ -35,6 +35,8 @@ extern "C" {
struct troop; struct troop;
struct item; struct item;
struct order; struct order;
struct storage;
struct gamedata;
typedef struct item { typedef struct item {
struct item *next; struct item *next;
@ -247,6 +249,9 @@ extern "C" {
void i_freeall(item ** i); void i_freeall(item ** i);
item *i_new(const item_type * it, int number); 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: */ /* convenience: */
item *i_change(item ** items, const item_type * it, int delta); item *i_change(item ** items, const item_type * it, int delta);
int i_get(const item * i, const item_type * it); int i_get(const item * i, const item_type * it);

View File

@ -394,33 +394,6 @@ void create_backup(char *file)
#endif #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) static void read_alliances(struct gamedata *data)
{ {
storage *store = data->store; storage *store = data->store;
@ -553,19 +526,6 @@ void write_alliances(struct gamedata *data)
WRITE_SECTION(data->store); 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) static int resolve_owner(variant id, void *address)
{ {
region_owner *owner = (region_owner *)address; region_owner *owner = (region_owner *)address;

View File

@ -50,9 +50,6 @@ extern "C" {
int current_turn(void); 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 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_spellbook(const struct spellbook *book, struct storage *store);

View File

@ -32,7 +32,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <kernel/order.h> #include <kernel/order.h>
#include <kernel/plane.h> #include <kernel/plane.h>
#include <kernel/region.h> #include <kernel/region.h>
#include <kernel/save.h>
#include <kernel/terrain.h> #include <kernel/terrain.h>
#include <kernel/unit.h> #include <kernel/unit.h>
#include <kernel/faction.h> #include <kernel/faction.h>

View File

@ -162,7 +162,7 @@ static void test_attrib_rwchars(CuTest *tc) {
mstream_init(&data.strm); mstream_init(&data.strm);
gamedata_init(&data, &store, RELEASE_VERSION); gamedata_init(&data, &store, RELEASE_VERSION);
a_writeint(&a, NULL, &store); a_writeint(&a, NULL, &store);
memset(a.data.ca, 42, 0); memset(a.data.ca, 0, 4);
data.strm.api->rewind(data.strm.handle); data.strm.api->rewind(data.strm.handle);
a_readint(&a, NULL, &data); a_readint(&a, NULL, &data);
CuAssertIntEquals(tc, 1, a.data.ca[0]); CuAssertIntEquals(tc, 1, a.data.ca[0]);
@ -183,7 +183,7 @@ static void test_attrib_rwshorts(CuTest *tc) {
mstream_init(&data.strm); mstream_init(&data.strm);
gamedata_init(&data, &store, RELEASE_VERSION); gamedata_init(&data, &store, RELEASE_VERSION);
a_writeint(&a, NULL, &store); a_writeint(&a, NULL, &store);
memset(a.data.ca, 42, 0); memset(a.data.ca, 0, 4);
data.strm.api->rewind(data.strm.handle); data.strm.api->rewind(data.strm.handle);
a_readint(&a, NULL, &data); a_readint(&a, NULL, &data);
CuAssertIntEquals(tc, -4, a.data.sa[0]); CuAssertIntEquals(tc, -4, a.data.sa[0]);