⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 final++.cpp

📁 文件编码转换程序
💻 CPP
字号:
//6_28 9:23 cd
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <conio.h>
#include <ctype.h>
#include <string.h>

using namespace std;

typedef vector<string> sub;

ofstream outFile("result.txt", ios_base::out);

//邮件标识
enum content_mode {text, multipart, othercontent};
enum text_mode {plain, html, othertext};
enum coding_mode {pure, Base64, QP, othercoding};

content_mode content_m = othercontent;
text_mode textype_m = othertext;
coding_mode coding_m = othercoding;

string boundary = "";


void unBase64(string & content, string &uncoded);                                   //Base64译码
void DecodeQuoted(string & content, string & uncoded, int size);                    //QP译码
void print(vector<string> &body);													//
void ana_header(vector<string> &header);                                            //信头分析
void cut(vector<string> & mail, vector<string> &header, vector<string> &body);      //邮件分割(信头,信体)

int main()
{
	cout<<"输入要处理的文件名:";
	
	string infile;
	cin>>infile;
	ifstream inFile(infile.c_str(),ios_base::in);

	if(!inFile)
	{
		cerr<<"file is not exist!";
		return 0;
	}

	vector<string> mail;
	string temp = "";

	while(!inFile.eof())
	{
		getline(inFile, temp);
		mail.push_back(temp);
	}
	


	vector<string> header;
	vector<string> body;

	cut(mail, header, body);

	ana_header(header);

	print(body);

	return 0;
}

//--------------------------------------------------------------------------------
void cut(vector<string> &mail, vector<string> &header, vector<string> &body)
{//cut mail to header and body
	for(vector<string>::iterator iter = mail.begin(); iter != mail.end(); iter++)
	{
		if((*iter).size() == 0)
		{
			break;
		}

	}
	header.assign(mail.begin(), iter);
	body.assign(iter++, mail.end());
}
//----------------------------------------------------------------------------------

//------------------------------------------------------------------------------------
void ana_header(vector<string> &header)
{
	vector<string>::iterator iter;

	for(iter = header.end()-1; iter != header.begin()-1; iter--)
	{
		if( (*iter)[0] == '\t')
		{	
			*(iter-1) += *iter;
			header.erase(iter);
		}
	}
	
	string::size_type pos;	 
	
	for(iter = header.begin(); iter != header.end(); iter++)
	{
		string temp = "";
		temp = *iter;
		for(int i = 0; i < (*iter).size(); i++)
		{
			(*iter).at(i) = (char)toupper((*iter).at(i));
			
		}
		
		
		if(pos = (*iter).find("SUBJECT:") != string::npos)
		{//subjectw1
			outFile<<*iter;
		}

		if( pos = (*iter).find("CONTENT-TYPE:") != string::npos)
		{//Content-Type
		
			if( pos = (*iter).find("MULTIPART") != string::npos)
			{	
				content_m = multipart;
				//find boundary
				string::size_type pos1 = (temp).find_first_of("\"");
				string::size_type pos2 = (temp).find_last_of("\"");
				boundary.assign(temp, pos1+1, pos2-pos1-1);
			}

			if( pos = (*iter).find("TEXT") != string::npos)
			{
				content_m = text;
			}
			
			if( pos = (*iter).find("PLAIN") != string::npos)
			{
				textype_m = plain;
			}


			if( pos = (*iter).find("HTML") != string::npos)
			{
				textype_m = html;
			}
		}

		

		if( pos = (*iter).find("CONTENT-TRANSFER-ENCODING:") != string::npos)
		{
			if( pos = (*iter).find("BASE64") != string::npos)
			{
				coding_m = Base64;
			}

			if( pos = (*iter).find("QUOTED-PRINTABLE") != string::npos)
			{
				coding_m = QP;
			}
		}
		*iter = temp;
	}
}

//------------------------------------------------------------------------------



