Skip to content

Commit

Permalink
Added pattern_match implementation for Windows (not tested yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
iWas-Coder committed Nov 8, 2024
1 parent 1fa8014 commit 6d1e7fa
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/carbon_fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,22 +212,55 @@ char *carbon_fs_get_bin_directory(void) {

char **carbon_fs_pattern_match(const char *pattern, usz *out_count) {
static usz i = 0;
#ifdef _WIN32
static usz counts[CARBON_FS_PATMAT_MAX_STRUCTS];
static char *results[CARBON_FS_PATMAT_MAX_STRUCTS][MAX_PATH];
HANDLE h_find;
WIN32_FIND_DATA find_data;
for (usz j = 0; j < counts[i]; ++j) free(results[i][j]);
counts[i] = 0;
h_find = FindFirstFile(pattern, &find_data);
if (h_find == INVALID_HANDLE_VALUE) {
CARBON_ERROR("no found matches");
*out_count = 0;
return 0;
}
do {
if (counts[i] < MAX_PATH) {
results[i][counts[i]] = carbon_string_dup(find_data.cFileName);
++counts[i];
}
else {
CARBON_ERROR("too many matches");
break;
}
} while (FindNextFile(h_find, &find_data));
FindClose(h_find);
*out_count = counts[i];
++i;
if (i >= CARBON_FS_PATMAT_MAX_STRUCTS) i = 0;
return results[i];
#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;
case GLOB_ABORTED:
CARBON_ERROR("read error");
*out_count = 0;
return 0;
case GLOB_NOMATCH:
CARBON_ERROR("no found matches");
*out_count = 0;
return 0;
}
++i;
if (i >= CARBON_FS_PATMAT_MAX_STRUCTS) i = 0;
*out_count = x->gl_pathc;
return x->gl_pathv;
#endif
}

0 comments on commit 6d1e7fa

Please sign in to comment.