Commit 017f6bdbe932a8cd60fa575d46f0e54909f50caf

jeremy 2024-12-03T12:21:26

wip libkc3 serializeé

diff --git a/libkc3/buf.c b/libkc3/buf.c
index fdb3b3f..5c3abd5 100644
--- a/libkc3/buf.c
+++ b/libkc3/buf.c
@@ -1382,6 +1382,7 @@ sw buf_write_u8 (s_buf *buf, u8 x)
   return size;
 }
 
+// XXX endianness
 sw buf_write_u16 (s_buf *buf, u16 x)
 {
   const sw size = 2;
diff --git a/libkc3/f128.c b/libkc3/f128.c
index 1bc4ad0..fa98d96 100644
--- a/libkc3/f128.c
+++ b/libkc3/f128.c
@@ -104,8 +104,6 @@ f128 * f128_init_copy (f128 *x, const f128 *src)
 
 typedef union _128 {
   f128 f128;
-  s128 s128;
-  u128 u128;
   u64 u64[2];
   s64 s64[2];
 } u_128;
@@ -117,7 +115,8 @@ f128 * f128_random (f128 *x)
   arc4random_buf(&u, 15);
   mask.u64[0] = 0x7FFFFFFFFFFFFFFF;
   mask.u64[1] = 0xFFFFFFFFFFFFC000;
-  u.u128 &= mask.u128;
+  u.u64[0] &= mask.u64[0];
+  u.u64[1] &= mask.u64[1];
   *x = u.f128;
   return x;
 }
diff --git a/libkc3/serialize.c b/libkc3/serialize.c
new file mode 100644
index 0000000..c8fcb2d
--- /dev/null
+++ b/libkc3/serialize.c
@@ -0,0 +1,73 @@
+/* kc3
+ * Copyright 2022,2023,2024 kmx.io <contact@kmx.io>
+ *
+ * Permission is hereby granted to use this software granted the above
+ * copyright notice and this permission paragraph are included in all
+ * copies and substantial portions of this software.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
+ * PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
+ * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
+ * THIS SOFTWARE.
+ */
+#include "assert.h"
+#include "serialize.h"
+
+void serialize_clean (s_serialize *serialize)
+{
+  assert(serialize);
+  (void) serialize;
+}
+
+s_serialize * serialize_init (s_serialize *serialize)
+{
+  s_serialize tmp = {0};
+  *serialize = tmp;
+  return serialize;
+}
+
+s_str * serialize_to_buf (const s_serialize *serialize, s_buf *buf);
+
+s_str * serialize_to_str (const s_serialize *serialize, s_str *dest);
+
+s_serialize * serialize_bool (s_serialize *serialize, bool b);
+
+s_serialize * serialize_character (s_serialize *serialize,
+                                   character c);
+
+s_serialize * serialize_list (s_serialize *serialize,
+                              const s_list *list);
+
+s_serialize * serialize_tag (s_serialize *serialize,
+                             const s_tag *tag);
+
+s_serialize * serialize_tuple (s_serialize *serialize,
+                               const s_tuple *tuple);
+
+s_serialize * serialize_s8 (s_serialize *serialize, s8 x);
+
+s_serialize * serialize_s16 (s_serialize *serialize, s16 x);
+
+s_serialize * serialize_s32 (s_serialize *serialize, s32 x);
+
+s_serialize * serialize_s64 (s_serialize *serialize, s64 x);
+
+s_serialize * serialize_str (s_serialize *serialize, const s_str *str);
+
+s_serialize * serialize_sw (s_serialize *serialize, sw x);
+
+s_serialize * serialize_u8 (s_serialize *serialize, u8 x)
+{
+  assert(serialize);
+  if ((r = buf_write_u8(serialize->buf, x)) < 0)
+    return NULL;
+  return serialize;
+}
+
+s_serialize * serialize_u16 (s_serialize *serialize, u16 x);
+
+s_serialize * serialize_u32 (s_serialize *serialize, u32 x);
+
+s_serialize * serialize_u64 (s_serialize *serialize, u64 x);
+
+s_serialize * serialize_uw (s_serialize *serialize, uw x);
diff --git a/libkc3/serialize.h b/libkc3/serialize.h
index ba29dfb..ee7d9c3 100644
--- a/libkc3/serialize.h
+++ b/libkc3/serialize.h
@@ -19,6 +19,8 @@
 #ifndef LIBKC3_SERIALIZE_H
 #define LIBKC3_SERIALIZE_H
 
