fx.serial: toml: update array and hashtable usage

This commit is contained in:
2026-05-29 20:04:15 +01:00
parent 1516eee8d1
commit 65f490b900
5 changed files with 197 additions and 265 deletions
+53 -75
View File
@@ -1,24 +1,26 @@
#include <fx/bool.h>
#include <fx/collections/array.h>
#include <fx/collections/datetime.h>
#include <fx/collections/hashtable.h>
#include <fx/datetime.h>
#include <fx/float.h>
#include <fx/int.h>
#include <fx/serial/ctx.h>
#include <fx/serial/toml.h>
#include <fx/string.h>
#include <inttypes.h>
#include <math.h>
void write_tagged_value(fx_object *data);
void write_tagged_value(const fx_value *data);
void write_raw_string(const fx_string *data)
{
fx_stream_write_cstr(fx_stdout, "\"", NULL);
const fx_iterator *it = fx_iterator_cbegin(data);
const fx_iterator *it = fx_iterator_begin(data);
fx_foreach(val, it)
{
fx_wchar c;
fx_value_get_wchar(&val, &c);
fx_value_get_wchar(val, &c);
if (c >= 0x10000) {
c -= 0x10000;
long hi = 0xD800 | ((c >> 10) & 0x3FF);
@@ -54,75 +56,49 @@ void write_tagged_string(fx_string *data)
fx_stream_write_cstr(fx_stdout, " }", NULL);
}
void write_tagged_integer(fx_int *data)
void write_tagged_integer(const fx_value *data)
{
#if 0
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_fmt(fx_stdout, NULL, "%lld", data->v_i64, NULL);
fx_stream_write_cstr(fx_stdout, "\" }", NULL);
#endif
}
void write_tagged_float(fx_double *data)
void write_tagged_float(const fx_value *data)
{
#if 0
fx_stream_write_cstr(
fx_stdout,
"{ \"type\": \"float\", \"value\": \"",
NULL);
if (fx_double_is_inf_positive(data)) {
double d = data->v_double;
if (d == INFINITY) {
fx_stream_write_cstr(fx_stdout, "inf", NULL);
} else if (fx_double_is_inf_negative(data)) {
} else if (d == -INFINITY) {
fx_stream_write_cstr(fx_stdout, "-inf", NULL);
} else if (fx_double_is_nan_positive(data)) {
} else if (d == NAN) {
fx_stream_write_cstr(fx_stdout, "nan", NULL);
} else if (fx_double_is_nan_negative(data)) {
} else if (d == -NAN) {
fx_stream_write_cstr(fx_stdout, "-nan", NULL);
} else if (d <= 1e-9 || d >= 1e9) {
fx_stream_write_fmt(fx_stdout, NULL, "%g", d, 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_fmt(fx_stdout, NULL, "%.9lf", d, NULL);
}
fx_stream_write_cstr(fx_stdout, "\" }", NULL);
#endif
}
void write_tagged_bool(fx_bool *data)
void write_tagged_bool(const fx_value *data)
{
#if 0
fx_stream_write_fmt(
fx_stdout,
NULL,
"{ \"type\": \"bool\", \"value\": \"%s\" }",
fx_bool_get_value(data) ? "true" : "false",
data->v_bool ? "true" : "false",
NULL);
#endif
}
void write_tagged_datetime(fx_datetime *data)
@@ -152,75 +128,78 @@ void write_tagged_datetime(fx_datetime *data)
fx_stream_write_cstr(fx_stdout, "\", \"value\": \"", NULL);
fx_string *new_data = fx_string_create();
fx_stringstream *new_data = fx_stringstream_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, fx_stringstream_ptr(new_data), NULL);
fx_stream_write_cstr(fx_stdout, "\" }", NULL);
fx_string_unref(new_data);
fx_stringstream_unref(new_data);
}
#if 0
void write_tagged_dict(fx_dict *data)
void write_tagged_hashtable(fx_hashtable *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)
const fx_iterator *it = fx_iterator_begin(data);
fx_foreach(v, it)
{
fx_hashtable_item *item;
fx_value_get_object(v, &item);
if (i++ > 0) {
fx_stream_write_cstr(fx_stdout, ", ", NULL);
}
write_raw_string(item->key);
const fx_value *key = fx_hashtable_item_get_key(item);
const fx_value *value = fx_hashtable_item_get_value(item);
fx_string *key_str;
fx_value_get_object(key, &key_str);
write_raw_string(key_str);
fx_stream_write_cstr(fx_stdout, ": ", NULL);
write_tagged_value(item->value);
write_tagged_value(value);
}
fx_iterator_unref(it);
fx_stream_write_cstr(fx_stdout, " }", NULL);
}
#endif
void write_tagged_array(fx_array *data)
{
fx_stream_write_cstr(fx_stdout, "[ ", NULL);
int i = 0;
fx_iterator *it = fx_iterator_begin(data);
const fx_iterator *it = fx_iterator_begin(data);
fx_foreach(val, it)
{
fx_object *obj;
fx_value_get_object(&val, &obj);
if (i++ > 0) {
fx_stream_write_cstr(fx_stdout, ", ", NULL);
}
write_tagged_value(obj);
write_tagged_value(val);
}
fx_iterator_unref(it);
fx_stream_write_cstr(fx_stdout, " ]", NULL);
}
void write_tagged_value(fx_object *data)
void write_tagged_value(const fx_value *data)
{
#if 0
if (fx_object_is_type(data, FX_TYPE_DICT)) {
write_tagged_dict(data);
} else
#endif
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_BOOL)) {
if (fx_value_is_type(data, FX_TYPE_HASHTABLE)) {
write_tagged_hashtable(data->v_object);
} else if (fx_value_is_type(data, FX_TYPE_ARRAY)) {
write_tagged_array(data->v_object);
} else if (fx_value_is_type(data, FX_TYPE_STRING)) {
write_tagged_string(data->v_object);
} else if (fx_value_is_type(data, FX_TYPE_DATETIME)) {
write_tagged_datetime(data->v_object);
} else if (fx_value_is_type(data, FX_TYPE_BOOL)) {
write_tagged_bool(data);
} else if (fx_value_is_type(data, FX_TYPE_I64)) {
write_tagged_integer(data);
} else if (fx_value_is_type(data, FX_TYPE_DOUBLE)) {
write_tagged_float(data);
}
}
@@ -231,19 +210,18 @@ int main(void)
fx_serial_ctx *ctx = fx_toml_serial_ctx_create();
fx_object *data;
fx_value data;
fx_result result = fx_serial_ctx_deserialise(ctx, src, &data, 0);
if (fx_result_is_error(result)) {
fx_throw(result);
return -1;
return 1;
}
write_tagged_value(data);
write_tagged_value(&data);
fx_stream_write_char(fx_stdout, '\n');
fx_serial_ctx_unref(ctx);
fx_object_unref(data);
fx_value_unset(&data);
return 0;
}