OSDN Git Service

2780b218ef4be308d73c72cade8169b0f746c2ea
[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 "SysProgressDlg.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, CSysProgressDlg * 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\\TortoiseGit\\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 #if 0\r
105         apr_file_t * outfile = NULL;\r
106         apr_pool_t * pool = svn_pool_create(NULL);\r
107 \r
108         svn_error_t * err = svn_io_file_open (&outfile, svn_path_canonicalize(CUnicodeUtils::GetUTF8(output), pool),\r
109                 APR_WRITE | APR_CREATE | APR_BINARY | APR_TRUNCATE,\r
110                 APR_OS_DEFAULT, pool);\r
111         if (err == NULL)\r
112         {\r
113                 svn_stream_t * stream = svn_stream_from_aprfile2(outfile, false, pool);\r
114                 if (stream)\r
115                 {\r
116                         svn_diff_t * diff = NULL;\r
117                         svn_diff_file_options_t * opts = svn_diff_file_options_create(pool);\r
118                         opts->ignore_eol_style = false;\r
119                         opts->ignore_space = svn_diff_file_ignore_space_none;\r
120                         err = svn_diff_file_diff_2(&diff, svn_path_canonicalize(CUnicodeUtils::GetUTF8(orig), pool), \r
121                                 svn_path_canonicalize(CUnicodeUtils::GetUTF8(modified), pool), opts, pool);\r
122                         if (err == NULL)\r
123                         {\r
124                                 err = svn_diff_file_output_unified(stream, diff, svn_path_canonicalize(CUnicodeUtils::GetUTF8(orig), pool), \r
125                                         svn_path_canonicalize(CUnicodeUtils::GetUTF8(modified), pool),\r
126                                         NULL, NULL, pool);\r
127                                 svn_stream_close(stream);\r
128                         }\r
129                 }\r
130                 apr_file_close(outfile);\r
131         }\r
132         if (err)\r
133         {\r
134                 if (bShowError)\r
135                         AfxMessageBox(CAppUtils::GetErrorString(err), MB_ICONERROR);\r
136                 svn_error_clear(err);\r
137                 svn_pool_destroy(pool);\r
138                 return false;\r
139         }\r
140         svn_pool_destroy(pool);\r
141 #endif\r
142         return true;\r
143 }\r
144 \r
145 CString CAppUtils::GetErrorString(git_error_t * Err)\r
146 {\r
147 #if 0\r
148         CString msg;\r
149         CString temp;\r
150         char errbuf[256];\r
151 \r
152         if (Err != NULL)\r
153         {\r
154                 svn_error_t * ErrPtr = Err;\r
155                 if (ErrPtr->message)\r
156                         msg = CUnicodeUtils::GetUnicode(ErrPtr->message);\r
157                 else\r
158                 {\r
159                         /* Is this a Subversion-specific error code? */\r
160                         if ((ErrPtr->apr_err > APR_OS_START_USEERR)\r
161                                 && (ErrPtr->apr_err <= APR_OS_START_CANONERR))\r
162                                 msg = svn_strerror (ErrPtr->apr_err, errbuf, sizeof (errbuf));\r
163                         /* Otherwise, this must be an APR error code. */\r
164                         else\r
165                         {\r
166                                 svn_error_t *temp_err = NULL;\r
167                                 const char * err_string = NULL;\r
168                                 temp_err = svn_utf_cstring_to_utf8(&err_string, apr_strerror (ErrPtr->apr_err, errbuf, sizeof (errbuf)-1), ErrPtr->pool);\r
169                                 if (temp_err)\r
170                                 {\r
171                                         svn_error_clear (temp_err);\r
172                                         msg = _T("Can't recode error string from APR");\r
173                                 }\r
174                                 else\r
175                                 {\r
176                                         msg = CUnicodeUtils::GetUnicode(err_string);\r
177                                 }\r
178                         }\r
179                 }\r
180                 while (ErrPtr->child)\r
181                 {\r
182                         ErrPtr = ErrPtr->child;\r
183                         msg += _T("\n");\r
184                         if (ErrPtr->message)\r
185                                 temp = CUnicodeUtils::GetUnicode(ErrPtr->message);\r
186                         else\r
187                         {\r
188                                 /* Is this a Subversion-specific error code? */\r
189                                 if ((ErrPtr->apr_err > APR_OS_START_USEERR)\r
190                                         && (ErrPtr->apr_err <= APR_OS_START_CANONERR))\r
191                                         temp = svn_strerror (ErrPtr->apr_err, errbuf, sizeof (errbuf));\r
192                                 /* Otherwise, this must be an APR error code. */\r
193                                 else\r
194                                 {\r
195                                         svn_error_t *temp_err = NULL;\r
196                                         const char * err_string = NULL;\r
197                                         temp_err = svn_utf_cstring_to_utf8(&err_string, apr_strerror (ErrPtr->apr_err, errbuf, sizeof (errbuf)-1), ErrPtr->pool);\r
198                                         if (temp_err)\r
199                                         {\r
200                                                 svn_error_clear (temp_err);\r
201                                                 temp = _T("Can't recode error string from APR");\r
202                                         }\r
203                                         else\r
204                                         {\r
205                                                 temp = CUnicodeUtils::GetUnicode(err_string);\r
206                                         }\r
207                                 }\r
208                         }\r
209                         msg += temp;\r
210                 }\r
211                 return msg;\r
212         }\r
213 #endif\r
214         return _T("");\r
215 }\r
216 \r
217 bool CAppUtils::HasClipboardFormat(UINT format)\r
218 {\r
219         if (OpenClipboard(NULL))\r
220         {\r
221                 UINT enumFormat = 0;\r
222                 do \r
223                 {\r
224                         if (enumFormat == format)\r
225                         {\r
226                                 CloseClipboard();\r
227                                 return true;\r
228                         }\r
229                 } while((enumFormat = EnumClipboardFormats(enumFormat))!=0);\r
230                 CloseClipboard();\r
231         }\r
232         return false;\r
233 }\r
234 \r
235 \r
236 \r
237 \r