mirror of
https://github.com/falk-werner/webfuse-provider
synced 2024-10-27 20:44:10 +00:00
added stub of json writer
This commit is contained in:
parent
792f805909
commit
c6c9edcd81
170
lib/webfuse_provider/impl/json/writer.c
Normal file
170
lib/webfuse_provider/impl/json/writer.c
Normal file
@ -0,0 +1,170 @@
|
||||
#include "webfuse_provider/impl/json/writer.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define WFP_JSON_WRITER_MAX_LEVEL 8
|
||||
|
||||
enum wfp_json_writer_state
|
||||
{
|
||||
WFP_JSON_WRITER_STATE_INIT
|
||||
};
|
||||
|
||||
struct wfp_json_writer
|
||||
{
|
||||
enum wfp_json_writer_state state[WFP_JSON_WRITER_MAX_LEVEL];
|
||||
size_t level;
|
||||
size_t capacity;
|
||||
size_t pre;
|
||||
size_t offset;
|
||||
char * data;
|
||||
char * raw_data;
|
||||
};
|
||||
|
||||
static void
|
||||
wfp_impl_json_writer_reserve(
|
||||
struct wfp_json_writer * writer,
|
||||
size_t needed)
|
||||
{
|
||||
if ((writer->capacity - writer->offset) < needed)
|
||||
{
|
||||
size_t new_capacity = 2 * writer->capacity;
|
||||
|
||||
while ((new_capacity - writer->offset) < needed)
|
||||
{
|
||||
new_capacity *= 2;
|
||||
}
|
||||
|
||||
writer->raw_data = realloc(writer->raw_data, writer->pre + new_capacity);
|
||||
writer->data = &(writer->data[writer->pre]);
|
||||
writer->capacity = new_capacity;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
wfp_impl_json_writer_write_raw_char(
|
||||
struct wfp_json_writer * writer,
|
||||
char value)
|
||||
{
|
||||
writer->data[writer->offset] = value;
|
||||
writer->offset++;
|
||||
}
|
||||
|
||||
void
|
||||
wfp_impl_json_writer_write_raw(
|
||||
struct wfp_json_writer * writer,
|
||||
char const * value,
|
||||
size_t length)
|
||||
{
|
||||
memcpy(&(writer->data[writer->offset]), value, length);
|
||||
writer->offset += length;
|
||||
}
|
||||
|
||||
struct wfp_json_writer *
|
||||
wfp_impl_json_writer_create(
|
||||
size_t initial_capacity,
|
||||
size_t pre)
|
||||
{
|
||||
struct wfp_json_writer * writer = malloc(sizeof(struct wfp_json_writer));
|
||||
writer->level = 0;
|
||||
writer->state[writer->level] = WFP_JSON_WRITER_STATE_INIT;
|
||||
writer->pre = pre;
|
||||
writer->capacity = initial_capacity;
|
||||
writer->offset = 0;
|
||||
writer->raw_data = malloc(writer->pre + writer->capacity);
|
||||
writer->data = &(writer->raw_data[pre]);
|
||||
|
||||
return writer;
|
||||
}
|
||||
|
||||
void
|
||||
wfp_impl_json_writer_dispose(
|
||||
struct wfp_json_writer * writer)
|
||||
{
|
||||
free(writer->raw_data);
|
||||
free(writer);
|
||||
}
|
||||
|
||||
char *
|
||||
wfp_impl_json_writer_take_data(
|
||||
struct wfp_json_writer * writer)
|
||||
{
|
||||
wfp_impl_json_writer_reserve(writer, 1);
|
||||
writer->data[writer->offset] = '\0';
|
||||
|
||||
char * result = writer->raw_data;
|
||||
writer->raw_data = NULL;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void
|
||||
wfp_impl_json_writer_write_int(
|
||||
struct wfp_json_writer * writer,
|
||||
int value)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void
|
||||
wfp_impl_json_writer_write_string(
|
||||
struct wfp_json_writer * writer,
|
||||
char const * value)
|
||||
{
|
||||
size_t length = strlen(value);
|
||||
wfp_impl_json_writer_reserve(writer, length + 2);
|
||||
|
||||
wfp_impl_json_writer_write_raw_char(writer, '\"');
|
||||
wfp_impl_json_writer_write_raw(writer, value, length);
|
||||
wfp_impl_json_writer_write_raw_char(writer, '\"');
|
||||
|
||||
}
|
||||
|
||||
void
|
||||
wfp_impl_json_writer_write_bytes(
|
||||
struct wfp_json_writer * writer,
|
||||
char const * data,
|
||||
size_t length)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void
|
||||
wfp_impl_json_writer_object_begin(
|
||||
struct wfp_json_writer * writer)
|
||||
{
|
||||
wfp_impl_json_writer_reserve(writer, 1);
|
||||
wfp_impl_json_writer_write_raw_char(writer, '{');
|
||||
}
|
||||
|
||||
void
|
||||
wfp_impl_json_writer_object_end(
|
||||
struct wfp_json_writer * writer)
|
||||
{
|
||||
wfp_impl_json_writer_reserve(writer, 1);
|
||||
wfp_impl_json_writer_write_raw_char(writer, '}');
|
||||
}
|
||||
|
||||
void
|
||||
wfp_impl_json_writer_object_key(
|
||||
struct wfp_json_writer * writer,
|
||||
char const * key)
|
||||
{
|
||||
wfp_impl_json_writer_reserve(writer, 1);
|
||||
wfp_impl_json_writer_write_raw_char(writer, '[');
|
||||
}
|
||||
|
||||
void
|
||||
wfp_impl_json_writer_array_begin(
|
||||
struct wfp_json_writer * writer)
|
||||
{
|
||||
wfp_impl_json_writer_reserve(writer, 1);
|
||||
wfp_impl_json_writer_write_raw_char(writer, '[');
|
||||
}
|
||||
|
||||
void
|
||||
wfp_impl_json_writer_array_end(
|
||||
struct wfp_json_writer * writer)
|
||||
{
|
||||
wfp_impl_json_writer_reserve(writer, 1);
|
||||
wfp_impl_json_writer_write_raw_char(writer, ']');
|
||||
}
|
72
lib/webfuse_provider/impl/json/writer.h
Normal file
72
lib/webfuse_provider/impl/json/writer.h
Normal file
@ -0,0 +1,72 @@
|
||||
#ifndef WFP_IMPL_JSON_WRITER_H
|
||||
#define WFP_IMPL_JSON_WRTIER_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stddef.h>
|
||||
#else
|
||||
#include <cstddef.h>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wfp_json_writer;
|
||||
|
||||
extern struct wfp_json_writer *
|
||||
wfp_impl_json_writer_create(
|
||||
size_t initial_capacity,
|
||||
size_t pre);
|
||||
|
||||
extern void
|
||||
wfp_impl_json_writer_dispose(
|
||||
struct wfp_json_writer * writer);
|
||||
|
||||
extern char *
|
||||
wfp_impl_json_writer_take_data(
|
||||
struct wfp_json_writer * writer);
|
||||
|
||||
extern void
|
||||
wfp_impl_json_writer_write_int(
|
||||
struct wfp_json_writer * writer,
|
||||
int value);
|
||||
|
||||
extern void
|
||||
wfp_impl_json_writer_write_string(
|
||||
struct wfp_json_writer * writer,
|
||||
char const * value);
|
||||
|
||||
extern void
|
||||
wfp_impl_json_writer_write_bytes(
|
||||
struct wfp_json_writer * writer,
|
||||
char const * data,
|
||||
size_t length);
|
||||
|
||||
extern void
|
||||
wfp_impl_json_writer_object_begin(
|
||||
struct wfp_json_writer * writer);
|
||||
|
||||
extern void
|
||||
wfp_impl_json_writer_object_end(
|
||||
struct wfp_json_writer * writer);
|
||||
|
||||
extern void
|
||||
wfp_impl_json_writer_object_key(
|
||||
struct wfp_json_writer * writer,
|
||||
char const * key);
|
||||
|
||||
extern void
|
||||
wfp_impl_json_writer_array_begin(
|
||||
struct wfp_json_writer * writer);
|
||||
|
||||
extern void
|
||||
wfp_impl_json_writer_array_end(
|
||||
struct wfp_json_writer * writer);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -29,6 +29,7 @@ webfuse_provider_static = static_library('webfuse_provider',
|
||||
'lib/webfuse_provider/impl/timer/manager.c',
|
||||
'lib/webfuse_provider/impl/timer/timepoint.c',
|
||||
'lib/webfuse_provider/impl/timer/timer.c',
|
||||
'lib/webfuse_provider/impl/json/writer.c',
|
||||
'lib/webfuse_provider/impl/jsonrpc/proxy.c',
|
||||
'lib/webfuse_provider/impl/jsonrpc/proxy_variadic.c',
|
||||
'lib/webfuse_provider/impl/jsonrpc/request.c',
|
||||
|
Loading…
Reference in New Issue
Block a user