Skip to content

Commit 5f0e4cf

Browse files
committed
Make .recursively & .wildcard optional parameters for nob_read_entire_dir()
Signed-off-by: gaogao-qwq <gaogaoqwq@gmail.com>
1 parent 3851c4a commit 5f0e4cf

File tree

5 files changed

+118
-158
lines changed

5 files changed

+118
-158
lines changed

nob.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ const char *test_names[] = {
1717
"win32_error",
1818
#endif //_WIN32
1919
"read_entire_dir",
20-
"read_entire_dir_recursively",
21-
"read_entire_dir_recursively_wildcard",
2220
"da_resize",
2321
"da_last",
2422
"da_remove_unordered",

nob.h

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -229,16 +229,24 @@ typedef enum {
229229
NOB_FILE_OTHER,
230230
} Nob_File_Type;
231231

232+
// Options for nob_read_entire_dir_opt() function.
233+
typedef struct {
234+
// Read dir recursively
235+
bool recursively;
236+
// Match recursively retrieved results by belowed wildcard string
237+
const char *wildcard;
238+
} Nob_Read_Entire_Dir_Opt;
239+
232240
NOBDEF bool nob_mkdir_if_not_exists(const char *path);
233241
NOBDEF bool nob_copy_file(const char *src_path, const char *dst_path);
234242
NOBDEF bool nob_copy_directory_recursively(const char *src_path, const char *dst_path);
235-
NOBDEF bool nob_read_entire_dir(const char *parent, Nob_File_Paths *children);
236-
NOBDEF bool nob_read_entire_dir_recursively(const char *parent, Nob_File_Paths *children);
237-
NOBDEF bool nob_read_entire_dir_recursively_wildcard(const char *parent, const char *pattern, Nob_File_Paths *children);
243+
NOBDEF bool nob_read_entire_dir_opt(const char *parent, Nob_File_Paths *children, Nob_Read_Entire_Dir_Opt opt);
238244
NOBDEF bool nob_write_entire_file(const char *path, const void *data, size_t size);
239245
NOBDEF Nob_File_Type nob_get_file_type(const char *path);
240246
NOBDEF bool nob_delete_file(const char *path);
241247

248+
// Same as nob_read_entire_dir_opt but using cool variadic macro to set the default options.
249+
#define nob_read_entire_dir(parent, children, ...) nob_read_entire_dir_opt((parent), (children), (Nob_Read_Entire_Dir_Opt){__VA_ARGS__})
242250
#define nob_return_defer(value) do { result = (value); goto defer; } while(0)
243251

244252
// Initial capacity of a dynamic array
@@ -1534,7 +1542,7 @@ NOBDEF void nob_log(Nob_Log_Level level, const char *fmt, ...)
15341542
fprintf(stderr, "\n");
15351543
}
15361544

1537-
NOBDEF bool nob_read_entire_dir(const char *parent, Nob_File_Paths *children)
1545+
NOBDEF bool nob__read_entire_dir(const char *parent, Nob_File_Paths *children)
15381546
{
15391547
bool result = true;
15401548
DIR *dir = NULL;
@@ -1571,7 +1579,7 @@ NOBDEF bool nob_read_entire_dir(const char *parent, Nob_File_Paths *children)
15711579
return result;
15721580
}
15731581

