shithub: blie

ref: 13590f759547f4ef544b251f2966b4ed2d82038c
dir: /db.h/

View raw version
typedef struct Db Db;
typedef struct Dpack Dpack;
typedef struct Dtuple Dtuple;

/* format:
<i1> <k1>=<v1> <k2>=<v2>
<i2> <k1>=<v1> <k2>=<v2>
*/

struct Db {
	Dpack *dpack;
	char *file;
};

struct Dpack {
	Dpack *next;
	char *id;
	Dtuple *tuple;
};

struct Dtuple {
	Dtuple *next;
	char *key;
	char *value;
};

Db *opendb(char *file);
void freedb(Db*);

int writedb(Db*, char *file);

Dpack *getdpack(Db*, char *id);
void setdval(Dpack*, char *key, char *value);
char *getdval(Dpack*, char *key, char *def);
void deldtuple(Dpack*, char *key);
void deldpack(Db*, Dpack*);


/* serializable structure:

struct mystruct {
	int a;
	char *b;
	int c;
};
Sdata data[] = {
	{ INT, "a", "5" },
	{ STRING, "b", "hello" },
	{ INT, "c", nil },
	{ NIL, nil, nil },
};
*/

#define STRUCT(name, type, dtype, dval) dtype name;
#define SDATA(name, type, dtype, dval) { type, "name", dval },
#define ENDSDATA { NIL, nil, nil },

typedef enum {
	NIL = 0,
	INT,
	STRING,
	FLOAT,
} Stype;

typedef struct Sdata Sdata;
struct Sdata {
	Stype type;
	char *name;
	char *def;
};

void serialize(Dpack *dv, void *data, Sdata *desc);
void deserialize(Dpack *dv, void *data, Sdata *desc);