mirror of
https://github.com/falk-werner/webfuse-provider
synced 2024-10-27 20:44:10 +00:00
refactor: remove jansson implementation of dirbuffer
This commit is contained in:
parent
da74c9cdbc
commit
5cdeb6d759
@ -1,10 +1,15 @@
|
||||
#include "webfuse_provider/impl/dirbuffer.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define WFP_IMPL_DIRBUFFER_DEFAULT_CAPACITY 8
|
||||
|
||||
struct wfp_dirbuffer * wfp_impl_dirbuffer_create(void)
|
||||
{
|
||||
struct wfp_dirbuffer * buffer = malloc(sizeof(struct wfp_dirbuffer));
|
||||
buffer->entries = json_array();
|
||||
buffer->size = 0;
|
||||
buffer->capacity = WFP_IMPL_DIRBUFFER_DEFAULT_CAPACITY;
|
||||
buffer->entries = malloc(sizeof(struct wfp_dirbuffer_entry) * buffer->capacity);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
@ -12,11 +17,12 @@ struct wfp_dirbuffer * wfp_impl_dirbuffer_create(void)
|
||||
void wfp_impl_dirbuffer_dispose(
|
||||
struct wfp_dirbuffer * buffer)
|
||||
{
|
||||
if (NULL != buffer->entries)
|
||||
for (size_t i = 0; i < buffer->size; i++)
|
||||
{
|
||||
json_decref(buffer->entries);
|
||||
free(buffer->entries[i].name);
|
||||
}
|
||||
|
||||
free(buffer->entries);
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
@ -25,18 +31,35 @@ void wfp_impl_dirbuffer_add(
|
||||
char const * name,
|
||||
ino_t inode)
|
||||
{
|
||||
json_t * entry = json_object();
|
||||
json_object_set_new(entry, "name", json_string(name));
|
||||
json_object_set_new(entry, "inode", json_integer(inode));
|
||||
if (buffer->size >= buffer->capacity)
|
||||
{
|
||||
buffer->capacity *= 2;
|
||||
buffer->entries = realloc(buffer->entries, (sizeof(struct wfp_dirbuffer_entry) * buffer->capacity));
|
||||
}
|
||||
|
||||
json_array_append_new(buffer->entries, entry);
|
||||
buffer->entries[buffer->size].name = strdup(name);
|
||||
buffer->entries[buffer->size].inode = inode;
|
||||
buffer->size++;
|
||||
}
|
||||
|
||||
json_t * wfp_impl_dirbuffer_take(
|
||||
size_t
|
||||
wfp_impl_dirbuffer_size(
|
||||
struct wfp_dirbuffer * buffer)
|
||||
{
|
||||
json_t * entries = buffer->entries;
|
||||
|
||||
buffer->entries = NULL;
|
||||
return entries;
|
||||
return buffer->size;
|
||||
}
|
||||
|
||||
struct wfp_dirbuffer_entry const *
|
||||
wfp_impl_dirbuffer_entry_at(
|
||||
struct wfp_dirbuffer * buffer,
|
||||
size_t pos)
|
||||
{
|
||||
struct wfp_dirbuffer_entry const * entry = NULL;
|
||||
|
||||
if (pos < buffer->size)
|
||||
{
|
||||
entry = &(buffer->entries[pos]);
|
||||
}
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
@ -4,16 +4,23 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <jansson.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wfp_dirbuffer_entry
|
||||
{
|
||||
char * name;
|
||||
ino_t inode;
|
||||
};
|
||||
|
||||
struct wfp_dirbuffer
|
||||
{
|
||||
json_t * entries;
|
||||
struct wfp_dirbuffer_entry * entries;
|
||||
size_t size;
|
||||
size_t capacity;
|
||||
};
|
||||
|
||||
extern struct wfp_dirbuffer * wfp_impl_dirbuffer_create(void);
|
||||
@ -26,9 +33,14 @@ extern void wfp_impl_dirbuffer_add(
|
||||
char const * name,
|
||||
ino_t inode);
|
||||
|
||||
extern json_t * wfp_impl_dirbuffer_take(
|
||||
extern size_t
|
||||
wfp_impl_dirbuffer_size(
|
||||
struct wfp_dirbuffer * buffer);
|
||||
|
||||
extern struct wfp_dirbuffer_entry const *
|
||||
wfp_impl_dirbuffer_entry_at(
|
||||
struct wfp_dirbuffer * buffer,
|
||||
size_t pos);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@ -99,22 +99,19 @@ wfp_impl_message_writer_add_dirbuffer(
|
||||
wfp_impl_json_writer_object_key(writer->json_writer, "result");
|
||||
wfp_impl_json_writer_array_begin(writer->json_writer);
|
||||
|
||||
json_t * entries = wfp_impl_dirbuffer_take(dirbuffer);
|
||||
for (size_t i = 0; i < json_array_size(entries); i++)
|
||||
size_t const count = wfp_impl_dirbuffer_size(dirbuffer);
|
||||
for (size_t i = 0; i < count; i++)
|
||||
{
|
||||
json_t * entry = json_array_get(entries, i);
|
||||
char const * name = json_string_value(json_object_get(entry, "name"));
|
||||
int inode = json_integer_value(json_object_get(entry, "inode"));
|
||||
struct wfp_dirbuffer_entry const * entry = wfp_impl_dirbuffer_entry_at(dirbuffer, i);
|
||||
|
||||
wfp_impl_json_writer_object_begin(writer->json_writer);
|
||||
wfp_impl_json_writer_object_key(writer->json_writer, "name");
|
||||
wfp_impl_json_writer_write_string(writer->json_writer, name);
|
||||
wfp_impl_json_writer_write_string(writer->json_writer, entry->name);
|
||||
wfp_impl_json_writer_object_key(writer->json_writer, "inode");
|
||||
wfp_impl_json_writer_write_int(writer->json_writer, inode);
|
||||
wfp_impl_json_writer_write_int(writer->json_writer, entry->inode);
|
||||
wfp_impl_json_writer_object_end(writer->json_writer);
|
||||
|
||||
}
|
||||
json_decref(entries);
|
||||
|
||||
wfp_impl_json_writer_array_end(writer->json_writer);
|
||||
|
||||
|
@ -12,21 +12,19 @@ TEST(DirBuffer, Add)
|
||||
wfp_dirbuffer * buffer = wfp_impl_dirbuffer_create();
|
||||
wfp_impl_dirbuffer_add(buffer, "answer", 42);
|
||||
|
||||
ASSERT_EQ(1, json_array_size(buffer->entries));
|
||||
ASSERT_EQ(1, wfp_impl_dirbuffer_size(buffer));
|
||||
|
||||
json_t * entry = json_array_get(buffer->entries, 0);
|
||||
ASSERT_STREQ("answer", json_string_value(json_object_get(entry, "name")));
|
||||
ASSERT_EQ(42, json_integer_value(json_object_get(entry, "inode")));
|
||||
auto * entry = wfp_impl_dirbuffer_entry_at(buffer, 0);
|
||||
ASSERT_STREQ("answer", entry->name);
|
||||
ASSERT_EQ(42, entry->inode);
|
||||
|
||||
wfp_impl_dirbuffer_dispose(buffer);
|
||||
}
|
||||
|
||||
TEST(DirBuffer, Take)
|
||||
TEST(DirBuffer, EntryAt)
|
||||
{
|
||||
wfp_dirbuffer * buffer = wfp_impl_dirbuffer_create();
|
||||
json_t * entries = wfp_impl_dirbuffer_take(buffer);
|
||||
auto * entry = wfp_impl_dirbuffer_entry_at(buffer, 0);
|
||||
ASSERT_EQ(nullptr, entry);
|
||||
wfp_impl_dirbuffer_dispose(buffer);
|
||||
|
||||
ASSERT_TRUE(json_is_array(entries));
|
||||
json_decref(entries);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user