forked from github/server
move read/write_items to item.c.
fix bad memset in attrib.test.c.
This commit is contained in:
parent
3cc719ba93
commit
d28d3f4690
|
@ -51,6 +51,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|||
#include <util/umlaut.h>
|
||||
#include <util/rng.h>
|
||||
|
||||
#include <storage.h>
|
||||
|
||||
/* libc includes */
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -32,7 +32,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|||
#include <kernel/order.h>
|
||||
#include <kernel/plane.h>
|
||||
#include <kernel/region.h>
|
||||
#include <kernel/save.h>
|
||||
#include <kernel/terrain.h>
|
||||
#include <kernel/unit.h>
|
||||
#include <kernel/faction.h>
|
||||
|
|
|
@ -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]);
|
||||
|
|
Loading…
Reference in New Issue