Commit d3f69ed5648dcfea00cc08e7cf0d2d7d5d9eccc3

Thomas de Grivel 2024-11-08T20:04:29

fix time litterals and in structs

diff --git a/libkc3/buf_parse.c b/libkc3/buf_parse.c
index b7a48d1..7a968cc 100644
--- a/libkc3/buf_parse.c
+++ b/libkc3/buf_parse.c
@@ -4350,6 +4350,125 @@ sw buf_parse_tag_void (s_buf *buf, s_tag *dest)
 sw buf_parse_time (s_buf *buf, s_time *dest)
 {
   sw r;
+  (r = buf_parse_time_as_sw(buf, dest)) > 0 ||
+    (r = buf_parse_time_as_tags(buf, dest));
+  return r;
+}
+
+sw buf_parse_time_as_sw (s_buf *buf, s_time *dest)
+{
+  sw r;
+  sw result = 0;
+  s_buf_save save;
+  s_time tmp = {0};
+  buf_save_init(buf, &save);
+  if ((r = buf_read_1(buf, "%Time{")) <= 0)
+    goto clean;
+  result += r;
+  if ((r = buf_parse_comments(buf)) < 0)
+    goto restore;
+  result += r;
+  if ((r = buf_ignore_spaces(buf)) < 0)
+    goto restore;
+  result += r;
+  if ((r = buf_read_1(buf, "}")) < 0)
+    goto restore;
+  if (r > 0) {
+    result += r;
+    *dest = tmp;
+    r = result;
+    goto clean;
+  }
+  while (1) {
+    if ((r = buf_read_1(buf, "tv_sec:")) < 0)
+      goto restore;
+    if (r > 0) {
+      result += r;
+      if ((r = buf_parse_comments(buf)) < 0)
+        goto restore;
+      result += r;
+      if ((r = buf_ignore_spaces(buf)) < 0)
+        goto restore;
+      result += r;
+      if ((r = buf_parse_sw(buf, &tmp.tv_sec)) <= 0)
+        goto restore;
+      if ((r = buf_parse_comments(buf)) < 0)
+        goto restore;
+      result += r;
+      if ((r = buf_ignore_spaces(buf)) < 0)
+        goto restore;
+      result += r;
+      if ((r = buf_read_1(buf, "}")) < 0)
+        goto restore;
+      if (r > 0) {
+        result += r;
+        *dest = tmp;
+        r = result;
+        goto clean;
+      }
+      if ((r = buf_read_1(buf, ",")) < 0)
+        goto restore;
+      if (r > 0) {
+        result += r;
+        if ((r = buf_parse_comments(buf)) < 0)
+          goto restore;
+        result += r;
+        if ((r = buf_ignore_spaces(buf)) < 0)
+          goto restore;
+        result += r;
+      }
+    }
+    if ((r = buf_read_1(buf, "tv_nsec:")) < 0)
+      goto restore;
+    if (r > 0) {
+      result += r;
+      if ((r = buf_parse_comments(buf)) < 0)
+        goto restore;
+      result += r;
+      if ((r = buf_ignore_spaces(buf)) < 0)
+        goto restore;
+      result += r;
+      if ((r = buf_parse_sw(buf, &tmp.tv_nsec)) <= 0)
+        goto restore;
+      if ((r = buf_parse_comments(buf)) < 0)
+        goto restore;
+      result += r;
+      if ((r = buf_ignore_spaces(buf)) < 0)
+        goto restore;
+      result += r;
+      if ((r = buf_read_1(buf, "}")) < 0)
+        goto restore;
+      if (r > 0) {
+        result += r;
+        *dest = tmp;
+        r = result;
+        goto clean;
+      }
+      if ((r = buf_read_1(buf, ",")) < 0)
+        goto restore;
+      if (r > 0) {
+        result += r;
+        if ((r = buf_parse_comments(buf)) < 0)
+          goto restore;
+        result += r;
+        if ((r = buf_ignore_spaces(buf)) < 0)
+          goto restore;
+        result += r;
+      }
+    }
+  }
+  r = -1;
+ restore:
+  buf_save_restore_rpos(buf, &save);
+  time_clean(&tmp);
+ clean:
+  buf_save_clean(buf, &save);
+  return r;
+}
+
+sw buf_parse_time_as_tags (s_buf *buf, s_time *dest)
+{
+  sw r;
   sw result = 0;
   s_buf_save save;
   s_time tmp = {0};
@@ -4424,9 +4543,6 @@ sw buf_parse_time (s_buf *buf, s_time *dest)
       if ((r = buf_ignore_spaces(buf)) < 0)
         goto restore;
       result += r;
-      if (! tmp.tag &&
-          ! time_allocate(&tmp))
-        goto restore;
       if ((r = buf_parse_tag(buf, tmp.tag + 1)) <= 0)
         goto restore;
       if ((r = buf_parse_comments(buf)) < 0)
diff --git a/libkc3/buf_parse.h b/libkc3/buf_parse.h
index 4f3ac42..eb95f1d 100644
--- a/libkc3/buf_parse.h
+++ b/libkc3/buf_parse.h
@@ -142,6 +142,8 @@ sw buf_parse_tag_unquote (s_buf *buf, s_tag *dest);
 sw buf_parse_tag_var (s_buf *buf, s_tag *dest);
 sw buf_parse_tag_void (s_buf *buf, s_tag *dest);
 sw buf_parse_time (s_buf *buf, s_time *dest);
+sw buf_parse_time_as_sw (s_buf *buf, s_time *dest);
+sw buf_parse_time_as_tags (s_buf *buf, s_time *dest);
 sw buf_parse_tuple (s_buf *buf, s_tuple *dest);
 sw buf_parse_u64_hex (s_buf *buf, u64 *dest);
 sw buf_parse_unquote (s_buf *buf, s_unquote *dest);
diff --git a/libkc3/data.c b/libkc3/data.c
index 78a16d7..6898dc9 100644
--- a/libkc3/data.c
+++ b/libkc3/data.c
@@ -71,6 +71,8 @@ sw data_buf_inspect (s_buf *buf, const s_sym *type, const void *data)
     return buf_inspect_sym(buf, data);
   if (type == &g_sym_Tag)
     return buf_inspect_tag(buf, data);
+  if (type == &g_sym_Time)
+    return buf_inspect_time(buf, data);
   if (type == &g_sym_Tuple)
     return buf_inspect_tuple(buf, data);
   if (type == &g_sym_U8)
@@ -161,6 +163,8 @@ sw data_buf_inspect_size (s_pretty *pretty, const s_sym *type,
     return buf_inspect_sym_size(pretty, data);
   if (type == &g_sym_Tag)
     return buf_inspect_tag_size(pretty, data);
+  if (type == &g_sym_Time)
+    return buf_inspect_time_size(pretty, data);
   if (type == &g_sym_Tuple)
     return buf_inspect_tuple_size(pretty, data);
   if (type == &g_sym_U8)
@@ -293,6 +297,10 @@ bool data_clean (const s_sym *type, void *data)
     tag_clean(data);
     return true;
   }
+  if (type == &g_sym_Time) {
+    time_clean(data);
+    return true;
+  }
   if (type == &g_sym_Tuple) {
     tuple_clean(data);
     return true;
@@ -390,6 +398,8 @@ bool data_compare (const s_sym *type, const void *a, const void *b)
     return compare_sw(*(sw *) a, *(sw *) b);
   if (type == &g_sym_Sym)
     return compare_sym(a, b);
+  if (type == &g_sym_Time)
+    return compare_time(a, b);
   if (type == &g_sym_Tuple)
     return compare_tuple(a, b);
   if (type == &g_sym_U8)
@@ -479,6 +489,8 @@ bool data_hash_update (const s_sym *type, t_hash *hash, const void *data)
     return hash_update_sym(hash, data);
   if (type == &g_sym_Tag)
     return hash_update_tag(hash, data);
+  if (type == &g_sym_Time)
+    return hash_update_time(hash, data);
   if (type == &g_sym_Tuple)
     return hash_update_tuple(hash, data);
   if (type == &g_sym_U8)
@@ -569,6 +581,8 @@ void * data_init_cast (void *data, const s_sym * const *type,
     return sym_init_cast(data, type, tag);
   if (t == &g_sym_Tag)
     return tag_init_copy(data, tag);
+  if (t == &g_sym_Time)
+    return time_init_cast(data, type, tag);
   if (t == &g_sym_Tuple)
     return tuple_init_cast(data, type, tag);
   if (t == &g_sym_U8)
@@ -659,6 +673,8 @@ void * data_init_copy (const s_sym *type, void *data, const void *src)
     return sym_init_copy(data, src);
   if (type == &g_sym_Tag)
     return tag_init_copy(data, src);
+  if (type == &g_sym_Time)
+    return time_init_copy(data, src);
   if (type == &g_sym_Tuple)
     return tuple_init_copy(data, src);
   if (type == &g_sym_U8)
diff --git a/libkc3/time.c b/libkc3/time.c
index f9f3467..fef0c9d 100644
--- a/libkc3/time.c
+++ b/libkc3/time.c
@@ -65,6 +65,21 @@ s_time * time_init_add (s_time *time, const s_time *a, const s_time *b)
   return time;
 }
 
+s_time * time_init_cast (s_time *time, const s_sym * const *type,
+                         const s_tag *src)
+{
+  assert(time);
+  assert(type);
+  assert(*type);
+  assert(src);
+  (void) type;
+  if (src->type == TAG_TIME)
+    return time_init_copy(time, &src->data.time);
+  err_puts("time_init_cast: cannot cast to Time");
+  assert(! "time_init_cast: cannot cast to Time");
+  return NULL;
+}
+
 s_time * time_init_copy (s_time *time, const s_time *src)
 {
   s_time tmp = {0};
diff --git a/libkc3/time.h b/libkc3/time.h
index a44493d..95c4623 100644
--- a/libkc3/time.h
+++ b/libkc3/time.h
@@ -25,6 +25,8 @@
 void     time_clean (s_time *time);
 s_time * time_init (s_time *time);
 s_time * time_init_add (s_time *time, const s_time *a, const s_time *b);
+s_time * time_init_cast (s_time *time, const s_sym * const *type,
+                         const s_tag *src);
 s_time * time_init_copy (s_time *time, const s_time *src);
 s_time * time_init_now (s_time *time);
 s_time * time_init_str (s_time *time, const s_str *src);