serial: update to use new object system
This commit is contained in:
@@ -1,12 +1,11 @@
|
|||||||
#ifndef BLUELIB_SERIAL_H_
|
#ifndef BLUE_SERIAL_H_
|
||||||
#define BLUELIB_SERIAL_H_
|
#define BLUE_SERIAL_H_
|
||||||
|
|
||||||
#include <blue/core/misc.h>
|
#include <blue/core/misc.h>
|
||||||
|
#include <blue/core/object.h>
|
||||||
#include <blue/core/status.h>
|
#include <blue/core/status.h>
|
||||||
#include <blue/core/stream.h>
|
#include <blue/core/stream.h>
|
||||||
|
|
||||||
struct b_dsref;
|
|
||||||
|
|
||||||
typedef enum b_serial_format {
|
typedef enum b_serial_format {
|
||||||
B_SERIAL_FORMAT_NONE = 0,
|
B_SERIAL_FORMAT_NONE = 0,
|
||||||
B_SERIAL_FORMAT_BITCODE,
|
B_SERIAL_FORMAT_BITCODE,
|
||||||
@@ -25,11 +24,11 @@ BLUE_API b_status b_serial_ctx_create(b_serial_ctx **out);
|
|||||||
BLUE_API b_status b_serial_ctx_destroy(b_serial_ctx *ctx);
|
BLUE_API b_status b_serial_ctx_destroy(b_serial_ctx *ctx);
|
||||||
|
|
||||||
BLUE_API b_status b_serial_ctx_serialise(
|
BLUE_API b_status b_serial_ctx_serialise(
|
||||||
b_serial_ctx *ctx, b_serial_format fmt, struct b_dsref *src,
|
b_serial_ctx *ctx, b_serial_format fmt, b_object *src, b_stream *dest,
|
||||||
b_stream *dest, b_serial_flags flags);
|
b_serial_flags flags);
|
||||||
|
|
||||||
BLUE_API b_status b_serial_ctx_deserialise(
|
BLUE_API b_status b_serial_ctx_deserialise(
|
||||||
b_serial_ctx *ctx, b_serial_format fmt, b_stream *src,
|
b_serial_ctx *ctx, b_serial_format fmt, b_stream *src, b_object **dest,
|
||||||
struct b_dsref **dest, b_serial_flags flags);
|
b_serial_flags flags);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
130
serial/json.c
130
serial/json.c
@@ -40,7 +40,6 @@
|
|||||||
#include <blue/ds/array.h>
|
#include <blue/ds/array.h>
|
||||||
#include <blue/ds/dict.h>
|
#include <blue/ds/dict.h>
|
||||||
#include <blue/ds/number.h>
|
#include <blue/ds/number.h>
|
||||||
#include <blue/ds/object.h>
|
|
||||||
#include <blue/ds/string.h>
|
#include <blue/ds/string.h>
|
||||||
#include <blue/ds/uuid.h>
|
#include <blue/ds/uuid.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
@@ -2933,21 +2932,21 @@ CJSON_PUBLIC(void) cJSON_free(void *object)
|
|||||||
object = NULL;
|
object = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static enum b_status b_dsref_to_cJSON(const struct b_dsref *object, cJSON **out);
|
static enum b_status b_object_to_cJSON(const b_object *object, cJSON **out);
|
||||||
|
|
||||||
static enum b_status serialise_array(const struct b_dsref *object, cJSON **out)
|
static enum b_status serialise_array(const b_object *object, cJSON **out)
|
||||||
{
|
{
|
||||||
cJSON *json_array = cJSON_CreateArray();
|
cJSON *json_array = cJSON_CreateArray();
|
||||||
if (!json_array) {
|
if (!json_array) {
|
||||||
return B_ERR_NO_MEMORY;
|
return B_ERR_NO_MEMORY;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct b_array *array = B_ARRAY(object);
|
b_array *array = (b_array *)object;
|
||||||
b_array_iterator it;
|
b_array_iterator it;
|
||||||
b_array_foreach(&it, array)
|
b_array_foreach(&it, array)
|
||||||
{
|
{
|
||||||
cJSON *child = NULL;
|
cJSON *child = NULL;
|
||||||
enum b_status status = b_dsref_to_cJSON(it.value, &child);
|
enum b_status status = b_object_to_cJSON(it.value, &child);
|
||||||
if (!B_OK(status)) {
|
if (!B_OK(status)) {
|
||||||
cJSON_Delete(json_array);
|
cJSON_Delete(json_array);
|
||||||
return status;
|
return status;
|
||||||
@@ -2960,19 +2959,19 @@ static enum b_status serialise_array(const struct b_dsref *object, cJSON **out)
|
|||||||
return B_SUCCESS;
|
return B_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static enum b_status serialise_dict(const struct b_dsref *object, cJSON **out)
|
static enum b_status serialise_dict(const b_object *object, cJSON **out)
|
||||||
{
|
{
|
||||||
cJSON *json_dict = cJSON_CreateObject();
|
cJSON *json_dict = cJSON_CreateObject();
|
||||||
if (!json_dict) {
|
if (!json_dict) {
|
||||||
return B_ERR_NO_MEMORY;
|
return B_ERR_NO_MEMORY;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct b_dict *dict = B_DICT(object);
|
b_dict *dict = (b_dict *)object;
|
||||||
b_dict_iterator it;
|
b_dict_iterator it;
|
||||||
b_dict_foreach(&it, dict)
|
b_dict_foreach(&it, dict)
|
||||||
{
|
{
|
||||||
cJSON *child = NULL;
|
cJSON *child = NULL;
|
||||||
enum b_status status = b_dsref_to_cJSON(it.value, &child);
|
enum b_status status = b_object_to_cJSON(it.value, &child);
|
||||||
if (!B_OK(status)) {
|
if (!B_OK(status)) {
|
||||||
cJSON_Delete(json_dict);
|
cJSON_Delete(json_dict);
|
||||||
return status;
|
return status;
|
||||||
@@ -2985,15 +2984,15 @@ static enum b_status serialise_dict(const struct b_dsref *object, cJSON **out)
|
|||||||
return B_SUCCESS;
|
return B_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static enum b_status serialise_number(const struct b_dsref *object, cJSON **out)
|
static enum b_status serialise_number(const b_object *object, cJSON **out)
|
||||||
{
|
{
|
||||||
cJSON *json_number = cJSON_CreateNumber(0);
|
cJSON *json_number = cJSON_CreateNumber(0);
|
||||||
if (!json_number) {
|
if (!json_number) {
|
||||||
return B_ERR_NO_MEMORY;
|
return B_ERR_NO_MEMORY;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct b_number *number = B_NUMBER(object);
|
b_number *number = (b_number *)object;
|
||||||
enum b_number_type type = b_number_get_type(number);
|
enum b_number_type type = b_number_get_number_type(number);
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case B_NUMBER_INT8:
|
case B_NUMBER_INT8:
|
||||||
@@ -3049,9 +3048,9 @@ static enum b_status serialise_number(const struct b_dsref *object, cJSON **out)
|
|||||||
return B_SUCCESS;
|
return B_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static enum b_status serialise_string(const struct b_dsref *object, cJSON **out)
|
static enum b_status serialise_string(const b_object *object, cJSON **out)
|
||||||
{
|
{
|
||||||
struct b_string *string = B_STRING(object);
|
b_string *string = (b_string *)object;
|
||||||
cJSON *json_string = cJSON_CreateStringReference(b_string_ptr(string));
|
cJSON *json_string = cJSON_CreateStringReference(b_string_ptr(string));
|
||||||
if (!json_string) {
|
if (!json_string) {
|
||||||
return B_ERR_NO_MEMORY;
|
return B_ERR_NO_MEMORY;
|
||||||
@@ -3061,9 +3060,9 @@ static enum b_status serialise_string(const struct b_dsref *object, cJSON **out)
|
|||||||
return B_SUCCESS;
|
return B_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static enum b_status serialise_uuid(const struct b_dsref *object, cJSON **out)
|
static enum b_status serialise_uuid(const b_object *object, cJSON **out)
|
||||||
{
|
{
|
||||||
struct b_uuid *uuid = B_UUID(object);
|
b_uuid *uuid = (b_uuid *)object;
|
||||||
char s[B_UUID_STRING_MAX];
|
char s[B_UUID_STRING_MAX];
|
||||||
b_uuid_to_cstr(uuid, s);
|
b_uuid_to_cstr(uuid, s);
|
||||||
|
|
||||||
@@ -3076,39 +3075,28 @@ static enum b_status serialise_uuid(const struct b_dsref *object, cJSON **out)
|
|||||||
return B_SUCCESS;
|
return B_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef enum b_status (*object_serialise_callback)(
|
static enum b_status b_object_to_cJSON(const b_object *object, cJSON **out)
|
||||||
const struct b_dsref *, cJSON **);
|
|
||||||
|
|
||||||
static const object_serialise_callback object_serialise[] = {
|
|
||||||
[B_DSREF_TYPE_ARRAY] = serialise_array,
|
|
||||||
[B_DSREF_TYPE_DICT] = serialise_dict,
|
|
||||||
[B_DSREF_TYPE_NUMBER] = serialise_number,
|
|
||||||
[B_DSREF_TYPE_STRING] = serialise_string,
|
|
||||||
[B_DSREF_TYPE_UUID] = serialise_uuid,
|
|
||||||
};
|
|
||||||
static const size_t nr_object_serialise
|
|
||||||
= sizeof object_serialise / sizeof object_serialise[0];
|
|
||||||
|
|
||||||
static enum b_status b_dsref_to_cJSON(const struct b_dsref *object, cJSON **out)
|
|
||||||
{
|
{
|
||||||
b_dsref_type_id type = b_typeid(object);
|
if (b_object_is_type(object, B_TYPE_ARRAY)) {
|
||||||
if (type >= nr_object_serialise) {
|
return serialise_array(object, out);
|
||||||
|
} else if (b_object_is_type(object, B_TYPE_DICT)) {
|
||||||
|
return serialise_dict(object, out);
|
||||||
|
} else if (b_object_is_type(object, B_TYPE_NUMBER)) {
|
||||||
|
return serialise_number(object, out);
|
||||||
|
} else if (b_object_is_type(object, B_TYPE_STRING)) {
|
||||||
|
return serialise_string(object, out);
|
||||||
|
} else if (b_object_is_type(object, B_TYPE_UUID)) {
|
||||||
|
return serialise_uuid(object, out);
|
||||||
|
} else {
|
||||||
return B_ERR_NOT_SUPPORTED;
|
return B_ERR_NOT_SUPPORTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
object_serialise_callback callback = object_serialise[type];
|
|
||||||
if (!callback) {
|
|
||||||
return B_ERR_NOT_SUPPORTED;
|
|
||||||
}
|
|
||||||
|
|
||||||
return callback(object, out);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static enum b_status cJSON_to_b_dsref(cJSON *json, struct b_dsref **out);
|
static enum b_status cJSON_to_b_object(cJSON *json, b_object **out);
|
||||||
|
|
||||||
static enum b_status deserialise_dict(cJSON *json, struct b_dsref **out)
|
static enum b_status deserialise_dict(cJSON *json, b_object **out)
|
||||||
{
|
{
|
||||||
struct b_dict *dict = b_dict_create();
|
b_dict *dict = b_dict_create();
|
||||||
if (!dict) {
|
if (!dict) {
|
||||||
return B_ERR_NO_MEMORY;
|
return B_ERR_NO_MEMORY;
|
||||||
}
|
}
|
||||||
@@ -3117,29 +3105,29 @@ static enum b_status deserialise_dict(cJSON *json, struct b_dsref **out)
|
|||||||
enum b_status status = B_SUCCESS;
|
enum b_status status = B_SUCCESS;
|
||||||
|
|
||||||
while (item) {
|
while (item) {
|
||||||
struct b_dsref *child = NULL;
|
b_object *child = NULL;
|
||||||
status = cJSON_to_b_dsref(item, &child);
|
status = cJSON_to_b_object(item, &child);
|
||||||
if (!B_OK(status)) {
|
if (!B_OK(status)) {
|
||||||
b_dict_release(dict);
|
b_dict_unref(dict);
|
||||||
dict = NULL;
|
dict = NULL;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (child) {
|
if (child) {
|
||||||
b_dict_put(dict, item->string, child);
|
b_dict_put(dict, item->string, child);
|
||||||
b_release(child);
|
b_object_unref(child);
|
||||||
}
|
}
|
||||||
|
|
||||||
item = item->next;
|
item = item->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
*out = B_DSREF(dict);
|
*out = (dict);
|
||||||
return B_SUCCESS;
|
return B_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static enum b_status deserialise_array(cJSON *json, struct b_dsref **out)
|
static enum b_status deserialise_array(cJSON *json, b_object **out)
|
||||||
{
|
{
|
||||||
struct b_array *array = b_array_create();
|
b_array *array = b_array_create();
|
||||||
if (!array) {
|
if (!array) {
|
||||||
return B_ERR_NO_MEMORY;
|
return B_ERR_NO_MEMORY;
|
||||||
}
|
}
|
||||||
@@ -3148,40 +3136,40 @@ static enum b_status deserialise_array(cJSON *json, struct b_dsref **out)
|
|||||||
enum b_status status = B_SUCCESS;
|
enum b_status status = B_SUCCESS;
|
||||||
|
|
||||||
while (item) {
|
while (item) {
|
||||||
struct b_dsref *child = NULL;
|
b_object *child = NULL;
|
||||||
status = cJSON_to_b_dsref(item, &child);
|
status = cJSON_to_b_object(item, &child);
|
||||||
if (!B_OK(status)) {
|
if (!B_OK(status)) {
|
||||||
b_array_release(array);
|
b_array_unref(array);
|
||||||
array = NULL;
|
array = NULL;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (child) {
|
if (child) {
|
||||||
b_array_append(array, child);
|
b_array_append(array, child);
|
||||||
b_release(child);
|
b_object_unref(child);
|
||||||
}
|
}
|
||||||
|
|
||||||
item = item->next;
|
item = item->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
*out = B_DSREF(array);
|
*out = (array);
|
||||||
return B_SUCCESS;
|
return B_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static enum b_status deserialise_string(cJSON *json, struct b_dsref **out)
|
static enum b_status deserialise_string(cJSON *json, b_object **out)
|
||||||
{
|
{
|
||||||
struct b_string *string = b_string_create_from_cstr(json->valuestring);
|
b_string *string = b_string_create_from_cstr(json->valuestring);
|
||||||
if (!string) {
|
if (!string) {
|
||||||
return B_ERR_NO_MEMORY;
|
return B_ERR_NO_MEMORY;
|
||||||
}
|
}
|
||||||
|
|
||||||
*out = B_DSREF(string);
|
*out = (string);
|
||||||
return B_SUCCESS;
|
return B_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static enum b_status deserialise_number(cJSON *json, struct b_dsref **out)
|
static enum b_status deserialise_number(cJSON *json, b_object **out)
|
||||||
{
|
{
|
||||||
struct b_number *number = NULL;
|
b_number *number = NULL;
|
||||||
|
|
||||||
if ((double)json->valueint == json->valuedouble) {
|
if ((double)json->valueint == json->valuedouble) {
|
||||||
number = B_INT(json->valueint);
|
number = B_INT(json->valueint);
|
||||||
@@ -3193,13 +3181,13 @@ static enum b_status deserialise_number(cJSON *json, struct b_dsref **out)
|
|||||||
return B_ERR_NO_MEMORY;
|
return B_ERR_NO_MEMORY;
|
||||||
}
|
}
|
||||||
|
|
||||||
*out = B_DSREF(number);
|
*out = (number);
|
||||||
return B_SUCCESS;
|
return B_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static enum b_status deserialise_bool(cJSON *json, struct b_dsref **out)
|
static enum b_status deserialise_bool(cJSON *json, b_object **out)
|
||||||
{
|
{
|
||||||
struct b_number *number = NULL;
|
b_number *number = NULL;
|
||||||
|
|
||||||
if (json->type == cJSON_True) {
|
if (json->type == cJSON_True) {
|
||||||
number = B_INT(1);
|
number = B_INT(1);
|
||||||
@@ -3211,11 +3199,11 @@ static enum b_status deserialise_bool(cJSON *json, struct b_dsref **out)
|
|||||||
return B_ERR_NO_MEMORY;
|
return B_ERR_NO_MEMORY;
|
||||||
}
|
}
|
||||||
|
|
||||||
*out = B_DSREF(number);
|
*out = (number);
|
||||||
return B_SUCCESS;
|
return B_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static enum b_status cJSON_to_b_dsref(cJSON *json, struct b_dsref **out)
|
static enum b_status cJSON_to_b_object(cJSON *json, b_object **out)
|
||||||
{
|
{
|
||||||
switch (json->type) {
|
switch (json->type) {
|
||||||
case cJSON_True:
|
case cJSON_True:
|
||||||
@@ -3238,11 +3226,11 @@ static enum b_status cJSON_to_b_dsref(cJSON *json, struct b_dsref **out)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static enum b_status json_serialise(
|
static enum b_status json_serialise(
|
||||||
struct b_serial_ctx *ctx, struct b_dsref *src, struct b_stream *dest,
|
struct b_serial_ctx *ctx, b_object *src, struct b_stream *dest,
|
||||||
enum b_serial_flags flags)
|
enum b_serial_flags flags)
|
||||||
{
|
{
|
||||||
cJSON *json;
|
cJSON *json;
|
||||||
enum b_status status = b_dsref_to_cJSON(src, &json);
|
enum b_status status = b_object_to_cJSON(src, &json);
|
||||||
|
|
||||||
if (!B_OK(status)) {
|
if (!B_OK(status)) {
|
||||||
return status;
|
return status;
|
||||||
@@ -3269,14 +3257,14 @@ static enum b_status json_serialise(
|
|||||||
}
|
}
|
||||||
|
|
||||||
static enum b_status json_deserialise(
|
static enum b_status json_deserialise(
|
||||||
struct b_serial_ctx *ctx, struct b_stream *src, struct b_dsref **dest,
|
struct b_serial_ctx *ctx, struct b_stream *src, b_object **dest,
|
||||||
enum b_serial_flags flags)
|
enum b_serial_flags flags)
|
||||||
{
|
{
|
||||||
struct b_string *json_string = b_string_create();
|
b_string *json_string = b_string_create();
|
||||||
struct b_stream *json_stream = NULL;
|
struct b_stream *json_stream = NULL;
|
||||||
enum b_status status = b_string_open_stream(json_string, &json_stream);
|
enum b_status status = b_string_open_stream(json_string, &json_stream);
|
||||||
if (!B_OK(status)) {
|
if (!B_OK(status)) {
|
||||||
b_string_release(json_string);
|
b_string_unref(json_string);
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3284,17 +3272,17 @@ static enum b_status json_deserialise(
|
|||||||
src, json_stream, ctx->ctx_pipeline, NULL);
|
src, json_stream, ctx->ctx_pipeline, NULL);
|
||||||
b_stream_close(json_stream);
|
b_stream_close(json_stream);
|
||||||
if (!B_OK(status)) {
|
if (!B_OK(status)) {
|
||||||
b_string_release(json_string);
|
b_string_unref(json_string);
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
cJSON *json = cJSON_Parse(b_string_ptr(json_string));
|
cJSON *json = cJSON_Parse(b_string_ptr(json_string));
|
||||||
b_string_release(json_string);
|
b_string_unref(json_string);
|
||||||
if (!json) {
|
if (!json) {
|
||||||
return B_ERR_BAD_FORMAT;
|
return B_ERR_BAD_FORMAT;
|
||||||
}
|
}
|
||||||
|
|
||||||
status = cJSON_to_b_dsref(json, dest);
|
status = cJSON_to_b_object(json, dest);
|
||||||
|
|
||||||
cJSON_Delete(json);
|
cJSON_Delete(json);
|
||||||
|
|
||||||
|
|||||||
@@ -52,8 +52,8 @@ static const struct b_serial_format_ops *get_formatter(enum b_serial_format fmt)
|
|||||||
}
|
}
|
||||||
|
|
||||||
enum b_status b_serial_ctx_serialise(
|
enum b_status b_serial_ctx_serialise(
|
||||||
struct b_serial_ctx *ctx, enum b_serial_format fmt,
|
struct b_serial_ctx *ctx, enum b_serial_format fmt, b_object *src,
|
||||||
struct b_dsref *src, struct b_stream *dest, enum b_serial_flags flags)
|
struct b_stream *dest, enum b_serial_flags flags)
|
||||||
{
|
{
|
||||||
const struct b_serial_format_ops *ops = get_formatter(fmt);
|
const struct b_serial_format_ops *ops = get_formatter(fmt);
|
||||||
if (!ops) {
|
if (!ops) {
|
||||||
@@ -69,7 +69,7 @@ enum b_status b_serial_ctx_serialise(
|
|||||||
|
|
||||||
enum b_status b_serial_ctx_deserialise(
|
enum b_status b_serial_ctx_deserialise(
|
||||||
struct b_serial_ctx *ctx, enum b_serial_format fmt,
|
struct b_serial_ctx *ctx, enum b_serial_format fmt,
|
||||||
struct b_stream *src, struct b_dsref **dest, enum b_serial_flags flags)
|
struct b_stream *src, b_object **dest, enum b_serial_flags flags)
|
||||||
{
|
{
|
||||||
const struct b_serial_format_ops *ops = get_formatter(fmt);
|
const struct b_serial_format_ops *ops = get_formatter(fmt);
|
||||||
if (!ops) {
|
if (!ops) {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#ifndef _SERIAL_SERIAL_H_
|
#ifndef _SERIAL_SERIAL_H_
|
||||||
#define _SERIAL_SERIAL_H_
|
#define _SERIAL_SERIAL_H_
|
||||||
|
|
||||||
|
#include <blue/core/object.h>
|
||||||
#include <blue/serial.h>
|
#include <blue/serial.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
@@ -10,10 +11,10 @@ struct b_serial_ctx {
|
|||||||
|
|
||||||
struct b_serial_format_ops {
|
struct b_serial_format_ops {
|
||||||
enum b_status (*fmt_serialise)(
|
enum b_status (*fmt_serialise)(
|
||||||
struct b_serial_ctx *, struct b_dsref *, struct b_stream *,
|
struct b_serial_ctx *, b_object *, struct b_stream *,
|
||||||
enum b_serial_flags);
|
enum b_serial_flags);
|
||||||
enum b_status (*fmt_deserialise)(
|
enum b_status (*fmt_deserialise)(
|
||||||
struct b_serial_ctx *, struct b_stream *, struct b_dsref **,
|
struct b_serial_ctx *, struct b_stream *, b_object **,
|
||||||
enum b_serial_flags);
|
enum b_serial_flags);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
197
serial/toml.c
197
serial/toml.c
@@ -119,7 +119,7 @@ struct ctx {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static void ctx_set_object_flags(
|
static void ctx_set_object_flags(
|
||||||
struct ctx *ctx, struct b_dsref *obj, enum object_flags flags)
|
struct ctx *ctx, b_object *obj, enum object_flags flags)
|
||||||
{
|
{
|
||||||
if (!obj) {
|
if (!obj) {
|
||||||
return;
|
return;
|
||||||
@@ -127,7 +127,7 @@ static void ctx_set_object_flags(
|
|||||||
|
|
||||||
b_hashmap_key key = {
|
b_hashmap_key key = {
|
||||||
.key_data = obj,
|
.key_data = obj,
|
||||||
.key_size = sizeof(struct b_dsref *),
|
.key_size = sizeof(b_object *),
|
||||||
.key_flags = B_HASHMAP_KEY_F_INTVALUE,
|
.key_flags = B_HASHMAP_KEY_F_INTVALUE,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -150,7 +150,7 @@ static void ctx_set_object_flags(
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void ctx_clear_object_flags(
|
static void ctx_clear_object_flags(
|
||||||
struct ctx *ctx, struct b_dsref *obj, enum object_flags mask)
|
struct ctx *ctx, b_object *obj, enum object_flags mask)
|
||||||
{
|
{
|
||||||
if (!obj) {
|
if (!obj) {
|
||||||
return;
|
return;
|
||||||
@@ -158,7 +158,7 @@ static void ctx_clear_object_flags(
|
|||||||
|
|
||||||
b_hashmap_key key = {
|
b_hashmap_key key = {
|
||||||
.key_data = obj,
|
.key_data = obj,
|
||||||
.key_size = sizeof(struct b_dsref *),
|
.key_size = sizeof(b_object *),
|
||||||
.key_flags = B_HASHMAP_KEY_F_INTVALUE,
|
.key_flags = B_HASHMAP_KEY_F_INTVALUE,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -180,7 +180,7 @@ static void ctx_clear_object_flags(
|
|||||||
b_hashmap_put(ctx->ctx_objects_flags, &key, &value);
|
b_hashmap_put(ctx->ctx_objects_flags, &key, &value);
|
||||||
}
|
}
|
||||||
|
|
||||||
static enum object_flags ctx_get_object_flags(struct ctx *ctx, struct b_dsref *obj)
|
static enum object_flags ctx_get_object_flags(struct ctx *ctx, b_object *obj)
|
||||||
{
|
{
|
||||||
if (!obj) {
|
if (!obj) {
|
||||||
return 0;
|
return 0;
|
||||||
@@ -188,7 +188,7 @@ static enum object_flags ctx_get_object_flags(struct ctx *ctx, struct b_dsref *o
|
|||||||
|
|
||||||
b_hashmap_key key = {
|
b_hashmap_key key = {
|
||||||
.key_data = obj,
|
.key_data = obj,
|
||||||
.key_size = sizeof(struct b_dsref *),
|
.key_size = sizeof(b_object *),
|
||||||
.key_flags = B_HASHMAP_KEY_F_INTVALUE,
|
.key_flags = B_HASHMAP_KEY_F_INTVALUE,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -231,7 +231,7 @@ static enum b_status refill_linebuf(struct ctx *ctx)
|
|||||||
return B_SUCCESS;
|
return B_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct b_string *get_wordbuf(struct ctx *ctx)
|
static b_string *get_wordbuf(struct ctx *ctx)
|
||||||
{
|
{
|
||||||
b_string_clear(ctx->ctx_wordbuf);
|
b_string_clear(ctx->ctx_wordbuf);
|
||||||
return ctx->ctx_wordbuf;
|
return ctx->ctx_wordbuf;
|
||||||
@@ -394,7 +394,7 @@ static void discard_token(struct ctx *ctx)
|
|||||||
free(tok);
|
free(tok);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool try_convert_word_to_timestamp(struct ctx *ctx, struct b_string *token_str)
|
static bool try_convert_word_to_timestamp(struct ctx *ctx, b_string *token_str)
|
||||||
{
|
{
|
||||||
b_datetime *dt = b_datetime_parse(
|
b_datetime *dt = b_datetime_parse(
|
||||||
B_DATETIME_FORMAT_RFC3339, b_string_ptr(token_str));
|
B_DATETIME_FORMAT_RFC3339, b_string_ptr(token_str));
|
||||||
@@ -410,7 +410,7 @@ static bool try_convert_word_to_timestamp(struct ctx *ctx, struct b_string *toke
|
|||||||
}
|
}
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
static bool try_convert_word_to_timestamp(struct ctx *ctx, struct b_string *token_str)
|
static bool try_convert_word_to_timestamp(struct ctx *ctx, b_string *token_str)
|
||||||
{
|
{
|
||||||
const char *s = b_string_ptr(token_str);
|
const char *s = b_string_ptr(token_str);
|
||||||
size_t len = b_string_get_size(token_str, B_STRLEN_NORMAL);
|
size_t len = b_string_get_size(token_str, B_STRLEN_NORMAL);
|
||||||
@@ -614,10 +614,10 @@ static bool has_trailing_zero(const char *s)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool try_convert_word_to_number(struct ctx *ctx, struct b_string *token_str)
|
static bool try_convert_word_to_number(struct ctx *ctx, b_string *token_str)
|
||||||
{
|
{
|
||||||
size_t len = b_string_get_size(token_str, B_STRLEN_NORMAL);
|
size_t len = b_string_get_size(token_str, B_STRLEN_NORMAL);
|
||||||
struct b_string *str = b_string_duplicate(token_str);
|
b_string *str = b_string_duplicate(token_str);
|
||||||
struct token *tok = NULL;
|
struct token *tok = NULL;
|
||||||
const char *s = b_string_ptr(str);
|
const char *s = b_string_ptr(str);
|
||||||
|
|
||||||
@@ -817,7 +817,7 @@ static bool try_convert_word_to_number(struct ctx *ctx, struct b_string *token_s
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool try_convert_word_to_bool(struct ctx *ctx, struct b_string *token_str)
|
static bool try_convert_word_to_bool(struct ctx *ctx, b_string *token_str)
|
||||||
{
|
{
|
||||||
const char *s = b_string_ptr(token_str);
|
const char *s = b_string_ptr(token_str);
|
||||||
struct token *tok = NULL;
|
struct token *tok = NULL;
|
||||||
@@ -837,7 +837,7 @@ static bool try_convert_word_to_bool(struct ctx *ctx, struct b_string *token_str
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void split_word(struct ctx *ctx, struct b_string *wordbuf)
|
static void split_word(struct ctx *ctx, b_string *wordbuf)
|
||||||
{
|
{
|
||||||
#if 0
|
#if 0
|
||||||
long len = b_string_get_size(wordbuf, B_STRLEN_NORMAL);
|
long len = b_string_get_size(wordbuf, B_STRLEN_NORMAL);
|
||||||
@@ -924,7 +924,7 @@ static void split_word(struct ctx *ctx, struct b_string *wordbuf)
|
|||||||
static void read_number(struct ctx *ctx)
|
static void read_number(struct ctx *ctx)
|
||||||
{
|
{
|
||||||
b_wchar c = 0;
|
b_wchar c = 0;
|
||||||
struct b_string *wordbuf = get_wordbuf(ctx);
|
b_string *wordbuf = get_wordbuf(ctx);
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
c = peek_char(ctx);
|
c = peek_char(ctx);
|
||||||
@@ -954,7 +954,7 @@ static void read_number(struct ctx *ctx)
|
|||||||
static void read_word(struct ctx *ctx)
|
static void read_word(struct ctx *ctx)
|
||||||
{
|
{
|
||||||
b_wchar c = 0;
|
b_wchar c = 0;
|
||||||
struct b_string *wordbuf = get_wordbuf(ctx);
|
b_string *wordbuf = get_wordbuf(ctx);
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
c = peek_char(ctx);
|
c = peek_char(ctx);
|
||||||
@@ -1084,7 +1084,7 @@ static void read_string(struct ctx *ctx, bool squote)
|
|||||||
|
|
||||||
bool multiline = false;
|
bool multiline = false;
|
||||||
struct token *tok = enqueue_token(ctx, TOK_STRING);
|
struct token *tok = enqueue_token(ctx, TOK_STRING);
|
||||||
struct b_string *str = get_wordbuf(ctx);
|
b_string *str = get_wordbuf(ctx);
|
||||||
|
|
||||||
b_wchar c = peek_char(ctx);
|
b_wchar c = peek_char(ctx);
|
||||||
if (c == term) {
|
if (c == term) {
|
||||||
@@ -1513,17 +1513,17 @@ static void ctx_cleanup(struct ctx *ctx)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (ctx->ctx_linebuf) {
|
if (ctx->ctx_linebuf) {
|
||||||
b_string_release(ctx->ctx_linebuf);
|
b_string_unref(ctx->ctx_linebuf);
|
||||||
ctx->ctx_linebuf = NULL;
|
ctx->ctx_linebuf = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ctx->ctx_wordbuf) {
|
if (ctx->ctx_wordbuf) {
|
||||||
b_string_release(ctx->ctx_wordbuf);
|
b_string_unref(ctx->ctx_wordbuf);
|
||||||
ctx->ctx_wordbuf = NULL;
|
ctx->ctx_wordbuf = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ctx->ctx_objects_flags) {
|
if (ctx->ctx_objects_flags) {
|
||||||
b_hashmap_release(ctx->ctx_objects_flags);
|
b_hashmap_unref(ctx->ctx_objects_flags);
|
||||||
ctx->ctx_objects_flags = NULL;
|
ctx->ctx_objects_flags = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1543,7 +1543,7 @@ static enum b_status ctx_init(struct ctx *ctx)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static enum b_status toml_serialise(
|
static enum b_status toml_serialise(
|
||||||
struct b_serial_ctx *serial, struct b_dsref *src, struct b_stream *dest,
|
struct b_serial_ctx *serial, b_object *src, struct b_stream *dest,
|
||||||
enum b_serial_flags flags)
|
enum b_serial_flags flags)
|
||||||
{
|
{
|
||||||
return B_SUCCESS;
|
return B_SUCCESS;
|
||||||
@@ -1636,35 +1636,35 @@ static void print_token(struct token *tok)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static enum b_status parse_value(struct ctx *ctx, struct b_dsref **result);
|
static enum b_status parse_value(struct ctx *ctx, b_object **result);
|
||||||
static enum b_status parse_key_value_pair(struct ctx *ctx, struct b_dict *container);
|
static enum b_status parse_key_value_pair(struct ctx *ctx, b_dict *container);
|
||||||
|
|
||||||
static enum b_status parse_timestamp(struct ctx *ctx, struct b_dsref **result)
|
static enum b_status parse_timestamp(struct ctx *ctx, b_object **result)
|
||||||
{
|
{
|
||||||
struct token *tok = peek_token(ctx);
|
struct token *tok = peek_token(ctx);
|
||||||
struct b_datetime *dt = tok->tok_value.time;
|
b_datetime *dt = tok->tok_value.time;
|
||||||
tok->tok_value.time = NULL;
|
tok->tok_value.time = NULL;
|
||||||
|
|
||||||
*result = B_DSREF(dt);
|
*result = (dt);
|
||||||
return B_SUCCESS;
|
return B_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static enum b_status parse_string(struct ctx *ctx, struct b_dsref **result)
|
static enum b_status parse_string(struct ctx *ctx, b_object **result)
|
||||||
{
|
{
|
||||||
struct token *tok = peek_token(ctx);
|
struct token *tok = peek_token(ctx);
|
||||||
struct b_string *str = b_string_duplicate(tok->tok_str);
|
b_string *str = b_string_duplicate(tok->tok_str);
|
||||||
if (!str) {
|
if (!str) {
|
||||||
return B_ERR_NO_MEMORY;
|
return B_ERR_NO_MEMORY;
|
||||||
}
|
}
|
||||||
|
|
||||||
*result = B_DSREF(str);
|
*result = (str);
|
||||||
return B_SUCCESS;
|
return B_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static enum b_status parse_int(struct ctx *ctx, struct b_dsref **result)
|
static enum b_status parse_int(struct ctx *ctx, b_object **result)
|
||||||
{
|
{
|
||||||
struct token *tok = peek_token(ctx);
|
struct token *tok = peek_token(ctx);
|
||||||
struct b_number *val = B_LONGLONG(tok->tok_value.i.v);
|
b_number *val = B_LONGLONG(tok->tok_value.i.v);
|
||||||
if (!val) {
|
if (!val) {
|
||||||
return B_ERR_NO_MEMORY;
|
return B_ERR_NO_MEMORY;
|
||||||
}
|
}
|
||||||
@@ -1683,14 +1683,14 @@ static enum b_status parse_int(struct ctx *ctx, struct b_dsref **result)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*result = B_DSREF(val);
|
*result = (val);
|
||||||
return B_SUCCESS;
|
return B_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static enum b_status parse_float(struct ctx *ctx, struct b_dsref **result)
|
static enum b_status parse_float(struct ctx *ctx, b_object **result)
|
||||||
{
|
{
|
||||||
struct token *tok = peek_token(ctx);
|
struct token *tok = peek_token(ctx);
|
||||||
struct b_number *val = B_DOUBLE(tok->tok_value.f.v);
|
b_number *val = B_DOUBLE(tok->tok_value.f.v);
|
||||||
if (!val) {
|
if (!val) {
|
||||||
return B_ERR_NO_MEMORY;
|
return B_ERR_NO_MEMORY;
|
||||||
}
|
}
|
||||||
@@ -1709,53 +1709,53 @@ static enum b_status parse_float(struct ctx *ctx, struct b_dsref **result)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*result = B_DSREF(val);
|
*result = (val);
|
||||||
return B_SUCCESS;
|
return B_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static enum b_status parse_bool(struct ctx *ctx, struct b_dsref **result)
|
static enum b_status parse_bool(struct ctx *ctx, b_object **result)
|
||||||
{
|
{
|
||||||
struct token *tok = peek_token(ctx);
|
struct token *tok = peek_token(ctx);
|
||||||
struct b_number *val = B_INT8(tok->tok_value.b);
|
b_number *val = B_INT8(tok->tok_value.b);
|
||||||
if (!val) {
|
if (!val) {
|
||||||
return B_ERR_NO_MEMORY;
|
return B_ERR_NO_MEMORY;
|
||||||
}
|
}
|
||||||
|
|
||||||
*result = B_DSREF(val);
|
*result = (val);
|
||||||
return B_SUCCESS;
|
return B_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static enum b_status parse_table_inline(struct ctx *ctx, struct b_dsref **result)
|
static enum b_status parse_table_inline(struct ctx *ctx, b_object **result)
|
||||||
{
|
{
|
||||||
DISABLE_EXTENDED_LEXING(ctx);
|
DISABLE_EXTENDED_LEXING(ctx);
|
||||||
|
|
||||||
advance_token(ctx);
|
advance_token(ctx);
|
||||||
|
|
||||||
struct b_dict *table = b_dict_create();
|
b_dict *table = b_dict_create();
|
||||||
if (!table) {
|
if (!table) {
|
||||||
return B_ERR_NO_MEMORY;
|
return B_ERR_NO_MEMORY;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct token *tok = peek_token(ctx);
|
struct token *tok = peek_token(ctx);
|
||||||
if (tok && tok->tok_type == TOK_RIGHT_BRACE) {
|
if (tok && tok->tok_type == TOK_RIGHT_BRACE) {
|
||||||
*result = B_DSREF(table);
|
*result = (table);
|
||||||
return B_SUCCESS;
|
return B_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool done = false;
|
bool done = false;
|
||||||
while (!done) {
|
while (!done) {
|
||||||
|
|
||||||
struct b_dsref *value;
|
b_object *value;
|
||||||
enum b_status status = parse_key_value_pair(ctx, table);
|
enum b_status status = parse_key_value_pair(ctx, table);
|
||||||
if (!B_OK(status)) {
|
if (!B_OK(status)) {
|
||||||
b_dict_release(table);
|
b_dict_unref(table);
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
tok = peek_token(ctx);
|
tok = peek_token(ctx);
|
||||||
|
|
||||||
if (!tok) {
|
if (!tok) {
|
||||||
b_dict_release(table);
|
b_dict_unref(table);
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1767,12 +1767,12 @@ static enum b_status parse_table_inline(struct ctx *ctx, struct b_dsref **result
|
|||||||
advance_token(ctx);
|
advance_token(ctx);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
b_dict_release(table);
|
b_dict_unref(table);
|
||||||
return B_ERR_BAD_FORMAT;
|
return B_ERR_BAD_FORMAT;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*result = B_DSREF(table);
|
*result = (table);
|
||||||
return B_SUCCESS;
|
return B_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1786,21 +1786,21 @@ static void skip_newlines(struct ctx *ctx)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static enum b_status parse_array_inline(struct ctx *ctx, struct b_dsref **result)
|
static enum b_status parse_array_inline(struct ctx *ctx, b_object **result)
|
||||||
{
|
{
|
||||||
bool done = false;
|
bool done = false;
|
||||||
ENABLE_EXTENDED_LEXING(ctx);
|
ENABLE_EXTENDED_LEXING(ctx);
|
||||||
|
|
||||||
advance_token(ctx);
|
advance_token(ctx);
|
||||||
|
|
||||||
struct b_array *array = b_array_create();
|
b_array *array = b_array_create();
|
||||||
if (!array) {
|
if (!array) {
|
||||||
return B_ERR_NO_MEMORY;
|
return B_ERR_NO_MEMORY;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct token *tok = peek_token(ctx);
|
struct token *tok = peek_token(ctx);
|
||||||
if (!tok) {
|
if (!tok) {
|
||||||
b_array_release(array);
|
b_array_unref(array);
|
||||||
return B_ERR_BAD_FORMAT;
|
return B_ERR_BAD_FORMAT;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1814,7 +1814,7 @@ static enum b_status parse_array_inline(struct ctx *ctx, struct b_dsref **result
|
|||||||
tok = peek_token(ctx);
|
tok = peek_token(ctx);
|
||||||
|
|
||||||
if (!tok) {
|
if (!tok) {
|
||||||
b_array_release(array);
|
b_array_unref(array);
|
||||||
return B_ERR_BAD_FORMAT;
|
return B_ERR_BAD_FORMAT;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1823,10 +1823,10 @@ static enum b_status parse_array_inline(struct ctx *ctx, struct b_dsref **result
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct b_dsref *value;
|
b_object *value;
|
||||||
enum b_status status = parse_value(ctx, &value);
|
enum b_status status = parse_value(ctx, &value);
|
||||||
if (!B_OK(status)) {
|
if (!B_OK(status)) {
|
||||||
b_array_release(array);
|
b_array_unref(array);
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1844,7 +1844,7 @@ static enum b_status parse_array_inline(struct ctx *ctx, struct b_dsref **result
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!tok || tok->tok_type != TOK_COMMA) {
|
if (!tok || tok->tok_type != TOK_COMMA) {
|
||||||
b_array_release(array);
|
b_array_unref(array);
|
||||||
return B_ERR_BAD_FORMAT;
|
return B_ERR_BAD_FORMAT;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1853,11 +1853,11 @@ static enum b_status parse_array_inline(struct ctx *ctx, struct b_dsref **result
|
|||||||
}
|
}
|
||||||
|
|
||||||
DISABLE_EXTENDED_LEXING(ctx);
|
DISABLE_EXTENDED_LEXING(ctx);
|
||||||
*result = B_DSREF(array);
|
*result = (array);
|
||||||
return B_SUCCESS;
|
return B_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static enum b_status parse_value(struct ctx *ctx, struct b_dsref **result)
|
static enum b_status parse_value(struct ctx *ctx, b_object **result)
|
||||||
{
|
{
|
||||||
|
|
||||||
struct token *tok = peek_token(ctx);
|
struct token *tok = peek_token(ctx);
|
||||||
@@ -1885,7 +1885,7 @@ static enum b_status parse_value(struct ctx *ctx, struct b_dsref **result)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static enum b_status parse_key_value_pair(struct ctx *ctx, struct b_dict *container)
|
static enum b_status parse_key_value_pair(struct ctx *ctx, b_dict *container)
|
||||||
{
|
{
|
||||||
struct token *tok = peek_token(ctx);
|
struct token *tok = peek_token(ctx);
|
||||||
if (!IS_VALID_KEY_COMPONENT(tok)) {
|
if (!IS_VALID_KEY_COMPONENT(tok)) {
|
||||||
@@ -1904,11 +1904,11 @@ static enum b_status parse_key_value_pair(struct ctx *ctx, struct b_dict *contai
|
|||||||
}
|
}
|
||||||
|
|
||||||
while (tok && tok->tok_type == TOK_DOT) {
|
while (tok && tok->tok_type == TOK_DOT) {
|
||||||
struct b_dsref *sub_dict = b_dict_at_sk(container, key);
|
b_object *sub_dict = b_dict_at_sk(container, key);
|
||||||
if (!sub_dict) {
|
if (!sub_dict) {
|
||||||
sub_dict = B_DSREF(b_dict_create());
|
sub_dict = (b_dict_create());
|
||||||
b_dict_put_sk(container, key, B_RV(sub_dict));
|
b_dict_put_sk(container, key, B_RV(sub_dict));
|
||||||
} else if (sub_dict && !B_DSREF_IS(sub_dict, DICT)) {
|
} else if (sub_dict && !b_object_is_type(sub_dict, B_TYPE_DICT)) {
|
||||||
free(key);
|
free(key);
|
||||||
return B_ERR_BAD_FORMAT;
|
return B_ERR_BAD_FORMAT;
|
||||||
}
|
}
|
||||||
@@ -1930,8 +1930,8 @@ static enum b_status parse_key_value_pair(struct ctx *ctx, struct b_dict *contai
|
|||||||
return B_ERR_BAD_FORMAT;
|
return B_ERR_BAD_FORMAT;
|
||||||
}
|
}
|
||||||
|
|
||||||
container = B_DICT(sub_dict);
|
container = sub_dict;
|
||||||
b_string_release(key);
|
b_string_unref(key);
|
||||||
key = b_string_duplicate(tok->tok_str);
|
key = b_string_duplicate(tok->tok_str);
|
||||||
if (!key) {
|
if (!key) {
|
||||||
return B_ERR_NO_MEMORY;
|
return B_ERR_NO_MEMORY;
|
||||||
@@ -1956,7 +1956,7 @@ static enum b_status parse_key_value_pair(struct ctx *ctx, struct b_dict *contai
|
|||||||
ENABLE_EXTENDED_LEXING(ctx);
|
ENABLE_EXTENDED_LEXING(ctx);
|
||||||
advance_token(ctx);
|
advance_token(ctx);
|
||||||
|
|
||||||
struct b_dsref *value = NULL;
|
b_object *value = NULL;
|
||||||
enum b_status status = parse_value(ctx, &value);
|
enum b_status status = parse_value(ctx, &value);
|
||||||
|
|
||||||
DISABLE_EXTENDED_LEXING(ctx);
|
DISABLE_EXTENDED_LEXING(ctx);
|
||||||
@@ -1972,7 +1972,8 @@ static enum b_status parse_key_value_pair(struct ctx *ctx, struct b_dict *contai
|
|||||||
|
|
||||||
b_dict_put_sk(container, key, B_RV(value));
|
b_dict_put_sk(container, key, B_RV(value));
|
||||||
|
|
||||||
if (B_DSREF_IS(value, DICT) || B_DSREF_IS(value, ARRAY)) {
|
if (b_object_is_type(value, B_TYPE_DICT)
|
||||||
|
|| b_object_is_type(value, B_TYPE_ARRAY)) {
|
||||||
ctx_set_object_flags(ctx, value, OBJECT_KV_END_DEFINED);
|
ctx_set_object_flags(ctx, value, OBJECT_KV_END_DEFINED);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1980,7 +1981,7 @@ static enum b_status parse_key_value_pair(struct ctx *ctx, struct b_dict *contai
|
|||||||
}
|
}
|
||||||
|
|
||||||
static enum b_status parse_table_header(
|
static enum b_status parse_table_header(
|
||||||
struct ctx *ctx, struct b_dict *container, struct b_dict **new_container)
|
struct ctx *ctx, b_dict *container, b_dict **new_container)
|
||||||
{
|
{
|
||||||
advance_token(ctx);
|
advance_token(ctx);
|
||||||
struct token *tok = peek_token(ctx);
|
struct token *tok = peek_token(ctx);
|
||||||
@@ -2000,17 +2001,15 @@ static enum b_status parse_table_header(
|
|||||||
}
|
}
|
||||||
|
|
||||||
while (tok && tok->tok_type == TOK_DOT) {
|
while (tok && tok->tok_type == TOK_DOT) {
|
||||||
struct b_dsref *sub_dict = b_dict_at_sk(container, key);
|
b_object *sub_dict = b_dict_at_sk(container, key);
|
||||||
enum object_flags flags = ctx_get_object_flags(ctx, sub_dict);
|
enum object_flags flags = ctx_get_object_flags(ctx, sub_dict);
|
||||||
if (!sub_dict) {
|
if (!sub_dict) {
|
||||||
sub_dict = B_DSREF(b_dict_create());
|
sub_dict = (b_dict_create());
|
||||||
b_dict_put_sk(container, key, B_RV(sub_dict));
|
b_dict_put_sk(container, key, B_RV(sub_dict));
|
||||||
} else if (B_DSREF_IS(sub_dict, ARRAY)) {
|
} else if (b_object_is_type(sub_dict, B_TYPE_ARRAY)) {
|
||||||
|
|
||||||
sub_dict = b_array_at(
|
sub_dict = b_array_at(sub_dict, b_array_size(sub_dict) - 1);
|
||||||
B_ARRAY(sub_dict),
|
} else if (!b_object_is_type(sub_dict, B_TYPE_DICT)) {
|
||||||
b_array_size(B_ARRAY(sub_dict)) - 1);
|
|
||||||
} else if (!B_DSREF_IS(sub_dict, DICT)) {
|
|
||||||
return B_ERR_BAD_FORMAT;
|
return B_ERR_BAD_FORMAT;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2026,8 +2025,8 @@ static enum b_status parse_table_header(
|
|||||||
|
|
||||||
ctx_set_object_flags(ctx, sub_dict, OBJECT_HEADER_MID_DEFINED);
|
ctx_set_object_flags(ctx, sub_dict, OBJECT_HEADER_MID_DEFINED);
|
||||||
|
|
||||||
container = B_DICT(sub_dict);
|
container = sub_dict;
|
||||||
b_string_release(key);
|
b_string_unref(key);
|
||||||
key = b_string_duplicate(tok->tok_str);
|
key = b_string_duplicate(tok->tok_str);
|
||||||
if (!key) {
|
if (!key) {
|
||||||
return B_ERR_NO_MEMORY;
|
return B_ERR_NO_MEMORY;
|
||||||
@@ -2041,7 +2040,7 @@ static enum b_status parse_table_header(
|
|||||||
return B_ERR_BAD_FORMAT;
|
return B_ERR_BAD_FORMAT;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct b_dict *new_table = B_DICT(b_dict_at_sk(container, key));
|
b_dict *new_table = b_dict_at_sk(container, key);
|
||||||
|
|
||||||
if (!new_table) {
|
if (!new_table) {
|
||||||
new_table = b_dict_create();
|
new_table = b_dict_create();
|
||||||
@@ -2054,19 +2053,19 @@ static enum b_status parse_table_header(
|
|||||||
b_dict_put_sk(container, key, B_RV(new_table));
|
b_dict_put_sk(container, key, B_RV(new_table));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!B_DSREF_IS(B_DSREF(new_table), DICT)) {
|
if (!b_object_is_type((new_table), B_TYPE_DICT)) {
|
||||||
return B_ERR_BAD_FORMAT;
|
return B_ERR_BAD_FORMAT;
|
||||||
}
|
}
|
||||||
|
|
||||||
enum object_flags flags = ctx_get_object_flags(ctx, B_DSREF(new_table));
|
enum object_flags flags = ctx_get_object_flags(ctx, (new_table));
|
||||||
if (flags
|
if (flags
|
||||||
& (OBJECT_HEADER_END_DEFINED | OBJECT_KV_MID_DEFINED
|
& (OBJECT_HEADER_END_DEFINED | OBJECT_KV_MID_DEFINED
|
||||||
| OBJECT_KV_END_DEFINED)) {
|
| OBJECT_KV_END_DEFINED)) {
|
||||||
return B_ERR_BAD_FORMAT;
|
return B_ERR_BAD_FORMAT;
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx_set_object_flags(ctx, B_DSREF(new_table), OBJECT_HEADER_END_DEFINED);
|
ctx_set_object_flags(ctx, (new_table), OBJECT_HEADER_END_DEFINED);
|
||||||
b_string_release(key);
|
b_string_unref(key);
|
||||||
|
|
||||||
advance_token(ctx);
|
advance_token(ctx);
|
||||||
*new_container = new_table;
|
*new_container = new_table;
|
||||||
@@ -2074,7 +2073,7 @@ static enum b_status parse_table_header(
|
|||||||
}
|
}
|
||||||
|
|
||||||
static enum b_status parse_array_header(
|
static enum b_status parse_array_header(
|
||||||
struct ctx *ctx, struct b_dict *container, struct b_dict **new_container)
|
struct ctx *ctx, b_dict *container, b_dict **new_container)
|
||||||
{
|
{
|
||||||
advance_token(ctx);
|
advance_token(ctx);
|
||||||
struct token *tok = peek_token(ctx);
|
struct token *tok = peek_token(ctx);
|
||||||
@@ -2094,15 +2093,13 @@ static enum b_status parse_array_header(
|
|||||||
}
|
}
|
||||||
|
|
||||||
while (tok && tok->tok_type == TOK_DOT) {
|
while (tok && tok->tok_type == TOK_DOT) {
|
||||||
struct b_dsref *sub_dict = b_dict_at_sk(container, key);
|
b_object *sub_dict = b_dict_at_sk(container, key);
|
||||||
if (!sub_dict) {
|
if (!sub_dict) {
|
||||||
sub_dict = B_DSREF(b_dict_create());
|
sub_dict = (b_dict_create());
|
||||||
b_dict_put_sk(container, key, B_RV(sub_dict));
|
b_dict_put_sk(container, key, B_RV(sub_dict));
|
||||||
} else if (B_DSREF_IS(sub_dict, ARRAY)) {
|
} else if (b_object_is_type(sub_dict, B_TYPE_ARRAY)) {
|
||||||
sub_dict = b_array_at(
|
sub_dict = b_array_at(sub_dict, b_array_size(sub_dict) - 1);
|
||||||
B_ARRAY(sub_dict),
|
} else if (!b_object_is_type(sub_dict, B_TYPE_DICT)) {
|
||||||
b_array_size(B_ARRAY(sub_dict)) - 1);
|
|
||||||
} else if (!B_DSREF_IS(sub_dict, DICT)) {
|
|
||||||
return B_ERR_BAD_FORMAT;
|
return B_ERR_BAD_FORMAT;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2112,8 +2109,8 @@ static enum b_status parse_array_header(
|
|||||||
return B_ERR_BAD_FORMAT;
|
return B_ERR_BAD_FORMAT;
|
||||||
}
|
}
|
||||||
|
|
||||||
container = B_DICT(sub_dict);
|
container = sub_dict;
|
||||||
b_string_release(key);
|
b_string_unref(key);
|
||||||
key = b_string_duplicate(tok->tok_str);
|
key = b_string_duplicate(tok->tok_str);
|
||||||
if (!key) {
|
if (!key) {
|
||||||
return B_ERR_NO_MEMORY;
|
return B_ERR_NO_MEMORY;
|
||||||
@@ -2127,21 +2124,21 @@ static enum b_status parse_array_header(
|
|||||||
return B_ERR_BAD_FORMAT;
|
return B_ERR_BAD_FORMAT;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct b_array *array = B_ARRAY(b_dict_get_sk(container, key));
|
b_array *array = b_dict_get_sk(container, key);
|
||||||
if (!array) {
|
if (!array) {
|
||||||
array = b_array_create();
|
array = b_array_create();
|
||||||
b_dict_put_sk(container, key, B_RV(array));
|
b_dict_put_sk(container, key, B_RV(array));
|
||||||
} else if (!B_DSREF_IS(array, ARRAY)) {
|
} else if (!b_object_is_type(array, B_TYPE_ARRAY)) {
|
||||||
return B_ERR_BAD_FORMAT;
|
return B_ERR_BAD_FORMAT;
|
||||||
}
|
}
|
||||||
free(key);
|
free(key);
|
||||||
|
|
||||||
enum object_flags flags = ctx_get_object_flags(ctx, B_DSREF(array));
|
enum object_flags flags = ctx_get_object_flags(ctx, (array));
|
||||||
if (flags & OBJECT_KV_END_DEFINED) {
|
if (flags & OBJECT_KV_END_DEFINED) {
|
||||||
return B_ERR_NO_MEMORY;
|
return B_ERR_NO_MEMORY;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct b_dict *new_table = b_dict_create();
|
b_dict *new_table = b_dict_create();
|
||||||
if (!new_table) {
|
if (!new_table) {
|
||||||
return B_ERR_NO_MEMORY;
|
return B_ERR_NO_MEMORY;
|
||||||
}
|
}
|
||||||
@@ -2153,11 +2150,11 @@ static enum b_status parse_array_header(
|
|||||||
return B_SUCCESS;
|
return B_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static enum b_status parse_root(struct ctx *ctx, struct b_dict **result)
|
static enum b_status parse_root(struct ctx *ctx, b_dict **result)
|
||||||
{
|
{
|
||||||
enum b_status status = B_SUCCESS;
|
enum b_status status = B_SUCCESS;
|
||||||
struct b_dict *root = b_dict_create();
|
b_dict *root = b_dict_create();
|
||||||
struct b_dict *current = root;
|
b_dict *current = root;
|
||||||
|
|
||||||
while (!(ctx->ctx_flags & CTX_EOF) && B_OK(status)) {
|
while (!(ctx->ctx_flags & CTX_EOF) && B_OK(status)) {
|
||||||
struct token *tok = peek_token(ctx);
|
struct token *tok = peek_token(ctx);
|
||||||
@@ -2215,7 +2212,7 @@ static enum b_status parse_root(struct ctx *ctx, struct b_dict **result)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!B_OK(status)) {
|
if (!B_OK(status)) {
|
||||||
b_dict_release(root);
|
b_dict_unref(root);
|
||||||
root = NULL;
|
root = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2224,8 +2221,8 @@ static enum b_status parse_root(struct ctx *ctx, struct b_dict **result)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static enum b_status toml_deserialise(
|
static enum b_status toml_deserialise(
|
||||||
struct b_serial_ctx *serial, struct b_stream *src,
|
struct b_serial_ctx *serial, struct b_stream *src, b_object **dest,
|
||||||
struct b_dsref **dest, enum b_serial_flags flags)
|
enum b_serial_flags flags)
|
||||||
{
|
{
|
||||||
struct ctx ctx = {0};
|
struct ctx ctx = {0};
|
||||||
enum b_status status = ctx_init(&ctx);
|
enum b_status status = ctx_init(&ctx);
|
||||||
@@ -2244,17 +2241,17 @@ static enum b_status toml_deserialise(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (ctx.ctx_flags & CTX_EOF) {
|
if (ctx.ctx_flags & CTX_EOF) {
|
||||||
*dest = B_DSREF(b_dict_create());
|
*dest = (b_dict_create());
|
||||||
return B_SUCCESS;
|
return B_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct b_dict *result = NULL;
|
b_dict *result = NULL;
|
||||||
status = parse_root(&ctx, &result);
|
status = parse_root(&ctx, &result);
|
||||||
if (!B_OK(status)) {
|
if (!B_OK(status)) {
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
*dest = B_DSREF(result);
|
*dest = (result);
|
||||||
#if 0
|
#if 0
|
||||||
ctx.ctx_flags
|
ctx.ctx_flags
|
||||||
= CTX_ENABLE_NUMBERS | CTX_ENABLE_TIMESTAMPS | CTX_ENABLE_BOOLS;
|
= CTX_ENABLE_NUMBERS | CTX_ENABLE_TIMESTAMPS | CTX_ENABLE_BOOLS;
|
||||||
|
|||||||
Reference in New Issue
Block a user