(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

@ -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)
@ -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)
@ -250,8 +249,8 @@ 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)
@ -265,7 +264,7 @@ int libretrodb_find_entry(libretrodb_t *db, const char *index_name,
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,14 +340,14 @@ void libretrodb_cursor_close(libretrodb_cursor_t *cursor)
return;
fclose(cursor->fp);
cursor->is_valid = 0;
cursor->fp = NULL;
cursor->eof = 1;
cursor->db = NULL;
if (cursor->query)
libretrodb_query_free(cursor->query);
cursor->is_valid = 0;
cursor->fp = NULL;
cursor->eof = 1;
cursor->db = NULL;
cursor->query = NULL;
}
@ -370,18 +364,16 @@ 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->is_valid = 1;
libretrodb_cursor_reset(cursor);
cursor->query = q;
if (q)
@ -414,12 +406,12 @@ 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);
@ -435,7 +427,7 @@ int libretrodb_create_index(libretrodb_t *db,
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)
{

View File

@ -185,7 +185,7 @@ 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.bool_ = 0;
@ -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,7 +258,7 @@ 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;
struct rmsgpack_dom_value res = {0};
unsigned i = 0;
res.type = RDT_BOOL;
@ -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;
int i;
uint64_t decimal = (dec < 0) ? (uint64_t)-dec : (uint64_t)+dec;
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--)
@ -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;
@ -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;
struct dom_reader_state s = {0};
int rv = 0;
s.i = 0;
s.stack[0] = out;
rv = rmsgpack_read(fp, &dom_reader_callbacks, &s);