update to latest version of storage library.

This commit is contained in:
Enno Rehling 2014-11-03 22:29:04 +01:00
parent f623133344
commit dbf60a7ce5
5 changed files with 99 additions and 77 deletions

View File

@ -18,6 +18,8 @@ without prior permission by the authors of Eressea.
#include <kernel/version.h>
#include <storage.h>
#include <stream.h>
#include <filestream.h>
#include <binarystore.h>
#include <math.h>
@ -29,36 +31,37 @@ without prior permission by the authors of Eressea.
static int tolua_storage_create(lua_State * L)
{
const char *filename = tolua_tostring(L, 1, 0);
const char *type = tolua_tostring(L, 2, "rb");
gamedata *data = (gamedata *)calloc(1, sizeof(gamedata));
storage *store = (storage *)calloc(1, sizeof(storage));
FILE * F;
const char *filename = tolua_tostring(L, 1, 0);
const char *type = tolua_tostring(L, 2, "rb");
gamedata *data = (gamedata *)calloc(1, sizeof(gamedata));
storage *store = (storage *)calloc(1, sizeof(storage));
FILE * F;
data->store = store;
data->store = store;
F = fopen(filename, type);
if (strchr(type, 'r')) {
fread(&data->version, sizeof(int), 1, F);
fseek(F, sizeof(int), SEEK_CUR);
}
else if (strchr(type, 'w')) {
int n = STREAM_VERSION;
data->version = RELEASE_VERSION;
fwrite(&data->version, sizeof(int), 1, F);
fwrite(&n, sizeof(int), 1, F);
}
binstore_init(store, F);
tolua_pushusertype(L, (void *)data, TOLUA_CAST "storage");
return 1;
F = fopen(filename, type);
if (strchr(type, 'r')) {
fread(&data->version, sizeof(int), 1, F);
fseek(F, sizeof(int), SEEK_CUR);
}
else if (strchr(type, 'w')) {
int n = STREAM_VERSION;
data->version = RELEASE_VERSION;
fwrite(&data->version, sizeof(int), 1, F);
fwrite(&n, sizeof(int), 1, F);
}
fstream_init(&data->strm, F);
binstore_init(store, &data->strm);
tolua_pushusertype(L, (void *)data, TOLUA_CAST "storage");
return 1;
}
static int tolua_storage_read_unit(lua_State * L)
{
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
struct unit *u = read_unit(data);
tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit");
return 1;
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
struct unit *u = read_unit(data);
tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit");
return 1;
}
static int tolua_storage_write_unit(lua_State * L)
@ -71,76 +74,78 @@ static int tolua_storage_write_unit(lua_State * L)
static int tolua_storage_read_float(lua_State * L)
{
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
float num;
READ_FLT(data->store, &num);
tolua_pushnumber(L, (lua_Number) num);
return 1;
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
float num;
READ_FLT(data->store, &num);
tolua_pushnumber(L, (lua_Number)num);
return 1;
}
static int tolua_storage_read_int(lua_State * L)
{
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
int num;
READ_INT(data->store, &num);
tolua_pushnumber(L, (lua_Number) num);
return 1;
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
int num;
READ_INT(data->store, &num);
tolua_pushnumber(L, (lua_Number)num);
return 1;
}
static int tolua_storage_write(lua_State * L)
{
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
if (data->version && tolua_isnumber(L, 2, 0, 0)) {
lua_Number num = tolua_tonumber(L, 2, 0);
double n;
if (modf(num, &n) == 0.0) {
WRITE_INT(data->store, (int)num);
} else {
WRITE_FLT(data->store, (float)num);
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
if (data->version && tolua_isnumber(L, 2, 0, 0)) {
lua_Number num = tolua_tonumber(L, 2, 0);
double n;
if (modf(num, &n) == 0.0) {
WRITE_INT(data->store, (int)num);
}
else {
WRITE_FLT(data->store, (float)num);
}
}
}
return 0;
return 0;
}
static int tolua_storage_tostring(lua_State * L)
{
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
char name[64];
_snprintf(name, sizeof(name), "<storage enc=%d ver=%d>", data->encoding,
data->version);
lua_pushstring(L, name);
return 1;
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
char name[64];
_snprintf(name, sizeof(name), "<storage enc=%d ver=%d>", data->encoding,
data->version);
lua_pushstring(L, name);
return 1;
}
static int tolua_storage_close(lua_State * L)
{
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
binstore_done(data->store);
return 0;
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
binstore_done(data->store);
fstream_done(&data->strm);
return 0;
}
void tolua_storage_open(lua_State * L)
{
/* register user types */
tolua_usertype(L, TOLUA_CAST "storage");
/* register user types */
tolua_usertype(L, TOLUA_CAST "storage");
tolua_module(L, NULL, 0);
tolua_beginmodule(L, NULL);
{
tolua_cclass(L, TOLUA_CAST "storage", TOLUA_CAST "storage", TOLUA_CAST "",
NULL);
tolua_beginmodule(L, TOLUA_CAST "storage");
tolua_module(L, NULL, 0);
tolua_beginmodule(L, NULL);
{
tolua_function(L, TOLUA_CAST "__tostring", tolua_storage_tostring);
tolua_function(L, TOLUA_CAST "write", tolua_storage_write);
tolua_function(L, TOLUA_CAST "read_int", tolua_storage_read_int);
tolua_function(L, TOLUA_CAST "read_float", tolua_storage_read_float);
tolua_function(L, TOLUA_CAST "write_unit", tolua_storage_write_unit);
tolua_function(L, TOLUA_CAST "read_unit", tolua_storage_read_unit);
tolua_function(L, TOLUA_CAST "close", tolua_storage_close);
tolua_function(L, TOLUA_CAST "create", tolua_storage_create);
tolua_cclass(L, TOLUA_CAST "storage", TOLUA_CAST "storage", TOLUA_CAST "",
NULL);
tolua_beginmodule(L, TOLUA_CAST "storage");
{
tolua_function(L, TOLUA_CAST "__tostring", tolua_storage_tostring);
tolua_function(L, TOLUA_CAST "write", tolua_storage_write);
tolua_function(L, TOLUA_CAST "read_int", tolua_storage_read_int);
tolua_function(L, TOLUA_CAST "read_float", tolua_storage_read_float);
tolua_function(L, TOLUA_CAST "write_unit", tolua_storage_write_unit);
tolua_function(L, TOLUA_CAST "read_unit", tolua_storage_read_unit);
tolua_function(L, TOLUA_CAST "close", tolua_storage_close);
tolua_function(L, TOLUA_CAST "create", tolua_storage_create);
}
tolua_endmodule(L);
}
tolua_endmodule(L);
}
tolua_endmodule(L);
}

View File

@ -5,6 +5,8 @@
#include "faction.h"
#include "unit.h"
#include "region.h"
#include <stream.h>
#include <filestream.h>
#include <storage.h>
#include <binarystore.h>
@ -19,9 +21,11 @@ static void test_group_readwrite(CuTest * tc)
ally *al;
storage store;
FILE *F;
stream strm;
F = fopen("test.dat", "wb");
binstore_init(&store, F);
fstream_init(&strm, F);
binstore_init(&store, &strm);
test_cleanup();
test_create_world();
f = test_create_faction(0);
@ -30,13 +34,16 @@ static void test_group_readwrite(CuTest * tc)
al->status = HELP_GIVE;
write_groups(&store, f);
binstore_done(&store);
fstream_done(&strm);
F = fopen("test.dat", "rb");
binstore_init(&store, F);
fstream_init(&strm, F);
binstore_init(&store, &strm);
f->groups = 0;
free_group(g);
read_groups(&store, f);
binstore_done(&store);
fstream_done(&strm);
CuAssertPtrNotNull(tc, f->groups);
CuAssertPtrNotNull(tc, f->groups->allies);

View File

@ -70,6 +70,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/umlaut.h>
#include <util/unicode.h>
#include <stream.h>
#include <filestream.h>
#include <storage.h>
#include <binarystore.h>
@ -1457,6 +1459,7 @@ int readgame(const char *filename, int backup)
const struct building_type *bt_lighthouse = bt_find("lighthouse");
gamedata gdata = { 0 };
storage store;
stream strm;
FILE *F;
init_locales();
@ -1482,7 +1485,8 @@ int readgame(const char *filename, int backup)
assert(gdata.version <= RELEASE_VERSION || !"unsupported data format");
gdata.encoding = enc_gamedata;
binstore_init(&store, F);
fstream_init(&strm, F);
binstore_init(&store, &strm);
gdata.store = &store;
global.data_version = gdata.version; /* HACK: attribute::read does not have access to gamedata, only storage */
@ -1733,7 +1737,7 @@ int readgame(const char *filename, int backup)
read_borders(&store);
binstore_done(&store);
fstream_done(&strm);
/* Unaufgeloeste Zeiger initialisieren */
log_printf(stdout, "fixing unresolved references.\n");
resolve();
@ -1797,6 +1801,7 @@ int writegame(const char *filename)
char path[MAX_PATH];
gamedata gdata;
storage store;
stream strm;
FILE *F;
clear_monster_orders();
@ -1826,7 +1831,8 @@ int writegame(const char *filename)
fwrite(&gdata.version, sizeof(int), 1, F);
fwrite(&n, sizeof(int), 1, F);
binstore_init(&store, F);
fstream_init(&strm, F);
binstore_init(&store, &strm);
/* globale Variablen */
@ -1942,6 +1948,7 @@ int writegame(const char *filename)
WRITE_SECTION(&store);
binstore_done(&store);
fstream_done(&strm);
log_printf(stdout, "\nOk.\n");
return 0;

View File

@ -18,6 +18,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#ifndef H_KRNL_SAVE
#define H_KRNL_SAVE
#include <stream.h>
#ifdef __cplusplus
extern "C" {
#endif
@ -31,6 +33,7 @@ extern "C" {
typedef struct gamedata {
struct storage *store;
stream strm;
int version;
int encoding;
} gamedata;

@ -1 +1 @@
Subproject commit c6103e59c0938b173c0e08a852ff1cbbc4e284e3
Subproject commit 5f0b7095d42209762c9eac73c866c614018b440d