(libretrodb) cleanups

This commit is contained in:
Twinaphex 2015-06-03 16:26:54 +02:00
parent efdb991f12
commit 030c3ce49f
4 changed files with 64 additions and 77 deletions

View File

@ -216,7 +216,7 @@ static int database_cursor_iterate(libretrodb_cursor_t *cur,
db_info->developer = strdup(val->string.buff);
break;
case DB_CURSOR_ORIGIN:
db_info->origin = strdup(val->string.buff);
db_info->origin = strdup(val->string.buff);
break;
case DB_CURSOR_FRANCHISE:
db_info->franchise = strdup(val->string.buff);

View File

@ -49,8 +49,7 @@ static int libretrodb_write_metadata(FILE *fp, libretrodb_metadata_t *md)
static int validate_document(const struct rmsgpack_dom_value * doc)
{
unsigned i;
struct rmsgpack_dom_value key;
struct rmsgpack_dom_value value;
struct rmsgpack_dom_value key, value;
int rv = 0;
if (doc->type != RDT_MAP)
@ -58,7 +57,7 @@ static int validate_document(const struct rmsgpack_dom_value * doc)
for (i = 0; i < doc->map.len; i++)
{
key = doc->map.items[i].key;
key = doc->map.items[i].key;
value = doc->map.items[i].value;
if (key.type != RDT_STRING)
@ -156,9 +155,9 @@ void libretrodb_close(libretrodb_t *db)
int libretrodb_open(const char *path, libretrodb_t *db)
{
int rv;
libretrodb_header_t header;
libretrodb_metadata_t md;
int rv;
FILE *fp = fopen(path, "rb");
if (fp == NULL)
@ -188,9 +187,9 @@ int libretrodb_open(const char *path, libretrodb_t *db)
goto error;
}
db->count = md.count;
db->count = md.count;
db->first_index_offset = flseek(fp, 0, SEEK_CUR);
db->fp = fp;
db->fp = fp;
return 0;
error:
fclose(fp);
@ -250,22 +249,22 @@ int libretrodb_find_entry(libretrodb_t *db, const char *index_name,
{
libretrodb_index_t idx;
int rv;
void * buff;
uint64_t offset;
void *buff = NULL;
ssize_t bufflen, nread = 0;
if (libretrodb_find_index(db, index_name, &idx) < 0)
return -1;
bufflen = idx.next;
buff = malloc(bufflen);
buff = malloc(bufflen);
if (!buff)
return -ENOMEM;
while (nread < bufflen)
{
void * buff_ = (uint64_t *)buff + nread;
void *buff_ = (uint64_t *)buff + nread;
rv = fread(buff_, 1, bufflen - nread, db->fp);
if (rv <= 0)
@ -282,12 +281,7 @@ int libretrodb_find_entry(libretrodb_t *db, const char *index_name,
if (rv == 0)
flseek(db->fp, offset, SEEK_SET);
rv = rmsgpack_dom_read(db->fp, out);
if (rv < 0)
return rv;
return rv;
return rmsgpack_dom_read(db->fp, out);
}
/**
@ -346,15 +340,15 @@ void libretrodb_cursor_close(libretrodb_cursor_t *cursor)
return;
fclose(cursor->fp);
if (cursor->query)
libretrodb_query_free(cursor->query);
cursor->is_valid = 0;
cursor->fp = NULL;
cursor->eof = 1;
cursor->db = NULL;
if (cursor->query)
libretrodb_query_free(cursor->query);
cursor->query = NULL;
cursor->fp = NULL;
cursor->eof = 1;
cursor->db = NULL;
cursor->query = NULL;
}
/**
@ -370,19 +364,17 @@ void libretrodb_cursor_close(libretrodb_cursor_t *cursor)
int libretrodb_cursor_open(libretrodb_t *db, libretrodb_cursor_t *cursor,
libretrodb_query_t *q)
{
#ifdef _WIN32
cursor->fp = fopen(db->path, "rb");
#else
cursor->fp = fopen(db->path, "r");
#endif
if (cursor->fp == NULL)
return -errno;
cursor->db = db;
cursor->db = db;
cursor->is_valid = 1;
libretrodb_cursor_reset(cursor);
cursor->query = q;
cursor->query = q;
if (q)
libretrodb_query_inc_ref(q);
@ -392,18 +384,18 @@ int libretrodb_cursor_open(libretrodb_t *db, libretrodb_cursor_t *cursor,
static int node_iter(void * value, void * ctx)
{
struct node_iter_ctx *nictx = (struct node_iter_ctx*)ctx;
size_t size = nictx->idx->key_size + sizeof(uint64_t);
struct node_iter_ctx *nictx = (struct node_iter_ctx*)ctx;
size_t size = nictx->idx->key_size + sizeof(uint64_t);
if (fwrite(value, 1, size, nictx->db->fp) == size)
return 0;
if (fwrite(value, 1, size, nictx->db->fp) == size)
return 0;
return -1;
return -1;
}
static uint64_t libretrodb_tell(libretrodb_t *db)
{
return ftello(db->fp);
return ftello(db->fp);
}
int libretrodb_create_index(libretrodb_t *db,
@ -414,14 +406,14 @@ int libretrodb_create_index(libretrodb_t *db,
struct rmsgpack_dom_value key;
libretrodb_index_t idx;
struct rmsgpack_dom_value item;
struct rmsgpack_dom_value * field;
struct rmsgpack_dom_value *field;
struct bintree tree;
uint64_t idx_header_offset;
libretrodb_cursor_t cur = {0};
void * buff = NULL;
uint64_t * buff_u64 = NULL;
void *buff = NULL;
uint64_t *buff_u64 = NULL;
uint8_t field_size = 0;
uint64_t item_loc = libretrodb_tell(db);
uint64_t item_loc = libretrodb_tell(db);
bintree_new(&tree, node_compare, &field_size);
@ -431,11 +423,11 @@ int libretrodb_create_index(libretrodb_t *db,
goto clean;
}
key.type = RDT_STRING;
key.string.len = strlen(field_name);
key.type = RDT_STRING;
key.string.len = strlen(field_name);
/* We know we aren't going to change it */
key.string.buff = (char *) field_name;
key.string.buff = (char*)field_name;
while (libretrodb_cursor_read_item(&cur, &item) == 0)
{
@ -512,11 +504,11 @@ int libretrodb_create_index(libretrodb_t *db,
idx.name[49] = '\0';
idx.key_size = field_size;
idx.next = db->count * (field_size + sizeof(uint64_t));
idx.next = db->count * (field_size + sizeof(uint64_t));
libretrodb_write_index_header(db->fp, &idx);
nictx.db = db;
nictx.idx = &idx;
nictx.db = db;
nictx.idx = &idx;
bintree_iterate(&tree, node_iter, &nictx);
bintree_free(&tree);
clean:

View File

@ -185,9 +185,9 @@ static struct buffer parse_argument(struct buffer buff, struct argument *arg,
static struct rmsgpack_dom_value is_true(struct rmsgpack_dom_value input,
unsigned argc, const struct argument *argv)
{
struct rmsgpack_dom_value res;
struct rmsgpack_dom_value res = {0};
res.type = RDT_BOOL;
res.type = RDT_BOOL;
res.bool_ = 0;
if (argc > 0 || input.type != RDT_BOOL)
@ -201,8 +201,8 @@ static struct rmsgpack_dom_value is_true(struct rmsgpack_dom_value input,
static struct rmsgpack_dom_value equals(struct rmsgpack_dom_value input,
unsigned argc, const struct argument * argv)
{
struct rmsgpack_dom_value res;
struct argument arg;
struct rmsgpack_dom_value res = {0};
res.type = RDT_BOOL;
@ -230,8 +230,8 @@ static struct rmsgpack_dom_value equals(struct rmsgpack_dom_value input,
static struct rmsgpack_dom_value operator_or(struct rmsgpack_dom_value input,
unsigned argc, const struct argument * argv)
{
struct rmsgpack_dom_value res;
unsigned i;
struct rmsgpack_dom_value res = {0};
res.type = RDT_BOOL;
res.bool_ = 0;
@ -258,8 +258,8 @@ static struct rmsgpack_dom_value operator_or(struct rmsgpack_dom_value input,
static struct rmsgpack_dom_value between(struct rmsgpack_dom_value input,
unsigned argc, const struct argument * argv)
{
struct rmsgpack_dom_value res;
unsigned i = 0;
struct rmsgpack_dom_value res = {0};
unsigned i = 0;
res.type = RDT_BOOL;
res.bool_ = 0;
@ -291,8 +291,8 @@ static struct rmsgpack_dom_value between(struct rmsgpack_dom_value input,
static struct rmsgpack_dom_value operator_and(struct rmsgpack_dom_value input,
unsigned argc, const struct argument * argv)
{
struct rmsgpack_dom_value res;
unsigned i;
struct rmsgpack_dom_value res = {0};
res.type = RDT_BOOL;
res.bool_ = 0;
@ -320,8 +320,8 @@ static struct rmsgpack_dom_value operator_and(struct rmsgpack_dom_value input,
static struct rmsgpack_dom_value q_glob(struct rmsgpack_dom_value input,
unsigned argc, const struct argument * argv)
{
struct rmsgpack_dom_value res;
unsigned i = 0;
struct rmsgpack_dom_value res = {0};
res.type = RDT_BOOL;
res.bool_ = 0;
@ -346,9 +346,9 @@ static struct rmsgpack_dom_value all_map(struct rmsgpack_dom_value input,
unsigned argc, const struct argument *argv)
{
unsigned i;
struct rmsgpack_dom_value res;
struct argument arg;
struct rmsgpack_dom_value nil_value;
struct rmsgpack_dom_value res = {0};
struct rmsgpack_dom_value *value = NULL;
nil_value.type = RDT_NULL;
@ -405,8 +405,6 @@ struct registered_func registered_functions[100] = {
static struct buffer chomp(struct buffer buff)
{
off_t i = 0;
(void)i;
for (; buff.offset < buff.len && isspace(buff.data[buff.offset]); buff.offset++);
return buff;
}
@ -616,7 +614,7 @@ static struct buffer parse_method_call(struct buffer buff,
unsigned i;
struct argument args[MAX_ARGS];
unsigned argi = 0;
struct registered_func * rf = registered_functions;
struct registered_func *rf = registered_functions;
invocation->func = NULL;
@ -845,7 +843,7 @@ static struct buffer parse_argument(struct buffer buff,
void libretrodb_query_free(void *q)
{
unsigned i;
struct query * real_q = (struct query*)q;
struct query *real_q = (struct query*)q;
real_q->ref_count--;
if (real_q->ref_count > 0)
@ -859,13 +857,11 @@ void *libretrodb_query_compile(libretrodb_t *db,
const char *query, size_t buff_len, const char **error)
{
struct buffer buff;
struct query *q = (struct query*)malloc(sizeof(struct query));
struct query *q = (struct query*)calloc(1, sizeof(*q));
if (!q)
goto clean;
memset(q, 0, sizeof(struct query));
q->ref_count = 1;
buff.data = query;
buff.len = buff_len;

View File

@ -28,11 +28,11 @@ static struct rmsgpack_dom_value *dom_reader_state_pop(
static void puts_i64(int64_t dec)
{
signed char digits[19 + 1]; /* max i64: 9,223,372,036,854,775,807 */
uint64_t decimal;
register int i;
decimal = (dec < 0) ? (uint64_t)-dec : (uint64_t)+dec;
int i;
uint64_t decimal = (dec < 0) ? (uint64_t)-dec : (uint64_t)+dec;
digits[19] = '\0';
for (i = sizeof(digits) - 2; i >= 0; i--)
{
digits[i] = decimal % 10;
@ -53,7 +53,7 @@ static void puts_i64(int64_t dec)
static void puts_u64(uint64_t decimal)
{
char digits[20 + 1]; /* max u64: 18,446,744,073,709,551,616 */
register int i;
int i;
digits[20] = '\0';
for (i = sizeof(digits) - 2; i >= 0; i--)
@ -184,11 +184,11 @@ static int dom_read_array_start(uint32_t len, void *data)
struct rmsgpack_dom_value *v = dom_reader_state_pop(dom_state);
struct rmsgpack_dom_value *items = NULL;
v->type = RDT_ARRAY;
v->array.len = len;
v->type = RDT_ARRAY;
v->array.len = len;
v->array.items = NULL;
items = (struct rmsgpack_dom_value *)calloc(len, sizeof(struct rmsgpack_dom_pair));
items = (struct rmsgpack_dom_value *)calloc(len, sizeof(struct rmsgpack_dom_pair));
if (!items)
return -ENOMEM;
@ -284,11 +284,11 @@ int rmsgpack_dom_value_cmp(
case RDT_NULL:
return 0;
case RDT_BOOL:
return a->bool_ == b->bool_ ? 0 : 1;
return (a->bool_ == b->bool_) ? 0 : 1;
case RDT_INT:
return a->int_ == b->int_ ? 0 : 1;
return (a->int_ == b->int_) ? 0 : 1;
case RDT_UINT:
return a->uint_ == b->uint_ ? 0 : 1;
return (a->uint_ == b->uint_) ? 0 : 1;
case RDT_STRING:
if (a->string.len != b->string.len)
return 1;
@ -381,7 +381,7 @@ void rmsgpack_dom_value_print(struct rmsgpack_dom_value *obj)
int rmsgpack_dom_write(FILE *fp, const struct rmsgpack_dom_value *obj)
{
unsigned i;
int rv = 0;
int rv = 0;
int written = 0;
switch (obj->type)
@ -430,10 +430,9 @@ int rmsgpack_dom_write(FILE *fp, const struct rmsgpack_dom_value *obj)
int rmsgpack_dom_read(FILE *fp, struct rmsgpack_dom_value *out)
{
struct dom_reader_state s;
int rv = 0;
struct dom_reader_state s = {0};
int rv = 0;
s.i = 0;
s.stack[0] = out;
rv = rmsgpack_read(fp, &dom_reader_callbacks, &s);