Commit 47e30f02d18bd31628eff11448d2b7e140f80c57

Thomas de Grivel 2024-07-31T17:04:39

str_init_cast: cast from [Map, Struct, Tuple] to Str

diff --git a/libkc3/str.c b/libkc3/str.c
index 79a4885..7df45b6 100644
--- a/libkc3/str.c
+++ b/libkc3/str.c
@@ -37,16 +37,19 @@
 #include "tag.h"
 #include "tag_type.h"
 
-#define DEF_STR_INIT(type)                                             \
-  s_str * str_init_ ## type (s_str *str, type x)                       \
+#define DEF_STR_INIT(name, type)                                       \
+  s_str * str_init_ ## name (s_str *str, type x)                       \
   {                                                                    \
     s_buf buf;                                                         \
     sw size;                                                           \
-    size = buf_inspect_ ## type ## _size(&x);                          \
+    size = buf_inspect_ ## name ## _size(&x);                          \
     if (size <= 0)                                                     \
       return NULL;                                                     \
     buf_init_alloc(&buf, size);                                        \
-    buf_inspect_ ## type(&buf, &x);                                    \
+    if (buf_inspect_ ## name(&buf, &x) < 0) {                          \
+      buf_clean(&buf);                                                 \
+      return NULL;                                                     \
+    }                                                                  \
     assert(buf.wpos == (uw) size);                                     \
     return buf_to_str(&buf, str);                                      \
   }
@@ -65,6 +68,25 @@
     return buf_to_str(&buf, str);                                      \
   }
 
+#define DEF_STR_INIT_STRUCT(name)                                      \
+  s_str * str_init_ ## name (s_str *str, const s_ ## name *x)          \
+  {                                                                    \
+    s_buf buf;                                                         \
+    sw size;                                                           \
+    size = buf_inspect_ ## name ## _size(x);                           \
+    if (! size)                                                        \
+      return str_init_empty(str);                                      \
+    if (size < 0)                                                      \
+      return NULL;                                                     \
+    buf_init_alloc(&buf, size);                                        \
+    if (buf_inspect_ ## name(&buf, x) < 0) {                           \
+      buf_clean(&buf);                                                 \
+      return NULL;                                                     \
+    }                                                                  \
+    assert(buf.wpos == (uw) size);                                     \
+    return buf_to_str(&buf, str);                                      \
+  }
+
 sw str_character (const s_str *str, uw position, character *dest)
 {
   character c;
@@ -181,6 +203,8 @@ s_str * str_init_cast (s_str *str, const s_sym * const *type,
   switch (tag->type) {
   case TAG_CHARACTER:
     return str_init_character(str, tag->data.character);
+  case TAG_MAP:
+    return str_init_map(str, &tag->data.map);
   case TAG_S8:
     return str_init_s8(str, tag->data.s8);
   case TAG_S16:
@@ -191,10 +215,14 @@ s_str * str_init_cast (s_str *str, const s_sym * const *type,
     return str_init_s64(str, tag->data.s64);
   case TAG_STR:
     return str_init_copy(str, &tag->data.str);
+  case TAG_STRUCT:
+    return str_init_struct(str, &tag->data.struct_);
   case TAG_SYM:
     return str_init_copy(str, &tag->data.sym->str);
   case TAG_SW:
     return str_init_sw(str, tag->data.sw);
+  case TAG_TUPLE:
+    return str_init_tuple(str, &tag->data.tuple);
   case TAG_U8:
     return str_init_u8(str, tag->data.u8);
   case TAG_U16:
@@ -306,10 +334,12 @@ s_str * str_init_f (s_str *str, const char *fmt, ...)
   return str;
 }
 
+DEF_STR_INIT_STRUCT(map)
 DEF_STR_INIT_INT(s8)
 DEF_STR_INIT_INT(s16)
 DEF_STR_INIT_INT(s32)
 DEF_STR_INIT_INT(s64)
+DEF_STR_INIT_STRUCT(struct)
 DEF_STR_INIT_INT(sw)
 
 s_str * str_init_slice (s_str *str, const s_str *src, sw start, sw end)
@@ -347,6 +377,7 @@ s_str * str_init_slice (s_str *str, const s_str *src, sw start, sw end)
   return str;
 }
 
+DEF_STR_INIT_STRUCT(tuple)
 DEF_STR_INIT_INT(u8)
 DEF_STR_INIT_INT(u16)
 DEF_STR_INIT_INT(u32)
diff --git a/libkc3/str.h b/libkc3/str.h
index 7e911dc..371318c 100644
--- a/libkc3/str.h
+++ b/libkc3/str.h
@@ -26,9 +26,15 @@
 
 #define STR_MAX (16 * 1024 * 1024)
 
-#define PROTOTYPE_STR_INIT(type)                                      \
+#define PROTOTYPE_STR_INIT(name, type)                          \
+  s_str * str_init_ ## name (s_str *str, type x)
+
+#define PROTOTYPE_STR_INIT_INT(type)                            \
   s_str * str_init_ ## type (s_str *str, type x)
 
+#define PROTOTYPE_STR_INIT_STRUCT(type)                         \
+  s_str * str_init_ ## type (s_str *str, const s_ ## type *x)
+
 /* Stack allocation compatible functions */
 void    str_clean (s_str *str);
 s_str * str_init (s_str *str, char *free, uw size, const char *p);
@@ -42,19 +48,22 @@ s_str * str_init_copy (s_str *str, const s_str *src);
 s_str * str_init_copy_1 (s_str *str, const char *p);
 s_str * str_init_empty (s_str *str);
 s_str * str_init_f (s_str *str, const char *fmt, ...);
-PROTOTYPE_STR_INIT(s8);
-PROTOTYPE_STR_INIT(s16);
-PROTOTYPE_STR_INIT(s32);
-PROTOTYPE_STR_INIT(s64);
+PROTOTYPE_STR_INIT_STRUCT(map);
+PROTOTYPE_STR_INIT_INT(s8);
+PROTOTYPE_STR_INIT_INT(s16);
+PROTOTYPE_STR_INIT_INT(s32);
+PROTOTYPE_STR_INIT_INT(s64);
 s_str * str_init_slice (s_str *str, const s_str *src, sw start, sw end);
 s_str * str_init_slice_utf8 (s_str *str, const s_str *src, sw start,
                              sw end);
-PROTOTYPE_STR_INIT(sw);
-PROTOTYPE_STR_INIT(u8);
-PROTOTYPE_STR_INIT(u16);
-PROTOTYPE_STR_INIT(u32);
-PROTOTYPE_STR_INIT(u64);
-PROTOTYPE_STR_INIT(uw);
+PROTOTYPE_STR_INIT_STRUCT(struct);
+PROTOTYPE_STR_INIT_INT(sw);
+PROTOTYPE_STR_INIT_STRUCT(tuple);
+PROTOTYPE_STR_INIT_INT(u8);
+PROTOTYPE_STR_INIT_INT(u16);
+PROTOTYPE_STR_INIT_INT(u32);
+PROTOTYPE_STR_INIT_INT(u64);
+PROTOTYPE_STR_INIT_INT(uw);
 s_str * str_init_vf (s_str *str, const char *fmt, va_list ap);
 
 /* Constructors, call str_delete after use */