-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileutil.hpp
69 lines (58 loc) · 2.01 KB
/
fileutil.hpp
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
#ifndef FILEUTIL_HPP
#define FILEUTIL_HPP
#include <iostream>
#include <string>
#include <vector>
#include <filesystem>
#include <sstream>
#include <fstream>
#include <regex>
namespace fs = std::filesystem;
using ist = std::istreambuf_iterator<char>;
using ost = std::ostreambuf_iterator<char>;
// https://www.fluentcpp.com/2017/04/21/how-to-split-a-string-in-c/
// separated by , For example: cpp,inl ==> [ cpp, inl ]
std::vector<std::string> collectFileExts(const std::string &arg) {
std::vector<std::string> tokens;
std::string token;
std::istringstream tokenStream(arg);
while (std::getline(tokenStream, token, ',')) {
tokens.push_back(token);
}
return tokens;
}
// https://stackoverflow.com/questions/40633529/how-to-list-all-the-files-with-given-extension-from-directory-using-filesystem
// File search for each file extension
std::vector<std::string> collectFiles(std::string dir,
std::vector<std::string> exts) {
std::vector<std::string> tokens;
// for(const auto& p : fs::directory_iterator(dir))
for (const auto &p : fs::recursive_directory_iterator(dir)) {
for (auto ext : exts) {
if (p.path().extension() == '.' + ext) {
tokens.push_back(p.path().u8string());
}
}
}
return tokens;
}
// Change the include statement if needed
std::string getChangedOrNotSourceLine(std::string line, bool &changed) {
std::string includeLine = "#include";
if (line.find(includeLine) == std::string::npos) {
// not contain #include so it does not change
return line;
} else {
std::string line2 = line;
// TODO: regex not good enough, fails for:
// #include <SFML\Graphics.hpp> // \test -> #include <SFML/Graphics.hpp> // /test
line = std::regex_replace(line, std::regex(R"(\\)"), R"(/)");
if (line != line2) {
changed = true;
// https://www.rosettacode.org/wiki/Globally_replace_text_in_several_files#C.2B.2B
std::cout << line2 << " -> " << line << std::endl;
}
return line;
}
}
#endif // FILEUTIL_HPP