Commit 0ca99830fe68299d1f824f89e4ecab723b83d668

Thomas de Grivel 2024-08-06T19:59:21

File.list(path)

diff --git a/.ikc3_history b/.ikc3_history
index d6ec06d..28a7516 100644
--- a/.ikc3_history
+++ b/.ikc3_history
@@ -1,7 +1,3 @@
-q
-%KC3.Operator{}
-%KC3.Operator{sym: :-}
-op = %KC3.Operator{sym: :-}
 op.sym
 op = %{id: 1, sym: :-}
 op.id
@@ -97,4 +93,8 @@ File.is_directory?("env")
 File.is_directory?("libkc3")
 File.is_directory?("plop1234")
 File.is_directory?("env")
+File.list(lib)
+File.list("lib")
+File.list("lib/kc3")
+File.list("lib/kc3/0.1")
 
diff --git a/lib/kc3/0.1/file.kc3 b/lib/kc3/0.1/file.kc3
index ea0ebaa..f101ff1 100644
--- a/lib/kc3/0.1/file.kc3
+++ b/lib/kc3/0.1/file.kc3
@@ -11,6 +11,8 @@ defmodule File do
     end
   end
 
+  def list = cfn List "file_list" (Str, Result)
+
   def stat = cfn File.Stat "file_stat" (Str, Result)
 
 end
diff --git a/libkc3/file.c b/libkc3/file.c
index 66e7a25..dfbbdc8 100644
--- a/libkc3/file.c
+++ b/libkc3/file.c
@@ -15,6 +15,7 @@
 #include <errno.h>
 #include <string.h>
 #include <sys/types.h>
+#include <dirent.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include "buf.h"
@@ -131,6 +132,38 @@ bool * file_exists (const s_str *path, bool *dest)
   return dest;
 }
 
+s_list ** file_list (const s_str *path, s_list **dest)
+{
+  DIR           *dir;
+  struct dirent *dirent;
+  s32 e;
+  s_list **tail;
+  s_list *tmp = NULL;
+  dir = opendir(path->ptr.pchar);
+  if (! dir) {
+    e = errno;
+    err_write_1("file_list: opendir: ");
+    err_write_1(strerror(e));
+    err_write_1(": ");
+    err_write_str(path);
+    return NULL;
+  }
+  tail = &tmp;
+  while ((dirent = readdir(dir))) {
+    *tail = list_new_str_alloc_copy(dirent->d_namlen,
+                                    dirent->d_name, NULL);
+    if (! *tail) {
+      list_delete_all(tmp);
+      closedir(dir);
+      return NULL;
+    }
+    tail = &(*tail)->next.data.list;
+  }
+  closedir(dir);
+  *dest = tmp;
+  return dest;
+}
+
 s_tag * file_mtime (const s_str *path, s_tag *dest)
 {
   s32 e;
@@ -261,4 +294,4 @@ s_file_stat * file_stat (const s_str *path, s_file_stat *dest)
   tmp.st_ctim.tv_nsec = sb.st_ctim.tv_nsec;
   *dest = tmp;
   return dest;
-}
\ No newline at end of file
+}
diff --git a/libkc3/list_init.c b/libkc3/list_init.c
index 1efdeef..c04366a 100644
--- a/libkc3/list_init.c
+++ b/libkc3/list_init.c
@@ -377,6 +377,18 @@ s_list * list_init_str_1 (s_list *list, char *p_free, const char *p,
   return list;
 }
 
+s_list * list_init_str_alloc_copy (s_list *list, uw size,
+                                   const char *p, s_list *next)
+{
+  s_list tmp = {0};
+  assert(list);
+  list_init(&tmp, next);
+  if (! tag_init_str_alloc_copy(&tmp.tag, size, p))
+    return NULL;
+  *list = tmp;
+  return list;
+}
+
 s_list * list_init_str_concatenate (s_list *list, const s_str *a,
                                     const s_str *b, s_list *next)
 {
@@ -990,6 +1002,19 @@ s_list * list_new_str_1 (char *p_free, const char *p, s_list *next)
   return list;
 }
 
