Skip to content

Instantly share code, notes, and snippets.

@letoh
Last active August 29, 2015 14:18
Show Gist options
  • Save letoh/22c006133f968849aedb to your computer and use it in GitHub Desktop.
Save letoh/22c006133f968849aedb to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include <db.h>
#define F_NUM(name) \
long name
#define F_TEXT(name,n) \
struct { int len; char data[n]; } name
#define REC(name) struct Rec_##name
#define D_REC(name, ...) \
REC(name) {\
__VA_ARGS__ \
}
#define F_REC(name, ...) \
D_REC(, __VA_ARGS__) name
D_REC(rec,
F_NUM(id);
F_TEXT(label, 32);
F_REC(user,
F_NUM(foo);
);
);
int main(int argc, char *argv[])
{
REC(rec) rec = {
.id = 1,
.label = {5, "label"},
.user = {
.foo = 3
}
};
db_create(&db, NULL, 0);
db->open(db, NULL, NULL, NULL, DB_BTREE, DB_CREATE, 0664);
memset(&key, 0, sizeof key);
key.data = &rec;
key.size = sizeof rec;
memset(&value, 0, sizeof value);
value.data = "test value";
value.size = 11;
db->put(db, NULL, &key, &value, 0);
memset(&value, 0, sizeof value);
db->get(db, NULL, &key, &value, 0);
printf("value: '%s'\n", (char*)value.data);
db->close(db, 0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment