Commit cb6f223a57d901f9dcaee2d1ae08f9539ddfb526

Thomas de Grivel 2024-08-02T12:35:36

Struct.offset(%HTTP.Request, :message)

diff --git a/.ikc3_history b/.ikc3_history
index 0d444bd..d3dab1d 100644
--- a/.ikc3_history
+++ b/.ikc3_history
@@ -1,5 +1,3 @@
-module()
-List.reverse([1, 2, 3])
 List.reverse([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
 (List) {1, 2}
 ?
@@ -97,3 +95,5 @@ server = Socket.listen("192.168.1.50
 server = Socket.listen("192.168.2.50", "58000")
 server_client = Socket.Buf.accept(server)
 req = HTTP.Request.buf_parse(server_client.buf_rw.r)
+%HTTP.Response{}
+Struct.offset(%HTTP.Response{}, :message)
diff --git a/libkc3/struct.c b/libkc3/struct.c
index 5965c2b..69ef986 100644
--- a/libkc3/struct.c
+++ b/libkc3/struct.c
@@ -369,6 +369,24 @@ s_struct * struct_new_copy (const s_struct *src)
   return s;
 }
 
+uw * struct_offset (const s_struct *s, const s_sym * const *key,
+                    uw *dest)
+{
+  uw i = 0;
+  assert(s);
+  assert(s->type);
+  assert(key);
+  if (! struct_find_key_index(s, *key, &i)) {
+    err_write_1("struct_offset: key not found: ");
+    err_inspect_sym(key);
+    err_write_1("\n");
+    return NULL;
+  }
+  assert(i < s->type->map.count);
+  *dest = s->type->offset[i];
+  return dest;
+}
+
 s_struct * struct_set (s_struct *s, const s_sym *key,
                        const s_tag *value)
 {
diff --git a/libkc3/struct.h b/libkc3/struct.h
index 29da30f..4d747fd 100644
--- a/libkc3/struct.h
+++ b/libkc3/struct.h
@@ -56,5 +56,8 @@ const s_sym ** struct_get_type (const s_struct *s, const s_sym *key,
 const s_sym ** struct_get_sym (const s_struct *s, const s_sym *key);
 const s_tag *  struct_get_tag (const s_struct *s, const s_sym *key);
 u8             struct_get_u8 (const s_struct *s, const s_sym *key);
+uw *           struct_offset (const s_struct *s,
+                              const s_sym * const *key,
+                              uw *dest);
 
 #endif /* LIBKC3_STRUCT_H */