Repository navigation
Expand file tree
/
Copy pathDirectoryReader.cpp
More file actions
82 lines (76 loc) · 2.42 KB
/
Copy pathDirectoryReader.cpp
File metadata and controls
82 lines (76 loc) · 2.42 KB
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
#include "DirectoryReader.h"
#ifdef WIN32
#include "pch.h"
#include <Windows.h>
#include <tchar.h>
#elif __linux__
#include <dirent.h>
#endif
#include <iostream>
namespace DmsLog {
vector<string> DirectoryReader::collectFileNames(wstring pattern) {
std::vector<std::string> ans;
#ifdef __linux__
//This part assumes <dirent.h> method...
string tmpPattern (pattern.begin(), pattern.end());
std::cout<<" pattern " << tmpPattern<<std::endl;
DIR *dirp = opendir(".");
while (dirp)
{
errno = 0;
dirent *dp = nullptr;
if ((dp = readdir(dirp)) != nullptr) {
std::string fname(dp->d_name);
if (patternMatch (fname, tmpPattern)) {
ans.push_back(fname);
}
}
else {
if (errno == 0) {
closedir(dirp);
break;
}
closedir(dirp);
break;
}
}
#elif _WIN32
//This part assumes <windows.h>, <tchar.h> method...
ans = get_all_files_names_within_folder(pattern);
#endif
return ans;
}
bool DirectoryReader::patternMatch(string name, string pattern) {
if (name.size() < pattern.size()) return false;
if (pattern == "*") return true;
else {
int i = 0;
for ( ;i < pattern.size(); ++ i) {
if (pattern[i] == '*') break;
}
string first = pattern.substr(0, i);
string second = i+1>=pattern.size() ? "" : pattern.substr (i+1);
if (name.substr (0,i) == first && name.substr (name.size() - second.size() ,second.size()) == second ) { return true;}
else return false;
}
}
#ifdef _WIN32
vector<string> DirectoryReader::get_all_files_names_within_folder(wstring pattern)
{
vector<string> names;
WIN32_FIND_DATA fd;
HANDLE hFind = ::FindFirstFile(pattern.c_str(), &fd);
if (hFind != INVALID_HANDLE_VALUE) {
do {
if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
wstring ws(fd.cFileName);
string fname(ws.begin(), ws.end());
names.push_back(fname);
}
} while (::FindNextFile(hFind, &fd));
::FindClose(hFind);
}
return names;
}
#endif
}