1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2024-10-27 20:44:10 +00:00

refactor: removed depencency to libjansson of credentials

This commit is contained in:
Falk Werner 2020-07-11 00:44:50 +02:00
parent e0b01cfe19
commit 7542f0bbc0
2 changed files with 39 additions and 11 deletions

View File

@ -4,18 +4,27 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define WFP_IMPL_CREDENTIALS_DEFAULT_SIZE 8
void wfp_impl_credentials_init( void wfp_impl_credentials_init(
struct wfp_credentials * credentials) struct wfp_credentials * credentials)
{ {
credentials->type = NULL; credentials->type = NULL;
credentials->contents = json_object(); credentials->size = 0;
credentials->capacity = WFP_IMPL_CREDENTIALS_DEFAULT_SIZE;
credentials->entries = malloc(sizeof(struct wfp_credentials_entry) * credentials->capacity);
} }
void wfp_impl_credentials_cleanup( void wfp_impl_credentials_cleanup(
struct wfp_credentials * credentials) struct wfp_credentials * credentials)
{ {
for(size_t i = 0; i < credentials->size; i++)
{
free(credentials->entries[i].key);
free(credentials->entries[i].value);
}
free(credentials->entries);
free(credentials->type); free(credentials->type);
json_decref(credentials->contents);
} }
void wfp_impl_credentials_set_type( void wfp_impl_credentials_set_type(
@ -31,7 +40,15 @@ void wfp_impl_credentials_add(
char const * key, char const * key,
char const * value) char const * value)
{ {
json_object_set_new(credentials->contents, key, json_string(value)); if (credentials->size >= credentials->capacity)
{
credentials->capacity *= 2;
credentials->entries = realloc(credentials->entries, sizeof(struct wfp_credentials_entry) * credentials->capacity);
}
credentials->entries[credentials->size].key = strdup(key);
credentials->entries[credentials->size].value = strdup(value);
credentials->size++;
} }
void void
@ -42,14 +59,12 @@ wfp_impl_credentials_write(
struct wfp_credentials * credentials = (struct wfp_credentials *) data; struct wfp_credentials * credentials = (struct wfp_credentials *) data;
wfp_impl_json_writer_object_begin(writer); wfp_impl_json_writer_object_begin(writer);
char const * key; for(size_t i = 0; i < credentials->size; i++)
json_t * value;
json_t * contents = credentials->contents;
json_object_foreach(contents, key, value)
{ {
wfp_impl_json_writer_object_write_string(writer, key, json_string_value(value)); wfp_impl_json_writer_object_write_string(writer,
credentials->entries[i].key,
credentials->entries[i].value);
} }
wfp_impl_json_writer_object_end(writer); wfp_impl_json_writer_object_end(writer);
} }

View File

@ -3,7 +3,12 @@
#include "webfuse_provider/credentials.h" #include "webfuse_provider/credentials.h"
#include "webfuse_provider/impl/jsonrpc/proxy_intern.h" #include "webfuse_provider/impl/jsonrpc/proxy_intern.h"
#include <jansson.h>
#ifndef __cplusplus
#include <stddef.h>
#else
#include <cstddef>
#endif
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"
@ -12,10 +17,18 @@ extern "C"
struct wfp_json_writer; struct wfp_json_writer;
struct wfp_credentials_entry
{
char * key;
char * value;
};
struct wfp_credentials struct wfp_credentials
{ {
char * type; char * type;
json_t * contents; struct wfp_credentials_entry * entries;
size_t size;
size_t capacity;
}; };
extern void wfp_impl_credentials_init( extern void wfp_impl_credentials_init(