From dbf60a7ce53df3c844f126d1d8825cfd617c5068 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 3 Nov 2014 22:29:04 +0100 Subject: [PATCH] update to latest version of storage library. --- src/bind_storage.c | 147 +++++++++++++++++++++------------------- src/kernel/group.test.c | 11 ++- src/kernel/save.c | 13 +++- src/kernel/save.h | 3 + storage | 2 +- 5 files changed, 99 insertions(+), 77 deletions(-) diff --git a/src/bind_storage.c b/src/bind_storage.c index 8aa363d43..baf1a67e2 100644 --- a/src/bind_storage.c +++ b/src/bind_storage.c @@ -18,6 +18,8 @@ without prior permission by the authors of Eressea. #include #include +#include +#include #include #include @@ -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), "", 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), "", 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); } diff --git a/src/kernel/group.test.c b/src/kernel/group.test.c index b248b65d2..1272fc493 100644 --- a/src/kernel/group.test.c +++ b/src/kernel/group.test.c @@ -5,6 +5,8 @@ #include "faction.h" #include "unit.h" #include "region.h" +#include +#include #include #include @@ -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); diff --git a/src/kernel/save.c b/src/kernel/save.c index 4133f0971..a7d6ce0c2 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -70,6 +70,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include +#include +#include #include #include @@ -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; diff --git a/src/kernel/save.h b/src/kernel/save.h index 2c119a5eb..512946aca 100644 --- a/src/kernel/save.h +++ b/src/kernel/save.h @@ -18,6 +18,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #ifndef H_KRNL_SAVE #define H_KRNL_SAVE + +#include #ifdef __cplusplus extern "C" { #endif @@ -31,6 +33,7 @@ extern "C" { typedef struct gamedata { struct storage *store; + stream strm; int version; int encoding; } gamedata; diff --git a/storage b/storage index c6103e59c..5f0b7095d 160000 --- a/storage +++ b/storage @@ -1 +1 @@ -Subproject commit c6103e59c0938b173c0e08a852ff1cbbc4e284e3 +Subproject commit 5f0b7095d42209762c9eac73c866c614018b440d