//----------------------------------------------------------------------
//print text according to tag
void print(vector<string> &body)  
{
	vector<string>::iterator iter;
	if( content_m == text && textype_m == plain )     
	{
		if(coding_m == pure || coding_m == othercoding)
		{
			for(iter = body.begin(); iter != body.end(); iter++)
			{
				outFile << *iter;
			}
		}

		if(coding_m == Base64)
		{
			string temp4 = "";
			string temp5 = "";
			for(iter = body.begin(); iter < body.end(); iter++)
			{
				temp4 += *iter;
			}
			unBase64(temp4, temp5);
			outFile << temp5;
		}

		if(coding_m == QP)
		{
			string temp2 = ""; 
			string temp3 = "";
			for(iter = body.begin(); iter < body.end(); iter++)
			{
				if((*iter)[(*iter).size()-1] == '=')
				{
					(*iter).erase((*iter).size()-1);
				}	
				temp2 += *iter;
			}
			
			DecodeQuoted(temp2, temp3, temp2.size());
			outFile << temp3;
		}
	}
	
	//if text/html 
	if( content_m == text && textype_m == html )     //
	{
		;//
	}

	//////////////////////////////////////////////////////////
	//if multipart


	if( content_m == multipart )    //content_m == multipart
	{	
		vector<sub> SubContent;                //SubContent的每个元素对应于multipart的一个部分

		vector<vector<string>::iterator> Piterator;       //每个Piterator的元素对应于一个subcontent,纪录了子部分开始位置

		string end_boundary = boundary + "--";

		string::size_type pos;	
		int k = 0;                                                             //k-1个sub            
		
		for(iter = body.begin(); iter != body.end(); iter++)
		{
			if(pos = (*iter).find(boundary) != string::npos)
			{
				Piterator.push_back(iter);
				k++;
			}

		}
		
		vector<string> temp1;
		vector<string> sub_header;

		for(int j = 0; j < k-1; j++)
		{
			temp1.assign(Piterator[j]+1,Piterator[j+1]);
			SubContent.push_back(temp1);
			for(int i = 0; i < SubContent.size(); i++)
			{
				cut(SubContent[i], sub_header, body);
				ana_header(sub_header);
				print(body);
			}
		}
	}
}
//-------------------------------------------------------------------------
//----------------------------------------------------------------------------


//----------------------------------------------------------------------
//----------------unBase64------------------------------------------------
void unBase64(string & content, string &uncoded)
{
	char t = NULL;
	char chuue[4] = {' ',' ',' ',' '};
	char chasc[3] = {' ',' ',' '};

	for(int k = 0; k < content.size()/4; k++)
	{
		for(int j = 0; j < 4; j++)
		{
			chuue[j] = *(content.c_str()+4*k+j);
		}
		int i,k=2;
		unsigned char t=NULL;
 
		for(i=0;i<4;i++)
		{
			if((*(chuue+i)>=65)&&(*(chuue+i)<=90)) *(chuue+i)-=65;
			else if((*(chuue+i)>=97)&&(*(chuue+i)<=122)) *(chuue+i)-=71;
			else if((*(chuue+i)>=48)&&(*(chuue+i)<=57)) *(chuue+i)+=4;
			else if(*(chuue+i)==43) *(chuue+i)=62;
			else if(*(chuue+i)==47) *(chuue+i)=63;
			else if(*(chuue+i)==61) *(chuue+i)=0;
		}

		for(i=0;i<3;i++)
		{
			*(chasc+i)=*(chuue+i)<<k;
			k+=2;
			t=*(chuue+i+1)>>(8-k);
			*(chasc+i)|=t;
		}
		uncoded.append(chasc, 3);
	}
}
//-------------------------------------Base64---------------------------------------------
//----------------------------------------------------------------------------------------


//------------------------------------------------------------------------------------------
//QP译码--------------------------------------------------------------------------------------------
void DecodeQuoted(string & content, string & uncoded, int size)
{
	char ch1;
	char ch2;
	char ch3;

	string temp2 = "";

	const char * p = content.c_str();

	int i = 0;                                                  //count

	while(i < size)
	{
		if(*p == ' ')
		{
			p++;
			i++;
			continue;
		}

		if(*p == '=')
		{		
			if(i < size)
			{
				ch1 = content[++i];
				ch2 = content[++i];
				ch3 = (ch1>'9'?(ch1-'A'+10):(ch1-'0'))*16+ (ch2>'9'?(ch2-'A'+10):(ch2-'0'));
				temp2 = ch3;
				uncoded = uncoded + temp2;
				i++;
				p+=3;
			}
			else
			{
				continue;
			}
		}
		else
		{
			temp2 = *p;
			uncoded = uncoded + temp2;
			p++;
			i++;
		}
	}
}
//------------------------------------QP----------------------------------
//------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -