Skip to content

Commit

Permalink
Cleaned up the pattern_match API to make it more convenient to use
Browse files Browse the repository at this point in the history
  • Loading branch information
iWas-Coder committed Dec 3, 2024
1 parent b707447 commit 319b4ca
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 44 deletions.
9 changes: 8 additions & 1 deletion carbon.h
Original file line number Diff line number Diff line change
Expand Up @@ -606,13 +606,20 @@ 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,
CBN_FILE_FORMAT_TGA,
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);
Expand All @@ -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);
Expand Down
26 changes: 13 additions & 13 deletions src/carbon_fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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) {
Expand All @@ -251,32 +252,31 @@ 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];
memset(x, 0, sizeof(glob_t));
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
}

Expand Down
58 changes: 28 additions & 30 deletions src/make.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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");
Expand Down

0 comments on commit 319b4ca

Please sign in to comment.