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