Files
fx/fx.serial/test/toml-decode.c
T

240 lines
5.5 KiB
C

#include <fx/bool.h>
#include <fx/collections/array.h>
#include <fx/collections/datetime.h>
#include <fx/collections/dict.h>
#include <fx/double.h>
#include <fx/int.h>
#include <fx/serial/ctx.h>
#include <fx/serial/toml.h>
#include <fx/string.h>
#include <inttypes.h>
void write_tagged_value(fx_object *data);
void write_raw_string(const fx_string *data)
{
fx_stream_write_cstr(fx_stdout, "\"", NULL);
const fx_iterator *it = fx_iterator_cbegin(data);
fx_foreach_c(fx_wchar, c, it)
{
if (c >= 0x10000) {
c -= 0x10000;
long hi = 0xD800 | ((c >> 10) & 0x3FF);
long lo = 0xDC00 | (c & 0x3FF);
fx_stream_write_fmt(
fx_stdout,
NULL,
"\\u%04x\\u%04x",
hi,
lo);
} else if (c <= 0x1F || c >= 0x7F) {
fx_stream_write_fmt(fx_stdout, NULL, "\\u%04x", c);
} else if (c == '\\' || c == '"') {
fx_stream_write_fmt(fx_stdout, NULL, "\\%c", c);
} else {
fx_stream_write_char(fx_stdout, c);
}
}
fx_iterator_unref(it);
fx_stream_write_cstr(fx_stdout, "\"", NULL);
}
void write_tagged_string(fx_string *data)
{
fx_stream_write_cstr(
fx_stdout,
"{ \"type\": \"string\", \"value\": ",
NULL);
write_raw_string(data);
fx_stream_write_cstr(fx_stdout, " }", NULL);
}
void write_tagged_integer(fx_int *data)
{
fx_stream_write_cstr(
fx_stdout,
"{ \"type\": \"integer\", \"value\": \"",
NULL);
if (fx_int_is_inf_positive(data)) {
fx_stream_write_cstr(fx_stdout, "inf", NULL);
} else if (fx_int_is_inf_negative(data)) {
fx_stream_write_cstr(fx_stdout, "-inf", NULL);
} else if (fx_int_is_nan_positive(data)) {
fx_stream_write_cstr(fx_stdout, "nan", NULL);
} else if (fx_int_is_nan_negative(data)) {
fx_stream_write_cstr(fx_stdout, "-nan", NULL);
} else {
fx_stream_write_fmt(
fx_stdout,
NULL,
"%lld",
fx_int_get_value(data),
NULL);
}
fx_stream_write_cstr(fx_stdout, "\" }", NULL);
}
void write_tagged_float(fx_double *data)
{
fx_stream_write_cstr(
fx_stdout,
"{ \"type\": \"float\", \"value\": \"",
NULL);
if (fx_double_is_inf_positive(data)) {
fx_stream_write_cstr(fx_stdout, "inf", NULL);
} else if (fx_double_is_inf_negative(data)) {
fx_stream_write_cstr(fx_stdout, "-inf", NULL);
} else if (fx_double_is_nan_positive(data)) {
fx_stream_write_cstr(fx_stdout, "nan", NULL);
} else if (fx_double_is_nan_negative(data)) {
fx_stream_write_cstr(fx_stdout, "-nan", NULL);
} else {
double v = fx_double_get_value(data);
if ((v <= 0.00000001 && v > 0) || (v >= -0.00000001 && v < 0)
|| (v >= 1000000000) || (v <= -1000000000)) {
fx_stream_write_fmt(fx_stdout, NULL, "%.15e", v, NULL);
} else {
fx_stream_write_fmt(fx_stdout, NULL, "%.15f", v, NULL);
}
}
fx_stream_write_cstr(fx_stdout, "\" }", NULL);
}
void write_tagged_bool(fx_bool *data)
{
fx_stream_write_fmt(
fx_stdout,
NULL,
"{ \"type\": \"bool\", \"value\": \"%s\" }",
fx_bool_get_value(data) ? "true" : "false",
NULL);
}
void write_tagged_datetime(fx_datetime *data)
{
bool has_date = fx_datetime_has_date(data);
bool has_time = fx_datetime_has_time(data);
bool localtime = fx_datetime_is_localtime(data);
fx_stream_write_cstr(fx_stdout, "{ \"type\": \"", NULL);
if (has_date && has_time) {
fx_stream_write_cstr(
fx_stdout,
localtime ? "datetime-local" : "datetime",
NULL);
} else if (has_date) {
fx_stream_write_cstr(
fx_stdout,
localtime ? "date-local" : "date",
NULL);
} else if (has_time) {
fx_stream_write_cstr(
fx_stdout,
localtime ? "time-local" : "time",
NULL);
}
fx_stream_write_cstr(fx_stdout, "\", \"value\": \"", NULL);
fx_string *new_data = fx_string_create();
fx_datetime_to_string(data, FX_DATETIME_FORMAT_RFC3339, new_data);
fx_stream_write_cstr(fx_stdout, fx_string_get_cstr(new_data), NULL);
fx_stream_write_cstr(fx_stdout, "\" }", NULL);
fx_string_unref(new_data);
}
void write_tagged_dict(fx_dict *data)
{
fx_stream_write_cstr(fx_stdout, "{ ", NULL);
int i = 0;
fx_iterator *it = fx_iterator_begin(data);
fx_foreach(fx_dict_item *, item, it)
{
if (i++ > 0) {
fx_stream_write_cstr(fx_stdout, ", ", NULL);
}
write_raw_string(item->key);
fx_stream_write_cstr(fx_stdout, ": ", NULL);
write_tagged_value(item->value);
}
fx_iterator_unref(it);
fx_stream_write_cstr(fx_stdout, " }", NULL);
}
void write_tagged_array(fx_array *data)
{
fx_stream_write_cstr(fx_stdout, "[ ", NULL);
int i = 0;
fx_iterator *it = fx_iterator_begin(data);
fx_foreach(fx_object *, obj, it)
{
if (i++ > 0) {
fx_stream_write_cstr(fx_stdout, ", ", NULL);
}
write_tagged_value(obj);
}
fx_iterator_unref(it);
fx_stream_write_cstr(fx_stdout, " ]", NULL);
}
void write_tagged_value(fx_object *data)
{
if (fx_object_is_type(data, FX_TYPE_DICT)) {
write_tagged_dict(data);
} else if (fx_object_is_type(data, FX_TYPE_ARRAY)) {
write_tagged_array(data);
} else if (fx_object_is_type(data, FX_TYPE_STRING)) {
write_tagged_string(data);
} else if (fx_object_is_type(data, FX_TYPE_DATETIME)) {
write_tagged_datetime(data);
} else if (fx_object_is_type(data, FX_TYPE_INT)) {
write_tagged_integer(data);
} else if (fx_object_is_type(data, FX_TYPE_BOOL)) {
write_tagged_bool(data);
} else if (fx_object_is_type(data, FX_TYPE_DOUBLE)) {
write_tagged_float(data);
}
}
int main(void)
{
fx_stream *src = fx_stdin;
fx_stream *dest = fx_stdout;
fx_serial_ctx *ctx = fx_toml_serial_ctx_create();
fx_object *data;
fx_result result = fx_serial_ctx_deserialise(ctx, src, &data, 0);
if (fx_result_is_error(result)) {
fx_throw(result);
return -1;
}
write_tagged_value(data);
fx_stream_write_char(fx_stdout, '\n');
fx_serial_ctx_unref(ctx);
fx_object_unref(data);
return 0;
}