⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmpmngr.cpp

📁 A File Comparer with visual output
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	for (i=bHm.begin(),ordi=0; i != bHm.end(); i++,ordi++)
	{
		compValue = i->second;
		if (start >= cHm.end()) 
		{
			 start = last_match;
			 start_ordj = last_match_orj;
		}
		for (j=start,ordj=start_ordj; j != cHm.end(); j++,ordj++)
		{
			if ((j->second == compValue) && (0 != compValue))
			{
				dHm.push_back(match_pair (ordi,ordj));
				last_match = j+1;
				last_match_orj = ordj+1;
				break;
			}
		}
		start = j;start_ordj=ordj;
	}
 
	z = dHm.size();
	for (j=dHm.begin(); j != dHm.end(); j++)
	{
		ofs<<j->first<<", "<<j->second<<"\n";
	}
	ofs.close();
	return true;
}

//////////////////////////////////////////////////////////////////////////////////////
//  A utility function 
//////////////////////////////////////////////////////////////////////////////////////
bool fillDiffLine(diffLine& dL, std::string aLine, int aLineNo, char aStatus)
{
	dL.line = aLine;
	dL.lineNo = aLineNo;
	dL.ststus = aStatus;
	return true;
}

//////////////////////////////////////////////////////////////////////////////////////
//  Print the difference information in a HTML file(s), Actually 3 frames.
//////////////////////////////////////////////////////////////////////////////////////
bool CCmpMngr::print_diff_in_HTML(std::string outDir)
{
 hashed_filemapIt j;
 int first,second,i;
 int preFirst,preSecond,blockFirst,blockSecond,bigBlock;
 std::string lineF,lineS,current_dir,leftFile,rightFile,diffFile;
 diffLine dL;
 char *c;

   	
   nAddedLines = nDeletedLines = nChangedLines = 0;
   c = new char[100];   
   leftFile = outDir + "left.html";
   rightFile = outDir + "right.html";
   diffFile = outDir + "diff.html";
   ofstream ofs(diffFile.c_str());   
   ofstream ofsL(leftFile.c_str());
   ofstream ofsR(rightFile.c_str());

   HTML_mainFrame(ofs);
	HTML_template(std::string("start"),ofsL,ofsR);
	ofsL<<baseFile.c_str()<<"\n";
	ofsR<<compFile.c_str()<<"\n";
	first=preFirst=second=preSecond = 0;
	for (j=match_pairs.begin(); j != match_pairs.end(); j++)
	{
		first = j->first; second = j->second;
		blockFirst = first - preFirst;
		blockSecond = second - preSecond;
      // Added Block
		if ((blockFirst == 1) && (blockSecond > 1))
		{
			ofsL << "<font style=\"background : #C0C0C0\" color=\"#008080\" >";
			ofsR << "<font style=\"background : #C0C0C0\" color=\"#008080\" >"; 
			for (i=1; i<blockSecond; i++) 
			{
					lineF = "(+)";
					lineS = "(+)" + compFileMap[preSecond+i].second;
					fillDiffLine(dL,compFileMap[preSecond+i].second,preSecond+i,ADDED);
					nAddedLines++;

					theDiffFile.baseFile.push_back(dL);
					theDiffFile.compFile.push_back(dL);
					// Since we are producing HTML "<" is replaced by "&lt;"
					ofsL << strReplace(lineF,"<","&lt;").c_str() << "\n"; 
					ofsR << strReplace(lineS,"<","&lt;").c_str() << "\n";

			}
			ofsL << "</font>";ofsR << "</font>";// << "\n";
		}
		// Deleted Block
		else if ((blockSecond == 1) && (blockFirst > 1))
		{
			ofsL << "<font style=\"background : #C0C0C0\" color=\"#FF0000\" >";
			ofsR << "<font style=\"background : #C0C0C0\" color=\"#FF0000\" >"; 
			for (i=1; i<blockFirst; i++) 
			{
					lineS = "(-)";
					lineF = "(-)" +baseFileMap[preFirst+i].second;
					fillDiffLine(dL,baseFileMap[preFirst+i].second,preFirst+i,DELETED);				
					nDeletedLines++;

					theDiffFile.baseFile.push_back(dL);
					theDiffFile.compFile.push_back(dL);
					// Since we are producing HTML "<" is replaced by "&lt;"
					ofsL << strReplace(lineF,"<","&lt;").c_str() << "\n"; 
					ofsR << strReplace(lineS,"<","&lt;").c_str() << "\n";
                                                                  
			}
			ofsL << "</font>";ofsR << "</font>";
		}
		//Changed Block
		else
		{

			bigBlock = (blockFirst>blockSecond)?blockFirst:blockSecond;
			// walk down the block of unmatched lines between previous match_pair and this match_pair
			// (*)ABC => means changed line
			// (+)    => Added line
			// (-)    => Deleted line
			if (bigBlock > 1)
			{
				ofsL << "<font style=\"background : #C0C0C0\" color=\"#0000FF\" >";
				ofsR << "<font style=\"background : #C0C0C0\" color=\"#0000FF\" >";
				for (i=1; i<bigBlock; i++) 
				{			
					if (preFirst+i < first) 
					{
						lineF = "(*)" +baseFileMap[preFirst+i].second;
 						fillDiffLine(dL,baseFileMap[preFirst+i].second,preFirst+i,CHANGED);
  						nChangedLines++;
					}
					else						
					{
						lineF = "(+)";
  						fillDiffLine(dL,"(+)",preFirst+i,ADDED);
  						nAddedLines++;
					}
					theDiffFile.baseFile.push_back(dL);
					if (preSecond+i < second)
					{
						lineS = "(*)" + compFileMap[preSecond+i].second;
						fillDiffLine(dL,compFileMap[preSecond+i].second,preSecond+i,CHANGED);
						//nChangedLines++;
					}
					else
					{
						lineS = "(-)";
						fillDiffLine(dL,compFileMap[preSecond+i].second,preSecond+i,DELETED);
						nDeletedLines++;
					}
					theDiffFile.compFile.push_back(dL);
					// Since we are producing HTML "<" is replaced by "&lt;"
					ofsL << strReplace(lineF,"<","&lt;").c_str() << "\n";
					ofsR << strReplace(lineS,"<","&lt;").c_str() << "\n";                                                                  
				}
				ofsL << "</font>";ofsR << "</font>";
			}
		}
		// put into the diffFile
		fillDiffLine(dL,baseFileMap[first].second,first,NOCHANGE);		
		theDiffFile.baseFile.push_back(dL);
		fillDiffLine(dL,compFileMap[second].second,second,NOCHANGE);		
		theDiffFile.compFile.push_back(dL);
		// Since we are producing HTML "<" is replaced by "&lt;"
		ofsL << strReplace(baseFileMap[first].second,"<","&lt;").c_str() << "\n";
		ofsR << strReplace(compFileMap[second].second,"<","&lt;").c_str() << "\n";

		preFirst = first; preSecond = second;
	}
	//AfxMessageBox("Printing Diff in HTML Over",MB_OK,0);
	HTML_template(std::string("end"),ofsL,ofsR);
	ofsL.close();ofsR.close();
	return true;
}

