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

📄 cpp2htmldlg.cpp

📁 把C++源代码转换成HTML
💻 CPP
📖 第 1 页 / 共 2 页
字号:
				// Otherwise, simply add the character to the HTML output line.
				else StrAppend(html,*p);
			}
			else if(strchr(strNumber,*p))// Catch all numeric characters.
			{
				// If this character is not in a number, comment, string or token,
				// then add font coloring for a number to the HTML output line and
				// indicate that the start of a number has been found.
				if(!bParseNumber && !bInComment && !bInString && !bParseToken)
				{
					strcat(html,"<FONT COLOR=#800080>");
					bParseNumber = TRUE;
				}
				// If this is part of a token, then add it it to the token.  Otherwise
				// simply add it to the HTML output line.
				if(bParseToken) StrAppend(strToken,*p);
				else StrAppend(html,*p);
			}
			else if(strchr(strOperator,*p))// Catch most of the C and C++ operators.
			{
				// If a token was being built, process it and add it to the HTML
				// output line.
				if(bParseToken)
				{
					for(j=0;j<99;j++) if(strcmp(m_strKeywords[j],strToken)==0) break;
					if(j<99)
					{
						strcat(html,"<B><FONT COLOR=#0000FF>");
						strcat(html,strToken);
						strcat(html,"</FONT></B>");
					}
					else strcat(html,strToken);
					// Reset the token to blank and indicate that no token has been found.
					strToken[0] = '\0';
					bParseToken = FALSE;
				}
				// If the operator is not in a comment or a string, then turn the
				// coloring to dark red.          
				if (!bInComment && !bInString) strcat(html,"<FONT COLOR=#800000>");
				// The '&', '<' and '>' are special symbols in HTML and must be
				// handled differently.  For all other operators, add them directly
				// to the HTML output line.          
				switch(*p)
				{
				case '&':
					strcat(html,"&amp;");
					break;
				case '<':
					strcat(html,"&lt;");
					break;
				case '>':
					strcat(html,"&gt;");
					break;
				default:
					StrAppend(html,*p);
				}
				// If the operator is not in a comment or a string, then turn off
				// the coloring.          
				if (!bInComment && !bInString) strcat(html,"</FONT>");
			}
			else if(*p=='/')
			{
				// Process a '/' which can be a division operator or the start or end
				// of a comment string.
				if(bParseToken)
				{
					for(j=0;j<99;j++) if(strcmp(m_strKeywords[j],strToken)==0) break;
					if(j<99)
					{
						if(m_bBoldKryword)
						{
							strcat(html,"<B><FONT COLOR=#0000FF>");
							strcat(html,strToken);
							strcat(html,"</FONT></B>");
						}
						else
						{
							strcat(html,"<FONT COLOR=#0000FF>");
							strcat(html,strToken);
							strcat(html,"</FONT>");
						}
					}
					else strcat(html,strToken);
					// Reset the token to blank and indicate that no token has been found.
					strToken[0] = '\0';
					bParseToken = FALSE;
				}
				// Ensure that the character is not in a comment or a string.
				if(!bInComment && !bInString)
				{
					// If the current character is not the last character in the line.
					if(p[1])
					{
						// If the next character is also a slash, then indicate that
						// this is a C++ style end-of-line comment.
						if (p[1] == '/' )
						{
							bInComment = TRUE;
							bInEOLComment = TRUE;
						}
						// Otherwise, if the next character is an asterisk, then indicate
						// that this is a C style comment.
						else if(p[1]=='*') bInComment = TRUE;
					}
					// If this is a comment, then set the color to dark green for
					// comments and italicize the text.
					if(bInComment)
					{
						if(m_bItalicComment) strcat(html,"<FONT COLOR=#008000><I>");
						else strcat(html,"<FONT COLOR=#008000>");
					}
					// Otherwise, color the division operator dark red.
					else strcat(html,"<FONT COLOR=#800000>");
				}
				//Add the character to the HTML output line.          
				StrAppend(html,*p);
				// If this is not in a string, then determine if this is the end of
				// a comment string.
				if(!bInString)
				{
					// If the character is part of a comment.
					if(bInComment)
					{
						// If it is not in a C++ style end-of-line comment.
						if(!bInEOLComment)
						{
							// If this is not the start of the line and the previous
							// character was an asterisk, then turn off the italicized
							// text, end the comment coloring and turn off the comment flag.
							if(p>line+1 && *(p-1)=='*')
							{
								if(m_bItalicComment) strcat(html,"</I></FONT>");
								else strcat(html,"</FONT>");
								bInComment = FALSE;
							}
						}
					}
					// Otherwise this is a division operator, end operator coloring.
					else strcat(html,"</FONT>");
				}
			}
			else if(strchr("\"'",*p))// Process a quote or tick mark.
			{
				if(!pStrStartPos) bContinueString = FALSE;
				else if(*p != *pStrStartPos) bContinueString = TRUE;
				else
				{
					bInAStr = FALSE;
					pQuotes = pStrStartPos;
					for(;pQuotes<=p;)
					{
						if(*pQuotes==*p)
						{
							bInAStr = !bInAStr;
							++pQuotes;
						}
						else if(*pQuotes=='\\')
						{
							bESC = FALSE;
							for(;pQuotes<=p && *pQuotes=='\\';++pQuotes) bESC = !bESC;
							if(bESC && *pQuotes==*p) bInAStr = !bInAStr;
						}
						else ++pQuotes;
					}
					bContinueString = bInAStr;
				}
				// If a token was being built, process it and add it to the HTML
				// output line.
				if(bParseToken)
				{
					for(j=0;j<99;j++) if(strcmp(m_strKeywords[j],strToken)==0) break;
					if(j<99)
					{
						if(m_bBoldKryword)
						{
							strcat(html,"<B><FONT COLOR=#0000FF>");
							strcat(html,strToken);
							strcat(html,"</FONT></B>");
						}
						else
						{
							strcat(html,"<FONT COLOR=#0000FF>");
							strcat(html,strToken);
							strcat(html,"</FONT>");
						}
					}
					else strcat(html,strToken);
					// Reset the token to blank and indicate that no token has been found.
					strToken[0] = '\0';
					bParseToken = FALSE;
				}
				// Determine if the character is escaped by checking to see if the
				// previous character was a backslash.
				bEscapeChar = (p>line && *(p-1)=='\\');
				// If the previous character was an escape character, ensure that
				// the character prior to that was not a back slash also.  A double
				// backslash indicates a backslash inside a string.  If this is the
				// case, then the quote or tick are not escaped.
				if(bEscapeChar && p>line+1 && *(p-2)=='\\') bEscapeChar = FALSE;
				// If the character is not in a comment or a string and it is not
				// escaped, then color the text read and indicate that a string has
				// been started.  Note that startString is used instead of inString.
				// This is done to avoid turning the string coloring off once the
				// character has been added to the HTML output string.
				if(!bInComment && !bInString && !bEscapeChar)
				{
					strcat(html,"<FONT COLOR=#FF0000>");
					bStartString = TRUE;
					pStrStartPos = p;
				}
				// Since the quote is a special HTML character and must be handled
				// differently.  Simply add the tick to the output line.
				if(*p=='\"') strcat(html,"&quot;");
				else strcat(html,"\'");
				// If this is not a comment and the character is part of a string
				// and it is not escaped, then turn off the quoted coloring.
				if(!bInComment && bInString && ! bEscapeChar && !bContinueString)
				{
					strcat(html,"</FONT>");
					bInString = FALSE;
					pStrStartPos = NULL;
				}
				// If a string has been started, then turn on the in string indicator.
				if(bStartString)
				{
					bStartString = FALSE;
					bInString = TRUE;
				}
			}
			else if(*p=='#')
			{
				// If a token was being built, process it and add it to the HTML
				// output line.
				if(bParseToken)
				{
					for(j=0;j<99;j++) if(strcmp(m_strKeywords[j],strToken)==0) break;
					if(j<99)
					{
						if(m_bBoldKryword)
						{
							strcat(html,"<B><FONT COLOR=#0000FF>");
							strcat(html,strToken);
							strcat(html,"</FONT></B>");
						}
						else
						{
							strcat(html,"<FONT COLOR=#0000FF>");
							strcat(html,strToken);
							strcat(html,"</FONT>");
						}
					}
					else strcat(html,strToken);
					// Reset the token to blank and indicate that no token has been found.
					strToken[0] = '\0';
					bParseToken = FALSE;
				}
				// If the character is not in a comment or a string, then add the
				// pound sign with a blue color and bold it.
				if(!bInComment && !bInString)
				{
					if(m_bBoldKryword) strcat(html,"<FONT COLOR=#0000FF><B>#</B></FONT>");
					else strcat(html,"<FONT COLOR=#0000FF>#</B></FONT>");
					bPreprocessor = TRUE;
				}
				// Otherwise, add the character to the HTML output line.
				else StrAppend(html,*p);//"#";
			}
			else// Catch all other characters.
			{
				// If a token was being built, process it and add it to the HTML
				// output line.
				if(bParseToken)
				{
					for(j=0;j<99;j++) if(strcmp(m_strKeywords[j],strToken)==0) break;
					if(j<99)
					{
						if(m_bBoldKryword)
						{
							strcat(html,"<B><FONT COLOR=#0000FF>");
							strcat(html,strToken);
							strcat(html,"</FONT></B>");
						}
						else
						{
							strcat(html,"<FONT COLOR=#0000FF>");
							strcat(html,strToken);
							strcat(html,"</FONT>");
						}
					}
					else strcat(html,strToken);
					// Reset the token to blank and indicate that no token has been found.
					strToken[0] = '\0';
					bParseToken = FALSE;
				}
				// Add the character unedited to the HTML output line.
				if(*p!='\t') StrAppend(html,*p);
				else
				{
					int nCount = strlen(html);
					nCount %= 4;
					if(!nCount) strcat(html,"    ");
					else for(nCount = 4 - nCount;nCount;--nCount) StrAppend(html,' ');
				}
			}
		}//end of for(;n>0;n--)
		// If a number is being processed and it is not part of a token, then
		// terminate coloring.      
		if(bParseNumber	&& !bParseToken)
		{
			strcat(html,"</FONT>");
			bParseNumber = FALSE;
		}
		// If there are any unprocessed tokens, process them now.
		if(bParseToken)
		{
			for(j=0;j<99;j++) if(strcmp(m_strKeywords[j],strToken)==0) break;
			if(j<99)
			{
				if(m_bBoldKryword)
				{
					strcat(html,"<B><FONT COLOR=#0000FF>");
					strcat(html,strToken);
					strcat(html,"</FONT></B>");
				}
				else
				{
					strcat(html,"<FONT COLOR=#0000FF>");
					strcat(html,strToken);
					strcat(html,"</FONT>");
				}
			}
			else strcat(html,strToken);
			// Reset the token to blank and indicate that no token has been found.
			strToken[0] = '\0';
			bParseToken = FALSE;
		}
		// If a C++ style end of line comment was started, then turn off the coloring
		// and italicizing in the HTML output line and indicate that no comments
		// are in effect.
		if(bInEOLComment)
		{
			if(m_bItalicComment) strcat(html,"</I></FONT>");
			else strcat(html,"</FONT>");
			bInComment = FALSE;
			bInEOLComment = FALSE;
		}
		// If this line has an include preprocessor directive, then determine if
		// the system style includes should be colored as strings.
		if(bPreprocessor)
		{
			strInclude = strstr(html,"include");
			if(strInclude)
			{
				strInclude += 5;
				for(;*strInclude;strInclude++)
				{
					if(*strInclude>' ') break;
				}
				if(strncmp(strInclude,"&quot",5)==0)
				{
					strcpy(tmp,"<A HREF=\"");
					pStr = tmp;
					for(strInclude+=5;*strInclude!='&';) *pStr++ = *strInclude++;
					*pStr = '\0';
					for(pStr = strchr(tmp,'.');pStr;)
					{
						if(pStr[1]=='\\' || pStr[1]=='.') pStr = strchr(tmp,'.');
						else break;
					}
					if(pStr) strcpy(pStr,".htm");
					strcat(tmp,"\">");
					strcat(tmp,src);
					strcat(tmp,"</A>");
					strInclude = strstr(html,"include");
					strInclude += 5;//strlen("include");
					for(;strInclude;strInclude++)
					{
						if(*strInclude>' ') break;
					}
					strInclude += 5;//strlen("include");
					pStr = strchr(strInclude,'&');
					pStr = strchr(pStr,'&');
					//pStr += 5;//strlen("&quot");
					memmove(pStr,pStr+(int)(strlen(tmp)-(DWORD)pStr+(DWORD)strInclude),strlen(pStr));
					for(pStr=&tmp[0];*pStr;) *strInclude++ = *pStr++;
				}
			}
		}
		// Output the HTML output line to standard out.
		WriteString(hDst,html);
		//WriteString(hDst,"\r\n");
	}
	// End the HTML page by turning of preformatting and ending the page.
	WriteString(hDst,"</PRE>\r\n");
	WriteString(hDst,"</BODY>\r\n");
	WriteString(hDst,"</HTML>\r\n");
	CloseHandle(hDst);
	GlobalFree(pBuf);
	ShowStatus("转换完成");
}

