Handle out of memory exception when parsing patch file
[TortoiseGit.git] / src / TortoiseProc / Patch.cpp
blobff6b11eddf0fc5943b2555668c257b6b700e2b23
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, bool useMAPI)
25 : CSendMailCombineable(To, CC, subject, bAttachment, bCombine, useMAPI)
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, m_bUseMAPI);
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 g_Git.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, m_bUseMAPI);
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 #if 0
111 int i=0;
112 while(i<4)
114 PatchFile.ReadString(str);
115 if(i==1)
116 this->m_Author=str.Right( str.GetLength() - 6 );
117 if(i==2)
118 this->m_Date = str.Right( str.GetLength() - 6 );
119 if(i==3)
120 this->m_Subject = str.Right( str.GetLength() - 8 );
122 ++i;
125 LONGLONG offset=PatchFile.GetPosition();
126 #endif
127 PatchFile.Read(m_Body.GetBuffer((UINT)PatchFile.GetLength()), (UINT)PatchFile.GetLength());
128 m_Body.ReleaseBuffer();
129 PatchFile.Close();
133 int start=0;
134 CStringA one;
135 one=m_Body.Tokenize("\n",start);
137 one=m_Body.Tokenize("\n",start);
138 if(one.GetLength()>6)
139 g_Git.StringAppend(&m_Author, (BYTE*)one.GetBuffer() + 6, CP_UTF8, one.GetLength() - 6);
141 one=m_Body.Tokenize("\n",start);
142 if(one.GetLength()>6)
143 g_Git.StringAppend(&m_Date, (BYTE*)one.GetBuffer() + 6, CP_UTF8, one.GetLength() - 6);
145 one=m_Body.Tokenize("\n",start);
146 if(one.GetLength()>9)
147 g_Git.StringAppend(&m_Subject, (BYTE*)one.GetBuffer() + 9, CP_UTF8, one.GetLength() - 9);
149 //one=m_Body.Tokenize("\n",start);
151 g_Git.StringAppend(&m_strBody, (BYTE*)m_Body.GetBuffer() + start + 1, CP_UTF8, m_Body.GetLength() - start - 1);
153 catch (CException *)
155 return -1;
158 return 0;