+#include "types.h"
+
 /* Stack-allocation compatible functions, call serialize_clean
    after use. */
 void          serialize_clean (s_serialize *serialize);
diff --git a/libkc3/types.h b/libkc3/types.h
index d07ff51..7ff711e 100644
--- a/libkc3/types.h
+++ b/libkc3/types.h
@@ -58,13 +58,13 @@ typedef int8_t             s8;
 typedef int16_t            s16;
 typedef int32_t            s32;
 typedef int64_t            s64;
-typedef long long          s128;
+//XXX (32 bits) typedef long long          s128;
 typedef long               sw;
 typedef uint8_t            u8;
 typedef uint16_t           u16;
 typedef uint32_t           u32;
 typedef uint64_t           u64;
-typedef unsigned long long u128;
+//XXX (32 bits) typedef unsigned long long u128;
 typedef unsigned long      uw;
 
 #ifdef SW_MAX
@@ -204,6 +204,7 @@ typedef struct queue                   s_queue;
 typedef struct quote                   s_quote;
 typedef struct ratio                   s_ratio;
 typedef struct sequence                s_sequence;
+typedef struct serialize               s_serialize;
 typedef struct str                     s_str;
 typedef struct struct_                 s_struct;
 typedef struct struct_type             s_struct_type;
@@ -501,6 +502,11 @@ struct ratio {
   s_integer denominator;
 };
 
+struct serialize {
+  s_buf heap;
+  s_buf buf;
+};
+
 struct sym {
   s_str str;
 };
diff --git a/markdown/sources.mk b/markdown/sources.mk
index b762875..0916b02 100644
--- a/markdown/sources.mk
+++ b/markdown/sources.mk
@@ -2,12 +2,12 @@
 HEADERS = \
 	"entity.h" \
 	"markdown.h" \
-	"md4c-html.h" \
 	"md4c.h" \
+	"md4c-html.h" \
 
 SOURCES = \
 	"entity.c" \
 	"markdown.c" \
-	"md4c-html.c" \
 	"md4c.c" \
+	"md4c-html.c" \
 
diff --git a/markdown/sources.sh b/markdown/sources.sh
index 858b532..cfa8856 100644
--- a/markdown/sources.sh
+++ b/markdown/sources.sh
@@ -1,3 +1,3 @@
 # sources.sh generated by update_sources
-HEADERS='entity.h markdown.h md4c-html.h md4c.h '
-SOURCES='entity.c markdown.c md4c-html.c md4c.c '
+HEADERS='entity.h markdown.h md4c.h md4c-html.h '
+SOURCES='entity.c markdown.c md4c.c md4c-html.c '
diff --git a/socket/sources.mk b/socket/sources.mk
index e54659f..8cec70b 100644
--- a/socket/sources.mk
+++ b/socket/sources.mk
@@ -1,12 +1,12 @@
 # sources.mk generated by update_sources
 HEADERS = \
-	"socket.h" \
 	"socket_addr.h" \
 	"socket_buf.h" \
+	"socket.h" \
 	"types.h" \
 
 SOURCES = \
-	"socket.c" \
 	"socket_addr.c" \
 	"socket_buf.c" \
+	"socket.c" \
 
diff --git a/socket/sources.sh b/socket/sources.sh
index 6875ff3..26ed740 100644
--- a/socket/sources.sh
+++ b/socket/sources.sh
@@ -1,3 +1,3 @@
 # sources.sh generated by update_sources
-HEADERS='socket.h socket_addr.h socket_buf.h types.h '
-SOURCES='socket.c socket_addr.c socket_buf.c '
+HEADERS='socket_addr.h socket_buf.h socket.h types.h '
+SOURCES='socket_addr.c socket_buf.c socket.c '
diff --git a/test/sources.mk b/test/sources.mk
index 37bed6a..4a28f33 100644
--- a/test/sources.mk
+++ b/test/sources.mk
@@ -27,10 +27,10 @@ SOURCES = \
 	"character_test.c" \
 	"compare_test.c" \
 	"env_test.c" \
-	"fact_test.c" \
 	"facts_cursor_test.c" \
 	"facts_test.c" \
 	"facts_with_test.c" \
