-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinks_extractor.cpp
More file actions
62 lines (57 loc) · 1.56 KB
/
links_extractor.cpp
File metadata and controls
62 lines (57 loc) · 1.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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <unordered_map>
#include <filesystem>
#include <cstdio>
namespace fs = std::filesystem;
void getLinks(fs::path &filepath){
std::ifstream inFile(filepath);
if (!inFile.is_open()){
return;
}
std::string line;
std::getline(inFile, line);
std::string before = "URL: ";
std::string after = " Doc number:";
size_t s = line.find(before);
if (s == std::string::npos){
return;
}
s += before.size();
size_t e = line.find(after, s);
if (e == std::string::npos){
return;
}
std::string url = line.substr(s, e-s);
std::cout << url << " ";
bool in_links = false;
while (std::getline(inFile, line)) {
if (!line.empty() && line[0] == '<') {
if (line.find("<links>") != std::string::npos) {
in_links = true;
} else if (in_links && line.find("</links>") != std::string::npos) {
break;
}
} else if (in_links) {
std::cout << line << " ";
}
}
std::cout << "\n";
}
int main(int argc, char** argv) {
if (argc < 2) {
std::cerr << "Expected: " << argv[0] << "<directory_path>\n";
}
int count = 0;
std::string dirpath = std::string(argv[1]);
for (const auto &entry : fs::directory_iterator(dirpath)) {
if (!entry.is_regular_file()) continue;
auto path = entry.path();
if (path.extension() == ".parsed") {
getLinks(path);
count++;
}
}
}