-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
148 lines (141 loc) · 3.55 KB
/
main.cpp
File metadata and controls
148 lines (141 loc) · 3.55 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <algorithm>
#include <fstream>
#include <iostream>
#include <map>
#include <regex>
#include <sstream>
#include <string>
#include <stdexcept>
#include <vector>
using std::cerr;
using std::cout;
using std::endl;
using std::exception;
using std::ifstream;
using std::invalid_argument;
using std::make_pair;
using std::map;
using std::ofstream;
using std::pair;
using std::regex;
using std::regex_search;
using std::smatch;
using std::sort;
using std::sregex_iterator;
using std::string;
using std::stringstream;
using std::vector;
struct args {
int N;
string in;
string out;
};
regex r(R"(\b(http:\/\/|https:\/\/)([a-zA-Z0-9.-]+)(\/[a-zA-Z0-9.,\/+_]*)?)");
args parseCmdLineParams(int argc, char** argv) {
args a;
a.N = 0;
if(!(argc == 3 || argc == 5)) {
throw invalid_argument("Invalid arguments\nUsage: main [-n NNN] in.txt out.txt");
}
if(argc == 5 && strcmp(argv[1], "-n") != 0) {
throw invalid_argument("Invalid arguments\nUsage: main [-n NNN] in.txt out.txt");
}
if(argc == 3) {
a.in = string(argv[1]);
a.out = string(argv[2]);
} else if(argc == 5) {
a.N = atoi(argv[2]);
a.in = string(argv[3]);
a.out = string(argv[4]);
}
return a;
}
string make_output(int urls, int N, vector<pair<int, string>>& d, vector<pair<int, string>>& p) {
stringstream ss;
sort(begin(d), end(d), [](pair<int, string>& x, pair<int, string> &y) {
if(x.first > y.first) {
return true;
} else if(x.first == y.first) {
return x.second < y.second;
} else {
return false;
}
});
sort(begin(p), end(p), [](pair<int, string>& x, pair<int, string> &y) {
if(x.first > y.first) {
return true;
} else if(x.first == y.first) {
return x.second < y.second;
} else {
return false;
}
});
ss << "total urls " << urls << ", domains " << d.size()
<< ", paths " << p.size() << endl;
ss << endl;
ss << "top domains" << endl;
if(N == 0) {
for(auto& i: d) {
ss << i.first << " " << i.second << endl;
}
ss << endl << "top paths" << endl;
for(auto& i: p) {
ss << i.first << " " << i.second << endl;
}
} else {
int ND = std::min((unsigned long)N, d.size());
int NP = std::min((unsigned long)N, p.size());
for(auto i = 0; i < ND; i++) {
ss << d[i].first << " " << d[i].second << endl;
}
ss << endl << "top paths" << endl;
for(auto i = 0; i < NP; i++) {
ss << p[i].first << " " << p[i].second << endl;
}
}
return ss.str();
}
void parseInputFileAndOutputResults(args &a) {
map<string, int> domains;
map<string, int> paths;
int urls = 0;
vector<pair<int, string>> domains_v;
vector<pair<int, string>> paths_v;
ifstream in(a.in);
ofstream o(a.out);
stringstream buffer;
buffer << in.rdbuf();
string s = buffer.str();
smatch sm;
for(sregex_iterator it(s.begin(), s.end(), r), it_end; it != it_end; it++) {
sm = *it;
domains[sm[2]]++;
paths[sm[3]]++;
urls++;
}
paths["/"] += paths["\0"];
paths.erase("\0");
if(paths["/"] == 0) {
paths.erase("/");
}
for(auto sss: domains) {
domains_v.push_back(make_pair(sss.second, sss.first));
}
for(auto sss: paths) {
paths_v.push_back(make_pair(sss.second, sss.first));
}
o << make_output(urls, a.N, domains_v, paths_v) << endl;
}
int main(int argc, char** argv) {
try {
args a = parseCmdLineParams(argc, argv);
parseInputFileAndOutputResults(a);
} catch(exception& e) {
cerr << e.what() << endl;
return -1;
} catch(...) {
cerr << "Unknown error" << endl;
return -1;
}
return 0;
}