This program reads "emails" (really just a .txt file structured like an email) and does various things with it (storing data in maps and manipulating it).
However, I'm having an unusual issue outputting the "message" of an e-mail based on searching for the subject. First - here is the code (you can probably ignore everything except the end and the find_message function.)
#include <string>
#include <vector>
#include <map>
#include <fstream>
#include <iostream>
using namespace std;
typedef vector<string>::const_iterator Line_iter;
class Message { // a Message points to the first and the last lines of a message
Line_iter first;
Line_iter last;
public:
Message(Line_iter p1, Line_iter p2) :first(p1), last(p2) {}
Line_iter begin() const { return first; }
Line_iter end() const { return last; }
//...
};
typedef vector<Message>::const_iterator Mess_iter;
struct Mail_file { // a Mail_file holds all the lines from a file
// and simplifies access to messages
string name; // file name
vector<string> lines; // the lines in order
vector<Message> m; // Messages in order
Mail_file(const string& n); // read file n into lines
Mess_iter begin() const { return m.begin(); }
Mess_iter end() const { return m.end(); }
};
Mail_file::Mail_file(const string& n)
// open file named "n"
// read the lines from "n" into "lines"
// find the messages in the lines and compose them in m
// for simplicity assume every message is ended by a "----" line
{
ifstream in(n.c_str()); // open the file
if (!in) {
cerr << "no " << n << '\n';
exit(1); // terminate the program
}
string s;
while (getline(in,s)) lines.push_back(s); // build the vector of lines
Line_iter first = lines.begin(); // build the vector of Messages
for (Line_iter p = lines.begin(); p!=lines.end(); ++p) {
if (*p == "----") { // end of message
m.push_back(Message(first,p));
first = p+1; // ---- not part of message
}
}
}
int is_prefix(const string& s, const string& p)
// is p the first part of s?
{
int n = p.size();
if (string(s,0,n)==p) return n;
return 0;
}
bool find_from_addr(const Message* m, string& s)
{
for (Line_iter p = m->begin(); p!=m->end(); ++p)
if (int n = is_prefix(*p, "From: ")) {
s = string(*p, n);
return true;
}
return false;
}
string find_subject(const Message* m)
{
for (Line_iter p = m->begin(); p!=m->end(); ++p)
if (int n = is_prefix(*p, "Subject: ")) return string(*p, n);
return "";
}
string find_message(const Message* m)
{
for (Line_iter p = m->begin(); p!=m->end(); ++p)
if (int n = is_prefix(*p, "Subject: ")) {
p += 2;
return string(*p);
}
return "";
}
int main()
{
Mail_file mfile("my-mail-file.txt"); // initialize mfile from a file
// first gather messages from each sender together in a multimap
multimap<string, const Message*> sender;
multimap<string, const Message*> subject;
for (Mess_iter p = mfile.begin(); p!=mfile.end(); ++p) {
const Message& m = *p;
string s;
if (find_from_addr(&m,s))
sender.insert(make_pair(s,&m));
subject.insert(make_pair(find_subject(&m), &m));
}
// now iterate through the multimap and extract the subjects of John Doe's messages
typedef multimap<string, const Message*>::const_iterator MCI;
pair<MCI,MCI> pp = sender.equal_range("John Doe");
for (MCI p = pp.first; p != pp.second; ++p)
cout << find_subject(p->second) << '\n';
string subject_search;
cout << '\n' << "Enter a subject to search for: ";
cin >> subject_search;
pair<MCI, MCI> sub = subject.equal_range(subject_search);
for (MCI p = sub.first; p != sub.second; ++p)
cout << '\n' << find_message(p->second);
}
For some reason the message output outputting anything. I did some basic testing and tried
cout << find_message(sub.first->second) << endl;
just to see if the function wasn't working and it output the message fine. So obviously there is something wrong with the for loop, and even though p is assigned to the same iterator that worked, it doesn't output anything. Any help is appreciated, thanks.
P.S. Obviously the find_message() function isn't completed yet and only outputs the first line with a magic number, but bear with me.