-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistFilesInDirectory.h
172 lines (140 loc) · 3.49 KB
/
listFilesInDirectory.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
#pragma once
#ifdef _WIN32
#include <windows.h>
#else
#include "dirent.h"
#include <sys/stat.h>
#endif
#include <vector>
#include <string>
#include <iostream>
#pragma comment(lib, "User32.lib")
static bool endsWith( const std::string & filename, const std::string & ending)
{
if (filename.length() >= ending.length())
{
return (filename.compare(filename.length() - ending.length(), ending.length(), ending) == 0);
}
return false;
}
static bool isValidDirectory(const std::string & path)
{
#ifdef _WIN32
DWORD dwAttrib = GetFileAttributes(path.c_str());
return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
// Ubuntu
#else
struct stat sb;
return (stat(&path[0], &sb) == 0 && S_ISDIR(sb.st_mode));
#endif
}
static std::vector<std::string> listFilesInDirectory(const std::string & directory)
{
#ifdef _WIN32
std::vector<std::string> files;
if (directory.length() > (MAX_PATH - 3))
{
std::cout << "Directory path is too long." << std::endl;
return files;
}
std::string dirAppended = directory;
if(endsWith(dirAppended, "\\"))
{
dirAppended.append("*");
}
else
{
dirAppended.append("\\*");
}
WIN32_FIND_DATA ffd;
HANDLE hFind = FindFirstFile(dirAppended.c_str(), &ffd);
if (INVALID_HANDLE_VALUE == hFind)
{
std::cout << "Error Handle" << std::endl;
return files;
}
do
{
if (!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) //file is no directory
{
std::string filename = directory;
if(!endsWith(filename, "\\"))
{
filename.append("\\");
}
filename.append(ffd.cFileName);
files.push_back(filename);
}
} while (FindNextFile(hFind, &ffd) != 0);
if (GetLastError() != ERROR_NO_MORE_FILES)
{
std::cout << "Unknown error" << std::endl;
}
FindClose(hFind);
return files;
// Ubuntu
#else
DIR *dirHandle;
struct stat sb;
struct dirent *entry;
std::vector<std::string> files;
std::string dir = directory;
if (dir.length() > (255 - 3))
{
std::cout << "Directory path is too long." << std::endl;
return files;
}
if(!endsWith(dir, "/"))
{
dir.append("/");
}
if((dirHandle = opendir (&dir[0])) != NULL)
{
/* print all the files and directories within directory */
while ((entry = readdir (dirHandle)) != NULL)
{
std::string file = dir + entry->d_name;
if (stat(&file[0], &sb) == 0 && S_ISREG(sb.st_mode))
{
files.push_back(file);
}
}
closedir(dirHandle);
}
else
{
/* could not open directory */
std::cout << "Directory Open Error" << std::endl;
}
return files;
#endif
}
static std::vector<std::string> filterFileList(const std::vector<std::string> & fileList, const std::string & ending)
{
std::vector<std::string> filteredList;
for (size_t i = 0; i < fileList.size(); ++i)
{
if (endsWith(fileList[i], ending))
{
filteredList.push_back(fileList[i]);
}
}
return filteredList;
}
static std::vector<std::string> filterFileList(const std::vector<std::string> & fileList, const std::vector<std::string> & endings)
{
std::vector<std::string> filteredList;
for (size_t i = 0; i < fileList.size(); ++i)
{
for (size_t j = 0; j < endings.size(); ++j)
{
if (endsWith(fileList[i], endings[j]))
{
filteredList.push_back(fileList[i]);
break;
}
}
}
return filteredList;
}