+s_list * list_new_str_alloc_copy (uw size, const char *p, s_list *next)
+{
+  s_list *list;
+  list = list_new(next);
+  if (! list)
+    return NULL;
+  if (! tag_init_str_alloc_copy(&list->tag, size, p)) {
+    free(list);
+    return NULL;
+  }
+  return list;
+}
+
 s_list * list_new_str_concatenate (const s_str *a, const s_str *b,
                                    s_list *next)
 {
diff --git a/libkc3/list_init.h b/libkc3/list_init.h
index f7c9bd3..77ffcf2 100644
--- a/libkc3/list_init.h
+++ b/libkc3/list_init.h
@@ -58,6 +58,8 @@ s_list * list_init_str (s_list *list, char *p_free, uw size,
                         const char *p, s_list *next);
 s_list * list_init_str_1 (s_list *list, char *p_free, const char *p,
                           s_list *next);
+s_list * list_init_str_alloc_copy (s_list *list, uw size,
+                                   const char *p, s_list *next);
 s_list * list_init_str_concatenate (s_list *list, const s_str *a,
                                     const s_str *b, s_list *next);
 s_list * list_init_str_concatenate_list (s_list *list,
@@ -127,6 +129,7 @@ s_list * list_new_s64 (s64 i, s_list *next);
 s_list * list_new_str (char *p_free, uw size, const char *p,
                        s_list *next);
 s_list * list_new_str_1 (char *p_free, const char *p, s_list *next);
+s_list * list_new_str_alloc_copy (uw size, const char *p, s_list *next);
 s_list * list_new_str_concatenate (const s_str *a, const s_str *b,
                                    s_list *next);
 s_list * list_new_str_concatenate_list (const s_list **src,
@@ -189,6 +192,7 @@ s_list * list_s32 (s_list *list, s32 i);
 s_list * list_s64 (s_list *list, s64 i);
 s_list * list_str (s_list *list, char *p_free, uw size, const char *p);
 s_list * list_str_1 (s_list *list, char *p_free, const char *p);
+s_list * list_str_alloc_copy (s_list *list, uw size, const char *p);
 s_list * list_str_concatenate (s_list *list, const s_str *a,
                                const s_str *b);
 s_list * list_str_concatenate_list (s_list *list, const s_list **src);
diff --git a/libkc3/tag_init.c b/libkc3/tag_init.c
index 30eb29b..20777ac 100644
--- a/libkc3/tag_init.c
+++ b/libkc3/tag_init.c
@@ -371,6 +371,17 @@ s_tag * tag_init_str_1 (s_tag *tag, char *p_free, const char *p)
   return tag;
 }
 
+s_tag * tag_init_str_alloc_copy (s_tag *tag, uw size, const char *p)
+{
+  s_tag tmp = {0};
+  assert(tag);
+  tmp.type = TAG_STR;
+  if (! str_init_alloc_copy(&tmp.data.str, size, p))
+    return NULL;
+  *tag = tmp;
+  return tag;
+}
+
 s_tag * tag_init_str_concatenate (s_tag *tag, const s_str *a,
                                   const s_str *b)
 {
@@ -976,6 +987,20 @@ s_tag * tag_new_str_1 (char *p_free, const char *p)
   return tag;
 }
 
+s_tag * tag_new_str_alloc_copy (uw size, const char *p)
+{
+  s_tag *tag;
+  tag = alloc(sizeof(s_tag));
+  if (! tag)
+    return NULL;
+  tag->type = TAG_STR;
+  if (! str_init_alloc_copy(&tag->data.str, size, p)) {
+    free(tag);
+    return NULL;
+  }
+  return tag;
+}
+
 s_tag * tag_new_str_concatenate (const s_str *a, const s_str *b)
 {
   s_tag *tag;
@@ -1582,6 +1607,18 @@ s_tag * tag_str_1 (s_tag *tag, char *p_free, const char *p)
   return tag;
 }
 
+s_tag * tag_str_alloc_copy (s_tag *tag, uw size, const char *p)
+{
+  s_tag tmp = {0};
+  assert(tag);
+  tag_clean(tag);
+  tmp.type = TAG_STR;
+  if (! str_init_alloc_copy(&tmp.data.str, size, p))
+    return NULL;
+  *tag = tmp;
+  return tag;
+}
+
 s_tag * tag_str_concatenate (s_tag *tag, const s_str *a, const s_str *b)
 {
   s_tag tmp = {0};
diff --git a/libkc3/tag_init.h b/libkc3/tag_init.h
index a42a0c4..4d60669 100644
--- a/libkc3/tag_init.h
+++ b/libkc3/tag_init.h
@@ -49,6 +49,7 @@ s_tag * tag_init_s32 (s_tag *tag, s32 i);
 s_tag * tag_init_s64 (s_tag *tag, s64 i);
 s_tag * tag_init_str (s_tag *tag, char *p_free, uw size, const char *p);
 s_tag * tag_init_str_1 (s_tag *tag, char *p_free, const char *p);
+s_tag * tag_init_str_alloc_copy (s_tag *tag, uw size, const char *p);
 s_tag * tag_init_str_concatenate (s_tag *tag, const s_str *a,
                                   const s_str *b);
 s_tag * tag_init_str_concatenate_list (s_tag *tag, const s_list **src);
@@ -109,6 +110,7 @@ s_tag * tag_new_s32 (s32 i);
 s_tag * tag_new_s64 (s64 i);
 s_tag * tag_new_str (char *p_free, uw size, const char *p);
 s_tag * tag_new_str_1 (char *p_free, const char *p);
+s_tag * tag_new_str_alloc_copy (uw size, const char *p);
 s_tag * tag_new_str_concatenate (const s_str *a, const s_str *b);
 s_tag * tag_new_str_concatenate_list (const s_list **src);
 s_tag * tag_new_str_empty (void);
@@ -166,6 +168,7 @@ s_tag * tag_s32 (s_tag *tag, s32 i);
 s_tag * tag_s64 (s_tag *tag, s64 i);
 s_tag * tag_str (s_tag *tag, char *p_free, uw size, const char *p);
 s_tag * tag_str_1 (s_tag *tag, char *p_free, const char *p);
+s_tag * tag_str_alloc_copy (s_tag *tag, uw size, const char *p);
 s_tag * tag_str_concatenate (s_tag *tag, const s_str *a,
                              const s_str *b);
 s_tag * tag_str_concatenate_list (s_tag *tag, const s_list **src);
diff --git a/libkc3/tag_init.rb b/libkc3/tag_init.rb
index c8edd64..845a39a 100644
--- a/libkc3/tag_init.rb
+++ b/libkc3/tag_init.rb
@@ -367,6 +367,9 @@ class TagInitList
        TagInit.new("str", "1", "TAG_STR", :init_mode_init,
                    [Arg.new("char *", "p_free"),
                     Arg.new("const char *", "p")]),
+       TagInit.new("str", "alloc_copy", "TAG_STR", :init_mode_init,
+                   [Arg.new("uw", "size"),
+                    Arg.new("const char *", "p")]),
        TagInit.new("str", "concatenate", "TAG_STR", :init_mode_init,
                    [Arg.new("const s_str *", "a"),
                     Arg.new("const s_str *", "b")]),