(libretroDB) Style nits

This commit is contained in:
twinaphex 2015-01-24 04:04:56 +01:00
parent 66641d7828
commit b5040ef3dc
8 changed files with 924 additions and 943 deletions

View File

@ -35,8 +35,9 @@
int rl_fnmatch(const char *pattern, const char *string, int flags)
{
const char *c;
int charmatch = 0;
int rv;
int charmatch = 0;
for (c = pattern; *c != '\0'; c++)
{
/* String ended before pattern */

View File

@ -23,7 +23,8 @@ static char *strndup_(const char *s, size_t n)
return buff;
}
static struct rmsgpack_dom_value *get_map_value(const struct rmsgpack_dom_value *m, char* key)
static struct rmsgpack_dom_value *get_map_value(
const struct rmsgpack_dom_value *m, char* key)
{
struct rmsgpack_dom_value v;
@ -261,10 +262,12 @@ int main(int argc, char **argv)
int rv = 0;
int src = -1;
int dst = -1;
if (argc != 3)
printf("Usage: %s <dat file> <output file>\n", argv[0]);
src = open(argv[1], O_RDONLY);
if (src == -1)
{
printf("Could not open source file '%s': %s\n", argv[1], strerror(errno));
@ -273,6 +276,7 @@ int main(int argc, char **argv)
}
dst = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if (dst == -1)
{
printf("Could not open destination file '%s': %s\n", argv[1], strerror(errno));

View File

@ -20,124 +20,115 @@
#include "libretrodb_endian.h"
#include "query.h"
struct node_iter_ctx {
struct node_iter_ctx
{
libretrodb_t *db;
libretrodb_index_t *idx;
};
static struct rmsgpack_dom_value sentinal;
static int libretrodb_read_metadata(
int fd,
libretrodb_metadata_t *md
){
return rmsgpack_dom_read_into(fd, "count", &md->count, NULL);
static int libretrodb_read_metadata(int fd, libretrodb_metadata_t *md)
{
return rmsgpack_dom_read_into(fd, "count", &md->count, NULL);
}
static int libretrodb_write_metadata(
int fd,
libretrodb_metadata_t *md
){
rmsgpack_write_map_header(fd, 1);
rmsgpack_write_string(fd, "count", strlen("count"));
return rmsgpack_write_uint(fd, md->count);
static int libretrodb_write_metadata(int fd, libretrodb_metadata_t *md)
{
rmsgpack_write_map_header(fd, 1);
rmsgpack_write_string(fd, "count", strlen("count"));
return rmsgpack_write_uint(fd, md->count);
}
static int validate_document(const struct rmsgpack_dom_value * doc) {
int rv = 0;
unsigned i;
struct rmsgpack_dom_value key;
struct rmsgpack_dom_value value;
static int validate_document(const struct rmsgpack_dom_value * doc)
{
unsigned i;
struct rmsgpack_dom_value key;
struct rmsgpack_dom_value value;
int rv = 0;
if (doc->type != RDT_MAP) {
return -EINVAL;
}
if (doc->type != RDT_MAP)
return -EINVAL;
for (i = 0; i < doc->map.len; i++) {
key = doc->map.items[i].key;
value = doc->map.items[i].value;
if (key.type != RDT_STRING) {
return -EINVAL;
}
if (key.string.len <= 0) {
return -EINVAL;
}
if (key.string.buff[0] == '$') {
return -EINVAL;
}
for (i = 0; i < doc->map.len; i++)
{
key = doc->map.items[i].key;
value = doc->map.items[i].value;
if (value.type == RDT_MAP) {
if ((rv == validate_document(&value)) != 0) {
return rv;
}
}
}
if (key.type != RDT_STRING)
return -EINVAL;
return rv;
if (key.string.len <= 0)
return -EINVAL;
if (key.string.buff[0] == '$')
return -EINVAL;
if (value.type != RDT_MAP)
continue;
if ((rv == validate_document(&value)) != 0)
return rv;
}
return rv;
}
int libretrodb_create(
int fd,
libretrodb_value_provider value_provider,
void * ctx
){
int rv;
off_t root;
libretrodb_metadata_t md;
uint64_t item_count = 0;
struct rmsgpack_dom_value item = {};
libretrodb_header_t header = {};
int libretrodb_create(int fd, libretrodb_value_provider value_provider,
void * ctx)
{
int rv;
off_t root;
libretrodb_metadata_t md;
uint64_t item_count = 0;
struct rmsgpack_dom_value item = {};
libretrodb_header_t header = {};
memcpy(header.magic_number, MAGIC_NUMBER, sizeof(MAGIC_NUMBER)-1);
root = lseek(fd, 0, SEEK_CUR);
memcpy(header.magic_number, MAGIC_NUMBER, sizeof(MAGIC_NUMBER)-1);
root = lseek(fd, 0, SEEK_CUR);
/* We write the header in the end because we need to know the size of
/* We write the header in the end because we need to know the size of
* the db first */
lseek(fd, sizeof(libretrodb_header_t), SEEK_CUR);
lseek(fd, sizeof(libretrodb_header_t), SEEK_CUR);
while ((rv = value_provider(ctx, &item)) == 0) {
while ((rv = value_provider(ctx, &item)) == 0)
{
if ((rv = validate_document(&item)) < 0)
goto clean;
if ((rv = validate_document(&item)) < 0)
goto clean;
if ((rv = rmsgpack_dom_write(fd, &item)) < 0)
goto clean;
if ((rv = rmsgpack_dom_write(fd, &item)) < 0)
goto clean;
item_count++;
}
item_count++;
}
if (rv < 0)
goto clean;
if (rv < 0)
goto clean;
if ((rv = rmsgpack_dom_write(fd, &sentinal)) < 0)
goto clean;
if ((rv = rmsgpack_dom_write(fd, &sentinal)) < 0)
goto clean;
header.metadata_offset = httobe64(lseek(fd, 0, SEEK_CUR));
md.count = item_count;
libretrodb_write_metadata(fd, &md);
lseek(fd, root, SEEK_SET);
write(fd, &header, sizeof(header));
header.metadata_offset = httobe64(lseek(fd, 0, SEEK_CUR));
md.count = item_count;
libretrodb_write_metadata(fd, &md);
lseek(fd, root, SEEK_SET);
write(fd, &header, sizeof(header));
clean:
rmsgpack_dom_value_free(&item);
return rv;
rmsgpack_dom_value_free(&item);
return rv;
}
static int libretrodb_read_index_header(
int fd, libretrodb_index_t *idx)
static int libretrodb_read_index_header(int fd, libretrodb_index_t *idx)
{
uint64_t name_len = 50;
return rmsgpack_dom_read_into(
fd,
"name", idx->name, &name_len,
"key_size", &idx->key_size,
"next", &idx->next,
NULL
);
uint64_t name_len = 50;
return rmsgpack_dom_read_into(fd,
"name", idx->name, &name_len,
"key_size", &idx->key_size,
"next", &idx->next, NULL);
}
static void libretrodb_write_index_header(
int fd, libretrodb_index_t * idx)
static void libretrodb_write_index_header(int fd, libretrodb_index_t * idx)
{
rmsgpack_write_map_header(fd, 3);
rmsgpack_write_string(fd, "name", strlen("name"));
@ -154,54 +145,55 @@ void libretrodb_close(libretrodb_t * db)
db->fd = -1;
}
int libretrodb_open(
const char * path,
libretrodb_t * db
)
int libretrodb_open(const char * path, libretrodb_t * db)
{
libretrodb_header_t header;
libretrodb_metadata_t md;
int rv;
int fd = open(path, O_RDWR);
libretrodb_header_t header;
libretrodb_metadata_t md;
int rv;
int fd = open(path, O_RDWR);
if (fd == -1)
return -errno;
if (fd == -1)
return -errno;
db->root = lseek(fd, 0, SEEK_CUR);
if ((rv = read(fd, &header, sizeof(header))) == -1) {
rv = -errno;
goto error;
}
db->root = lseek(fd, 0, SEEK_CUR);
if (strncmp(header.magic_number, MAGIC_NUMBER, sizeof(MAGIC_NUMBER)) != 0) {
rv = -EINVAL;
goto error;
}
if ((rv = read(fd, &header, sizeof(header))) == -1)
{
rv = -errno;
goto error;
}
header.metadata_offset = betoht64(header.metadata_offset);
lseek(fd, header.metadata_offset, SEEK_SET);
if (libretrodb_read_metadata(fd, &md) < 0) {
rv = -EINVAL;
goto error;
}
db->count = md.count;
db->first_index_offset = lseek(fd, 0, SEEK_CUR);
db->fd = fd;
return 0;
if (strncmp(header.magic_number, MAGIC_NUMBER, sizeof(MAGIC_NUMBER)) != 0)
{
rv = -EINVAL;
goto error;
}
header.metadata_offset = betoht64(header.metadata_offset);
lseek(fd, header.metadata_offset, SEEK_SET);
if (libretrodb_read_metadata(fd, &md) < 0)
{
rv = -EINVAL;
goto error;
}
db->count = md.count;
db->first_index_offset = lseek(fd, 0, SEEK_CUR);
db->fd = fd;
return 0;
error:
close(fd);
return rv;
close(fd);
return rv;
}
static int libretrodb_find_index(
libretrodb_t *db,
const char *index_name,
libretrodb_index_t *idx
){
off_t eof = lseek(db->fd, 0, SEEK_END);
off_t offset = lseek(db->fd, db->first_index_offset, SEEK_SET);
static int libretrodb_find_index(libretrodb_t *db, const char *index_name,
libretrodb_index_t *idx)
{
off_t eof = lseek(db->fd, 0, SEEK_END);
off_t offset = lseek(db->fd, db->first_index_offset, SEEK_SET);
while (offset < eof)
while (offset < eof)
{
libretrodb_read_index_header(db->fd, idx);
if (strncmp(index_name, idx->name, strlen(idx->name)) == 0)
@ -209,83 +201,79 @@ static int libretrodb_find_index(
offset = lseek(db->fd, idx->next, SEEK_CUR);
}
return -1;
return -1;
}
static int node_compare(
const void * a,
const void * b,
void * ctx
){
return memcmp(a, b, *(uint8_t *)ctx);
static int node_compare(const void * a, const void * b, void * ctx)
{
return memcmp(a, b, *(uint8_t *)ctx);
}
static int binsearch(
const void * buff,
const void * item,
uint64_t count,
uint8_t field_size,
uint64_t * offset
){
int mid = count / 2;
int item_size = field_size + sizeof(uint64_t);
uint64_t * current = (uint64_t *)buff + (mid * item_size);
int rv = node_compare(current, item, &field_size);
static int binsearch(const void * buff, const void * item,
uint64_t count, uint8_t field_size, uint64_t * offset)
{
int mid = count / 2;
int item_size = field_size + sizeof(uint64_t);
uint64_t * current = (uint64_t *)buff + (mid * item_size);
int rv = node_compare(current, item, &field_size);
if (rv == 0) {
*offset = *(uint64_t *)(current + field_size);
return 0;
} else if (count == 0)
return -1;
else if (rv > 0)
return binsearch(buff, item, mid, field_size, offset);
if (rv == 0)
{
*offset = *(uint64_t *)(current + field_size);
return 0;
}
else if (count == 0)
return -1;
else if (rv > 0)
return binsearch(buff, item, mid, field_size, offset);
return binsearch(current + item_size, item, count - mid, field_size, offset);
return binsearch(current + item_size, item,
count - mid, field_size, offset);
}
int libretrodb_find_entry(
libretrodb_t *db,
const char *index_name,
const void *key,
struct rmsgpack_dom_value * out
) {
libretrodb_index_t idx;
int rv;
void * buff;
uint64_t offset;
ssize_t bufflen, nread = 0;
int libretrodb_find_entry(libretrodb_t *db, const char *index_name,
const void *key, struct rmsgpack_dom_value *out)
{
libretrodb_index_t idx;
int rv;
void * buff;
uint64_t offset;
ssize_t bufflen, nread = 0;
if (libretrodb_find_index(db, index_name, &idx) < 0)
return -1;
if (libretrodb_find_index(db, index_name, &idx) < 0)
return -1;
bufflen = idx.next;
buff = malloc(bufflen);
bufflen = idx.next;
buff = malloc(bufflen);
if (!buff)
return -ENOMEM;
if (!buff)
return -ENOMEM;
while (nread < bufflen) {
void * buff_ = (uint64_t *)buff + nread;
rv = read(db->fd, buff_, bufflen - nread);
if (rv <= 0) {
free(buff);
return -errno;
}
nread += rv;
}
while (nread < bufflen)
{
void * buff_ = (uint64_t *)buff + nread;
rv = read(db->fd, buff_, bufflen - nread);
rv = binsearch(buff, key, db->count, idx.key_size, &offset);
free(buff);
if (rv <= 0)
{
free(buff);
return -errno;
}
nread += rv;
}
if (rv == 0)
lseek(db->fd, offset, SEEK_SET);
rv = binsearch(buff, key, db->count, idx.key_size, &offset);
free(buff);
rv = rmsgpack_dom_read(db->fd, out);
if (rv < 0)
return rv;
if (rv == 0)
lseek(db->fd, offset, SEEK_SET);
rv = rmsgpack_dom_read(db->fd, out);
if (rv < 0)
return rv;
return rv;
return rv;
}
/**
@ -299,38 +287,37 @@ int libretrodb_find_entry(
int libretrodb_cursor_reset(libretrodb_cursor_t *cursor)
{
cursor->eof = 0;
return lseek(
cursor->fd,
cursor->db->root + sizeof(libretrodb_header_t),
SEEK_SET
);
return lseek(cursor->fd,
cursor->db->root + sizeof(libretrodb_header_t),
SEEK_SET);
}
int libretrodb_cursor_read_item(
libretrodb_cursor_t *cursor,
struct rmsgpack_dom_value * out
) {
int rv;
if (cursor->eof)
return EOF;
int libretrodb_cursor_read_item(libretrodb_cursor_t *cursor,
struct rmsgpack_dom_value * out)
{
int rv;
if (cursor->eof)
return EOF;
retry:
rv = rmsgpack_dom_read(cursor->fd, out);
if (rv < 0)
return rv;
rv = rmsgpack_dom_read(cursor->fd, out);
if (rv < 0)
return rv;
if (out->type == RDT_NULL) {
cursor->eof = 1;
return EOF;
}
if (out->type == RDT_NULL)
{
cursor->eof = 1;
return EOF;
}
if (cursor->query) {
if (!libretrodb_query_filter(cursor->query, out)) {
goto retry;
}
}
if (cursor->query)
{
if (!libretrodb_query_filter(cursor->query, out))
goto retry;
}
return 0;
return 0;
}
/**
@ -398,11 +385,9 @@ static uint64_t libretrodb_tell(libretrodb_t *db)
return lseek(db->fd, 0, SEEK_CUR);
}
int libretrodb_create_index(
libretrodb_t *db,
const char *name,
const char *field_name
){
int libretrodb_create_index(libretrodb_t *db,
const char *name, const char *field_name)
{
int rv;
struct node_iter_ctx nictx;
struct rmsgpack_dom_value key;
@ -411,14 +396,16 @@ int libretrodb_create_index(
struct rmsgpack_dom_value * field;
struct bintree tree;
libretrodb_cursor_t cur;
uint64_t idx_header_offset;
void * buff = NULL;
uint64_t * buff_u64 = NULL;
uint8_t field_size = 0;
uint64_t idx_header_offset;
uint64_t item_loc = libretrodb_tell(db);
bintree_new(&tree, node_compare, &field_size);
if (libretrodb_cursor_open(db, &cur, NULL) != 0) {
if (libretrodb_cursor_open(db, &cur, NULL) != 0)
{
rv = -1;
goto clean;
}
@ -427,25 +414,33 @@ int libretrodb_create_index(
key.string.len = strlen(field_name);
/* We know we aren't going to change it */
key.string.buff = (char *) field_name;
while (libretrodb_cursor_read_item(&cur, &item) == 0) {
if (item.type != RDT_MAP) {
while (libretrodb_cursor_read_item(&cur, &item) == 0)
{
if (item.type != RDT_MAP)
{
rv = -EINVAL;
printf("Only map keys are supported\n");
goto clean;
}
field = rmsgpack_dom_value_map_value(&item, &key);
if (!field) {
if (!field)
{
rv = -EINVAL;
printf("field not found in item\n");
goto clean;
}
if (field->type != RDT_BINARY) {
if (field->type != RDT_BINARY)
{
rv = -EINVAL;
printf("field is not binary\n");
goto clean;
}
if (field->binary.len == 0) {
if (field->binary.len == 0)
{
rv = -EINVAL;
printf("field is empty\n");
goto clean;
@ -453,14 +448,16 @@ int libretrodb_create_index(
if (field_size == 0)
field_size = field->binary.len;
else if (field->binary.len != field_size) {
else if (field->binary.len != field_size)
{
rv = -EINVAL;
printf("field is not of correct size\n");
goto clean;
}
buff = malloc(field_size + sizeof(uint64_t));
if (!buff) {
if (!buff)
{
rv = -ENOMEM;
goto clean;
}
@ -469,7 +466,8 @@ int libretrodb_create_index(
buff_u64 = (uint64_t *)buff + field_size;
memcpy(buff_u64, &item_loc, sizeof(uint64_t));
if (bintree_insert(&tree, buff) != 0) {
if (bintree_insert(&tree, buff) != 0)
{
printf("Value is not unique: ");
rmsgpack_dom_value_print(field);
printf("\n");
@ -500,8 +498,7 @@ clean:
rmsgpack_dom_value_free(&item);
if (buff)
free(buff);
if (cur.is_valid) {
if (cur.is_valid)
libretrodb_cursor_close(&cur);
}
return 0;
}

View File

@ -52,29 +52,19 @@ typedef struct libretrodb_cursor
libretrodb_t * db;
} libretrodb_cursor_t;
typedef int (* libretrodb_value_provider)(
void * ctx,
struct rmsgpack_dom_value * out
);
typedef int (* libretrodb_value_provider)(void * ctx,
struct rmsgpack_dom_value * out);
int libretrodb_create(
int fd,
libretrodb_value_provider value_provider,
void * ctx
);
int libretrodb_create(int fd, libretrodb_value_provider value_provider,
void * ctx);
void libretrodb_close(libretrodb_t * db);
int libretrodb_open(
const char * path,
libretrodb_t * db
);
int libretrodb_open(const char * path, libretrodb_t * db);
int libretrodb_create_index(libretrodb_t * db, const char *name,
const char *field_name);
int libretrodb_create_index(
libretrodb_t * db,
const char * name,
const char * field_name
);
int libretrodb_find_entry(
libretrodb_t * db,
const char * index_name,
@ -125,10 +115,8 @@ void *libretrodb_query_compile(
void libretrodb_query_free(void *q);
int libretrodb_cursor_read_item(
libretrodb_cursor_t * cursor,
struct rmsgpack_dom_value * out
);
int libretrodb_cursor_read_item(libretrodb_cursor_t * cursor,
struct rmsgpack_dom_value * out);
#ifdef __cplusplus
}

View File

@ -4,88 +4,104 @@
#include "libretrodb.h"
#include "rmsgpack_dom.h"
int main(
int argc,
char ** argv
){
int rv;
libretrodb_t db;
libretrodb_cursor_t cur;
int main(int argc, char ** argv)
{
int rv;
libretrodb_t db;
libretrodb_cursor_t cur;
libretrodb_query_t *q;
struct rmsgpack_dom_value item;
struct rmsgpack_dom_value item;
const char *command, *path, *query_exp, *error;
if (argc < 3) {
printf("Usage: %s <db file> <command> [extra args...]\n", argv[0]);
printf("Available Commands:\n");
printf("\tlist\n");
printf("\tcreate-index <index name> <field name>\n");
printf("\tfind <query expression>\n");
return 1;
}
if (argc < 3)
{
printf("Usage: %s <db file> <command> [extra args...]\n", argv[0]);
printf("Available Commands:\n");
printf("\tlist\n");
printf("\tcreate-index <index name> <field name>\n");
printf("\tfind <query expression>\n");
return 1;
}
command = argv[2];
path = argv[1];
command = argv[2];
path = argv[1];
if ((rv = libretrodb_open(path, &db)) != 0) {
printf("Could not open db file '%s': %s\n", path, strerror(-rv));
return 1;
} else if (strcmp(command, "list") == 0) {
if ((rv = libretrodb_cursor_open(&db, &cur, NULL)) != 0) {
printf("Could not open cursor: %s\n", strerror(-rv));
return 1;
}
if (argc != 3) {
printf("Usage: %s <db file> list\n", argv[0]);
return 1;
}
while (libretrodb_cursor_read_item(&cur, &item) == 0) {
rmsgpack_dom_value_print(&item);
printf("\n");
rmsgpack_dom_value_free(&item);
}
} else if (strcmp(command, "find") == 0) {
if (argc != 4) {
printf("Usage: %s <db file> find <query expression>\n", argv[0]);
return 1;
}
query_exp = argv[3];
error = NULL;
q = libretrodb_query_compile(&db, query_exp, strlen(query_exp), &error);
if (error) {
printf("%s\n", error);
return 1;
}
if ((rv = libretrodb_cursor_open(&db, &cur, q)) != 0)
if ((rv = libretrodb_open(path, &db)) != 0)
{
printf("Could not open db file '%s': %s\n", path, strerror(-rv));
return 1;
}
else if (strcmp(command, "list") == 0)
{
if ((rv = libretrodb_cursor_open(&db, &cur, NULL)) != 0)
{
printf("Could not open cursor: %s\n", strerror(-rv));
return 1;
}
printf("Could not open cursor: %s\n", strerror(-rv));
return 1;
}
while (libretrodb_cursor_read_item(&cur, &item) == 0)
if (argc != 3)
{
rmsgpack_dom_value_print(&item);
printf("\n");
rmsgpack_dom_value_free(&item);
}
} else if (strcmp(command, "create-index") == 0) {
const char * index_name, * field_name;
printf("Usage: %s <db file> list\n", argv[0]);
return 1;
}
if (argc != 5) {
printf("Usage: %s <db file> create-index <index name> <field name>\n", argv[0]);
return 1;
}
while (libretrodb_cursor_read_item(&cur, &item) == 0)
{
rmsgpack_dom_value_print(&item);
printf("\n");
rmsgpack_dom_value_free(&item);
}
}
else if (strcmp(command, "find") == 0)
{
if (argc != 4)
{
printf("Usage: %s <db file> find <query expression>\n", argv[0]);
return 1;
}
index_name = argv[3];
field_name = argv[4];
query_exp = argv[3];
error = NULL;
q = libretrodb_query_compile(&db, query_exp, strlen(query_exp), &error);
libretrodb_create_index(&db, index_name, field_name);
} else {
printf("Unkonwn command %s\n", argv[2]);
return 1;
}
libretrodb_close(&db);
if (error)
{
printf("%s\n", error);
return 1;
}
if ((rv = libretrodb_cursor_open(&db, &cur, q)) != 0)
{
printf("Could not open cursor: %s\n", strerror(-rv));
return 1;
}
while (libretrodb_cursor_read_item(&cur, &item) == 0)
{
rmsgpack_dom_value_print(&item);
printf("\n");
rmsgpack_dom_value_free(&item);
}
}
else if (strcmp(command, "create-index") == 0)
{
const char * index_name, * field_name;
if (argc != 5)
{
printf("Usage: %s <db file> create-index <index name> <field name>\n", argv[0]);
return 1;
}
index_name = argv[3];
field_name = argv[4];
libretrodb_create_index(&db, index_name, field_name);
}
else
{
printf("Unkonwn command %s\n", argv[2]);
return 1;
}
libretrodb_close(&db);
}

File diff suppressed because it is too large Load Diff

View File

@ -7,9 +7,7 @@ void libretrodb_query_inc_ref(libretrodb_query_t *q);
void libretrodb_query_dec_ref(libretrodb_query_t *q);
int libretrodb_query_filter(
libretrodb_query_t *q,
struct rmsgpack_dom_value * v
);
int libretrodb_query_filter(libretrodb_query_t *q,
struct rmsgpack_dom_value * v);
#endif

View File

@ -55,32 +55,36 @@ static const uint8_t MPF_UINT64 = 0xcf;
static const uint8_t MPF_NIL = 0xc0;
int rmsgpack_write_array_header(
int fd,
uint32_t size
){
uint16_t tmp_i16;
uint32_t tmp_i32;
if (size < 16) {
size = (size | MPF_FIXARRAY);
if (write(fd, &size, sizeof(int8_t)) == -1)
return -errno;
return sizeof(int8_t);
} else if (size == (uint16_t)size) {
if (write(fd, &MPF_ARRAY16, sizeof(MPF_ARRAY16)) == -1)
return -errno;
tmp_i16 = httobe16(size);
if (write(fd, (void *)(&tmp_i16), sizeof(uint16_t)) == -1)
return -errno;
return sizeof(int8_t) + sizeof(uint16_t);
} else {
if (write(fd, &MPF_ARRAY32, sizeof(MPF_ARRAY32)) == -1)
return -errno;
tmp_i32 = httobe32(size);
if (write(fd, (void *)(&tmp_i32), sizeof(uint32_t)) == -1)
return -errno;
return sizeof(int8_t) + sizeof(uint32_t);
}
int rmsgpack_write_array_header(int fd, uint32_t size)
{
uint16_t tmp_i16;
uint32_t tmp_i32;
if (size < 16)
{
size = (size | MPF_FIXARRAY);
if (write(fd, &size, sizeof(int8_t)) == -1)
return -errno;
return sizeof(int8_t);
}
else if (size == (uint16_t)size)
{
if (write(fd, &MPF_ARRAY16, sizeof(MPF_ARRAY16)) == -1)
return -errno;
tmp_i16 = httobe16(size);
if (write(fd, (void *)(&tmp_i16), sizeof(uint16_t)) == -1)
return -errno;
return sizeof(int8_t) + sizeof(uint16_t);
}
else
{
if (write(fd, &MPF_ARRAY32, sizeof(MPF_ARRAY32)) == -1)
return -errno;
tmp_i32 = httobe32(size);
if (write(fd, (void *)(&tmp_i32), sizeof(uint32_t)) == -1)
return -errno;
return sizeof(int8_t) + sizeof(uint32_t);
}
}
int rmsgpack_write_map_header(