orderdb is glue code, belongs outside of kernel.

This commit is contained in:
Enno Rehling 2018-09-28 21:02:32 +02:00
parent 7b9e72e559
commit 91c49659ef
11 changed files with 116 additions and 48 deletions

View File

@ -113,6 +113,7 @@ set (ERESSEA_SRC
morale.c
move.c
names.c
orderdb.c
orderfile.c
piracy.c
prefix.c
@ -221,6 +222,7 @@ set(TESTS_SRC
monsters.test.c
move.test.c
names.test.c
orderdb.test.c
orderfile.test.c
piracy.test.c
prefix.test.c

View File

@ -8,7 +8,6 @@
#include "kernel/equipment.h"
#include "kernel/faction.h"
#include "kernel/item.h"
#include "kernel/orderdb.h"
#include "util/functions.h"
#include "util/language.h"
@ -28,6 +27,7 @@
#include "creport.h"
#include "report.h"
#include "names.h"
#include "orderdb.h"
#include "reports.h"
#include "spells.h"
#include "vortex.h"

View File

@ -27,7 +27,6 @@ group.test.c
item.test.c
messages.test.c
order.test.c
orderdb.test.c
# pathfinder.test.c
plane.test.c
pool.test.c
@ -61,7 +60,6 @@ group.c
item.c
messages.c
order.c
orderdb.c
pathfinder.c
plane.c
pool.c

View File

@ -1,6 +1,7 @@
#include <platform.h>
#include <kernel/config.h>
#include <kernel/faction.h>
#include <kernel/order.h>
#include "db/driver.h"
#include "orderdb.h"

View File

@ -14,7 +14,6 @@
#include <kernel/config.h>
#include "order.h"
#include "orderdb.h"
#include "skill.h"
#include "keyword.h"
@ -36,6 +35,36 @@
# define ORD_KEYWORD(ord) (keyword_t)((ord)->command & 0xFFFF)
# define OD_STRING(odata) ((odata) ? (odata)->_str : NULL)
void odata_create(order_data **pdata, size_t len, const char *str)
{
order_data *data;
char *result;
assert(pdata);
data = malloc(sizeof(order_data) + len + 1);
data->_refcount = 1;
result = (char *)(data + 1);
data->_str = (len > 0) ? result : NULL;
if (str) {
strcpy(result, str);
}
*pdata = data;
}
void odata_release(order_data * od)
{
if (od) {
if (--od->_refcount == 0) {
free(od);
}
}
}
void odata_addref(order_data *od)
{
++od->_refcount;
}
void replace_order(order ** dlist, order * orig, const order * src)
{
assert(src);

View File

@ -37,6 +37,18 @@ extern "C" {
#define CMD_PERSIST 0x020000
#define CMD_DEFAULT 0x040000
typedef struct order_data {
const char *_str;
int _refcount;
} order_data;
extern order_data *odata_load(int id);
extern int odata_save(order_data *od);
void odata_create(order_data **pdata, size_t len, const char *str);
void odata_release(order_data * od);
void odata_addref(order_data *od);
typedef struct order {
struct order *next;
/* do not access this data: */

View File

@ -1,7 +1,7 @@
#include <platform.h>
#include "config.h"
#include "db/driver.h"
#include "kernel/config.h"
#include "kernel/db/driver.h"
#include "orderdb.h"
@ -26,36 +26,6 @@ void orderdb_close(void)
db_driver_close(DB_SWAP);
}
void odata_create(order_data **pdata, size_t len, const char *str)
{
order_data *data;
char *result;
assert(pdata);
data = malloc(sizeof(order_data) + len + 1);
data->_refcount = 1;
result = (char *)(data + 1);
data->_str = (len > 0) ? result : NULL;
if (str) {
strcpy(result, str);
}
*pdata = data;
}
void odata_release(order_data * od)
{
if (od) {
if (--od->_refcount == 0) {
free(od);
}
}
}
void odata_addref(order_data *od)
{
++od->_refcount;
}
order_data *odata_load(int id)
{
if (id > 0) {

View File

@ -7,18 +7,6 @@
extern "C" {
#endif
typedef struct order_data {
const char *_str;
int _refcount;
} order_data;
void odata_create(order_data **pdata, size_t len, const char *str);
void odata_release(order_data * od);
void odata_addref(order_data *od);
order_data *odata_load(int id);
int odata_save(order_data *od);
void orderdb_open(void);
void orderdb_close(void);

43
src/orderdb.c Normal file
View File

@ -0,0 +1,43 @@
#include <platform.h>
#include "kernel/config.h"
#include "kernel/db/driver.h"
#include "orderdb.h"
#include <util/log.h>
#include <critbit.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
void orderdb_open(void)
{
const char *dbname;
dbname = config_get("game.dbswap");
db_driver_open(DB_SWAP, dbname);
}
void orderdb_close(void)
{
db_driver_close(DB_SWAP);
}
order_data *odata_load(int id)
{
if (id > 0) {
return db_driver_order_load(id);
}
return NULL;
}
int odata_save(order_data *od)
{
if (od->_str) {
return db_driver_order_save(od->_str);
}
return 0;
}

25
src/orderdb.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef H_ORDERDB
#define H_ORDERDB
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct order_data {
const char *_str;
int _refcount;
} order_data;
void odata_create(order_data **pdata, size_t len, const char *str);
void odata_release(order_data * od);
void odata_addref(order_data *od);
void orderdb_open(void);
void orderdb_close(void);
#ifdef __cplusplus
}
#endif
#endif