-
Notifications
You must be signed in to change notification settings - Fork 6
/
fsutils.cpp
130 lines (124 loc) · 3.34 KB
/
fsutils.cpp
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "fsutils.hpp"
#include <algorithm>
namespace fsutils
{
bool is_dir_exists(cxx17::string_view path)
{
posix_stat_st st;
if (0 == posix_stat(path.data(), &st))
return !!(st.st_mode & S_IFDIR);
return false;
}
bool is_file_exists(cxx17::string_view path)
{
posix_stat_st st;
if (0 == posix_stat(path.data(), &st))
return !!(st.st_mode & S_IFREG);
return false;
}
long long get_file_size(cxx17::string_view path, bool& isdir)
{
posix_stat_st st;
if (0 == posix_stat(path.data(), &st))
{
if (st.st_mode & S_IFREG)
return st.st_size;
isdir = true;
}
return -1;
}
#if defined(_WIN32) && !defined(__MINGW64__) && !defined(__MINGW32__)
static bool is_dir_exists_wide(const wchar_t* path);
bool is_dir_exists(cxx17::wstring_view path) { return is_dir_exists_wide(path.data()); }
bool is_file_exists(cxx17::wstring_view path)
{
posix_stat_st st;
if (0 == posix_ustat(path.data(), &st))
return !!(st.st_mode & S_IFREG);
return false;
}
static bool is_dir_exists_wide(const wchar_t* path)
{
posix_stat_st st;
if (0 == posix_ustat(path, &st))
return !!(st.st_mode & S_IFDIR);
return false;
}
static void list_files_wide(const wchar_t* dirPath,
const std::function<void(tinydir_file&)>& callback, bool recursively)
{
if (is_dir_exists_wide(dirPath))
{
tinydir_dir dir;
if (tinydir_open(&dir, dirPath) != -1)
{
while (dir.has_next)
{
tinydir_file file;
if (tinydir_readfile(&dir, &file) == -1)
{
// Error getting file
break;
}
cxx17::wstring_view fileName = file.name;
if (fileName != L"." && fileName != L"..")
{
callback(file);
if (file.is_dir && recursively)
list_files_wide(file.path, callback, recursively);
}
if (tinydir_next(&dir) == -1)
{
// Error getting next file
break;
}
}
}
tinydir_close(&dir);
}
}
void list_files(const std::string& dirPath, const std::function<void(tinydir_file&)>& callback,
bool recursively)
{
auto wpath = ntcvt::from_chars(dirPath, CP_UTF8);
list_files_wide(wpath.c_str(), callback, recursively);
}
void to_styled_path(std::string& path) { std::replace(path.begin(), path.end(), '/', '\\'); }
#else
void list_files(const std::string& dirPath, const std::function<void(tinydir_file&)>& callback,
bool recursively)
{
if (fsutils::is_dir_exists(dirPath))
{
tinydir_dir dir;
std::string fullpathstr = dirPath;
if (tinydir_open(&dir, &fullpathstr[0]) != -1)
{
while (dir.has_next)
{
tinydir_file file;
if (tinydir_readfile(&dir, &file) == -1)
{
// Error getting file
break;
}
std::string fileName = file.name;
if (fileName != "." && fileName != "..")
{
callback(file);
if (file.is_dir && recursively)
list_files(file.path, callback, recursively);
}
if (tinydir_next(&dir) == -1)
{
// Error getting next file
break;
}
}
}
tinydir_close(&dir);
}
}
void to_styled_path(std::string& /*path*/) {}
#endif
} // namespace fsutils