+	"fact_test.c" \
 	"fn_test.c" \
 	"hash_test.c" \
 	"ident_test.c" \
diff --git a/test/sources.sh b/test/sources.sh
index 8632c43..1a66fe6 100644
--- a/test/sources.sh
+++ b/test/sources.sh
@@ -1,3 +1,3 @@
 # sources.sh generated by update_sources
 HEADERS='buf_parse_test.h buf_parse_test_su.h compare_test.h fact_test.h tag_test.h test.h '
-SOURCES='array_test.c bool_test.c buf_file_test.c buf_inspect_test.c buf_parse_test.c buf_parse_test_s16.c buf_parse_test_s32.c buf_parse_test_s64.c buf_parse_test_s8.c buf_parse_test_u16.c buf_parse_test_u32.c buf_parse_test_u64.c buf_parse_test_u8.c buf_test.c call_test.c cfn_test.c character_test.c compare_test.c env_test.c fact_test.c facts_cursor_test.c facts_test.c facts_with_test.c fn_test.c hash_test.c ident_test.c inspect_test.c libkc3_test.c list_test.c ratio_test.c set__fact_test.c set__tag_test.c skiplist__fact_test.c str_test.c struct_test.c sym_test.c tag_test.c test.c tuple_test.c types_test.c '
+SOURCES='array_test.c bool_test.c buf_file_test.c buf_inspect_test.c buf_parse_test.c buf_parse_test_s16.c buf_parse_test_s32.c buf_parse_test_s64.c buf_parse_test_s8.c buf_parse_test_u16.c buf_parse_test_u32.c buf_parse_test_u64.c buf_parse_test_u8.c buf_test.c call_test.c cfn_test.c character_test.c compare_test.c env_test.c facts_cursor_test.c facts_test.c facts_with_test.c fact_test.c fn_test.c hash_test.c ident_test.c inspect_test.c libkc3_test.c list_test.c ratio_test.c set__fact_test.c set__tag_test.c skiplist__fact_test.c str_test.c struct_test.c sym_test.c tag_test.c test.c tuple_test.c types_test.c '
diff --git a/window/cairo/quartz/sources.mk b/window/cairo/quartz/sources.mk
index b1bdd8a..09a109b 100644
--- a/window/cairo/quartz/sources.mk
+++ b/window/cairo/quartz/sources.mk
@@ -1,18 +1,18 @@
 # sources.mk generated by update_sources
 HEADERS = \
 	"quartz_to_xkbcommon.h" \
-	"window_cairo_quartz.h" \
 	"window_cairo_quartz_app_delegate.h" \
-	"window_cairo_quartz_view.h" \
+	"window_cairo_quartz.h" \
 	"window_cairo_quartz_view_controller.h" \
+	"window_cairo_quartz_view.h" \
 	"xkbquartz.h" \
 
 SOURCES = \
 	"quartz_to_xkbcommon.c" \
 
 OBJC_SOURCES = \
-	"window_cairo_quartz.m" \
 	"window_cairo_quartz_app_delegate.m" \
-	"window_cairo_quartz_view.m" \
+	"window_cairo_quartz.m" \
 	"window_cairo_quartz_view_controller.m" \
+	"window_cairo_quartz_view.m" \
 
diff --git a/window/cairo/quartz/sources.sh b/window/cairo/quartz/sources.sh
index 03cc583..baf8b5a 100644
--- a/window/cairo/quartz/sources.sh
+++ b/window/cairo/quartz/sources.sh
@@ -1,4 +1,4 @@
 # sources.sh generated by update_sources
-HEADERS='quartz_to_xkbcommon.h window_cairo_quartz.h window_cairo_quartz_app_delegate.h window_cairo_quartz_view.h window_cairo_quartz_view_controller.h xkbquartz.h '
+HEADERS='quartz_to_xkbcommon.h window_cairo_quartz_app_delegate.h window_cairo_quartz.h window_cairo_quartz_view_controller.h window_cairo_quartz_view.h xkbquartz.h '
 SOURCES='quartz_to_xkbcommon.c '
-OBJC_SOURCES='window_cairo_quartz.m window_cairo_quartz_app_delegate.m window_cairo_quartz_view.m window_cairo_quartz_view_controller.m '
+OBJC_SOURCES='window_cairo_quartz_app_delegate.m window_cairo_quartz.m window_cairo_quartz_view_controller.m window_cairo_quartz_view.m '