joachim99@77: // uninstallHelper.cpp : Defines the entry point for the console application. joachim99@77: // joachim99@77: #define _CRT_SECURE_NO_DEPRECATE joachim99@77: #include "stdafx.h" joachim99@77: #include joachim99@77: #include joachim99@77: #include joachim99@77: #include joachim99@77: #include joachim99@77: #include joachim99@77: #include joachim99@77: joachim99@77: //#define __stdcall joachim99@77: joachim99@77: // For compilation download the NSIS source package and modify the following joachim99@77: // line to point to the exdll.h-file joachim99@77: #include "C:/Programme/NSIS/Contrib/ExDll/exdll.h" joachim99@77: joachim99@77: struct ReplacementItem joachim99@77: { char* fileType; char* operationType; }; joachim99@77: joachim99@77: ReplacementItem g_replacementTable[] = { joachim99@77: "text_file_delta", "xcompare", joachim99@77: "text_file_delta", "xmerge", joachim99@77: "whole_copy", "xcompare", joachim99@77: "whole_copy", "xmerge", joachim99@77: "z_text_file_delta", "xcompare", joachim99@77: "z_text_file_delta", "xmerge", joachim99@77: "z_whole_copy", "xcompare", joachim99@77: "z_whole_copy", "xmerge", joachim99@77: "_xml", "xcompare", joachim99@77: "_xml", "xmerge", joachim99@77: "_xml2", "xcompare", joachim99@77: "_xml2", "xmerge", joachim99@77: "_rftdef", "xcompare", joachim99@77: "_rftmap", "xcompare", joachim99@77: "_rftvp", "xcompare", joachim99@77: "_xtools", "xcompare", joachim99@77: 0,0 joachim99@77: }; joachim99@77: joachim99@77: struct LineItem joachim99@77: { joachim99@77: std::string fileType; joachim99@77: std::string opType; joachim99@77: std::string command; joachim99@77: std::string fileOpPart; joachim99@77: }; joachim99@77: joachim99@77: // Return true if successful, else false joachim99@77: bool readAndParseMapFile( const std::string& filename, std::list& lineItemList ) joachim99@77: { joachim99@77: // Read file joachim99@77: FILE* pFile = fopen( filename.c_str(), "r" ); joachim99@77: if (pFile) joachim99@77: { joachim99@77: fseek(pFile,0,SEEK_END); joachim99@77: int size = ftell(pFile); joachim99@77: fseek(pFile,0,SEEK_SET); joachim99@77: std::vector buf( size ); joachim99@77: fread( &buf[0], 1, size, pFile ); joachim99@77: fclose( pFile ); joachim99@77: joachim99@77: // Replace strings joachim99@77: int lineStartPos=0; joachim99@77: int wordInLine = 0; joachim99@77: LineItem lineItem; joachim99@77: for( int i=0; i& lineItemList ) joachim99@77: { joachim99@77: FILE* pFile = fopen( filename.c_str(), "w" ); joachim99@77: if (pFile) joachim99@77: { joachim99@77: std::list::const_iterator i = lineItemList.begin(); joachim99@77: for( ; i!=lineItemList.end(); ++i ) joachim99@77: { joachim99@77: const LineItem& li = *i; joachim99@77: fprintf( pFile, "%s%s\n", li.fileOpPart.c_str(), li.command.c_str() ); joachim99@77: } joachim99@77: fclose( pFile ); joachim99@77: } joachim99@77: else joachim99@77: { joachim99@77: return false; joachim99@77: } joachim99@77: return true; joachim99@77: } joachim99@77: joachim99@77: std::string toUpper( const std::string& s ) joachim99@77: { joachim99@77: std::string s2 = s; joachim99@77: joachim99@77: for( unsigned int i=0; i lineItemList; joachim99@77: bool bSuccess = readAndParseMapFile( path, lineItemList ); joachim99@77: if ( !bSuccess ) joachim99@77: { joachim99@77: std::cerr << "Error reading original map file.\n"; joachim99@77: return -1; joachim99@77: } joachim99@77: joachim99@77: // Create backup joachim99@77: if ( access( bakName.c_str(), 0 )!=0 ) // Create backup only if not exists yet joachim99@77: { joachim99@77: if ( rename( path.c_str(), bakName.c_str() ) ) joachim99@77: { joachim99@77: std::cerr << "Error renaming original map file.\n"; joachim99@77: return -1; joachim99@77: } joachim99@77: } joachim99@77: joachim99@77: std::list::iterator i = lineItemList.begin(); joachim99@77: for( ; i!=lineItemList.end(); ++i ) joachim99@77: { joachim99@77: LineItem& li = *i; joachim99@77: for (int j=0;;++j) joachim99@77: { joachim99@77: ReplacementItem& ri = g_replacementTable[j]; joachim99@77: if ( ri.fileType==0 || ri.operationType==0 ) joachim99@77: break; joachim99@77: if ( li.fileType == ri.fileType && li.opType == ri.operationType ) joachim99@77: { joachim99@77: li.command = kdiff3Command.c_str(); joachim99@77: break; joachim99@77: } joachim99@77: } joachim99@77: } joachim99@77: joachim99@77: bSuccess = writeMapFile( path, lineItemList ); joachim99@77: if ( !bSuccess ) joachim99@77: { joachim99@77: if ( rename( bakName.c_str(), path.c_str() ) ) joachim99@77: std::cerr << "Error writing new map file, restoring old file also failed.\n"; joachim99@77: else joachim99@77: std::cerr << "Error writing new map file, old file restored.\n"; joachim99@77: joachim99@77: return -1; joachim99@77: } joachim99@77: } joachim99@77: else if ( installCommand == _T("uninstall") ) joachim99@77: { joachim99@77: std::list lineItemList; joachim99@77: bool bSuccess = readAndParseMapFile( path, lineItemList ); joachim99@77: if ( !bSuccess ) joachim99@77: { joachim99@77: std::cerr << "Error reading original map file\n."; joachim99@77: return -1; joachim99@77: } joachim99@77: joachim99@77: std::list lineItemListBak; joachim99@77: bSuccess = readAndParseMapFile( bakName, lineItemListBak ); joachim99@77: if ( !bSuccess ) joachim99@77: { joachim99@77: std::cerr << "Error reading backup map file.\n"; joachim99@77: return -1; joachim99@77: } joachim99@77: joachim99@77: std::list::iterator i = lineItemList.begin(); joachim99@77: for( ; i!=lineItemList.end(); ++i ) joachim99@77: { joachim99@77: LineItem& li = *i; joachim99@77: if ((int)toUpper(li.command).find("KDIFF3")>=0) joachim99@77: { joachim99@77: std::list::const_iterator j = lineItemListBak.begin(); joachim99@77: for (;j!=lineItemListBak.end();++j) joachim99@77: { joachim99@77: const LineItem& bi = *j; // backup iterator joachim99@77: if ( li.fileType == bi.fileType && li.opType == bi.opType ) joachim99@77: { joachim99@77: li.command = bi.command; joachim99@77: break; joachim99@77: } joachim99@77: } joachim99@77: } joachim99@77: } joachim99@77: joachim99@77: bSuccess = writeMapFile( path, lineItemList ); joachim99@77: if ( !bSuccess ) joachim99@77: { joachim99@77: std::cerr << "Error writing map file."; joachim99@77: joachim99@77: return -1; joachim99@77: } joachim99@77: } joachim99@77: } joachim99@77: return 0; joachim99@77: } joachim99@77: joachim99@77: extern "C" joachim99@77: void __declspec(dllexport) nsisPlugin(HWND hwndParent, int string_size, joachim99@77: char *variables, stack_t **stacktop, joachim99@77: extra_parameters *extra) joachim99@77: { joachim99@77: //g_hwndParent=hwndParent; joachim99@77: joachim99@77: EXDLL_INIT(); joachim99@77: { joachim99@77: std::string param1( g_stringsize, ' ' ); joachim99@77: int retVal = popstring( ¶m1[0] ); joachim99@77: if ( retVal == 0 ) joachim99@77: { joachim99@77: std::string param2( g_stringsize, ' ' ); joachim99@77: retVal = popstring( ¶m2[0] ); joachim99@77: if ( retVal == 0 ) joachim99@77: install( param1.c_str(), param2.c_str() ); joachim99@77: return; joachim99@77: } joachim99@77: std::cerr << "Not enough parameters." << std::endl; joachim99@77: } joachim99@77: } joachim99@77: joachim99@77: /* joachim99@77: int _tmain(int argc, _TCHAR* argv[]) joachim99@77: { joachim99@77: if ( argc<3 ) joachim99@77: { joachim99@77: std::cout << "This program is needed to install/uninstall KDiff3 for clearcase.\n" joachim99@77: "It tries to patch the map file (clearcase-subdir\\lib\\mgrs\\map)\n" joachim99@77: "Usage 1: ccInstHelper install pathToKdiff3.exe\n" joachim99@77: "Usage 2: ccInstHelper uninstall pathToKdiff3.exe\n" joachim99@77: "Backups of the original map files are created in the dir of the map file.\n"; joachim99@77: } joachim99@77: else joachim99@77: { joachim99@77: return install( argv[1], argv[2] ); joachim99@77: } joachim99@77: joachim99@77: return 0; joachim99@77: } joachim99@77: */