Use CUnicodeUtils::GetUnicode instead of CGit::StringAppend if possible
[TortoiseGit.git] / src / TortoiseProc / Patch.cpp
blobc6e5a687adec8ef9396818782dcc3726507a5ac4
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2013 - TortoiseGit
4 // Copyright (C) 2011-2013 Sven Strickroth <email@cs-ware.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "stdafx.h"
22 #include "Patch.h"
24 CSendMailPatch::CSendMailPatch(CString &To, CString &CC, CString &subject, bool bAttachment, bool bCombine)
25 : CSendMailCombineable(To, CC, subject, bAttachment, bCombine)
29 CSendMailPatch::~CSendMailPatch()
33 int CSendMailPatch::SendAsSingleMail(CTGitPath &path, CGitProgressList * instance)
35 ASSERT(instance);
37 CString pathfile(path.GetWinPathString());
38 CPatch patch;
39 if (patch.Parse(pathfile))
41 instance->ReportError(_T("Could not open/parse ") + pathfile);
42 return -2;
45 CString body;
46 CStringArray attachments;
47 if (m_bAttachment)
48 attachments.Add(pathfile);
49 else
50 body = patch.m_strBody;
52 return SendMail(path, instance, m_sSenderName, m_sSenderMail, m_sTo, m_sCC, patch.m_Subject, body, attachments);
55 int CSendMailPatch::SendAsCombinedMail(CTGitPathList &list, CGitProgressList * instance)
57 ASSERT(instance);
59 CStringArray attachments;
60 CString body;
61 for (int i = 0; i < list.GetCount(); ++i)
63 CPatch patch;
64 if (patch.Parse((CString &)list[i].GetWinPathString()))
66 instance->ReportError(_T("Could not open/parse ") + list[i].GetWinPathString());
67 return -2;
69 if (m_bAttachment)
71 attachments.Add(list[i].GetWinPathString());
72 body += patch.m_Subject;
73 body += _T("\r\n");
75 else
77 try
79 CGit::StringAppend(&body, (BYTE*)patch.m_Body.GetBuffer(), CP_UTF8, patch.m_Body.GetLength());
81 catch (CMemoryException *)
83 instance->ReportError(_T("Out of memory. Could not parse ") + list[i].GetWinPathString());
84 return -2;
88 return SendMail(CTGitPath(), instance, m_sSenderName, m_sSenderMail, m_sTo, m_sCC, m_sSubject, body, attachments);
91 CPatch::CPatch()
95 CPatch::~CPatch()
99 int CPatch::Parse(CString &pathfile)
101 CString str;
103 m_PathFile = pathfile;
105 CFile PatchFile;
107 if (!PatchFile.Open(m_PathFile, CFile::modeRead))
108 return -1;
110 PatchFile.Read(m_Body.GetBuffer((UINT)PatchFile.GetLength()), (UINT)PatchFile.GetLength());
111 m_Body.ReleaseBuffer();
112 PatchFile.Close();
116 int start=0;
117 CStringA one;
118 one=m_Body.Tokenize("\n",start);
120 if (start == -1)
121 return -1;
122 one=m_Body.Tokenize("\n",start);
123 if(one.GetLength()>6)
124 CGit::StringAppend(&m_Author, (BYTE*)one.GetBuffer() + 6, CP_UTF8, one.GetLength() - 6);
126 if (start == -1)
127 return -1;
128 one=m_Body.Tokenize("\n",start);
129 if(one.GetLength()>6)
130 CGit::StringAppend(&m_Date, (BYTE*)one.GetBuffer() + 6, CP_UTF8, one.GetLength() - 6);
132 if (start == -1)
133 return -1;
134 one=m_Body.Tokenize("\n",start);
135 if(one.GetLength()>9)
137 CGit::StringAppend(&m_Subject, (BYTE*)one.GetBuffer() + 9, CP_UTF8, one.GetLength() - 9);
138 while (m_Body.GetLength() > start && m_Body.GetAt(start) == _T(' '))
140 one = m_Body.Tokenize("\n", start);
141 CGit::StringAppend(&m_Subject, (BYTE*)one.GetBuffer(), CP_UTF8, one.GetLength());
145 if (start + 1 < m_Body.GetLength())
146 CGit::StringAppend(&m_strBody, (BYTE*)m_Body.GetBuffer() + start + 1, CP_UTF8, m_Body.GetLength() - start - 1);
148 catch (CException *)
150 return -1;
153 return 0;