-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe3.cpp
163 lines (135 loc) · 3.53 KB
/
e3.cpp
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//
// Created by martin on 10/22/22.
//
#include "../std_lib.h"
typedef vector<string>::const_iterator Line_iter;
class Message
{
public:
Message (const Line_iter &first, const Line_iter &last) :
first(first),
last(last)
{
}
[[nodiscard]] Line_iter begin () const
{
return first;
}
[[nodiscard]] Line_iter end () const
{
return last;
}
private:
Line_iter first;
Line_iter last;
};
using Message_iter = vector<Message>::const_iterator;
struct Mail_file
{
string name;
vector<string> lines;
vector<Message> messages;
explicit Mail_file (const string &filename)
{
ifstream ifs { filename };
if (!ifs)
{
cerr << "no " << filename << endl;
exit(EXIT_FAILURE);
}
for (string line ; getline(ifs, line) ; /* */)
{
lines.push_back(line);
}
regex separator { R"(^----$)" };
smatch matches;
auto first = lines.begin();
for (auto p { lines.begin() } ; p != lines.end() ; ++p)
{
if (regex_search(*p, matches, separator))
{
messages.push_back(Message(first, p));
first = p + 1;
}
}
}
[[nodiscard]] Message_iter begin () const
{
return messages.begin();
}
[[nodiscard]] Message_iter end () const
{
return messages.end();
}
};
int is_prefix (const string &line, const string &prefix)
{
regex pattern { "^" + prefix };
smatch matches;
if (regex_search(line, matches, pattern))
{
return int(prefix.size());
}
return 0;
}
bool find_from_address (const Message *message, string &str)
{
for (const auto &line : *message)
{
if (auto n { is_prefix(line, "From: ") })
{
str = string(line, n);
return true;
}
}
return false;
}
string find_subject (const Message *message)
{
for (const auto &x : *message)
{
if (auto n { is_prefix(x, "Subject: ") })
{
return string(x, n);
}
}
return "";
}
int main (int argc, char *argv[])
{
Mail_file mail_file { "../c23/sample.mbox" };
// multimap<string, const Message *> sender;
//
// for (const auto &message : mail_file)
// {
// string str;
// if (find_from_address(&message, str))
// {
// sender.insert(make_pair(str, &message));
// }
// }
/*
auto pp = sender.equal_range("Neil Pickford <neilp@goldweb.com.au>");
for (auto p { pp.first } ; p != pp.second ; ++p)
{
cout << find_subject(p->second) << endl;
}
*/
using Subject = string;
multimap<Subject, const Message *> subjects;
for (const auto &message : mail_file)
{
auto subject = find_subject(&message);
subjects.insert(make_pair(subject, &message));
}
istringstream iss { "Report" };
for (auto [subject, message] : subjects)
{
auto found = subject.find(iss.str());
if (found != string::npos)
{
cout << subject << " -> " << message << endl;
}
}
return EXIT_SUCCESS;
}