From 3851c4a7bdfdc7298976f81fae2a85edad711068 Mon Sep 17 00:00:00 2001 From: gaogao-qwq Date: Sun, 24 Aug 2025 04:32:22 +0800 Subject: [PATCH 1/2] Implement `read_entire_dir_recursively` and `read_entire_dir_recursively_wildcard` Basically just a GNU Make wildcard() function Signed-off-by: gaogao-qwq --- nob.c | 2 + nob.h | 111 +++++++++++++++++++ tests/read_entire_dir_recursively.c | 59 ++++++++++ tests/read_entire_dir_recursively_wildcard.c | 78 +++++++++++++ 4 files changed, 250 insertions(+) create mode 100644 tests/read_entire_dir_recursively.c create mode 100644 tests/read_entire_dir_recursively_wildcard.c diff --git a/nob.c b/nob.c index a580562..78bfba1 100644 --- a/nob.c +++ b/nob.c @@ -17,6 +17,8 @@ const char *test_names[] = { "win32_error", #endif //_WIN32 "read_entire_dir", + "read_entire_dir_recursively", + "read_entire_dir_recursively_wildcard", "da_resize", "da_last", "da_remove_unordered", diff --git a/nob.h b/nob.h index 737f61f..c097165 100644 --- a/nob.h +++ b/nob.h @@ -159,12 +159,14 @@ # include # include # include +# include #else # include # include # include # include # include +# include #endif #ifdef _WIN32 @@ -231,6 +233,8 @@ NOBDEF bool nob_mkdir_if_not_exists(const char *path); NOBDEF bool nob_copy_file(const char *src_path, const char *dst_path); NOBDEF bool nob_copy_directory_recursively(const char *src_path, const char *dst_path); NOBDEF bool nob_read_entire_dir(const char *parent, Nob_File_Paths *children); +NOBDEF bool nob_read_entire_dir_recursively(const char *parent, Nob_File_Paths *children); +NOBDEF bool nob_read_entire_dir_recursively_wildcard(const char *parent, const char *pattern, Nob_File_Paths *children); NOBDEF bool nob_write_entire_file(const char *path, const void *data, size_t size); NOBDEF Nob_File_Type nob_get_file_type(const char *path); NOBDEF bool nob_delete_file(const char *path); @@ -1567,6 +1571,111 @@ NOBDEF bool nob_read_entire_dir(const char *parent, Nob_File_Paths *children) return result; } +NOBDEF bool nob_read_entire_dir_recursively(const char *parent, Nob_File_Paths *children) +{ + bool result = true; + DIR *dir = NULL; + + dir = opendir(parent); + if (dir == NULL) { + #ifdef _WIN32 + nob_log(NOB_ERROR, "Could not open directory %s: %s", parent, nob_win32_error_message(GetLastError())); + #else + nob_log(NOB_ERROR, "Could not open directory %s: %s", parent, strerror(errno)); + #endif // _WIN32 + nob_return_defer(false); + } + + errno = 0; + struct dirent *ent = readdir(dir); + char *path = NULL; + while (ent != NULL) { + if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) { + goto next_entry; + } + #ifdef _WIN32 + if (!strcmp(parent, ".") || !strcmp(parent, ".\\")) { + path = nob_temp_strdup(ent->d_name); + } else { + path = nob_temp_sprintf("%s\\%s", parent, ent->d_name); + } + #else + if (!strcmp(parent, ".") || !strcmp(parent, "./")) { + path = nob_temp_strdup(ent->d_name); + } else { + path = nob_temp_sprintf("%s/%s", parent, ent->d_name); + } + #endif + switch(nob_get_file_type(path)) { + case NOB_FILE_REGULAR: + nob_da_append(children, path); + break; + case NOB_FILE_DIRECTORY: + result = nob_read_entire_dir_recursively(path, children); + break; + case NOB_FILE_SYMLINK: + case NOB_FILE_OTHER: + break; + default: + NOB_UNREACHABLE("nob_read_entire_dir_recursively"); + } + next_entry: + ent = readdir(dir); + } + + if (errno != 0) { + #ifdef _WIN32 + nob_log(NOB_ERROR, "Could not read directory %s: %s", parent, nob_win32_error_message(GetLastError())); + #else + nob_log(NOB_ERROR, "Could not read directory %s: %s", parent, strerror(errno)); + #endif // _WIN32 + nob_return_defer(false); + } + +defer: + if (dir) closedir(dir); + return result; +} + +NOBDEF bool nob_read_entire_dir_recursively_wildcard(const char *parent, const char *pattern, Nob_File_Paths *children) +{ + Nob_File_Paths paths = {0}; + bool result = true; + + if (!nob_read_entire_dir_recursively(parent, &paths)) { + nob_return_defer(false); + } + #ifdef _WIN32 + // https://learn.microsoft.com/en-us/windows/win32/api/shlwapi/nf-shlwapi-pathmatchspecw + // https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/mbstowcs-s-mbstowcs-s-l + wchar_t pszFile[MAX_PATH], pszSpec[MAX_PATH]; + size_t pszFileSize, pszSpecSize; + if (mbstowcs_s(&pszSpecSize, pszSpec, MAX_PATH, pattern, strlen(pattern) + 1)) { + nob_log(NOB_ERROR, "Could not converts multibyte characters to wide characters", parent, nob_win32_error_message(GetLastError())); + nob_return_defer(false); + } + nob_da_foreach(const char *, path, &paths) { + if (mbstowcs_s(&pszFileSize, pszFile, MAX_PATH, *path, strlen(*path) + 1)) { + nob_log(NOB_ERROR, "Could not converts multibyte characters to wide characters", parent, nob_win32_error_message(GetLastError())); + continue; + } + if (PathMatchSpecW((LPCWSTR)pszFile, (LPCWSTR)pszSpec) { + nob_da_append(children, *path); + } + } + #else + nob_da_foreach(const char *, path, &paths) { + if(!fnmatch(pattern, *path, FNM_PATHNAME)) { + nob_da_append(children, *path); + } + } + #endif + +defer: + nob_da_free(paths); + return result; +} + NOBDEF bool nob_write_entire_file(const char *path, const void *data, size_t size) { bool result = true; @@ -2224,6 +2333,8 @@ NOBDEF int closedir(DIR *dirp) #define copy_file nob_copy_file #define copy_directory_recursively nob_copy_directory_recursively #define read_entire_dir nob_read_entire_dir + #define read_entire_dir_recursively nob_read_entire_dir_recursively + #define read_entire_dir_recursively_wildcard nob_read_entire_dir_recursively_wildcard #define write_entire_file nob_write_entire_file #define get_file_type nob_get_file_type #define delete_file nob_delete_file diff --git a/tests/read_entire_dir_recursively.c b/tests/read_entire_dir_recursively.c new file mode 100644 index 0000000..fd209b1 --- /dev/null +++ b/tests/read_entire_dir_recursively.c @@ -0,0 +1,59 @@ +#include "shared.h" +#define NOB_IMPLEMENTATION +#define NOB_STRIP_PREFIX +#include "nob.h" + +int main(void) +{ + // build/tests/read_entire_dir_recursively.cwd + // ├── external + // │ ├── foobar + // │ │ ├── foobar.h + // │ │ ├── libfoobar.a + // │ │ └── libfoobar.so + // │ └── foobarbaz + // │ ├── foobarbaz.h + // │ ├── libfoobarbaz.a + // │ └── libfoobarbaz.so + // ├── include + // │ ├── bar + // │ │ └── bar.c + // │ ├── baz.c + // │ └── foo + // │ └── foo.c + // └── src + // ├── bar + // │ └── bar.c + // ├── baz.c + // └── foo + // └── foo.c + if (!mkdir_if_not_exists("src")) return 1; + if (!mkdir_if_not_exists("src/foo")) return 1; + if (!mkdir_if_not_exists("src/bar")) return 1; + if (!mkdir_if_not_exists("include")) return 1; + if (!mkdir_if_not_exists("include/foo")) return 1; + if (!mkdir_if_not_exists("include/bar")) return 1; + if (!mkdir_if_not_exists("external")) return 1; + if (!mkdir_if_not_exists("external/foobar")) return 1; + if (!mkdir_if_not_exists("external/foobarbaz")) return 1; + if (!write_entire_file("src/foo/foo.c", NULL, 0)) return 1; + if (!write_entire_file("src/bar/bar.c", NULL, 0)) return 1; + if (!write_entire_file("src/baz.c", NULL, 0)) return 1; + if (!write_entire_file("include/foo/foo.c", NULL, 0)) return 1; + if (!write_entire_file("include/bar/bar.c", NULL, 0)) return 1; + if (!write_entire_file("include/baz.c", NULL, 0)) return 1; + if (!write_entire_file("external/foobar/foobar.h", NULL, 0)) return 1; + if (!write_entire_file("external/foobar/libfoobar.a", NULL, 0)) return 1; + if (!write_entire_file("external/foobar/libfoobar.so", NULL, 0)) return 1; + if (!write_entire_file("external/foobarbaz/foobarbaz.h", NULL, 0)) return 1; + if (!write_entire_file("external/foobarbaz/libfoobarbaz.a", NULL, 0)) return 1; + if (!write_entire_file("external/foobarbaz/libfoobarbaz.so", NULL, 0)) return 1; + + File_Paths children = {0}; + if (!read_entire_dir_recursively(".", &children)) return 1; + nob_log(INFO, "Tests:"); + for (size_t i = 0; i < children.count; ++i) { + nob_log(INFO, " %s", children.items[i]); + } + return 0; +} diff --git a/tests/read_entire_dir_recursively_wildcard.c b/tests/read_entire_dir_recursively_wildcard.c new file mode 100644 index 0000000..9c44939 --- /dev/null +++ b/tests/read_entire_dir_recursively_wildcard.c @@ -0,0 +1,78 @@ +#include "shared.h" +#define NOB_IMPLEMENTATION +#define NOB_STRIP_PREFIX +#include "nob.h" + +int main(void) +{ + // build/tests/read_entire_dir_recursively_wildcard.cwd + // ├── external + // │ ├── foobar + // │ │ ├── foobar.h + // │ │ ├── libfoobar.a + // │ │ └── libfoobar.so + // │ └── foobarbaz + // │ ├── foobarbaz.h + // │ ├── libfoobarbaz.a + // │ └── libfoobarbaz.so + // ├── include + // │ ├── bar + // │ │ └── bar.c + // │ ├── baz.c + // │ └── foo + // │ └── foo.c + // └── src + // ├── bar + // │ └── bar.c + // ├── baz.c + // └── foo + // └── foo.c + if (!mkdir_if_not_exists("src")) return 1; + if (!mkdir_if_not_exists("src/foo")) return 1; + if (!mkdir_if_not_exists("src/bar")) return 1; + if (!mkdir_if_not_exists("include")) return 1; + if (!mkdir_if_not_exists("include/foo")) return 1; + if (!mkdir_if_not_exists("include/bar")) return 1; + if (!mkdir_if_not_exists("external")) return 1; + if (!mkdir_if_not_exists("external/foobar")) return 1; + if (!mkdir_if_not_exists("external/foobarbaz")) return 1; + if (!write_entire_file("src/foo/foo.c", NULL, 0)) return 1; + if (!write_entire_file("src/bar/bar.c", NULL, 0)) return 1; + if (!write_entire_file("src/baz.c", NULL, 0)) return 1; + if (!write_entire_file("include/foo/foo.c", NULL, 0)) return 1; + if (!write_entire_file("include/bar/bar.c", NULL, 0)) return 1; + if (!write_entire_file("include/baz.c", NULL, 0)) return 1; + if (!write_entire_file("external/foobar/foobar.h", NULL, 0)) return 1; + if (!write_entire_file("external/foobar/libfoobar.a", NULL, 0)) return 1; + if (!write_entire_file("external/foobar/libfoobar.so", NULL, 0)) return 1; + if (!write_entire_file("external/foobarbaz/foobarbaz.h", NULL, 0)) return 1; + if (!write_entire_file("external/foobarbaz/libfoobarbaz.a", NULL, 0)) return 1; + if (!write_entire_file("external/foobarbaz/libfoobarbaz.so", NULL, 0)) return 1; + + File_Paths srcs = {0}, headers = {0}, libs = {0}, dlls = {0}; + if (!read_entire_dir_recursively_wildcard(".", "src/*.c", &srcs)) return 1; + if (!read_entire_dir_recursively_wildcard(".", "src/**/*.c", &srcs)) return 1; + if (!read_entire_dir_recursively_wildcard(".", "include/*.h", &headers)) return 1; + if (!read_entire_dir_recursively_wildcard(".", "include/**/*.h", &headers)) return 1; + if (!read_entire_dir_recursively_wildcard(".", "external/**/*.h", &headers)) return 1; + if (!read_entire_dir_recursively_wildcard(".", "external/**/*.a", &libs)) return 1; + if (!read_entire_dir_recursively_wildcard(".", "external/**/*.so", &dlls)) return 1; + nob_log(INFO, "Tests:"); + nob_log(INFO, "srcs:"); + da_foreach(const char *, src, &srcs) { + nob_log(INFO, " %s", *src); + } + nob_log(INFO, "headers:"); + da_foreach(const char *, header, &headers) { + nob_log(INFO, " %s", *header); + } + nob_log(INFO, "libs:"); + da_foreach(const char *, lib, &libs) { + nob_log(INFO, " %s", *lib); + } + nob_log(INFO, "dlls:"); + da_foreach(const char *, dll, &dlls) { + nob_log(INFO, " %s", *dll); + } + return 0; +} From 71006ba8aca4ab2d4f3202e39013c8f605cfe09f Mon Sep 17 00:00:00 2001 From: gaogao-qwq Date: Mon, 25 Aug 2025 11:57:31 +0800 Subject: [PATCH 2/2] Make `.recursively` & `.wildcard` optional parameters for `nob_read_entire_dir()` Signed-off-by: gaogao-qwq --- nob.c | 2 - nob.h | 41 +++++--- tests/read_entire_dir.c | 98 ++++++++++++++++++-- tests/read_entire_dir_recursively.c | 59 ------------ tests/read_entire_dir_recursively_wildcard.c | 78 ---------------- 5 files changed, 119 insertions(+), 159 deletions(-) delete mode 100644 tests/read_entire_dir_recursively.c delete mode 100644 tests/read_entire_dir_recursively_wildcard.c diff --git a/nob.c b/nob.c index 78bfba1..a580562 100644 --- a/nob.c +++ b/nob.c @@ -17,8 +17,6 @@ const char *test_names[] = { "win32_error", #endif //_WIN32 "read_entire_dir", - "read_entire_dir_recursively", - "read_entire_dir_recursively_wildcard", "da_resize", "da_last", "da_remove_unordered", diff --git a/nob.h b/nob.h index c097165..de702a6 100644 --- a/nob.h +++ b/nob.h @@ -229,16 +229,24 @@ typedef enum { NOB_FILE_OTHER, } Nob_File_Type; +// Options for nob_read_entire_dir_opt() function. +typedef struct { + // Read dir recursively + bool recursively; + // Match recursively retrieved results by belowed wildcard string + const char *wildcard; +} Nob_Read_Entire_Dir_Opt; + NOBDEF bool nob_mkdir_if_not_exists(const char *path); NOBDEF bool nob_copy_file(const char *src_path, const char *dst_path); NOBDEF bool nob_copy_directory_recursively(const char *src_path, const char *dst_path); -NOBDEF bool nob_read_entire_dir(const char *parent, Nob_File_Paths *children); -NOBDEF bool nob_read_entire_dir_recursively(const char *parent, Nob_File_Paths *children); -NOBDEF bool nob_read_entire_dir_recursively_wildcard(const char *parent, const char *pattern, Nob_File_Paths *children); +NOBDEF bool nob_read_entire_dir_opt(const char *parent, Nob_File_Paths *children, Nob_Read_Entire_Dir_Opt opt); NOBDEF bool nob_write_entire_file(const char *path, const void *data, size_t size); NOBDEF Nob_File_Type nob_get_file_type(const char *path); NOBDEF bool nob_delete_file(const char *path); +// Same as nob_read_entire_dir_opt but using cool variadic macro to set the default options. +#define nob_read_entire_dir(parent, children, ...) nob_read_entire_dir_opt((parent), (children), (Nob_Read_Entire_Dir_Opt){__VA_ARGS__}) #define nob_return_defer(value) do { result = (value); goto defer; } while(0) // Initial capacity of a dynamic array @@ -1534,7 +1542,7 @@ NOBDEF void nob_log(Nob_Log_Level level, const char *fmt, ...) fprintf(stderr, "\n"); } -NOBDEF bool nob_read_entire_dir(const char *parent, Nob_File_Paths *children) +NOBDEF bool nob__read_entire_dir(const char *parent, Nob_File_Paths *children) { bool result = true; DIR *dir = NULL; @@ -1571,7 +1579,7 @@ NOBDEF bool nob_read_entire_dir(const char *parent, Nob_File_Paths *children) return result; } -NOBDEF bool nob_read_entire_dir_recursively(const char *parent, Nob_File_Paths *children) +NOBDEF bool nob__read_entire_dir_recursively(const char *parent, Nob_File_Paths *children) { bool result = true; DIR *dir = NULL; @@ -1611,7 +1619,7 @@ NOBDEF bool nob_read_entire_dir_recursively(const char *parent, Nob_File_Paths * nob_da_append(children, path); break; case NOB_FILE_DIRECTORY: - result = nob_read_entire_dir_recursively(path, children); + result = result && nob__read_entire_dir_recursively(path, children); break; case NOB_FILE_SYMLINK: case NOB_FILE_OTHER: @@ -1637,12 +1645,12 @@ NOBDEF bool nob_read_entire_dir_recursively(const char *parent, Nob_File_Paths * return result; } -NOBDEF bool nob_read_entire_dir_recursively_wildcard(const char *parent, const char *pattern, Nob_File_Paths *children) +NOBDEF bool nob__read_entire_dir_wildcard(const char *parent, Nob_File_Paths *children, const char *pattern) { Nob_File_Paths paths = {0}; bool result = true; - if (!nob_read_entire_dir_recursively(parent, &paths)) { + if (!nob__read_entire_dir_recursively(parent, &paths)) { nob_return_defer(false); } #ifdef _WIN32 @@ -1659,7 +1667,7 @@ NOBDEF bool nob_read_entire_dir_recursively_wildcard(const char *parent, const c nob_log(NOB_ERROR, "Could not converts multibyte characters to wide characters", parent, nob_win32_error_message(GetLastError())); continue; } - if (PathMatchSpecW((LPCWSTR)pszFile, (LPCWSTR)pszSpec) { + if (PathMatchSpecW((LPCWSTR)pszFile, (LPCWSTR)pszSpec)) { nob_da_append(children, *path); } } @@ -1676,6 +1684,17 @@ NOBDEF bool nob_read_entire_dir_recursively_wildcard(const char *parent, const c return result; } +NOBDEF bool nob_read_entire_dir_opt(const char *parent, Nob_File_Paths *children, Nob_Read_Entire_Dir_Opt opt) +{ + if (opt.wildcard) { + return nob__read_entire_dir_wildcard(parent, children, opt.wildcard); + } + if (opt.recursively) { + return nob__read_entire_dir_recursively(parent, children); + } + return nob__read_entire_dir(parent, children); +} + NOBDEF bool nob_write_entire_file(const char *path, const void *data, size_t size) { bool result = true; @@ -2332,9 +2351,9 @@ NOBDEF int closedir(DIR *dirp) #define mkdir_if_not_exists nob_mkdir_if_not_exists #define copy_file nob_copy_file #define copy_directory_recursively nob_copy_directory_recursively + #define Read_Entire_Dir_Opt Nob_Read_Entire_Dir_Opt + #define read_entire_dir_opt nob_read_entire_dir_opt #define read_entire_dir nob_read_entire_dir - #define read_entire_dir_recursively nob_read_entire_dir_recursively - #define read_entire_dir_recursively_wildcard nob_read_entire_dir_recursively_wildcard #define write_entire_file nob_write_entire_file #define get_file_type nob_get_file_type #define delete_file nob_delete_file diff --git a/tests/read_entire_dir.c b/tests/read_entire_dir.c index 7483d86..cfc82d9 100644 --- a/tests/read_entire_dir.c +++ b/tests/read_entire_dir.c @@ -3,19 +3,99 @@ #define NOB_STRIP_PREFIX #include "nob.h" +bool mktestdir() { + // build/tests/read_entire_dir.cwd + // ├── external + // │ ├── foobar + // │ │ ├── foobar.h + // │ │ ├── libfoobar.a + // │ │ └── libfoobar.so + // │ └── foobarbaz + // │ ├── foobarbaz.h + // │ ├── libfoobarbaz.a + // │ └── libfoobarbaz.so + // ├── include + // │ ├── bar + // │ │ └── bar.h + // │ ├── baz.h + // │ └── foo + // │ └── foo.h + // └── src + // ├── bar + // │ └── bar.c + // ├── baz.c + // └── foo + // └── foo.c + return mkdir_if_not_exists("src") + && mkdir_if_not_exists("src/foo") + && mkdir_if_not_exists("src/bar") + && mkdir_if_not_exists("include") + && mkdir_if_not_exists("include/foo") + && mkdir_if_not_exists("include/bar") + && mkdir_if_not_exists("external") + && mkdir_if_not_exists("external/foobar") + && mkdir_if_not_exists("external/foobarbaz") + && write_entire_file("src/foo/foo.c", NULL, 0) + && write_entire_file("src/bar/bar.c", NULL, 0) + && write_entire_file("src/baz.c", NULL, 0) + && write_entire_file("include/foo/foo.h", NULL, 0) + && write_entire_file("include/bar/bar.h", NULL, 0) + && write_entire_file("include/baz.h", NULL, 0) + && write_entire_file("external/foobar/foobar.h", NULL, 0) + && write_entire_file("external/foobar/libfoobar.a", NULL, 0) + && write_entire_file("external/foobar/libfoobar.so", NULL, 0) + && write_entire_file("external/foobarbaz/foobarbaz.h", NULL, 0) + && write_entire_file("external/foobarbaz/libfoobarbaz.a", NULL, 0) + && write_entire_file("external/foobarbaz/libfoobarbaz.so", NULL, 0); +} + int main(void) { - if (!write_entire_file("foo.txt", NULL, 0)) return 1; - if (!write_entire_file("bar.txt", NULL, 0)) return 1; - if (!write_entire_file("baz.txt", NULL, 0)) return 1; - + // Test nob_read_entire_dir() + if (!mktestdir()) return 1; Nob_File_Paths children = {0}; - if (!nob_read_entire_dir(".", &children)) return 1; - nob_log(INFO, "Tests:"); - for (size_t i = 0; i < children.count; ++i) { - if (*children.items[i] != '.') { - nob_log(INFO, " %s", children.items[i]); + if (!read_entire_dir(".", &children)) return 1; + nob_log(INFO, "read_entire_dir():"); + da_foreach(const char *, child, &children) { + if (**child != '.') { + nob_log(INFO, " %s", *child); } } + + // Test nob_read_entire_dir() with recursively option + nob_da_free(children); + children = (Nob_File_Paths){0}; + if (!read_entire_dir(".", &children, .recursively = true)) return 1; + nob_log(INFO, "read_entire_dir() recursively:"); + da_foreach(const char *, child, &children) { + nob_log(INFO, " %s", *child); + } + + // Test nob_read_entire_dir() with wildcard option + File_Paths srcs = {0}, headers = {0}, libs = {0}, dlls = {0}; + if (!read_entire_dir(".", &srcs, .wildcard = "src/*.c")) return 1; + if (!read_entire_dir(".", &srcs, .wildcard = "src/**/*.c")) return 1; + if (!read_entire_dir(".", &headers, .wildcard = "include/*.h")) return 1; + if (!read_entire_dir(".", &headers, .wildcard = "include/**/*.h")) return 1; + if (!read_entire_dir(".", &headers, .wildcard = "external/**/*.h")) return 1; + if (!read_entire_dir(".", &libs, .wildcard = "external/**/*.a")) return 1; + if (!read_entire_dir(".", &dlls, .wildcard = "external/**/*.so")) return 1; + nob_log(INFO, "read_entire_dir() wildcard:"); + nob_log(INFO, "srcs:"); + da_foreach(const char *, src, &srcs) { + nob_log(INFO, " %s", *src); + } + nob_log(INFO, "headers:"); + da_foreach(const char *, header, &headers) { + nob_log(INFO, " %s", *header); + } + nob_log(INFO, "libs:"); + da_foreach(const char *, lib, &libs) { + nob_log(INFO, " %s", *lib); + } + nob_log(INFO, "dlls:"); + da_foreach(const char *, dll, &dlls) { + nob_log(INFO, " %s", *dll); + } return 0; } diff --git a/tests/read_entire_dir_recursively.c b/tests/read_entire_dir_recursively.c deleted file mode 100644 index fd209b1..0000000 --- a/tests/read_entire_dir_recursively.c +++ /dev/null @@ -1,59 +0,0 @@ -#include "shared.h" -#define NOB_IMPLEMENTATION -#define NOB_STRIP_PREFIX -#include "nob.h" - -int main(void) -{ - // build/tests/read_entire_dir_recursively.cwd - // ├── external - // │ ├── foobar - // │ │ ├── foobar.h - // │ │ ├── libfoobar.a - // │ │ └── libfoobar.so - // │ └── foobarbaz - // │ ├── foobarbaz.h - // │ ├── libfoobarbaz.a - // │ └── libfoobarbaz.so - // ├── include - // │ ├── bar - // │ │ └── bar.c - // │ ├── baz.c - // │ └── foo - // │ └── foo.c - // └── src - // ├── bar - // │ └── bar.c - // ├── baz.c - // └── foo - // └── foo.c - if (!mkdir_if_not_exists("src")) return 1; - if (!mkdir_if_not_exists("src/foo")) return 1; - if (!mkdir_if_not_exists("src/bar")) return 1; - if (!mkdir_if_not_exists("include")) return 1; - if (!mkdir_if_not_exists("include/foo")) return 1; - if (!mkdir_if_not_exists("include/bar")) return 1; - if (!mkdir_if_not_exists("external")) return 1; - if (!mkdir_if_not_exists("external/foobar")) return 1; - if (!mkdir_if_not_exists("external/foobarbaz")) return 1; - if (!write_entire_file("src/foo/foo.c", NULL, 0)) return 1; - if (!write_entire_file("src/bar/bar.c", NULL, 0)) return 1; - if (!write_entire_file("src/baz.c", NULL, 0)) return 1; - if (!write_entire_file("include/foo/foo.c", NULL, 0)) return 1; - if (!write_entire_file("include/bar/bar.c", NULL, 0)) return 1; - if (!write_entire_file("include/baz.c", NULL, 0)) return 1; - if (!write_entire_file("external/foobar/foobar.h", NULL, 0)) return 1; - if (!write_entire_file("external/foobar/libfoobar.a", NULL, 0)) return 1; - if (!write_entire_file("external/foobar/libfoobar.so", NULL, 0)) return 1; - if (!write_entire_file("external/foobarbaz/foobarbaz.h", NULL, 0)) return 1; - if (!write_entire_file("external/foobarbaz/libfoobarbaz.a", NULL, 0)) return 1; - if (!write_entire_file("external/foobarbaz/libfoobarbaz.so", NULL, 0)) return 1; - - File_Paths children = {0}; - if (!read_entire_dir_recursively(".", &children)) return 1; - nob_log(INFO, "Tests:"); - for (size_t i = 0; i < children.count; ++i) { - nob_log(INFO, " %s", children.items[i]); - } - return 0; -} diff --git a/tests/read_entire_dir_recursively_wildcard.c b/tests/read_entire_dir_recursively_wildcard.c deleted file mode 100644 index 9c44939..0000000 --- a/tests/read_entire_dir_recursively_wildcard.c +++ /dev/null @@ -1,78 +0,0 @@ -#include "shared.h" -#define NOB_IMPLEMENTATION -#define NOB_STRIP_PREFIX -#include "nob.h" - -int main(void) -{ - // build/tests/read_entire_dir_recursively_wildcard.cwd - // ├── external - // │ ├── foobar - // │ │ ├── foobar.h - // │ │ ├── libfoobar.a - // │ │ └── libfoobar.so - // │ └── foobarbaz - // │ ├── foobarbaz.h - // │ ├── libfoobarbaz.a - // │ └── libfoobarbaz.so - // ├── include - // │ ├── bar - // │ │ └── bar.c - // │ ├── baz.c - // │ └── foo - // │ └── foo.c - // └── src - // ├── bar - // │ └── bar.c - // ├── baz.c - // └── foo - // └── foo.c - if (!mkdir_if_not_exists("src")) return 1; - if (!mkdir_if_not_exists("src/foo")) return 1; - if (!mkdir_if_not_exists("src/bar")) return 1; - if (!mkdir_if_not_exists("include")) return 1; - if (!mkdir_if_not_exists("include/foo")) return 1; - if (!mkdir_if_not_exists("include/bar")) return 1; - if (!mkdir_if_not_exists("external")) return 1; - if (!mkdir_if_not_exists("external/foobar")) return 1; - if (!mkdir_if_not_exists("external/foobarbaz")) return 1; - if (!write_entire_file("src/foo/foo.c", NULL, 0)) return 1; - if (!write_entire_file("src/bar/bar.c", NULL, 0)) return 1; - if (!write_entire_file("src/baz.c", NULL, 0)) return 1; - if (!write_entire_file("include/foo/foo.c", NULL, 0)) return 1; - if (!write_entire_file("include/bar/bar.c", NULL, 0)) return 1; - if (!write_entire_file("include/baz.c", NULL, 0)) return 1; - if (!write_entire_file("external/foobar/foobar.h", NULL, 0)) return 1; - if (!write_entire_file("external/foobar/libfoobar.a", NULL, 0)) return 1; - if (!write_entire_file("external/foobar/libfoobar.so", NULL, 0)) return 1; - if (!write_entire_file("external/foobarbaz/foobarbaz.h", NULL, 0)) return 1; - if (!write_entire_file("external/foobarbaz/libfoobarbaz.a", NULL, 0)) return 1; - if (!write_entire_file("external/foobarbaz/libfoobarbaz.so", NULL, 0)) return 1; - - File_Paths srcs = {0}, headers = {0}, libs = {0}, dlls = {0}; - if (!read_entire_dir_recursively_wildcard(".", "src/*.c", &srcs)) return 1; - if (!read_entire_dir_recursively_wildcard(".", "src/**/*.c", &srcs)) return 1; - if (!read_entire_dir_recursively_wildcard(".", "include/*.h", &headers)) return 1; - if (!read_entire_dir_recursively_wildcard(".", "include/**/*.h", &headers)) return 1; - if (!read_entire_dir_recursively_wildcard(".", "external/**/*.h", &headers)) return 1; - if (!read_entire_dir_recursively_wildcard(".", "external/**/*.a", &libs)) return 1; - if (!read_entire_dir_recursively_wildcard(".", "external/**/*.so", &dlls)) return 1; - nob_log(INFO, "Tests:"); - nob_log(INFO, "srcs:"); - da_foreach(const char *, src, &srcs) { - nob_log(INFO, " %s", *src); - } - nob_log(INFO, "headers:"); - da_foreach(const char *, header, &headers) { - nob_log(INFO, " %s", *header); - } - nob_log(INFO, "libs:"); - da_foreach(const char *, lib, &libs) { - nob_log(INFO, " %s", *lib); - } - nob_log(INFO, "dlls:"); - da_foreach(const char *, dll, &dlls) { - nob_log(INFO, " %s", *dll); - } - return 0; -}