/*One example of email:

Return-Path: <Frankie.Chaney@kittymail.com>
Delivered-To: online.fr-fabien.pigere@free.fr
Received: (qmail 22595 invoked from network); 21 Feb 2005 15:23:36 -0000
Received: from yahoobb219199196069.bbtec.net (219.199.196.69)
		  by mx1-1.free.fr with SMTP; 21 Feb 2005 15:23:36 -0000
		  Return-Path: <Frankie.Chaney@kittymail.com>
Received: from 88.164.61.24  (HELO client.comcast.net) (173.236.226.215)
		  by mta293.mail.re6.yahoo.com with SMTP; Mon, 21 Feb 2005 16:21:51 +0100
Date: Mon, 21 Feb 2005 20:25:51 +0500
	  Message-Id: <2.86637.3132363037393934.4@ientrynetwork.net>
From: "Callie Bergeron" <Frankie.Chaney@kittymail.com>
To:     <fabien.marty@free.fr>
Subject: Updated Account 8177
		 X-Original-Return-Path: <Frankie.Chaney@kittymail.com>
		 X-Original-To: Frankie.Chaney@kittymail.com
		 X-Mailer: Netscape Webmail

		 You are permitted to get yourself a home loan online from one of the leading companies.

		 It'll help increase your financial position: 

http://www.bestlenderz.com/x/loan.php?id=ph15
*/

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <assert.h>
using namespace std;

class Cheader
	{
	string			mLineFrom;
	string			mLineTo;
	string			mLineReturnPath;
	string			mHeader;
	string			mLineCc;
	vector<string>	mTabCcName;
	string			mLineBcc;
	string			mLineSubject;
	string			mLineDeliveredTo;
public:
	Cheader()	{};

	bool	extractLine(const string& aFind,string&	aDst)
		{
		int	deb=mHeader.find(aFind);
		if (deb==string::npos)
			return	false;
		int	end=mHeader.find("\r\n",deb);
		if (end==string::npos)
			return	false;
		aDst=mHeader.substr(deb,end-deb);
		cout<<aFind<<"----->"<<aDst<<endl;
		return	true;
		}
/*
 *	extractField
 *	aSrc the line, the form is "name: field1 ,field2, field3.../r/n" the number of space is not know.
 *	aRes the resultat, all field stock in a vector of string.
 */
	void	extractField(const string& aSrc,vector<string>& aRes)
		{
		int	deb=mLineCc.find_first_of(":")+1;
		if (deb==string::npos)
			return;
		int	end=deb;
		while (1)
			{
			int res=mLineCc.find_first_not_of(" ,",deb);
			if (res!=string::npos)
				deb=res;
			end=mLineCc.find_first_of(" ,",deb);
			if (end==string::npos)
				{
				end=mLineCc.size();
				}
			if (end-deb==0)
				{
				return;
				}
			aRes.push_back(mLineCc.substr(deb,end-deb));
			cout<<"<--->"<<mTabCcName[mTabCcName.size()-1]<<endl;
			deb=end;
			}
		}
/*
 *	
 */
	void	init(char*	aSrc,int	aLength)
		{
		assert(aSrc);
		mHeader=string(aSrc,aLength);
		mHeader=mHeader.substr(0,mHeader.find("\r\n\r\n"));

		extractLine("From:",mLineFrom);
		extractLine("To:",mLineTo);
		extractLine("Return-Path:",mLineReturnPath);
		bool	b=extractLine("Cc:",mLineCc);
		if (b)
			{
			extractField(mLineCc,mTabCcName);
			}
		extractLine("Bcc:",mLineBcc);
		extractLine("Subject:",mLineSubject);
		extractLine("Delivered-To:",mLineDeliveredTo);
		}
	};

/**
 *	autotest
 */

int main()
	{

	ifstream email("email2.txt", ios::binary );
	// get length of file:
	email.seekg(0,ios::end);
	length = email.tellg();
	email.seekg(0,ios::beg);

	int length;
	char*	buffer=new char[length];
	// read data as a block:
	email.read(buffer,length);
	email.close();

	//cout.write(buffer,length);
	//cin.get();
	Cheader	h;
	h.init(buffer,length);
	}
