Make sure buffer is large enough for the nul terminator
[TortoiseGit.git] / src / TortoiseUDiff / TortoiseUDiff.cpp
blob6e658cc0f46e965273a74546bb377d78f4f98e84
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2008, 2010-2012, 2014-2015 - TortoiseSVN
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "stdafx.h"
20 #include "TortoiseUDiff.h"
21 #include "MainWindow.h"
22 #include "CmdLineParser.h"
23 #include "TaskbarUUID.h"
24 #include "registry.h"
25 #include "LangDll.h"
26 #include "../version.h"
27 #include "../Utils/CrashReport.h"
28 #include <algorithm>
29 #include <commctrl.h>
30 #pragma comment(lib, "comctl32.lib")
32 #pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
34 HINSTANCE hResource; // the resource dll
36 int APIENTRY _tWinMain(HINSTANCE hInstance,
37 HINSTANCE /*hPrevInstance*/,
38 LPTSTR lpCmdLine,
39 int /*nCmdShow*/)
41 SetDllDirectory(L"");
42 SetTaskIDPerUUID();
43 CRegStdDWORD loc = CRegStdDWORD(_T("Software\\TortoiseGit\\LanguageID"), 1033);
44 long langId = loc;
45 MSG msg;
46 HACCEL hAccelTable;
48 #if ENABLE_CRASHHANLDER
49 CCrashReportTGit crasher(_T("TortoiseGitUDiff ") _T(APP_X64_STRING), TGIT_VERMAJOR, TGIT_VERMINOR, TGIT_VERMICRO, TGIT_VERBUILD, TGIT_VERDATE);
50 CCrashReport::Instance().AddUserInfoToReport(L"CommandLine", GetCommandLine());
51 #endif
53 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
55 CLangDll langDLL;
56 hResource = langDLL.Init(_T("TortoiseGitUDiff"), langId);
57 if (hResource == NULL)
58 hResource = hInstance;
60 CCmdLineParser parser(lpCmdLine);
62 if (parser.HasKey(_T("?")) || parser.HasKey(_T("help")))
64 ResString rHelp(hResource, IDS_COMMANDLINEHELP);
65 MessageBox(nullptr, rHelp, L"TortoiseGitUDiff", MB_ICONINFORMATION);
66 return 0;
69 INITCOMMONCONTROLSEX used = {
70 sizeof(INITCOMMONCONTROLSEX),
71 ICC_STANDARD_CLASSES | ICC_BAR_CLASSES
73 InitCommonControlsEx(&used);
76 HMODULE hSciLexerDll = ::LoadLibrary(_T("SciLexer_tgit.dll"));
77 if (hSciLexerDll == NULL)
78 return FALSE;
80 CMainWindow mainWindow(hResource);
81 mainWindow.SetRegistryPath(_T("Software\\TortoiseGit\\UDiffViewerWindowPos"));
82 if (parser.HasVal(_T("title")))
83 mainWindow.SetTitle(parser.GetVal(_T("title")));
84 else if (parser.HasVal(_T("patchfile")))
85 mainWindow.SetTitle(parser.GetVal(_T("patchfile")));
86 else
88 ResString rPipeTitle(hResource, IDS_PIPETITLE);
89 mainWindow.SetTitle(rPipeTitle);
92 if (!mainWindow.RegisterAndCreateWindow())
94 FreeLibrary(hSciLexerDll);
95 return 0;
98 bool bLoadedSuccessfully = false;
99 if ( (lpCmdLine[0] == 0) ||
100 (parser.HasKey(_T("p"))) )
102 // input from console pipe
103 // set console to raw mode
104 DWORD oldMode;
105 GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &oldMode);
106 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), oldMode & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT));
108 bLoadedSuccessfully = mainWindow.LoadFile(GetStdHandle(STD_INPUT_HANDLE));
110 else if (parser.HasVal(_T("patchfile")))
111 bLoadedSuccessfully = mainWindow.LoadFile(parser.GetVal(_T("patchfile")));
112 else if (lpCmdLine[0] != 0)
114 // remove double quotes
115 std::wstring path = lpCmdLine;
116 path.erase(std::remove(path.begin(), path.end(), '"'), path.end());
117 bLoadedSuccessfully = mainWindow.LoadFile(path.c_str());
120 if (!bLoadedSuccessfully)
122 FreeLibrary(hSciLexerDll);
123 return 0;
126 ::ShowWindow(mainWindow.GetHWNDEdit(), SW_SHOW);
127 ::SetFocus(mainWindow.GetHWNDEdit());
129 hAccelTable = LoadAccelerators(hResource, MAKEINTRESOURCE(IDC_TORTOISEUDIFF));
131 // Main message loop:
132 while (GetMessage(&msg, NULL, 0, 0))
134 if (!TranslateAccelerator(mainWindow, hAccelTable, &msg))
136 TranslateMessage(&msg);
137 DispatchMessage(&msg);
141 FreeLibrary(hSciLexerDll);
142 return (int) msg.wParam;