Skip to content

Commit c9fba4a

Browse files
committed
fix(core): improve Windows compatibility in carp_io.h
1 parent 3e53929 commit c9fba4a

File tree

2 files changed

+30
-71
lines changed

2 files changed

+30
-71
lines changed

core/carp_io.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
#include <sys/stat.h>
2-
#include <dirent.h>
32
#ifdef _WIN32
43
#include <direct.h>
54
#define getcwd _getcwd
65
#define chdir _chdir
6+
#define stat _stat
7+
#define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
8+
#define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
79
#else
10+
#include <dirent.h>
811
#include <unistd.h>
912
#endif
1013

@@ -80,6 +83,31 @@ bool IO_dir_MINUS_exists_QMARK_(const String* path) {
8083
return false;
8184
}
8285

86+
#ifdef _WIN32
87+
Array IO_Raw_list_MINUS_dir(const String* path) {
88+
Array result = {0, 0, NULL};
89+
WIN32_FIND_DATA fd;
90+
char search_path[MAX_PATH];
91+
snprintf(search_path, MAX_PATH, "%s\\*", *path);
92+
HANDLE hFind = FindFirstFile(search_path, &fd);
93+
if (hFind != INVALID_HANDLE_VALUE) {
94+
do {
95+
if (strcmp(fd.cFileName, ".") == 0 || strcmp(fd.cFileName, "..") == 0) {
96+
continue;
97+
}
98+
String name = CARP_MALLOC(strlen(fd.cFileName) + 1);
99+
strcpy(name, fd.cFileName);
100+
result.len++;
101+
result.data = CARP_REALLOC(result.data, sizeof(String) * result.len);
102+
((String*)result.data)[result.len - 1] = name;
103+
} while (FindNextFile(hFind, &fd));
104+
FindClose(hFind);
105+
} else {
106+
result.len = -1;
107+
}
108+
return result;
109+
}
110+
#else
83111
Array IO_Raw_list_MINUS_dir(const String* path) {
84112
DIR *d;
85113
struct dirent *dir;
@@ -102,6 +130,7 @@ Array IO_Raw_list_MINUS_dir(const String* path) {
102130
}
103131
return result;
104132
}
133+
#endif
105134

106135
int IO_mkdir(const String* path) {
107136
#ifdef _WIN32

test/io_extensions.carp

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

0 commit comments

Comments
 (0)