From 7cd67dd7f3ebf38a2a65c6756090936f9a1b3b03 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Tue, 29 Oct 2024 14:36:49 -0400 Subject: Add file globbing --- stdlib/paths.c | 20 +++++++++++++++++++- stdlib/paths.h | 1 + 2 files changed, 20 insertions(+), 1 deletion(-) (limited to 'stdlib') diff --git a/stdlib/paths.c b/stdlib/paths.c index e8a1e1f6..8f6e1359 100644 --- a/stdlib/paths.c +++ b/stdlib/paths.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -535,6 +536,24 @@ public OptionalClosure_t Path$by_line(Path_t path) return (Closure_t){.fn=(void*)_next_line, .userdata=wrapper}; } +public Array_t Path$glob(Path_t path) +{ + glob_t glob_result; + int status = glob(Text$as_c_string(path), GLOB_BRACE | GLOB_TILDE | GLOB_TILDE_CHECK, NULL, &glob_result); + if (status != 0 && status != GLOB_NOMATCH) + fail("Failed to perform globbing"); + + Array_t glob_files = {}; + for (size_t i = 0; i < glob_result.gl_pathc; i++) { + size_t len = strlen(glob_result.gl_pathv[i]); + if ((len >= 2 && glob_result.gl_pathv[i][len-1] == '.' && glob_result.gl_pathv[i][len-2] == '/') + || (len >= 2 && glob_result.gl_pathv[i][len-1] == '.' && glob_result.gl_pathv[i][len-2] == '.' && glob_result.gl_pathv[i][len-3] == '/')) + continue; + Array$insert(&glob_files, (Text_t[1]){Text$from_str(glob_result.gl_pathv[i])}, I(0), sizeof(Text_t)); + } + return glob_files; +} + public const TypeInfo_t Path$info = { .size=sizeof(Path_t), .align=__alignof__(Path_t), @@ -542,5 +561,4 @@ public const TypeInfo_t Path$info = { .TextInfo={.lang="Path"}, }; - // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/stdlib/paths.h b/stdlib/paths.h index 39089f54..551467e9 100644 --- a/stdlib/paths.h +++ b/stdlib/paths.h @@ -47,6 +47,7 @@ Path_t Path$parent(Path_t path); Text_t Path$base_name(Path_t path); Text_t Path$extension(Path_t path, bool full); Closure_t Path$by_line(Path_t path); +Array_t Path$glob(Path_t path); #define Path$hash Text$hash #define Path$compare Text$compare -- cgit v1.2.3