From 319b4ca808bbeebd0a7b63b380123f5d0c286cd0 Mon Sep 17 00:00:00 2001 From: iWas-Coder Date: Tue, 3 Dec 2024 16:48:33 +0100 Subject: [PATCH] Cleaned up the `pattern_match` API to make it more convenient to use --- carbon.h | 9 +++++++- src/carbon_fs.c | 26 +++++++++++----------- src/make.c | 58 ++++++++++++++++++++++++------------------------- 3 files changed, 49 insertions(+), 44 deletions(-) diff --git a/carbon.h b/carbon.h index 0bc8351..0cfe52c 100644 --- a/carbon.h +++ b/carbon.h @@ -606,6 +606,8 @@ CARBON_API u8 carbon_strlist_contains(CBN_StrList *sl, const char *s); ** || Filesystem || ** $$========================$$ */ +#define carbon_fs_pattern_match_foreach(pmf) for (struct { usz i; char *f; } it = {0, (pmf).files[0]}; it.i < (pmf).count; ++it.i, it.i < (pmf).count ? it.f = (pmf).files[it.i] : it.f) + typedef enum { CBN_FILE_FORMAT_PNG, CBN_FILE_FORMAT_BMP, @@ -613,6 +615,11 @@ typedef enum { CBN_FILE_FORMAT_JPG, } CBN_FileFormat; +typedef struct { + char **files; + usz count; +} CBN_PatternMatchedFiles; + CARBON_API u8 carbon_fs_exists(const char *file); CARBON_API u8 carbon_fs_is_regular_file(const char *file); CARBON_API u8 carbon_fs_is_directory(const char *file); @@ -625,7 +632,7 @@ CARBON_API u8 carbon_fs_change_directory(const char *path); CARBON_API u8 carbon_fs_create_directory(const char *path); CARBON_API u8 carbon_fs_create_directories(const char *path); CARBON_API char *carbon_fs_get_bin_directory(void); -CARBON_API char **carbon_fs_pattern_match(const char *pattern, usz *out_count); +CARBON_API CBN_PatternMatchedFiles carbon_fs_pattern_match(const char *pattern); CARBON_API u32 carbon_fs_get_file_size(const char *file); CARBON_API u8 carbon_fs_read_entire_file(CBN_StrBuilder *sb, const char *file); CARBON_API CBN_List carbon_fs_read_img_from_file(const char *file); diff --git a/src/carbon_fs.c b/src/carbon_fs.c index 58f7cbd..62c3a36 100644 --- a/src/carbon_fs.c +++ b/src/carbon_fs.c @@ -225,8 +225,10 @@ char *carbon_fs_get_bin_directory(void) { return dir; } -char **carbon_fs_pattern_match(const char *pattern, usz *out_count) { +CBN_PatternMatchedFiles carbon_fs_pattern_match(const char *pattern) { static usz i = 0; + CBN_PatternMatchedFiles out; + memset(&out, 0, sizeof(CBN_PatternMatchedFiles)); #ifdef _WIN32 static usz counts[CARBON_FS_PATMAT_MAX_STRUCTS]; static char *results[CARBON_FS_PATMAT_MAX_STRUCTS][MAX_PATH]; @@ -237,8 +239,7 @@ char **carbon_fs_pattern_match(const char *pattern, usz *out_count) { h_find = FindFirstFile(pattern, &find_data); if (h_find == INVALID_HANDLE_VALUE) { CARBON_ERROR("no found matches"); - *out_count = 0; - return 0; + return out; } do { if (counts[i] < MAX_PATH) { @@ -251,10 +252,11 @@ char **carbon_fs_pattern_match(const char *pattern, usz *out_count) { } } while (FindNextFile(h_find, &find_data)); FindClose(h_find); - *out_count = counts[i]; + out.count = counts[i]; ++i; if (i >= CARBON_FS_PATMAT_MAX_STRUCTS) i = 0; - return results[i]; + out.files = results[i]; + return out; #else static glob_t xs[CARBON_FS_PATMAT_MAX_STRUCTS]; glob_t *x = &xs[i]; @@ -262,21 +264,19 @@ char **carbon_fs_pattern_match(const char *pattern, usz *out_count) { switch (glob(pattern, GLOB_TILDE, 0, x)) { case GLOB_NOSPACE: CARBON_ERROR("out of memory"); - *out_count = 0; - return 0; + return out; case GLOB_ABORTED: CARBON_ERROR("read error"); - *out_count = 0; - return 0; + return out; case GLOB_NOMATCH: CARBON_ERROR("no found matches"); - *out_count = 0; - return 0; + return out; } ++i; if (i >= CARBON_FS_PATMAT_MAX_STRUCTS) i = 0; - *out_count = x->gl_pathc; - return x->gl_pathv; + out.count = x->gl_pathc; + out.files = x->gl_pathv; + return out; #endif } diff --git a/src/make.c b/src/make.c index d67bae8..6198211 100644 --- a/src/make.c +++ b/src/make.c @@ -105,33 +105,32 @@ static void rebuild_myself(const char **host_argv) { static void run_tests(void) { CARBON_INFO_COLOR(CARBON_COLOR_YELLOW, "[*] Running tests..."); - usz c_files_count = 0, cxx_files_count = 0; - char **c_files = carbon_fs_pattern_match("test/*.c", &c_files_count); - char **cxx_files = carbon_fs_pattern_match("test/*.cc", &cxx_files_count); + CBN_PatternMatchedFiles c_files = carbon_fs_pattern_match("test/*.c"); + CBN_PatternMatchedFiles cxx_files = carbon_fs_pattern_match("test/*.cc"); CBN_StrBuilder cmd = {0}; - for (usz i = 0; i < c_files_count; ++i) { - CARBON_INFO(" CC %s", c_files[i]); - carbon_string_strip_substr(c_files[i], ".c"); + carbon_fs_pattern_match_foreach(c_files) { + CARBON_INFO(" CC %s", it.f); + carbon_string_strip_substr(it.f, ".c"); carbon_strbuilder_add_cstr(&cmd, CARBON_C_COMPILER " -I . " C_STD " " WARNS " -fPIE "); #ifdef CARBON_MAKE_USE_SANITIZERS carbon_strbuilder_add_cstr(&cmd, "-fsanitize=address,undefined "); #else carbon_strbuilder_add_cstr(&cmd, "-pipe -Os "); #endif - carbon_strbuilder_add_cstr(&cmd, carbon_string_fmt("-c %s.c -o %s.o", c_files[i], c_files[i])); + carbon_strbuilder_add_cstr(&cmd, carbon_string_fmt("-c %s.c -o %s.o", it.f, it.f)); call_cmd(carbon_strview_to_cstr(carbon_strview_from_strbuilder(&cmd))); carbon_strbuilder_free(&cmd); } - for (usz i = 0; i < cxx_files_count; ++i) { - CARBON_INFO(" CXX %s", cxx_files[i]); - carbon_string_strip_substr(cxx_files[i], ".cc"); + carbon_fs_pattern_match_foreach(cxx_files) { + CARBON_INFO(" CXX %s", it.f); + carbon_string_strip_substr(it.f, ".cc"); carbon_strbuilder_add_cstr(&cmd, CARBON_CXX_COMPILER " -I . " CXX_STD " " WARNS " -fPIE "); #ifdef CARBON_MAKE_USE_SANITIZERS carbon_strbuilder_add_cstr(&cmd, "-fsanitize=address,undefined "); #else carbon_strbuilder_add_cstr(&cmd, "-pipe -Os "); #endif - carbon_strbuilder_add_cstr(&cmd, carbon_string_fmt("-c %s.cc -o %s.o", cxx_files[i], cxx_files[i])); + carbon_strbuilder_add_cstr(&cmd, carbon_string_fmt("-c %s.cc -o %s.o", it.f, it.f)); call_cmd(carbon_strview_to_cstr(carbon_strview_from_strbuilder(&cmd))); carbon_strbuilder_free(&cmd); } @@ -157,27 +156,26 @@ static void run_tests(void) { static void build(void) { CARBON_INFO(" MKDIR " WORKDIR); if (!carbon_fs_create_directory(WORKDIR)) exit_gracefully(); - usz c_files_count = 0, cxx_files_count = 0, objc_files_count = 0; - char **c_files = carbon_fs_pattern_match("src/carbon_*.c", &c_files_count); - char **cxx_files = carbon_fs_pattern_match("src/carbon_*.cc", &cxx_files_count); - char **objc_files = carbon_fs_pattern_match("src/carbon_*.m", &objc_files_count); - for (usz i = 0; i < c_files_count; ++i) { - CARBON_INFO(" CC %s", c_files[i]); - carbon_string_strip_substr(c_files[i], "src/"); - carbon_string_strip_substr(c_files[i], ".c"); - call_cmd(carbon_string_fmt(CARBON_C_COMPILER " -I . " C_STD " " WARNS " -fPIC -pipe -Os -c src/%s.c -o %s/%s.o", c_files[i], WORKDIR, c_files[i])); + CBN_PatternMatchedFiles c_files = carbon_fs_pattern_match("src/carbon_*.c"); + CBN_PatternMatchedFiles cxx_files = carbon_fs_pattern_match("src/carbon_*.cc"); + CBN_PatternMatchedFiles objc_files = carbon_fs_pattern_match("src/carbon_*.m"); + carbon_fs_pattern_match_foreach(c_files) { + CARBON_INFO(" CC %s", it.f); + carbon_string_strip_substr(it.f, "src/"); + carbon_string_strip_substr(it.f, ".c"); + call_cmd(carbon_string_fmt(CARBON_C_COMPILER " -I . " C_STD " " WARNS " -fPIC -pipe -Os -c src/%s.c -o %s/%s.o", it.f, WORKDIR, it.f)); } - for (usz i = 0; i < cxx_files_count; ++i) { - CARBON_INFO(" CXX %s", cxx_files[i]); - carbon_string_strip_substr(cxx_files[i], "src/"); - carbon_string_strip_substr(cxx_files[i], ".cc"); - call_cmd(carbon_string_fmt(CARBON_CXX_COMPILER " -I . " CXX_STD " " WARNS " -fPIC -pipe -Os -c src/%s.cc -o %s/%s.o", cxx_files[i], WORKDIR, cxx_files[i])); + carbon_fs_pattern_match_foreach(cxx_files) { + CARBON_INFO(" CXX %s", it.f); + carbon_string_strip_substr(it.f, "src/"); + carbon_string_strip_substr(it.f, ".cc"); + call_cmd(carbon_string_fmt(CARBON_CXX_COMPILER " -I . " CXX_STD " " WARNS " -fPIC -pipe -Os -c src/%s.cc -o %s/%s.o", it.f, WORKDIR, it.f)); } - for (usz i = 0; i < objc_files_count; ++i) { - CARBON_INFO(" OBJC %s", objc_files[i]); - carbon_string_strip_substr(objc_files[i], "src/"); - carbon_string_strip_substr(objc_files[i], ".m"); - call_cmd(carbon_string_fmt(CARBON_C_COMPILER " -I . -x objective-c " C_STD " " WARNS " -fPIC -pipe -Os -c src/%s.m -o %s/%s.o", objc_files[i], WORKDIR, objc_files[i])); + carbon_fs_pattern_match_foreach(objc_files) { + CARBON_INFO(" OBJC %s", it.f); + carbon_string_strip_substr(it.f, "src/"); + carbon_string_strip_substr(it.f, ".m"); + call_cmd(carbon_string_fmt(CARBON_C_COMPILER " -I . -x objective-c " C_STD " " WARNS " -fPIC -pipe -Os -c src/%s.m -o %s/%s.o", it.f, WORKDIR, it.f)); } CARBON_INFO(" AR libcarbon.a"); call_cmd("ar -rcs " WORKDIR "/libcarbon.a " WORKDIR "/*.o");