-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlink_remover.cpp
More file actions
65 lines (53 loc) · 1.84 KB
/
link_remover.cpp
File metadata and controls
65 lines (53 loc) · 1.84 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
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <unordered_map>
#include <filesystem>
namespace fs = std::filesystem;
using std::cout, std::endl;
int main(int argc, char** argv) {
if (argc < 3) {
std::cerr << "Expected: " << argv[0] << "<link directory> <out file path>\n";
exit(EXIT_FAILURE);
}
std::string dirPath = std::string(argv[1]);
std::string outPath = std::string(argv[2]);
std::string line;
std::unordered_map<std::string, uint8_t> sources;
cout << "Getting source links" << endl;
for (const auto& entry : fs::directory_iterator(dirPath)) {
if (!entry.is_regular_file()) continue;
std::ifstream f(entry.path());
cout << "Starting " << entry.path() << "..." << endl;
while (std::getline(f, line)) {
std::istringstream stream(line);
std::string sourceUrl;
stream >> sourceUrl;
sources.insert({sourceUrl, 0});
}
cout << "... Finished " << entry.path() << endl;
}
cout << sources.size() << endl;
std::ofstream o(outPath);
cout << "Writing to file" << endl;
for (const auto& entry : fs::directory_iterator(dirPath)) {
std::ifstream f(entry.path());
cout << "Starting " << entry.path() << "..." << endl;
while (std::getline(f, line)) {
std::istringstream stream(line);
std::string sourceUrl;
stream >> sourceUrl;
if (sources[sourceUrl] == 1) continue;
o << sourceUrl << " ";
sources[sourceUrl]++;
std::string url;
while (stream >> url) {
if (sources.find(url) == sources.end()) continue;
o << url << " ";
}
o << "\n";
}
cout << "... Finished " << entry.path() << endl;
}
}