-
Notifications
You must be signed in to change notification settings - Fork 7
/
mrdir.h
270 lines (264 loc) · 6.91 KB
/
mrdir.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#ifndef MRDIR_H
#define MRDIR_H
#include "string"
#include "iostream"
#include "vector"
#include "mrutil.h"
//////////////////////////////////////////////////////////////////////////
///
/// EXISTS
/// MKDIR
/// SLEEP
//////////////////////////////////////////////////////////////////////////
#ifdef _WIN32
#include <io.h>
#include <direct.h>
#include <windows.h>
#define EXISTS(path) (_access(path, 0)!=-1)
#define MKDIR(path) _mkdir(path)
#define SLEEP(ms) Sleep(ms)
static bool exist(const char*filepath){return (_access(filepath, 0)) != -1;}
#else
#include <unistd.h>
#include <sys/stat.h>
#define EXISTS(path) (access(path, 0)!=-1)
#define MKDIR(path) mkdir(path, 0775)
#define SLEEP(ms) usleep(ms)
#endif
//////////////////////////////////////////////////////////////////////////
/// @brief
/// ANSIToUnicode
/// UnicodeToANSI
/// UTF8ToUnicode
/// UnicodeToUTF8
/// ANSIToUTF8
/// UTF8ToANSI
/// @param str
/// @retval
//////////////////////////////////////////////////////////////////////////
#ifdef _WIN32
#include <tchar.h>
#include "windows.h"
#if _UNICODE
static wchar_t * ANSIToUnicode(const char* str)
{
if (!str)
return NULL;
int textlen;
wchar_t * result;
textlen = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
result = (wchar_t *)malloc((textlen + 1)*sizeof(wchar_t));
memset(result, 0, (textlen + 1)*sizeof(wchar_t));
MultiByteToWideChar(CP_ACP, 0, str, -1, (LPWSTR)result, textlen);
result[textlen] = '\0';
return result;
}
static char * UnicodeToANSI(const wchar_t* str)
{
if (!str)
return NULL;
char* result;
int textlen;
textlen = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
result = (char *)malloc((textlen + 1)*sizeof(char));
memset(result, 0, sizeof(char)* (textlen + 1));
WideCharToMultiByte(CP_ACP, 0, str, -1, result, textlen, NULL, NULL);
result[textlen] = '\0';
return result;
}
static wchar_t * UTF8ToUnicode(const char* str)
{
int textlen;
wchar_t * result;
textlen = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
result = (wchar_t *)malloc((textlen + 1)*sizeof(wchar_t));
memset(result, 0, (textlen + 1)*sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8, 0, str, -1, (LPWSTR)result, textlen);
return result;
}
static char * UnicodeToUTF8(const wchar_t* str)
{
char* result;
int textlen;
textlen = WideCharToMultiByte(CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL);
result = (char *)malloc((textlen + 1)*sizeof(char));
memset(result, 0, sizeof(char)* (textlen + 1));
WideCharToMultiByte(CP_UTF8, 0, str, -1, result, textlen, NULL, NULL);
return result;
}
static char* ANSIToUTF8(const char* str)
{
return UnicodeToUTF8(ANSIToUnicode(str));
}
static char* UTF8ToANSI(const char* str)
{
return UnicodeToANSI(UTF8ToUnicode(str));
}
#endif
//////////////////////////////////////////////////////////////////////////
/// @brief
/// @param strDir
/// @param subdirs
//////////////////////////////////////////////////////////////////////////
static std::vector<std::string> getAllSubdirs(std::string strDir)
{
WIN32_FIND_DATA FindData;
HANDLE hError;
std::string file2find = strDir + "/*.*";
std::vector<std::string> subdirs;
#if _UNICODE
wchar_t *p = ANSIToUnicode(file2find.c_str());
hError = FindFirstFile(p, &FindData);
delete[]p;
#else
hError = FindFirstFile((LPCTSTR)file2find.c_str(), &FindData);
#endif
if (hError == INVALID_HANDLE_VALUE)
{
std::cout << "cannot find subdir in " << strDir<< std::endl;
return subdirs;
}
else
{
do
{
if (lstrcmp(FindData.cFileName, TEXT(".")) == 0 || lstrcmp(FindData.cFileName, TEXT("..")) == 0)
{
continue;
}
if (!(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
continue;
}
if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
#if _UNICODE
char*p = UnicodeToANSI(FindData.cFileName);
subdirs.push_back(p);
delete[]p;
#else
subdirs.push_back(FindData.cFileName);
#endif
}
} while (::FindNextFile(hError, &FindData));
}
FindClose(hError);
return subdirs;
}
//////////////////////////////////////////////////////////////////////////
/// @brief
/// @param strDir
/// @param subdirs
/// @param ext
//////////////////////////////////////////////////////////////////////////
static std::vector<std::string> getAllFilesinDir(std::string strDir, std::string ext = "*.*")
{
std::vector<std::string> files;
std::vector<std::string> items = split(ext, "|");
for (int i = 0; i < items.size(); i++) {
std::string file2find = strDir + "/" + items[i];
WIN32_FIND_DATA FindData;
HANDLE hError;
#if _UNICODE
wchar_t *p = ANSIToUnicode(file2find.c_str());
hError = FindFirstFile(p, &FindData);
delete[]p;
#else
hError = FindFirstFile((LPCTSTR)file2find.c_str(), &FindData);
#endif
if (hError == INVALID_HANDLE_VALUE)
{
continue;
}
else
{
do
{
if (lstrcmp(FindData.cFileName, TEXT(".")) == 0 || lstrcmp(FindData.cFileName, TEXT("..")) == 0)
{
continue;
}
else if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
continue;
}
else if (!(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
#if _UNICODE
char *p = UnicodeToANSI(FindData.cFileName);
files.push_back(p);
delete[]p;
#else
files.push_back(FindData.cFileName);
#endif
}
} while (::FindNextFile(hError, &FindData));
}
FindClose(hError);
}
return files;
}
#else
#include <dirent.h>
#include "string.h"
using std::string;
static std::vector<std::string> getAllSubdirs(std::string strDir)
{
DIR *dp;
struct dirent *entry;
std::vector<std::string> subdirs;
if (!EXISTS(strDir.c_str())){
return subdirs;
}
dp = opendir(strDir.c_str());
while((entry = readdir(dp)) != NULL)
{
if (strcmp(".", entry->d_name) == 0 || strcmp("..", entry->d_name) == 0)
continue;
if(entry->d_type==4)
{
subdirs.push_back(entry->d_name);
}
}
closedir(dp);
return subdirs;
}
inline bool ends_with(std::string const & value, std::string const & ending)
{
if (ending.size() > value.size())
return false;
return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
}
static std::vector<std::string> getAllFilesinDir(const std::string strDir, std::string ext="*.*")
{
std::vector<std::string> items = split(ext,"|");
for(int i = 0; i < items.size(); i++){
auto si = split(items[i], ".");
items[i] = si[si.size()-1];
}
struct dirent* ent = NULL;
DIR *pDir;
std::vector<std::string> files;
if (!EXISTS(strDir.c_str())){
return files;
}
pDir = opendir(strDir.c_str());
while (NULL != (ent = readdir(pDir))) {
if (ent->d_type == 8) {
for (int i = 0; i < items.size(); i++){
if (items[i] == "*" || ends_with(ent->d_name, items[i])){
files.push_back(ent->d_name);
}
}
}
else {
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
continue;
}
}
}
closedir(pDir);
return files;
}
#endif
#endif