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