void CCpp2HtmlDlg::OnBrowseCpp()
{
	CFileDialog dlg(TRUE);
	dlg.m_ofn.lpstrFilter = "C++ source files\0*.inl;*.cpp;*.cxx;*.c;*.c++;*.h;*.hpp;*.hxx\0All Files(*.*)\0*.*\0";
	dlg.m_ofn.lpstrTitle = "Select C++ source file to convert";
	if(dlg.DoModal()==IDOK)
	{
		SetDlgItemText(IDC_CPPFILE,dlg.GetPathName());
		char buf[512];
		strcpy(buf,dlg.GetPathName());
		PSTR p = strrchr(buf,'.');
		if(!p)
		{
			p = buf;
			p += strlen(p);
			*p++ = '.';
		}
		else
		{
			*p = '_';
			p += strlen(p);
		}
		strcpy(p,".htm");
		SetDlgItemText(IDC_HTMLFILE,buf);
		ShowStatus("选择了源文件");
	}
}

void CCpp2HtmlDlg::OnBrowseHtml() 
{
	CFileDialog dlg(FALSE);
	dlg.m_ofn.lpstrFilter = "html files\0*.htm;*.html\0All Files(*.*)\0*.*\0";
	dlg.m_ofn.lpstrTitle = "Specify converted HTML file name";
	if(dlg.DoModal()==IDOK)
	{
		SetDlgItemText(IDC_HTMLFILE,dlg.GetPathName());
		ShowStatus("选择了目的文件");
	}
}

