//QuotedPrintable.h #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #ifndef QuotedPrintableh #define QuotedPrintableh /* Encodes and Decodes Text in Quoted Printable format. Quoted Printable format is used to send files that are mostly plain text through e-mail systems that only handle plain text. Any special characters are replaced with an escaped sequence ('=' being the escape character). The encoded data is slightly larger than the original data. Any one encoded line of text will not exceed 80 characters. Usage: CQuotedPrintable::Encode("C:\\t.qp" ,"C:\\t.txt"); CQuotedPrintable::Decode("C:\\t1.txt","C:\\t.qp" ); */ class CQuotedPrintable { public: // Don't allow canonical behavior (i.e. don't allow this class to be passed by value) CQuotedPrintable(const CQuotedPrintable&) {}; CQuotedPrintable& operator=(const CQuotedPrintable&) {return(*this);}; static bool Encode(const char* outFilePath, const char* inFilePath) { CFile inFile,outFile; if(!inFile.Open(inFilePath,CFile::modeRead|CFile::shareDenyNone)) return false; if(!outFile.Open(outFilePath, CFile::modeCreate|CFile::modeWrite)) return false; CString S; CArchive src(& inFile,CArchive::load); CArchive dst(&outFile,CArchive::store); while(src.ReadString(S)) dst.WriteString(Encode(S)); return true; } static bool Decode(const char* outFilePath, const char* inFilePath) { CFile inFile,outFile; if(!inFile.Open(inFilePath,CFile::modeRead|CFile::shareDenyNone)) return false; if(!outFile.Open(outFilePath, CFile::modeCreate|CFile::modeWrite)) return false; CString S; CArchive src(& inFile,CArchive::load); CArchive dst(&outFile,CArchive::store); while(src.ReadString(S)) dst.WriteString(Decode(S)); return true; } static CString Encode(const char* S) { static const CString Q("?=!\"#$@[\\]^`{|}~"); CString Result; int Len=0; while(BYTE b=*S++) { if(b>127 || (Q.Find(b)!=-1) || (!*S && (b==' ' || b=='\t'))) {//Preserve trailing spaces BYTE B=b>>4; b&=0x0F; B=(B>9) ? B-10+'A' : B+'0'; b=(b>9) ? b-10+'A' : b+'0'; char Buffer[3]; char* ptr=Buffer; *ptr++=B; *ptr++=b; *ptr =0; Result=Result+'='+ Buffer; }else{ Result+=b; Len++; } if((Len==75) && b!='\r' && b!='\n') { Len=0; Result+="=\r\n"; //Soft break for long lines } } return Result+"\r\n"; //May append a NL at EOF } static CString Decode(const char* S) { CString Result; char* dst=Result.GetBufferSetLength(strlen(S)+3); if(*S) { const char* src=S; BYTE b; while(b=*src++) { if(b=='=') { if(b=*src++) { b|=0x20; BYTE B=(*src++)|0x20; b=(b>'9') ? b-'a'+10 : b-'0'; B=(B>'9') ? B-'a'+10 : B-'0'; b=(b<<4)|B; }else{ //Soft Break *dst=0; Result.ReleaseBuffer(); return Result; } } *dst++=b; } } *dst++='\r'; *dst++='\n'; *dst=0; Result.ReleaseBuffer(); return Result; } }; #endif // QuotedPrintableh