1574-
NOBDEF bool nob_read_entire_dir_recursively(const char *parent, Nob_File_Paths *children)
1582+
NOBDEF bool nob__read_entire_dir_recursively(const char *parent, Nob_File_Paths *children)
15751583
{
15761584
bool result = true;
15771585
DIR *dir = NULL;
@@ -1611,7 +1619,7 @@ NOBDEF bool nob_read_entire_dir_recursively(const char *parent, Nob_File_Paths *
16111619
nob_da_append(children, path);
16121620
break;
16131621
case NOB_FILE_DIRECTORY:
1614-
result = nob_read_entire_dir_recursively(path, children);
1622+
result = nob__read_entire_dir_recursively(path, children);
16151623
break;
16161624
case NOB_FILE_SYMLINK:
16171625
case NOB_FILE_OTHER:
@@ -1637,12 +1645,12 @@ NOBDEF bool nob_read_entire_dir_recursively(const char *parent, Nob_File_Paths *
16371645
return result;
16381646
}
16391647

1640-
NOBDEF bool nob_read_entire_dir_recursively_wildcard(const char *parent, const char *pattern, Nob_File_Paths *children)
1648+
NOBDEF bool nob__read_entire_dir_wildcard(const char *parent, Nob_File_Paths *children, const char *pattern)
16411649
{
16421650
Nob_File_Paths paths = {0};
16431651
bool result = true;
16441652

1645-
if (!nob_read_entire_dir_recursively(parent, &paths)) {
1653+
if (!nob__read_entire_dir_recursively(parent, &paths)) {
16461654
nob_return_defer(false);
16471655
}
16481656
#ifdef _WIN32
@@ -1676,6 +1684,17 @@ NOBDEF bool nob_read_entire_dir_recursively_wildcard(const char *parent, const c
16761684
return result;
16771685
}
16781686

1687+
NOBDEF bool nob_read_entire_dir_opt(const char *parent, Nob_File_Paths *children, Nob_Read_Entire_Dir_Opt opt)
1688+
{
1689+
if (opt.wildcard) {
1690+
return nob__read_entire_dir_wildcard(parent, children, opt.wildcard);
1691+
}
1692+
if (opt.recursively) {
1693+
return nob__read_entire_dir_recursively(parent, children);
1694+
}
1695+
return nob__read_entire_dir(parent, children);
1696+
}
1697+
16791698
NOBDEF bool nob_write_entire_file(const char *path, const void *data, size_t size)
16801699
{
16811700
bool result = true;
@@ -2332,9 +2351,9 @@ NOBDEF int closedir(DIR *dirp)
23322351
#define mkdir_if_not_exists nob_mkdir_if_not_exists
23332352
#define copy_file nob_copy_file
23342353
#define copy_directory_recursively nob_copy_directory_recursively
2354+
#define Read_Entire_Dir_Opt Nob_Read_Entire_Dir_Opt
2355+
#define read_entire_dir_opt nob_read_entire_dir_opt
23352356
#define read_entire_dir nob_read_entire_dir
2336-
#define read_entire_dir_recursively nob_read_entire_dir_recursively
2337-
#define read_entire_dir_recursively_wildcard nob_read_entire_dir_recursively_wildcard
23382357
#define write_entire_file nob_write_entire_file
23392358
#define get_file_type nob_get_file_type
23402359
#define delete_file nob_delete_file

tests/read_entire_dir.c

Lines changed: 89 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,99 @@
33
#define NOB_STRIP_PREFIX
44
#include "nob.h"
55

6+
bool mktestdir() {
7+
// build/tests/read_entire_dir.cwd
8+
// ├── external
9+
// │ ├── foobar
10+
// │ │ ├── foobar.h
11+
// │ │ ├── libfoobar.a
12+
// │ │ └── libfoobar.so
13+
// │ └── foobarbaz
14+
// │ ├── foobarbaz.h
15+
// │ ├── libfoobarbaz.a
16+
// │ └── libfoobarbaz.so
17+
// ├── include
18+
// │ ├── bar
19+
// │ │ └── bar.h
20+
// │ ├── baz.h
21+
// │ └── foo
22+
// │ └── foo.h
23+
// └── src
24+
// ├── bar
25+
// │ └── bar.c
26+
// ├── baz.c
27+
// └── foo
28+
// └── foo.c
29+
return mkdir_if_not_exists("src")
30+
&& mkdir_if_not_exists("src/foo")
31+
&& mkdir_if_not_exists("src/bar")
32+
&& mkdir_if_not_exists("include")
33+
&& mkdir_if_not_exists("include/foo")
34+
&& mkdir_if_not_exists("include/bar")
35+
&& mkdir_if_not_exists("external")
36+
&& mkdir_if_not_exists("external/foobar")
37+
&& mkdir_if_not_exists("external/foobarbaz")
38+
&& write_entire_file("src/foo/foo.c", NULL, 0)
39+
&& write_entire_file("src/bar/bar.c", NULL, 0)
40+
&& write_entire_file("src/baz.c", NULL, 0)
41+
&& write_entire_file("include/foo/foo.h", NULL, 0)
42+
&& write_entire_file("include/bar/bar.h", NULL, 0)
43+
&& write_entire_file("include/baz.h", NULL, 0)
44+
&& write_entire_file("external/foobar/foobar.h", NULL, 0)
45+
&& write_entire_file("external/foobar/libfoobar.a", NULL, 0)
46+
&& write_entire_file("external/foobar/libfoobar.so", NULL, 0)
47+
&& write_entire_file("external/foobarbaz/foobarbaz.h", NULL, 0)
48+
&& write_entire_file("external/foobarbaz/libfoobarbaz.a", NULL, 0)
49+
&& write_entire_file("external/foobarbaz/libfoobarbaz.so", NULL, 0);
50+
}
51+
652
int main(void)
753
{
8-
if (!write_entire_file("foo.txt", NULL, 0)) return 1;
9-
if (!write_entire_file("bar.txt", NULL, 0)) return 1;
10-
if (!write_entire_file("baz.txt", NULL, 0)) return 1;
11-
54+
// Test nob_read_entire_dir()
55+
if (!mktestdir()) return 1;
1256
Nob_File_Paths children = {0};
13-
if (!nob_read_entire_dir(".", &children)) return 1;
14-
nob_log(INFO, "Tests:");
15-
for (size_t i = 0; i < children.count; ++i) {
16-
if (*children.items[i] != '.') {
17-
nob_log(INFO, " %s", children.items[i]);
57+
if (!read_entire_dir(".", &children)) return 1;
58+
nob_log(INFO, "read_entire_dir():");
59+
da_foreach(const char *, child, &children) {
60+
if (**child != '.') {
61+
nob_log(INFO, " %s", *child);
1862
}
1963
}
64+
65+
// Test nob_read_entire_dir() with recursively option
66+
nob_da_free(children);
67+
children = (Nob_File_Paths){0};
68+
if (!read_entire_dir(".", &children, .recursively = true)) return 1;
69+
nob_log(INFO, "read_entire_dir() recursively:");
70+
da_foreach(const char *, child, &children) {
71+
nob_log(INFO, " %s", *child);
72+
}
73+
74+
// Test nob_read_entire_dir() with wildcard option
75+
File_Paths srcs = {0}, headers = {0}, libs = {0}, dlls = {0};
76+
if (!read_entire_dir(".", &srcs, .wildcard = "src/*.c")) return 1;
77+
if (!read_entire_dir(".", &srcs, .wildcard = "src/**/*.c")) return 1;
78+
if (!read_entire_dir(".", &headers, .wildcard = "include/*.h")) return 1;
79+
if (!read_entire_dir(".", &headers, .wildcard = "include/**/*.h")) return 1;
80+
if (!read_entire_dir(".", &headers, .wildcard = "external/**/*.h")) return 1;
81+
if (!read_entire_dir(".", &libs, .wildcard = "external/**/*.a")) return 1;
82+
if (!read_entire_dir(".", &dlls, .wildcard = "external/**/*.so")) return 1;
83+
nob_log(INFO, "read_entire_dir() wildcard:");
84+
nob_log(INFO, "srcs:");
85+
da_foreach(const char *, src, &srcs) {
86+
nob_log(INFO, " %s", *src);
87+
}
88+
nob_log(INFO, "headers:");
89+
da_foreach(const char *, header, &headers) {
90+
nob_log(INFO, " %s", *header);
91+
}
92+
nob_log(INFO, "libs:");
93+
da_foreach(const char *, lib, &libs) {
94+
nob_log(INFO, " %s", *lib);
95+
}
96+
nob_log(INFO, "dlls:");
97+
da_foreach(const char *, dll, &dlls) {
98+
nob_log(INFO, " %s", *dll);
99+
}
20100
return 0;
21101
}

tests/read_entire_dir_recursively.c

Lines changed: 0 additions & 59 deletions
This file was deleted.

tests/read_entire_dir_recursively_wildcard.c

Lines changed: 0 additions & 78 deletions
This file was deleted.

0 commit comments

Comments
 (0)