|
159 | 159 | # include <windows.h> |
160 | 160 | # include <direct.h> |
161 | 161 | # include <shellapi.h> |
| 162 | +# include <shlwapi.h> |
162 | 163 | #else |
163 | 164 | # include <sys/types.h> |
164 | 165 | # include <sys/wait.h> |
165 | 166 | # include <sys/stat.h> |
166 | 167 | # include <unistd.h> |
167 | 168 | # include <fcntl.h> |
| 169 | +# include <fnmatch.h> |
168 | 170 | #endif |
169 | 171 |
|
170 | 172 | #ifdef _WIN32 |
@@ -231,6 +233,8 @@ NOBDEF bool nob_mkdir_if_not_exists(const char *path); |
231 | 233 | NOBDEF bool nob_copy_file(const char *src_path, const char *dst_path); |
232 | 234 | NOBDEF bool nob_copy_directory_recursively(const char *src_path, const char *dst_path); |
233 | 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); |
234 | 238 | NOBDEF bool nob_write_entire_file(const char *path, const void *data, size_t size); |
235 | 239 | NOBDEF Nob_File_Type nob_get_file_type(const char *path); |
236 | 240 | 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) |
1567 | 1571 | return result; |
1568 | 1572 | } |
1569 | 1573 |
|
| 1574 | +NOBDEF bool nob_read_entire_dir_recursively(const char *parent, Nob_File_Paths *children) |
| 1575 | +{ |
| 1576 | + bool result = true; |
| 1577 | + DIR *dir = NULL; |
| 1578 | + |
| 1579 | + dir = opendir(parent); |
| 1580 | + if (dir == NULL) { |
| 1581 | + #ifdef _WIN32 |
| 1582 | + nob_log(NOB_ERROR, "Could not open directory %s: %s", parent, nob_win32_error_message(GetLastError())); |
| 1583 | + #else |
| 1584 | + nob_log(NOB_ERROR, "Could not open directory %s: %s", parent, strerror(errno)); |
| 1585 | + #endif // _WIN32 |
| 1586 | + nob_return_defer(false); |
| 1587 | + } |
| 1588 | + |
| 1589 | + errno = 0; |
| 1590 | + struct dirent *ent = readdir(dir); |
| 1591 | + char *path = NULL; |
| 1592 | + while (ent != NULL) { |
| 1593 | + if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) { |
| 1594 | + goto next_entry; |
| 1595 | + } |
| 1596 | + #ifdef _WIN32 |
| 1597 | + if (!strcmp(parent, ".") || !strcmp(parent, ".\\")) { |
| 1598 | + path = nob_temp_strdup(ent->d_name); |
| 1599 | + } else { |
| 1600 | + path = nob_temp_sprintf("%s\\%s", parent, ent->d_name); |
| 1601 | + } |
| 1602 | + #else |
| 1603 | + if (!strcmp(parent, ".") || !strcmp(parent, "./")) { |
| 1604 | + path = nob_temp_strdup(ent->d_name); |
| 1605 | + } else { |
| 1606 | + path = nob_temp_sprintf("%s/%s", parent, ent->d_name); |
| 1607 | + } |
| 1608 | + #endif |
| 1609 | + switch(nob_get_file_type(path)) { |
| 1610 | + case NOB_FILE_REGULAR: |
| 1611 | + nob_da_append(children, path); |
| 1612 | + break; |
| 1613 | + case NOB_FILE_DIRECTORY: |
| 1614 | + nob_read_entire_dir_recursively(path, children); |
| 1615 | + break; |
| 1616 | + case NOB_FILE_SYMLINK: |
| 1617 | + case NOB_FILE_OTHER: |
| 1618 | + break; |
| 1619 | + default: |
| 1620 | + NOB_UNREACHABLE("nob_read_entire_dir_recursively"); |
| 1621 | + } |
| 1622 | + next_entry: |
| 1623 | + ent = readdir(dir); |
| 1624 | + } |
| 1625 | + |
| 1626 | + if (errno != 0) { |
| 1627 | + #ifdef _WIN32 |
| 1628 | + nob_log(NOB_ERROR, "Could not read directory %s: %s", parent, nob_win32_error_message(GetLastError())); |
| 1629 | + #else |
| 1630 | + nob_log(NOB_ERROR, "Could not read directory %s: %s", parent, strerror(errno)); |
| 1631 | + #endif // _WIN32 |
| 1632 | + nob_return_defer(false); |
| 1633 | + } |
| 1634 | + |
| 1635 | +defer: |
| 1636 | + if (dir) closedir(dir); |
| 1637 | + return result; |
| 1638 | +} |
| 1639 | + |
| 1640 | +NOBDEF bool nob_read_entire_dir_recursively_wildcard(const char *parent, const char *pattern, Nob_File_Paths *children) |
| 1641 | +{ |
| 1642 | + Nob_File_Paths paths = {0}; |
| 1643 | + bool result = true; |
| 1644 | + |
| 1645 | + if (!nob_read_entire_dir_recursively(parent, &paths)) { |
| 1646 | + nob_return_defer(false); |
| 1647 | + } |
| 1648 | + #ifdef _WIN32 |
| 1649 | + // https://learn.microsoft.com/en-us/windows/win32/api/shlwapi/nf-shlwapi-pathmatchspecw |
| 1650 | + // https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/mbstowcs-s-mbstowcs-s-l |
| 1651 | + wchar_t pszFile[MAX_PATH], pszSpec[MAX_PATH]; |
| 1652 | + size_t pszFileSize, pszSpecSize; |
| 1653 | + if (mbstowcs_s(&pszSpecSize, pszSpec, MAX_PATH, pattern, strlen(pattern) + 1)) { |
| 1654 | + nob_log(NOB_ERROR, "Could not converts multibyte characters to wide characters", parent, nob_win32_error_message(GetLastError())); |
| 1655 | + nob_return_defer(false); |
| 1656 | + } |
| 1657 | + nob_da_foreach(const char *, path, &paths) { |
| 1658 | + if (mbstowcs_s(&pszFileSize, pszFile, MAX_PATH, *path, strlen(*path) + 1)) { |
| 1659 | + nob_log(NOB_ERROR, "Could not converts multibyte characters to wide characters", parent, nob_win32_error_message(GetLastError())); |
| 1660 | + continue; |
| 1661 | + } |
| 1662 | + if (PathMatchSpecW((LPCWSTR)pszFile, (LPCWSTR)pszSpec) { |
| 1663 | + nob_da_append(children, *path); |
| 1664 | + } |
| 1665 | + } |
| 1666 | + #else |
| 1667 | + nob_da_foreach(const char *, path, &paths) { |
| 1668 | + if(!fnmatch(pattern, *path, FNM_PATHNAME)) { |
| 1669 | + nob_da_append(children, *path); |
| 1670 | + } |
| 1671 | + } |
| 1672 | + #endif |
| 1673 | + |
| 1674 | +defer: |
| 1675 | + nob_da_free(paths); |
| 1676 | + return result; |
| 1677 | +} |
| 1678 | + |
1570 | 1679 | NOBDEF bool nob_write_entire_file(const char *path, const void *data, size_t size) |
1571 | 1680 | { |
1572 | 1681 | bool result = true; |
@@ -2224,6 +2333,8 @@ NOBDEF int closedir(DIR *dirp) |
2224 | 2333 | #define copy_file nob_copy_file |
2225 | 2334 | #define copy_directory_recursively nob_copy_directory_recursively |
2226 | 2335 | #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 |
2227 | 2338 | #define write_entire_file nob_write_entire_file |
2228 | 2339 | #define get_file_type nob_get_file_type |
2229 | 2340 | #define delete_file nob_delete_file |
|
0 commit comments