-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
105 lines (93 loc) · 2.56 KB
/
utils.cpp
File metadata and controls
105 lines (93 loc) · 2.56 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "utils.h"
#include <iostream>
#include <string.h>
namespace utils
{
// remove / from the end
const std::vector<std::string> fixExcludes(const std::vector<std::string> excludes)
{
std::vector<std::string> fixed_excludes;
for (auto& exclude : excludes)
{
if (exclude.back() == '/' || exclude.back() == '\\')
{
std::string tmp{exclude};
tmp.pop_back();
fixed_excludes.push_back(tmp);
}
else
{
fixed_excludes.emplace_back(std::string(exclude));
}
}
return fixed_excludes;
}
std::vector<std::filesystem::directory_entry> getFilesByFilter(const std::string path, const std::vector<std::string> extensions,
const std::vector<std::string> excludes)
{
std::vector<std::filesystem::directory_entry> files;
std::filesystem::recursive_directory_iterator entry(path);
for (; entry != std::filesystem::recursive_directory_iterator(); ++entry)
{
if (entry->is_directory())
{
for (auto&& exclude : fixExcludes(excludes))
{
if (strncmp(entry->path().c_str(), exclude.c_str(), exclude.size()) == 0)
{
entry.disable_recursion_pending();
}
}
}
else if (entry->is_regular_file())
{
for (const std::string& extension : extensions)
{
if (entry->path().filename().extension() == extension)
{
files.push_back(*entry);
}
}
}
}
return files;
}
std::vector<std::string> split(const std::string& line, char delim)
{
std::vector<std::string> result;
std::stringstream ss(line);
std::string item;
while (getline(ss, item, delim))
{
result.push_back(item);
}
return result;
}
std::string removeWhiteSpaces(const std::string& line)
{
std::string result;
bool start = true;
// remove: start, end, double spaces
for (size_t i = 0; i < line.size(); i++)
{
if (i + 1 < line.size() && line[i] == ' ' && line[i + 1] == ' ')
{
continue;
}
if (start)
{
if ((line[i] == ' ' || line[i] == '\t'))
{
continue;
}
start = false;
}
if (i + 1 == line.size() && line[i] == ' ')
{
continue;
}
result.push_back(line[i]);
}
return result;
}
} // namespace utils