void CCpp2HtmlDlg::ShowStatus(LPCSTR strMsg)
{
	SetDlgItemText(IDC_STATUS,strMsg);
}

void CCpp2HtmlDlg::WriteString(HANDLE hFile, LPCSTR strMsg)
{
	DWORD dw;
	WriteFile(hFile,strMsg,strlen(strMsg),&dw,NULL);
}

int CCpp2HtmlDlg::LineRead(PSTR& pBuf, PSTR strLine)
{
#ifdef _DEBUG
	for(int i=0;*pBuf!='\r' && *pBuf!='\n' && *pBuf;++i)
	{
		*strLine++ = *pBuf++;
		*strLine = '\0';
	}
	for(;(*pBuf=='\r' || *pBuf=='\n') && *pBuf;++i)
	{
		*strLine++ = *pBuf++;
		//++pBuf;
		*strLine = '\0';
	}
#else
	for(int i=0;*pBuf!='\r' && *pBuf!='\n' && *pBuf;++i) *strLine++ = *pBuf++;
	for(;(*pBuf=='\r' || *pBuf=='\n') && *pBuf;++i) *strLine++ = *pBuf++;//++pBuf;
#endif
	*strLine = '\0';
	return i;
}

void CCpp2HtmlDlg::StrAppend(PSTR str, char c)
{
	str += strlen(str);
	*str++ = c;
	*str = '\0';
}

⌨️ 快捷键说明

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