OSDN Git Service

fix some build error
[tortoisegit/TortoiseGitJp.git] / src / TortoiseMerge / AppUtils.cpp
1 // TortoiseMerge - a Diff/Patch program\r
2 \r
3 // Copyright (C) 2006-2008 - TortoiseSVN\r
4 \r
5 // This program is free software; you can redistribute it and/or\r
6 // modify it under the terms of the GNU General Public License\r
7 // as published by the Free Software Foundation; either version 2\r
8 // of the License, or (at your option) any later version.\r
9 \r
10 // This program is distributed in the hope that it will be useful,\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 // GNU General Public License for more details.\r
14 \r
15 // You should have received a copy of the GNU General Public License\r
16 // along with this program; if not, write to the Free Software Foundation,\r
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\r
18 //\r
19 #include "StdAfx.h"\r
20 #include "Registry.h"\r
21 #include "AppUtils.h"\r
22 #include "UnicodeUtils.h"\r
23 #include "ProgressDlg.h"\r
24 \r
25 #include "svn_pools.h"\r
26 #include "svn_io.h"\r
27 #include "svn_path.h"\r
28 #include "svn_diff.h"\r
29 #include "svn_string.h"\r
30 #include "svn_utf.h"\r
31 \r
32 CAppUtils::CAppUtils(void)\r
33 {\r
34 }\r
35 \r
36 CAppUtils::~CAppUtils(void)\r
37 {\r
38 }\r
39 \r
40 BOOL CAppUtils::GetVersionedFile(CString sPath, CString sVersion, CString sSavePath, CProgressDlg * progDlg, HWND hWnd /*=NULL*/)\r
41 {\r
42         CString sSCMPath = CRegString(_T("Software\\TortoiseMerge\\SCMPath"), _T(""));\r
43         if (sSCMPath.IsEmpty())\r
44         {\r
45                 // no path set, so use TortoiseSVN as default\r
46                 sSCMPath = CRegString(_T("Software\\TortoiseSVN\\ProcPath"), _T(""), false, HKEY_LOCAL_MACHINE);\r
47                 if (sSCMPath.IsEmpty())\r
48                 {\r
49                         TCHAR pszSCMPath[MAX_PATH];\r
50                         GetModuleFileName(NULL, pszSCMPath, MAX_PATH);\r
51                         sSCMPath = pszSCMPath;\r
52                         sSCMPath = sSCMPath.Left(sSCMPath.ReverseFind('\\'));\r
53                         sSCMPath += _T("\\TortoiseProc.exe");\r
54                 }\r
55                 sSCMPath += _T(" /command:cat /path:\"%1\" /revision:%2 /savepath:\"%3\" /hwnd:%4");\r
56         }\r
57         CString sTemp;\r
58         sTemp.Format(_T("%d"), hWnd);\r
59         sSCMPath.Replace(_T("%1"), sPath);\r
60         sSCMPath.Replace(_T("%2"), sVersion);\r
61         sSCMPath.Replace(_T("%3"), sSavePath);\r
62         sSCMPath.Replace(_T("%4"), sTemp);\r
63         // start the external SCM program to fetch the specific version of the file\r
64         STARTUPINFO startup;\r
65         PROCESS_INFORMATION process;\r
66         memset(&startup, 0, sizeof(startup));\r
67         startup.cb = sizeof(startup);\r
68         memset(&process, 0, sizeof(process));\r
69         if (CreateProcess(NULL, (LPTSTR)(LPCTSTR)sSCMPath, NULL, NULL, FALSE, 0, 0, 0, &startup, &process)==0)\r
70         {\r
71                 LPVOID lpMsgBuf;\r
72                 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | \r
73                         FORMAT_MESSAGE_FROM_SYSTEM | \r
74                         FORMAT_MESSAGE_IGNORE_INSERTS,\r
75                         NULL,\r
76                         GetLastError(),\r
77                         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language\r
78                         (LPTSTR) &lpMsgBuf,\r
79                         0,\r
80                         NULL \r
81                         );\r
82                 MessageBox(NULL, (LPCTSTR)lpMsgBuf, _T("TortoiseMerge"), MB_OK | MB_ICONERROR);\r
83                 LocalFree( lpMsgBuf );\r
84         }\r
85         DWORD ret = 0;\r
86         do\r
87         {\r
88                 ret = WaitForSingleObject(process.hProcess, 100);\r
89         } while ((ret == WAIT_TIMEOUT) && (!progDlg->HasUserCancelled()));\r
90         CloseHandle(process.hThread);\r
91         CloseHandle(process.hProcess);\r
92         \r
93         if (progDlg->HasUserCancelled())\r
94         {\r
95                 return FALSE;\r
96         }\r
97         if (!PathFileExists(sSavePath))\r
98                 return FALSE;\r
99         return TRUE;\r
100 }\r
101 \r
102 bool CAppUtils::CreateUnifiedDiff(const CString& orig, const CString& modified, const CString& output, bool bShowError)\r
103 {\r
104         apr_file_t * outfile = NULL;\r
105         apr_pool_t * pool = svn_pool_create(NULL);\r
106 \r
107         svn_error_t * err = svn_io_file_open (&outfile, svn_path_canonicalize(CUnicodeUtils::GetUTF8(output), pool),\r
108                 APR_WRITE | APR_CREATE | APR_BINARY | APR_TRUNCATE,\r
109                 APR_OS_DEFAULT, pool);\r
110         if (err == NULL)\r
111         {\r
112                 svn_stream_t * stream = svn_stream_from_aprfile2(outfile, false, pool);\r
113                 if (stream)\r
114                 {\r
115                         svn_diff_t * diff = NULL;\r
116                         svn_diff_file_options_t * opts = svn_diff_file_options_create(pool);\r
117                         opts->ignore_eol_style = false;\r
118                         opts->ignore_space = svn_diff_file_ignore_space_none;\r
119                         err = svn_diff_file_diff_2(&diff, svn_path_canonicalize(CUnicodeUtils::GetUTF8(orig), pool), \r
120                                 svn_path_canonicalize(CUnicodeUtils::GetUTF8(modified), pool), opts, pool);\r
121                         if (err == NULL)\r
122                         {\r
123                                 err = svn_diff_file_output_unified(stream, diff, svn_path_canonicalize(CUnicodeUtils::GetUTF8(orig), pool), \r
124                                         svn_path_canonicalize(CUnicodeUtils::GetUTF8(modified), pool),\r
125                                         NULL, NULL, pool);\r
126                                 svn_stream_close(stream);\r
127                         }\r
128                 }\r
129                 apr_file_close(outfile);\r
130         }\r
131         if (err)\r
132         {\r
133                 if (bShowError)\r
134                         AfxMessageBox(CAppUtils::GetErrorString(err), MB_ICONERROR);\r
135                 svn_error_clear(err);\r
136                 svn_pool_destroy(pool);\r
137                 return false;\r
138         }\r
139         svn_pool_destroy(pool);\r
140         return true;\r
141 }\r
142 \r
143 CString CAppUtils::GetErrorString(svn_error_t * Err)\r
144 {\r
145         CString msg;\r
146         CString temp;\r
147         char errbuf[256];\r
148 \r
149         if (Err != NULL)\r
150         {\r
151                 svn_error_t * ErrPtr = Err;\r
152                 if (ErrPtr->message)\r
153                         msg = CUnicodeUtils::GetUnicode(ErrPtr->message);\r
154                 else\r
155                 {\r
156                         /* Is this a Subversion-specific error code? */\r
157                         if ((ErrPtr->apr_err > APR_OS_START_USEERR)\r
158                                 && (ErrPtr->apr_err <= APR_OS_START_CANONERR))\r
159                                 msg = svn_strerror (ErrPtr->apr_err, errbuf, sizeof (errbuf));\r
160                         /* Otherwise, this must be an APR error code. */\r
161                         else\r
162                         {\r
163                                 svn_error_t *temp_err = NULL;\r
164                                 const char * err_string = NULL;\r
165                                 temp_err = svn_utf_cstring_to_utf8(&err_string, apr_strerror (ErrPtr->apr_err, errbuf, sizeof (errbuf)-1), ErrPtr->pool);\r
166                                 if (temp_err)\r
167                                 {\r
168                                         svn_error_clear (temp_err);\r
169                                         msg = _T("Can't recode error string from APR");\r
170                                 }\r
171                                 else\r
172                                 {\r
173                                         msg = CUnicodeUtils::GetUnicode(err_string);\r
174                                 }\r
175                         }\r
176                 }\r
177                 while (ErrPtr->child)\r
178                 {\r
179                         ErrPtr = ErrPtr->child;\r
180                         msg += _T("\n");\r
181                         if (ErrPtr->message)\r
182                                 temp = CUnicodeUtils::GetUnicode(ErrPtr->message);\r
183                         else\r
184                         {\r
185                                 /* Is this a Subversion-specific error code? */\r
186                                 if ((ErrPtr->apr_err > APR_OS_START_USEERR)\r
187                                         && (ErrPtr->apr_err <= APR_OS_START_CANONERR))\r
188                                         temp = svn_strerror (ErrPtr->apr_err, errbuf, sizeof (errbuf));\r
189                                 /* Otherwise, this must be an APR error code. */\r
190                                 else\r
191                                 {\r
192                                         svn_error_t *temp_err = NULL;\r
193                                         const char * err_string = NULL;\r
194                                         temp_err = svn_utf_cstring_to_utf8(&err_string, apr_strerror (ErrPtr->apr_err, errbuf, sizeof (errbuf)-1), ErrPtr->pool);\r
195                                         if (temp_err)\r
196                                         {\r
197                                                 svn_error_clear (temp_err);\r
198                                                 temp = _T("Can't recode error string from APR");\r
199                                         }\r
200                                         else\r
201                                         {\r
202                                                 temp = CUnicodeUtils::GetUnicode(err_string);\r
203                                         }\r
204                                 }\r
205                         }\r
206                         msg += temp;\r
207                 }\r
208                 return msg;\r
209         }\r
210         return _T("");\r
211 }\r
212 \r
213 bool CAppUtils::HasClipboardFormat(UINT format)\r
214 {\r
215         if (OpenClipboard(NULL))\r
216         {\r
217                 UINT enumFormat = 0;\r
218                 do \r
219                 {\r
220                         if (enumFormat == format)\r
221                         {\r
222                                 CloseClipboard();\r
223                                 return true;\r
224                         }\r
225                 } while((enumFormat = EnumClipboardFormats(enumFormat))!=0);\r
226                 CloseClipboard();\r
227         }\r
228         return false;\r
229 }\r
230 \r
231 \r
232 \r
233 \r