2016-10-13 18:46:10 +00:00
// NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
// Copyright (C) 2010 Winch Gate Property Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// branch_patcherDlg.cpp : implementation file
//
# include "stdafx.h"
# include "branch_patcher.h"
# include "branch_patcherDlg.h"
# include "shlobj.h"
# include "direct.h"
# ifdef _DEBUG
# define new DEBUG_NEW
# undef THIS_FILE
static char THIS_FILE [ ] = __FILE__ ;
# endif
/////////////////////////////////////////////////////////////////////////////
// CBranch_patcherDlg dialog
extern CBranch_patcherApp theApp ;
const CString TEMP_DIFF_FILE = " C: \\ tempFile.diff " ;
const CString DIFF_ERRORS = " C: \\ diffLog.txt " ;
const CString PATCH_RESULT = " C: \\ patchResult.txt " ;
const CString PATCH_ERRORS = " C: \\ patchErrors.txt " ;
CBranch_patcherDlg : : CBranch_patcherDlg ( CWnd * pParent /*=NULL*/ )
: CDialog ( CBranch_patcherDlg : : IDD , pParent )
{
//{{AFX_DATA_INIT(CBranch_patcherDlg)
m_SrcDir = _T ( " " ) ;
m_DestDir = _T ( " " ) ;
m_Filename = _T ( " " ) ;
m_Tokens = _T ( " " ) ;
m_SrcDirLabel = _T ( " " ) ;
m_TargetDirLabel = _T ( " " ) ;
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp ( ) - > LoadIcon ( IDR_MAINFRAME ) ;
m_Display = NULL ;
}
void CBranch_patcherDlg : : DoDataExchange ( CDataExchange * pDX )
{
CDialog : : DoDataExchange ( pDX ) ;
//{{AFX_DATA_MAP(CBranch_patcherDlg)
DDX_Text ( pDX , IDC_SRCDIR , m_SrcDir ) ;
DDX_Text ( pDX , IDC_DESTDIR , m_DestDir ) ;
DDX_Text ( pDX , IDC_Filename , m_Filename ) ;
DDX_Text ( pDX , IDC_CurrentTokens , m_Tokens ) ;
DDX_Text ( pDX , IDC_SrcDirLabel , m_SrcDirLabel ) ;
DDX_Text ( pDX , IDC_TargetDirLabel , m_TargetDirLabel ) ;
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP ( CBranch_patcherDlg , CDialog )
//{{AFX_MSG_MAP(CBranch_patcherDlg)
ON_WM_PAINT ( )
ON_WM_QUERYDRAGICON ( )
ON_BN_CLICKED ( IDC_ButtonSetSrcDir , OnButtonSetSrcDir )
ON_BN_CLICKED ( IDC_ButtonSetDestDir , OnButtonSetDestDir )
ON_BN_CLICKED ( IDC_ButtonPatch , OnButtonPatch )
ON_BN_CLICKED ( IDC_DoPatch , OnDoPatch )
ON_WM_SIZE ( )
ON_WM_CLOSE ( )
ON_BN_CLICKED ( IDC_ButtonExtractTokens , OnButtonExtractTokens )
ON_BN_CLICKED ( IDC_ButtonClearTokens , OnButtonClearTokens )
//}}AFX_MSG_MAP
END_MESSAGE_MAP ( )
/////////////////////////////////////////////////////////////////////////////
// CBranch_patcherDlg message handlers
BOOL CBranch_patcherDlg : : OnInitDialog ( )
{
CDialog : : OnInitDialog ( ) ;
SetIcon ( m_hIcon , TRUE ) ; // Set big icon
SetIcon ( m_hIcon , FALSE ) ; // Set small icon
2017-01-02 08:58:41 +00:00
2016-10-13 18:46:10 +00:00
// Extra initialization here
RECT cltRect ;
2017-01-02 08:58:41 +00:00
GetClientRect ( & cltRect ) ,
m_Display = new CRichEditCtrl ( ) ;
m_Display - > Create ( WS_CHILD | WS_VISIBLE | WS_BORDER | WS_HSCROLL | WS_VSCROLL | ES_AUTOHSCROLL | ES_AUTOVSCROLL | ES_MULTILINE ,
CRect ( 20 , 180 , cltRect . right - 20 , cltRect . bottom - 20 ) , this , 1 ) ;
2016-10-13 18:46:10 +00:00
// Initialize directories
loadConfiguration ( ) ;
processCommandLine ( ) ;
displayTokens ( ) ;
EnteringTokens = false ;
m_SrcDirLabel = " Source Dir " ;
m_TargetDirLabel = " Target Dir " ;
2017-01-02 08:58:41 +00:00
UpdateData ( false ) ;
( ( CButton * ) GetDlgItem ( IDC_DoPatch ) ) - > EnableWindow ( FALSE ) ;
2016-10-13 18:46:10 +00:00
return TRUE ; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
2017-01-02 08:58:41 +00:00
void CBranch_patcherDlg : : OnPaint ( )
2016-10-13 18:46:10 +00:00
{
if ( IsIconic ( ) )
{
CPaintDC dc ( this ) ; // device context for painting
2017-01-02 08:58:41 +00:00
SendMessage ( WM_ICONERASEBKGND , ( WPARAM ) dc . GetSafeHdc ( ) , 0 ) ;
2016-10-13 18:46:10 +00:00
// Center icon in client rectangle
int cxIcon = GetSystemMetrics ( SM_CXICON ) ;
int cyIcon = GetSystemMetrics ( SM_CYICON ) ;
CRect rect ;
GetClientRect ( & rect ) ;
int x = ( rect . Width ( ) - cxIcon + 1 ) / 2 ;
int y = ( rect . Height ( ) - cyIcon + 1 ) / 2 ;
// Draw the icon
dc . DrawIcon ( x , y , m_hIcon ) ;
}
else
{
CDialog : : OnPaint ( ) ;
}
}
HCURSOR CBranch_patcherDlg : : OnQueryDragIcon ( )
{
2017-01-02 08:58:41 +00:00
return ( HCURSOR ) m_hIcon ;
2016-10-13 18:46:10 +00:00
}
2017-01-02 08:58:41 +00:00
void CBranch_patcherDlg : : setSrcDirectory ( const CString & s )
2016-10-13 18:46:10 +00:00
{
m_SrcDir = s ;
2017-01-02 08:58:41 +00:00
UpdateData ( false ) ;
2016-10-13 18:46:10 +00:00
}
2017-01-02 08:58:41 +00:00
void CBranch_patcherDlg : : setDestDirectory ( const CString & s )
2016-10-13 18:46:10 +00:00
{
m_DestDir = s ;
2017-01-02 08:58:41 +00:00
UpdateData ( false ) ;
2016-10-13 18:46:10 +00:00
}
2017-01-02 08:58:41 +00:00
void CBranch_patcherDlg : : OnButtonSetSrcDir ( )
2016-10-13 18:46:10 +00:00
{
DirDialog . m_strTitle = " Please choose the SOURCE directory " ;
2017-01-02 08:58:41 +00:00
if ( DirDialog . DoBrowse ( ) = = TRUE )
2016-10-13 18:46:10 +00:00
{
2017-01-02 08:58:41 +00:00
setSrcDirectory ( DirDialog . m_strPath ) ;
2016-10-13 18:46:10 +00:00
guessDestDirectory ( ) ;
}
}
2017-01-02 08:58:41 +00:00
void CBranch_patcherDlg : : OnButtonSetDestDir ( )
2016-10-13 18:46:10 +00:00
{
DirDialog . m_strTitle = " Please choose the TARGET directory " ;
2017-01-02 08:58:41 +00:00
if ( DirDialog . DoBrowse ( ) = = TRUE )
2016-10-13 18:46:10 +00:00
{
2017-01-02 08:58:41 +00:00
setDestDirectory ( DirDialog . m_strPath ) ;
2016-10-13 18:46:10 +00:00
}
}
CDirDialog : : CDirDialog ( )
{ ////////////////////////////////////////////
}
CDirDialog : : ~ CDirDialog ( )
{ ///////////////////////////////////////////
}
2017-01-02 08:58:41 +00:00
int CDirDialog : : DoBrowse ( )
2016-10-13 18:46:10 +00:00
{ /////////////////////////////////////////
2017-01-02 08:58:41 +00:00
LPMALLOC pMalloc ;
if ( SHGetMalloc ( & pMalloc ) ! = NOERROR )
{
return 0 ;
}
BROWSEINFO bInfo ;
LPITEMIDLIST pidl ;
ZeroMemory ( ( PVOID ) & bInfo , sizeof ( BROWSEINFO ) ) ;
if ( ! m_strInitDir . IsEmpty ( ) )
{
OLECHAR olePath [ MAX_PATH ] ;
ULONG chEaten ;
ULONG dwAttributes ;
HRESULT hr ;
LPSHELLFOLDER pDesktopFolder ;
// // Get a pointer to the Desktop's IShellFolder interface. //
if ( SUCCEEDED ( SHGetDesktopFolder ( & pDesktopFolder ) ) )
{
# ifndef _UNICODE
//
// IShellFolder::ParseDisplayName requires the file name be in Unicode.
//
MultiByteToWideChar ( CP_ACP , MB_PRECOMPOSED , m_strInitDir . GetBuffer ( MAX_PATH ) , - 1 , olePath , MAX_PATH ) ;
m_strInitDir . ReleaseBuffer ( - 1 ) ;
# else
wcscpy ( olePath , ( LPCTSTR ) m_strInitDir ) ;
# endif
//
// Convert the path to an ITEMIDLIST.
//
hr = pDesktopFolder - > ParseDisplayName ( NULL ,
NULL ,
olePath ,
& chEaten ,
& pidl ,
& dwAttributes ) ;
if ( FAILED ( hr ) )
{
pMalloc - > Free ( pidl ) ;
pMalloc - > Release ( ) ;
return 0 ;
}
bInfo . pidlRoot = pidl ;
}
}
bInfo . hwndOwner = NULL ;
bInfo . pszDisplayName = m_strPath . GetBuffer ( MAX_PATH ) ;
bInfo . lpszTitle = ( m_strTitle . IsEmpty ( ) ) ? " Open " : m_strTitle ;
bInfo . ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS ;
if ( ( pidl = : : SHBrowseForFolder ( & bInfo ) ) = = NULL )
{
return 0 ;
}
m_strPath . ReleaseBuffer ( ) ;
m_iImageIndex = bInfo . iImage ;
if ( : : SHGetPathFromIDList ( pidl , m_strPath . GetBuffer ( MAX_PATH ) ) = = FALSE )
{
pMalloc - > Free ( pidl ) ;
pMalloc - > Release ( ) ;
return 0 ;
}
m_strPath . ReleaseBuffer ( ) ;
pMalloc - > Free ( pidl ) ;
pMalloc - > Release ( ) ;
return 1 ;
2016-10-13 18:46:10 +00:00
}
/*
* Adapted from function by Jonah Bishop < jonahb @ nc . rr . com >
*/
BOOL SendTextToClipboard ( CString source )
{
2017-01-02 08:58:41 +00:00
// Return value is TRUE if the text was sent
// Return value is FALSE if something went wrong
if ( OpenClipboard ( NULL ) )
{
HGLOBAL clipbuffer ;
char * buffer ;
2016-10-13 18:46:10 +00:00
2017-01-02 08:58:41 +00:00
EmptyClipboard ( ) ; // Empty whatever's already there
2016-10-13 18:46:10 +00:00
2017-01-02 08:58:41 +00:00
clipbuffer = GlobalAlloc ( GMEM_DDESHARE , source . GetLength ( ) + 1 ) ;
buffer = ( char * ) GlobalLock ( clipbuffer ) ;
strcpy ( buffer , LPCSTR ( source ) ) ;
GlobalUnlock ( clipbuffer ) ;
2016-10-13 18:46:10 +00:00
2017-01-02 08:58:41 +00:00
SetClipboardData ( CF_TEXT , clipbuffer ) ; // Send the data
2016-10-13 18:46:10 +00:00
2017-01-02 08:58:41 +00:00
CloseClipboard ( ) ; // VERY IMPORTANT
return TRUE ;
}
return FALSE ;
2016-10-13 18:46:10 +00:00
}
2017-01-02 08:58:41 +00:00
void CBranch_patcherDlg : : displayMessage ( const CString & msg , bool insertAtTop )
2016-10-13 18:46:10 +00:00
{
2017-01-02 08:58:41 +00:00
if ( insertAtTop )
m_Display - > SetSel ( 0 , 0 ) ;
2016-10-13 18:46:10 +00:00
else
2017-01-02 08:58:41 +00:00
m_Display - > SetSel ( 0 , - 1 ) ;
m_Display - > ReplaceSel ( msg ) ;
2016-10-13 18:46:10 +00:00
SaveDiff = false ;
}
2017-01-02 08:58:41 +00:00
void CBranch_patcherDlg : : OnButtonPatch ( )
2016-10-13 18:46:10 +00:00
{
2017-01-02 08:58:41 +00:00
UpdateData ( true ) ;
2016-10-13 18:46:10 +00:00
CString diffCmdLine ;
2017-01-02 08:58:41 +00:00
diffCmdLine . Format ( _T ( " cvs.exe diff -c > %s 2> %s " ) , TEMP_DIFF_FILE , DIFF_ERRORS ) ; // needs a valid cvs login before! and cvs.exe in the path
2016-10-13 18:46:10 +00:00
CString text ;
2017-01-02 08:58:41 +00:00
text . Format ( _T ( " Get diff from directory %s? \n \n Command (choose No to copy it into the clipboard): \n %s " ) , m_SrcDir , diffCmdLine ) ;
2016-10-13 18:46:10 +00:00
int result ;
2017-01-02 08:58:41 +00:00
if ( ( result = : : MessageBox ( m_hWnd , text , " Confirmation " , MB_YESNOCANCEL | MB_ICONQUESTION ) ) = = IDYES )
2016-10-13 18:46:10 +00:00
{
2017-01-02 08:58:41 +00:00
if ( _chdir ( m_SrcDir ) = = 0 )
2016-10-13 18:46:10 +00:00
{
2017-01-02 08:58:41 +00:00
system ( diffCmdLine ) ;
displayFile ( TEMP_DIFF_FILE ) ;
2016-10-13 18:46:10 +00:00
SaveDiff = true ;
colorizeDiff ( ) ;
2017-01-02 08:58:41 +00:00
m_Display - > LineScroll ( 0 ) ;
( ( CButton * ) GetDlgItem ( IDC_DoPatch ) ) - > EnableWindow ( TRUE ) ;
2016-10-13 18:46:10 +00:00
2017-01-02 08:58:41 +00:00
if ( ( m_Display - > GetLineCount ( ) = = 0 ) | |
( m_Display - > GetLineCount ( ) = = 1 & & m_Display - > LineLength ( 0 ) < 2 ) )
2016-10-13 18:46:10 +00:00
{
2017-01-02 08:58:41 +00:00
displayFile ( DIFF_ERRORS ) ;
displayMessage ( " Diff is empty. \r \n If this is not the expected result: \r \n - check if the source directory is part of a CVS tree \r \n - check if cvs.exe is in your PATH \r \n - check if you are logged to the cvs server with 'cvs login' (set your home cvs directory in the HOME environment variable if needed) \r \n - check if C: \\ has enough free space and access rights to write a file. \n \n Here is the log: \n \n " , true ) ;
2016-10-13 18:46:10 +00:00
}
else
{
m_Filename = TEMP_DIFF_FILE + " : " ;
2017-01-02 08:58:41 +00:00
UpdateData ( false ) ;
2016-10-13 18:46:10 +00:00
}
}
else
{
2017-01-02 08:58:41 +00:00
displayMessage ( " Source directory not found " ) ;
2016-10-13 18:46:10 +00:00
}
}
2017-01-02 08:58:41 +00:00
else if ( result = = IDNO )
2016-10-13 18:46:10 +00:00
{
2017-01-02 08:58:41 +00:00
SendTextToClipboard ( diffCmdLine ) ;
2016-10-13 18:46:10 +00:00
}
}
static unsigned long CALLBACK MyStreamInCallback ( DWORD_PTR dwCookie , LPBYTE pbBuff , LONG cb , LONG * pcb )
{
2017-01-02 08:58:41 +00:00
CFile * pFile = ( CFile * ) dwCookie ;
2016-10-13 18:46:10 +00:00
* pcb = pFile - > Read ( pbBuff , cb ) ;
return 0 ;
}
static unsigned long CALLBACK MyStreamOutCallback ( DWORD_PTR dwCookie , LPBYTE pbBuff , LONG cb , LONG * pcb )
{
2017-01-02 08:58:41 +00:00
CFile * pFile = ( CFile * ) dwCookie ;
2016-10-13 18:46:10 +00:00
pFile - > Write ( pbBuff , cb ) ;
* pcb = cb ;
return 0 ;
}
2017-01-02 08:58:41 +00:00
void CBranch_patcherDlg : : displayFile ( const CString & filename )
2016-10-13 18:46:10 +00:00
{
2017-01-02 08:58:41 +00:00
CFile cFile ( filename , CFile : : modeRead ) ;
2016-10-13 18:46:10 +00:00
EDITSTREAM es ;
2017-01-02 08:58:41 +00:00
es . dwCookie = ( DWORD_PTR ) & cFile ;
2016-10-13 18:46:10 +00:00
es . pfnCallback = MyStreamInCallback ;
2017-01-02 08:58:41 +00:00
m_Display - > StreamIn ( SF_TEXT , es ) ;
2016-10-13 18:46:10 +00:00
}
2017-01-02 08:58:41 +00:00
void CBranch_patcherDlg : : saveFile ( const CString & filename )
2016-10-13 18:46:10 +00:00
{
2017-01-02 08:58:41 +00:00
CFile cFile ( filename , CFile : : modeCreate | CFile : : modeWrite ) ;
2016-10-13 18:46:10 +00:00
EDITSTREAM es ;
2017-01-02 08:58:41 +00:00
es . dwCookie = ( DWORD_PTR ) & cFile ;
es . pfnCallback = MyStreamOutCallback ;
m_Display - > StreamOut ( SF_TEXT , es ) ;
2016-10-13 18:46:10 +00:00
}
void CBranch_patcherDlg : : colorizeDiff ( )
{
CHARFORMAT blue ;
2017-01-02 08:58:41 +00:00
ZeroMemory ( & blue , sizeof ( blue ) ) ;
2016-10-13 18:46:10 +00:00
blue . cbSize = sizeof ( blue ) ;
blue . dwMask = CFM_COLOR ;
2017-01-02 08:58:41 +00:00
blue . crTextColor = RGB ( 0 , 0 , 0xFF ) ;
2016-10-13 18:46:10 +00:00
CHARFORMAT red ;
2017-01-02 08:58:41 +00:00
ZeroMemory ( & red , sizeof ( red ) ) ;
2016-10-13 18:46:10 +00:00
red . cbSize = sizeof ( red ) ;
red . dwMask = CFM_COLOR ;
2017-01-02 08:58:41 +00:00
red . crTextColor = RGB ( 0xFF , 0 , 0 ) ;
2016-10-13 18:46:10 +00:00
CHARFORMAT green ;
2017-01-02 08:58:41 +00:00
ZeroMemory ( & green , sizeof ( green ) ) ;
2016-10-13 18:46:10 +00:00
green . cbSize = sizeof ( green ) ;
green . dwMask = CFM_COLOR ;
2017-01-02 08:58:41 +00:00
green . crTextColor = RGB ( 0 , 0x7F , 0 ) ;
for ( int i = 0 ; i ! = m_Display - > GetLineCount ( ) ; + + i )
2016-10-13 18:46:10 +00:00
{
2017-01-02 08:58:41 +00:00
int c = m_Display - > LineIndex ( i ) ;
int l = m_Display - > LineLength ( c ) ;
m_Display - > SetSel ( c , c + l ) ;
2016-10-13 18:46:10 +00:00
CString s = m_Display - > GetSelText ( ) ;
2017-01-02 08:58:41 +00:00
if ( ! s . IsEmpty ( ) )
2016-10-13 18:46:10 +00:00
{
2017-01-02 08:58:41 +00:00
if ( s . Left ( 2 ) = = " + " )
2016-10-13 18:46:10 +00:00
{
2017-01-02 08:58:41 +00:00
m_Display - > SetSelectionCharFormat ( blue ) ;
2016-10-13 18:46:10 +00:00
}
2017-01-02 08:58:41 +00:00
else if ( s . Left ( 2 ) = = " - " )
2016-10-13 18:46:10 +00:00
{
2017-01-02 08:58:41 +00:00
m_Display - > SetSelectionCharFormat ( red ) ;
2016-10-13 18:46:10 +00:00
}
2017-01-02 08:58:41 +00:00
else if ( s . Left ( 2 ) = = " ! " )
2016-10-13 18:46:10 +00:00
{
2017-01-02 08:58:41 +00:00
m_Display - > SetSelectionCharFormat ( green ) ;
2016-10-13 18:46:10 +00:00
}
}
}
}
2017-01-02 08:58:41 +00:00
void CBranch_patcherDlg : : OnDoPatch ( )
2016-10-13 18:46:10 +00:00
{
2017-01-02 08:58:41 +00:00
UpdateData ( true ) ;
if ( SaveDiff )
2016-10-13 18:46:10 +00:00
{
// Save the diff from the richedit
2017-01-02 08:58:41 +00:00
saveFile ( TEMP_DIFF_FILE ) ;
2016-10-13 18:46:10 +00:00
}
// Apply the patch
CString patchCmdLine , concatOutput , delPatchErrors ;
2017-01-02 08:58:41 +00:00
patchCmdLine . Format ( _T ( " %spatch.exe -c -p%u --verbose < %s > %s 2> %s " ) , PatchExeDir , CvsDiffDirLevel , TEMP_DIFF_FILE , PATCH_RESULT , PATCH_ERRORS ) ; // needs patch.exe in the path
concatOutput . Format ( _T ( " copy %s+%s %s " ) , PATCH_RESULT , PATCH_ERRORS , PATCH_RESULT ) ;
delPatchErrors . Format ( _T ( " del %s " ) , PATCH_ERRORS ) ;
2016-10-13 18:46:10 +00:00
CString text ;
2017-01-02 08:58:41 +00:00
text . Format ( _T ( " Patch diff to directory %s? \n \n Command (choose No to copy it into the clipboard): \n %s " ) , ( LPCTSTR ) m_DestDir , ( LPCTSTR ) patchCmdLine ) ;
2016-10-13 18:46:10 +00:00
int result ;
2017-01-02 08:58:41 +00:00
if ( ( result = : : MessageBox ( m_hWnd , text , _T ( " Confirmation " ) , MB_YESNOCANCEL | MB_ICONQUESTION ) ) = = IDYES )
2016-10-13 18:46:10 +00:00
{
2017-01-02 08:58:41 +00:00
if ( _chdir ( m_DestDir ) = = 0 )
2016-10-13 18:46:10 +00:00
{
2017-01-02 08:58:41 +00:00
system ( patchCmdLine ) ;
system ( concatOutput ) ;
system ( delPatchErrors ) ;
displayFile ( PATCH_RESULT ) ;
2016-10-13 18:46:10 +00:00
SaveDiff = false ;
2017-01-02 08:58:41 +00:00
m_Display - > LineScroll ( 0 ) ;
2016-10-13 18:46:10 +00:00
2017-01-02 08:58:41 +00:00
if ( ( m_Display - > GetLineCount ( ) = = 0 ) | |
( m_Display - > GetLineCount ( ) = = 1 & & m_Display - > LineLength ( 0 ) < 2 ) )
2016-10-13 18:46:10 +00:00
{
CString s ;
2017-01-02 08:58:41 +00:00
s . Format ( _T ( " Nothing was patched. \r \n If this is not the expected result: \r \n - check if the good patch.exe is in %s \r \n - check if %s exists (generated by previous diff) \r \n - check if C: \\ has enough free space and access rights to write a file. " ) , TEMP_DIFF_FILE ) ;
displayMessage ( s ) ;
2016-10-13 18:46:10 +00:00
}
else
{
m_Filename = PATCH_RESULT + " : " ;
2017-01-02 08:58:41 +00:00
UpdateData ( false ) ;
2016-10-13 18:46:10 +00:00
}
}
else
{
2017-01-02 08:58:41 +00:00
displayMessage ( " Target directory not found " ) ;
2016-10-13 18:46:10 +00:00
}
}
2017-01-02 08:58:41 +00:00
else if ( result = = IDNO )
2016-10-13 18:46:10 +00:00
{
2017-01-02 08:58:41 +00:00
SendTextToClipboard ( patchCmdLine ) ;
2016-10-13 18:46:10 +00:00
}
}
2017-01-02 08:58:41 +00:00
void CBranch_patcherDlg : : OnSize ( UINT nType , int cx , int cy )
2016-10-13 18:46:10 +00:00
{
CDialog : : OnSize ( nType , cx , cy ) ;
2017-01-02 08:58:41 +00:00
if ( m_Display )
2016-10-13 18:46:10 +00:00
{
RECT cltRect ;
2017-01-02 08:58:41 +00:00
GetClientRect ( & cltRect ) ;
2016-10-13 18:46:10 +00:00
CRect dispRect ;
2017-01-02 08:58:41 +00:00
m_Display - > MoveWindow ( 20 , 180 , cltRect . right - 40 , cltRect . bottom - 200 , true ) ;
2016-10-13 18:46:10 +00:00
}
}
2017-01-02 08:58:41 +00:00
void CBranch_patcherDlg : : OnClose ( )
2016-10-13 18:46:10 +00:00
{
saveConfiguration ( ) ;
CDialog : : OnClose ( ) ;
}
void CBranch_patcherDlg : : processCommandLine ( )
{
CString cmdLine = theApp . m_lpCmdLine ;
2017-01-02 08:58:41 +00:00
if ( ! cmdLine . IsEmpty ( ) )
2016-10-13 18:46:10 +00:00
{
2017-01-02 08:58:41 +00:00
setSrcDirectory ( cmdLine ) ;
2016-10-13 18:46:10 +00:00
guessDestDirectory ( ) ;
}
}
void CBranch_patcherDlg : : guessDestDirectory ( )
{
2017-01-02 08:58:41 +00:00
if ( hasTokens ( ) )
2016-10-13 18:46:10 +00:00
{
CString dir = m_SrcDir ;
2017-01-02 08:58:41 +00:00
if ( dir . Find ( " \\ " + Token1 + " \\ " , 0 ) ! = - 1 )
2016-10-13 18:46:10 +00:00
{
2017-01-02 08:58:41 +00:00
dir . Replace ( " \\ " + Token1 + " \\ " , " \\ " + Token2 + " \\ " ) ;
setDestDirectory ( dir ) ;
2016-10-13 18:46:10 +00:00
}
2017-01-02 08:58:41 +00:00
else if ( dir . Find ( " \\ " + Token2 + " \\ " , 0 ) ! = - 1 )
2016-10-13 18:46:10 +00:00
{
2017-01-02 08:58:41 +00:00
dir . Replace ( " \\ " + Token2 + " \\ " , " \\ " + Token1 + " \\ " ) ;
setDestDirectory ( dir ) ;
2016-10-13 18:46:10 +00:00
}
}
}
void CBranch_patcherDlg : : extractDirTokens ( )
{
int beginOfToken1 , beginOfToken2 , endOfToken1 , endOfToken2 ;
CString text ;
2017-01-02 08:58:41 +00:00
UpdateData ( true ) ;
2016-10-13 18:46:10 +00:00
// Search backward from the end until a different substring is found
2017-01-02 08:58:41 +00:00
int c1 = m_SrcDir . GetLength ( ) - 1 ;
int c2 = m_DestDir . GetLength ( ) - 1 ;
while ( ( c1 > = 0 ) & & ( c2 > = 0 ) & & ( m_SrcDir [ c1 ] = = m_DestDir [ c2 ] ) )
2016-10-13 18:46:10 +00:00
{
- - c1 ;
- - c2 ;
}
2017-01-02 08:58:41 +00:00
2016-10-13 18:46:10 +00:00
// Test if both strings are identical
2017-01-02 08:58:41 +00:00
if ( ( c1 < 0 ) | | ( c2 < 0 ) )
2016-10-13 18:46:10 +00:00
{
Token1 = m_SrcDir ;
Token2 = m_DestDir ;
return ;
}
2017-01-02 08:58:41 +00:00
endOfToken1 = c1 + 1 ;
endOfToken2 = c2 + 1 ;
2016-10-13 18:46:10 +00:00
// Search forward from the beginning until a different substring is found
c1 = 0 ;
c2 = 0 ;
2017-01-02 08:58:41 +00:00
while ( ( c1 < m_SrcDir . GetLength ( ) ) & & ( c2 < m_DestDir . GetLength ( ) ) & & ( m_SrcDir [ c1 ] = = m_DestDir [ c2 ] ) )
2016-10-13 18:46:10 +00:00
{
+ + c1 ;
+ + c2 ;
}
2017-01-02 08:58:41 +00:00
if ( ( c1 = = m_SrcDir . GetLength ( ) ) | | ( c2 = = m_DestDir . GetLength ( ) ) )
2016-10-13 18:46:10 +00:00
{
return ; // both strings are identical (should not occur again)
}
// If one of the token is empty, expand both downto the closest backslash
2017-01-02 08:58:41 +00:00
if ( ( c1 = = endOfToken1 ) | | ( c2 = = endOfToken2 ) )
2016-10-13 18:46:10 +00:00
{
- - c1 ;
2017-01-02 08:58:41 +00:00
while ( ( c1 > = 0 ) & & ( m_SrcDir [ c1 ] ! = ' \\ ' ) )
2016-10-13 18:46:10 +00:00
{
- - c1 ;
}
+ + c1 ;
- - c2 ;
2017-01-02 08:58:41 +00:00
while ( ( c2 > = 0 ) & & ( m_DestDir [ c2 ] ! = ' \\ ' ) )
2016-10-13 18:46:10 +00:00
{
- - c2 ;
}
+ + c2 ;
}
beginOfToken1 = c1 ;
beginOfToken2 = c2 ;
2017-01-02 08:58:41 +00:00
Token1 = m_SrcDir . Mid ( beginOfToken1 , endOfToken1 - beginOfToken1 ) ;
Token2 = m_DestDir . Mid ( beginOfToken2 , endOfToken2 - beginOfToken2 ) ;
2016-10-13 18:46:10 +00:00
2017-01-02 08:58:41 +00:00
//endExtract:
/*if ( hasTokens() )
{
text . Format ( " The two branch tokens '%s' and '%s' are now stored " , Token1 , Token2 ) ;
: : MessageBox ( m_hWnd , text , " Tokens found " , MB_OK | MB_ICONINFORMATION ) ;
return ;
} */
//notfound:
//::MessageBox( m_hWnd, "Tokens not found in the directories", "Extracting tokens", MB_OK | MB_ICONEXCLAMATION );
2016-10-13 18:46:10 +00:00
}
void CBranch_patcherDlg : : loadConfiguration ( )
{
// Read the dest directory from the registry
2017-01-02 08:58:41 +00:00
free ( ( void * ) theApp . m_pszRegistryKey ) ;
theApp . m_pszRegistryKey = _tcsdup ( _T ( " Nevrax " ) ) ;
2016-10-13 18:46:10 +00:00
CString savedSrcDir , savedTargetDir , token1 , token2 ;
2017-01-02 08:58:41 +00:00
if ( m_SrcDir . IsEmpty ( ) )
2016-10-13 18:46:10 +00:00
{
2017-01-02 08:58:41 +00:00
savedSrcDir = theApp . GetProfileString ( _T ( " " ) , _T ( " SourceDir " ) ) ;
if ( ! savedSrcDir . IsEmpty ( ) )
2016-10-13 18:46:10 +00:00
{
2017-01-02 08:58:41 +00:00
setSrcDirectory ( savedSrcDir ) ;
2016-10-13 18:46:10 +00:00
}
}
2017-01-02 08:58:41 +00:00
savedTargetDir = theApp . GetProfileString ( _T ( " " ) , _T ( " TargetDir " ) ) ;
if ( ! savedTargetDir . IsEmpty ( ) )
2016-10-13 18:46:10 +00:00
{
2017-01-02 08:58:41 +00:00
setDestDirectory ( savedTargetDir ) ;
2016-10-13 18:46:10 +00:00
}
2017-01-02 08:58:41 +00:00
Token1 = theApp . GetProfileString ( _T ( " " ) , _T ( " Token1 " ) ) ;
Token2 = theApp . GetProfileString ( _T ( " " ) , _T ( " Token2 " ) ) ;
PatchExeDir = theApp . GetProfileString ( _T ( " " ) , _T ( " PatchExeDir " ) ) ;
CvsDiffDirLevel = theApp . GetProfileInt ( _T ( " " ) , _T ( " CvsDiffDirLevel " ) , 1 ) ; // 0 for old version of CVS, 1 for new version of CVS
2016-10-13 18:46:10 +00:00
}
void CBranch_patcherDlg : : saveConfiguration ( )
{
2017-01-02 08:58:41 +00:00
UpdateData ( true ) ;
if ( ! EnteringTokens )
2016-10-13 18:46:10 +00:00
{
2017-01-02 08:58:41 +00:00
theApp . WriteProfileString ( _T ( " " ) , _T ( " SourceDir " ) , m_SrcDir ) ;
theApp . WriteProfileString ( _T ( " " ) , _T ( " TargetDir " ) , m_DestDir ) ;
2016-10-13 18:46:10 +00:00
}
2017-01-02 08:58:41 +00:00
theApp . WriteProfileString ( _T ( " " ) , _T ( " Token1 " ) , Token1 ) ;
theApp . WriteProfileString ( _T ( " " ) , _T ( " Token2 " ) , Token2 ) ;
2016-10-13 18:46:10 +00:00
}
2017-01-02 08:58:41 +00:00
void CBranch_patcherDlg : : OnButtonExtractTokens ( )
2016-10-13 18:46:10 +00:00
{
2017-01-02 08:58:41 +00:00
if ( ! EnteringTokens )
2016-10-13 18:46:10 +00:00
{
EnteringTokens = true ;
2017-01-02 08:58:41 +00:00
extractDirTokens ( ) ;
2016-10-13 18:46:10 +00:00
SrcDirBackup = m_SrcDir ;
TargetDirBackup = m_DestDir ;
m_SrcDir = Token1 ;
m_DestDir = Token2 ;
m_SrcDirLabel = " Enter Token 1 " ;
m_TargetDirLabel = " Enter Token 2 " ;
m_Filename = " The tokens above were extracted from the directories. " ;
2017-01-02 08:58:41 +00:00
( ( CButton * ) GetDlgItem ( IDC_ButtonExtractTokens ) ) - > SetWindowText ( _T ( " Store Tokens " ) ) ;
GetDlgItem ( IDC_TopText ) - > ShowWindow ( SW_HIDE ) ;
GetDlgItem ( IDC_ButtonClearTokens ) - > EnableWindow ( FALSE ) ;
GetDlgItem ( IDC_ButtonPatch ) - > ShowWindow ( SW_HIDE ) ;
GetDlgItem ( IDC_ButtonPatch ) - > EnableWindow ( FALSE ) ;
GetDlgItem ( IDC_DoPatch ) - > ShowWindow ( SW_HIDE ) ;
GetDlgItem ( IDC_Group ) - > ShowWindow ( SW_HIDE ) ;
UpdateData ( false ) ;
2016-10-13 18:46:10 +00:00
}
else
{
2017-01-02 08:58:41 +00:00
UpdateData ( true ) ;
2016-10-13 18:46:10 +00:00
EnteringTokens = false ;
Token1 = m_SrcDir ;
Token2 = m_DestDir ;
m_SrcDirLabel = " Source Dir " ;
m_TargetDirLabel = " Target Dir " ;
m_SrcDir = SrcDirBackup ;
m_DestDir = TargetDirBackup ;
2016-11-20 13:51:11 +00:00
m_Filename . Empty ( ) ;
2017-01-02 08:58:41 +00:00
( ( CButton * ) GetDlgItem ( IDC_ButtonExtractTokens ) ) - > SetWindowText ( _T ( " Enter Tokens " ) ) ;
GetDlgItem ( IDC_TopText ) - > ShowWindow ( SW_SHOW ) ;
GetDlgItem ( IDC_ButtonClearTokens ) - > EnableWindow ( TRUE ) ;
GetDlgItem ( IDC_ButtonPatch ) - > ShowWindow ( SW_SHOW ) ;
GetDlgItem ( IDC_ButtonPatch ) - > EnableWindow ( TRUE ) ;
GetDlgItem ( IDC_DoPatch ) - > ShowWindow ( SW_SHOW ) ;
GetDlgItem ( IDC_Group ) - > ShowWindow ( SW_SHOW ) ;
2016-10-13 18:46:10 +00:00
displayTokens ( ) ;
}
}
2017-01-02 08:58:41 +00:00
void CBranch_patcherDlg : : OnButtonClearTokens ( )
2016-10-13 18:46:10 +00:00
{
2016-11-20 13:51:11 +00:00
Token1 . Empty ( ) ;
Token2 . Empty ( ) ;
2016-10-13 18:46:10 +00:00
displayTokens ( ) ;
}
bool CBranch_patcherDlg : : hasTokens ( ) const
{
2017-01-02 08:58:41 +00:00
return ! ( Token1 . IsEmpty ( ) | | Token2 . IsEmpty ( ) ) ;
2016-10-13 18:46:10 +00:00
}
void CBranch_patcherDlg : : displayTokens ( )
{
2017-01-02 08:58:41 +00:00
( ( CButton * ) GetDlgItem ( IDC_ButtonClearTokens ) ) - > EnableWindow ( hasTokens ( ) ? TRUE : FALSE ) ;
if ( hasTokens ( ) )
2016-10-13 18:46:10 +00:00
{
m_Tokens = " Tokens: ' " + Token1 + " ' and ' " + Token2 + " ' " ;
}
else
{
m_Tokens = " No token " ;
}
2017-01-02 08:58:41 +00:00
UpdateData ( false ) ;
2016-10-13 18:46:10 +00:00
}