forked from Arkshine/Orpheu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesManager.cpp
168 lines (130 loc) · 2.83 KB
/
filesManager.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
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
#include <filesManager.h>
#include <configManager.h>
#if defined(WIN32)
#include <Windows.h>
#else
#include <sys/types.h>
#include <dirent.h>
#endif
namespace FilesManager
{
bool dirExists(const char *dir)
{
#if defined WIN32 || defined _WIN32
DWORD attr = GetFileAttributes(dir);
if (attr == INVALID_FILE_ATTRIBUTES)
return false;
if (attr & FILE_ATTRIBUTE_DIRECTORY)
return true;
#else
struct stat s;
if (stat(dir, &s) != 0)
return false;
if (S_ISDIR(s.st_mode))
return true;
#endif
return false;
}
ke::Vector<ke::AString>* getFolders(const char* directory)
{
ke::Vector<ke::AString>* folders = new ke::Vector<ke::AString>();
if (dirExists(directory))
{
#if defined WIN32 || defined _WIN32
char path[256];
UTIL_Format(path, sizeof(path) - 1, "%s*", directory);
WIN32_FIND_DATA fd;
HANDLE hFile = FindFirstFile(path, &fd);
if (hFile != INVALID_HANDLE_VALUE)
{
do
{
if (fd.cFileName[0] == '.')
{
continue;
}
UTIL_Format(path, sizeof(path) - 1, "%s%s", directory, fd.cFileName);
if (dirExists(path))
{
folders->append(ke::AString(fd.cFileName));
}
} while (FindNextFile(hFile, &fd));
FindClose(hFile);
}
#else
char path[256];
UTIL_Format(path, sizeof(path) - 1, "%s", directory);
DIR *dp = opendir(path);
if (dp != 0)
{
struct dirent *ep;
while ((ep = readdir(dp)))
{
if (ep->d_name[0] == '.')
{
continue;
}
UTIL_Format(path, sizeof(path) - 1, "%s%s", directory, ep->d_name);
if (dirExists(path))
{
folders->append(ke::AString(ep->d_name));
}
}
closedir(dp);
}
#endif
}
return folders;
}
ke::Vector<ke::AString>* getFiles(const char* directory)
{
ke::Vector<ke::AString>* files = new ke::Vector<ke::AString>();
if (dirExists(directory))
{
#if defined WIN32 || defined _WIN32
char path[256];
UTIL_Format(path, sizeof(path) - 1, "%s*", directory);
WIN32_FIND_DATA fd;
HANDLE hFile = FindFirstFile(path, &fd);
if (hFile != INVALID_HANDLE_VALUE)
{
do
{
if (fd.cFileName[0] == '.')
{
continue;
}
UTIL_Format(path, sizeof(path) - 1, "%s%s", directory, fd.cFileName);
if (!dirExists(path))
{
files->append(ke::AString(fd.cFileName));
}
} while (FindNextFile(hFile, &fd));
FindClose(hFile);
}
#else
char path[256];
UTIL_Format(path, sizeof(path) - 1, "%s", directory);
DIR *dp = opendir(path);
if (dp != 0)
{
struct dirent *ep;
while ((ep = readdir(dp)))
{
if (ep->d_name[0] == '.')
{
continue;
}
UTIL_Format(path, sizeof(path) - 1, "%s%s", directory, ep->d_name);
if (!dirExists(path))
{
files->append(ke::AString(ep->d_name));
}
}
closedir(dp);
}
#endif
}
return files;
}
}