-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisting.h
79 lines (59 loc) · 1.53 KB
/
listing.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#pragma once
#include <string>
#include <vector>
#ifdef _WIN32
#include <windows.h>
static inline
std::vector<std::string>
list_files() {
WIN32_FIND_DATA data;
HANDLE handle = ::FindFirstFile("./*", &data);
if (handle == INVALID_HANDLE_VALUE) {
return {};
}
std::vector<std::string> files;
do {
if (data.dwFileAttributes & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_HIDDEN)) {
continue;
}
const char* path = data.cFileName;
if (std::strlen(path) > UINT8_MAX) {
path = data.cAlternateFileName;
}
files.push_back(path);
} while (::FindNextFile(handle, &data));
::CloseHandle(handle);
return files;
}
#else
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
static inline
std::vector<std::string>
list_files() {
auto dir = ::opendir(".");
if (dir == nullptr) {
std::fprintf(stderr, "error: opendir()=%d\n", errno);
return {};
}
std::vector<std::string> files;
dirent* entry = nullptr;
while ((entry = ::readdir(dir)) != nullptr) {
struct stat info;
if (::stat(entry->d_name, &info) < 0) {
std::fprintf(stderr, "warning: stat()=%d\n", errno);
continue;
}
if (!(info.st_mode & S_IFREG) || (entry->d_name[0] == '.')) {
continue;
}
const char* path = entry->d_name;
if (std::strlen(path) > UINT8_MAX) {
continue;
}
files.push_back(path);
}
return files;
}
#endif