bool CCmpMngr::HTML_mainFrame(ofstream& ofs)
{
	ofs<<"<HTML>  \n";
   ofs<<"<HEAD>  \n";
   ofs<<"<TITLE>Diff View</TITLE> \n";
   ofs<<"</HEAD> \n";
   ofs<<"<FRAMESET COLS=""50%,50%"" FRAMEBORDER=""0"" BORDER=""no"" FRAMESPACING=""0""> \n";
   ofs<<"<FRAME name=""left"" SRC=""left.html"" SCROLLING=""yes"" /> \n";
   ofs<<"<FRAME name=""right"" SRC=""right.html"" SCROLLING=""yes"" /> \n";
   ofs<<"</FRAMESET> \n";
   ofs<<"</HTML> \n";

   return true;
}
	
bool CCmpMngr::HTML_template(std::string command, ofstream& ofsL, ofstream& ofsR)
{

	if (command == "start")
	{
		ofsR<<"   <html>									\n";
		ofsR<<"   <script>									\n";
		ofsR<<"   function myScroll()						\n";
		ofsR<<"   {											\n";
		ofsR<<"    if (document.layers) {//for netscape		\n";
		ofsR<<"   	 x  = window.pageXOffset;				\n";
		ofsR<<"      y  = window.pageYOffset;				\n";
		ofsR<<"    }      									\n";
		ofsR<<"    else if (document.all) {//for IE			\n";
		ofsR<<"		 x = document.body.scrollLeft;			\n";
		ofsR<<"      y = document.body.scrollTop;			\n";
		ofsR<<"    }     									\n";
		ofsR<<"    parent.left.scrollTo(x,y);				\n";
		ofsR<<"   }     									\n";
		ofsR<<"   </script>									\n";
		ofsR<<"   <script language=""JavaScript""><!--		\n";
		ofsR<<"   if (document.layers) {					\n";
		ofsR<<"   document.captureEvents(Event.MOUSEMOVE);	\n";
		ofsR<<"   document.onmousemove = myScroll;			\n";
		ofsR<<"   }      									\n";
		ofsR<<"   //--></script>							\n";
		ofsR<<"   <body onscroll=myScroll()>				\n";
		ofsR<<"   <form name=rightform> 					\n";
		ofsR<<"   <pre>										\n";
	};
	// content goes here
	if (command == "end")
	{
		ofsR<<"   </pre>									\n";
		ofsR<<"   </form>									\n";
		ofsR<<"   </body>									\n";
		ofsR<<"   </html>									\n";
	};


	if (command == "start")
	{
		ofsL<<"   <html>									\n";
		ofsL<<"   <script>									\n";
		ofsL<<"   function myScroll()						\n";
		ofsL<<"   {											\n";
		ofsL<<"    if (document.layers) {//for netscape		\n";
		ofsL<<"   	 x  = window.pageXOffset;				\n";
		ofsL<<"      y  = window.pageYOffset;				\n";
		ofsL<<"    }      									\n";
		ofsL<<"    else if (document.all) {//for IE			\n";
		ofsL<<"		 x = document.body.scrollLeft;			\n";
		ofsL<<"      y = document.body.scrollTop;			\n";
		ofsL<<"    }     									\n";
		ofsL<<"    parent.right.scrollTo(x,y);				\n";
		ofsL<<"   }     									\n";
		ofsL<<"   </script>									\n";
		ofsL<<"   <script language=""JavaScript""><!--		\n";
		ofsL<<"   if (document.layers) {					\n";
		ofsL<<"   document.captureEvents(Event.MOUSEMOVE);	\n";
		ofsL<<"   document.onmousemove = myScroll;			\n";
		ofsL<<"   }      									\n";
		ofsL<<"   //--></script>							\n";
		ofsL<<"   <body onscroll=myScroll()>				\n";
		ofsL<<"   <form name=leftform>						\n";
		ofsL<<"   <pre> 									\n";
	};
	// content goes here
	if (command == "end")
	{
		ofsL<<"   </pre>									\n";
		ofsL<<"   </form>									\n";
		ofsL<<"   </body>									\n";
		ofsL<<"   </html>									\n";
	};

	return true;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -