📄 cpp2htmldlg.cpp
字号:
szText = _T( "<table cellpadding=10><tr><td>\r\n");
HtmlFile.Write(szText,szText.GetLength());
szText = _T( "<pre class=C_CPP_file>\r\n");
HtmlFile.Write(szText,szText.GetLength());
int nReadByte;
/*Till End Of File*/
while ( TRUE )
{
/*if key flag reseted get a character from C/CPP file
this flag will be set in default case and case '/':
*/
if (!key_flag)
{
nReadByte=CppFile.Read(&ch,sizeof(unsigned char));
if(nReadByte<1)
{
break;
}
}
switch (ch)
{
case '/':
/*in this case check for comments*/
key_flag = 0;/*reset key flag*/
nReadByte=CppFile.Read(&ch,sizeof(unsigned char));
if(nReadByte<1)
{
break;
}/*get another character*/
if (ch == '/')/* // : Comment found */
{
szText = _T("<font color=\"#007766\">//");
HtmlFile.Write(szText,szText.GetLength());
while (ch != '\n')/* till new line comment the text*/
{
nReadByte=CppFile.Read(&ch,sizeof(unsigned char));
if(nReadByte<1)
{
break;
}
/* > and < cant be directly written in
the HTML file, U must be Knowing..*/
if (ch == '>')
{
szText=_T(">");
HtmlFile.Write(szText,szText.GetLength());
}
else if (ch == '<')
{
szText=_T("<");
HtmlFile.Write(szText,szText.GetLength());
}
else
{
HtmlFile.Write(&ch,sizeof(unsigned char));
}
}
szText=_T("</font>");
HtmlFile.Write(szText,szText.GetLength());
}
else if (ch == '*')/* Block Comment is found */
{
szText = _T("<font color=\"#007766\"> /*");
HtmlFile.Write(szText,szText.GetLength());
flag = 1;
while (flag)
{
/*Till closing tag of comment is found*/
nReadByte=CppFile.Read(&ch,sizeof(unsigned char));
if(nReadByte<1)
{
break;
}
/* > and < cant be directly written in
the HTML file*/
if (ch == '>')
{
szText = _T(">");
HtmlFile.Write(szText,szText.GetLength());
}
else if (ch == '<')
{
szText = _T("<");
HtmlFile.Write(szText,szText.GetLength());
}
else
{
HtmlFile.Write(&ch,sizeof(unsigned char));
}
if (ch == '*')
{
/* if '*' is found look for '/'
if '/'found then break loop otherwise
write charater and continue the loop
*/
nReadByte=CppFile.Read(&ch,sizeof(unsigned char));
if(nReadByte<1)
{
break;
}
if (ch == '/')
flag = 0;
HtmlFile.Write(&ch,sizeof(unsigned char));
}
}
szText = _T("</font>");
HtmlFile.Write(szText,szText.GetLength());
/*Comment Ended*/
}
else/* if no comment found */
{
szText = _T("<font color=\"#333399\">/</font>");
HtmlFile.Write(szText,szText.GetLength());
key_flag = 1;/*dont get next character from file
it is already there in ch*/
}
break;
/* > and < cant be directly written in
the HTML file*/
case '<':
/*convert < to <*/
key_flag = 0;
szText = _T("<font color=\"#333399\"><</font>");
HtmlFile.Write(szText,szText.GetLength());
break;
case '>':
/*convert > to >*/
key_flag = 0;
szText = _T("<font color=\"#333399\">></font>");
HtmlFile.Write(szText,szText.GetLength());
break;
/*various symbols including operators and paranthesis
to be written in a different color and size*/
case '!' :
case '$' :
case '%' :
case '&' :
case '(' :
case ')' :
case '*' :
case '+' :
case ',' :
case '-' :
case '.' :
case ':' :
case ';' :
case '=' :
case '?' :
case '@' :
case '[' :
case '\\':
case ']' :
case '^' :
case '{' :
case '|' :
case '}' :
case '~' :
key_flag = 0;
szText = _T("<font size=3 color=\"#333399\">");
HtmlFile.Write(szText,szText.GetLength());
HtmlFile.Write(&ch,sizeof(unsigned char));
szText = _T("</font>");
HtmlFile.Write(szText,szText.GetLength());
break;
case '"':
/*String*/
key_flag = 0;
flag = 1;
szText = _T("<font color=\"#ff00bf\">\"");
HtmlFile.Write(szText,szText.GetLength());
while (flag)
{
nReadByte=CppFile.Read(&ch,sizeof(unsigned char));
if(nReadByte<1)
{
break;
}
/* > and < cant be directly written in
the HTML file*/
if (ch == '>')
{
szText = _T(">");
HtmlFile.Write(szText,szText.GetLength());
}
else if (ch == '<')
{
szText = _T("<");
HtmlFile.Write(szText,szText.GetLength());
}
else if (ch == '\\')/*Escape sequence*/
{
HtmlFile.Write(&ch,sizeof(unsigned char));
nReadByte=CppFile.Read(&ch,sizeof(unsigned char));
if(nReadByte < 1)
{
break;
}
HtmlFile.Write(&ch,sizeof(unsigned char));
continue;
}
else if (ch == '"')/*string Ended*/
{
HtmlFile.Write(&ch,sizeof(unsigned char));
flag = 0;
}
else
HtmlFile.Write(&ch,sizeof(unsigned char));
}
szText = _T("</font>" );
HtmlFile.Write(szText,szText.GetLength());
break;
case '\'':
/*single quotes*/
key_flag = 0;
flag = 1;
szText = _T("<font color=\"#AA7700\">");
HtmlFile.Write(szText,szText.GetLength());
HtmlFile.Write(&ch,sizeof(unsigned char));
while (flag)
{
/*till ending ' is encountered*/
nReadByte=CppFile.Read(&ch,sizeof(unsigned char));
if(nReadByte<1)
{
break;
}
/* > and < cant be directly written in
the HTML file*/
if (ch == '>')
{
szText = _T(">");
HtmlFile.Write(szText,szText.GetLength());
}
else if (ch == '<')
{
szText = _T("<");
HtmlFile.Write(szText,szText.GetLength());
}
else if (ch == '\\')/*Escape Sequence*/
{
HtmlFile.Write(&ch,sizeof(unsigned char));
nReadByte=CppFile.Read(&ch,sizeof(unsigned char));
if(nReadByte < 1 )
{
break;
}
HtmlFile.Write(&ch,sizeof(unsigned char));
continue;
}
else
{
if (ch == '\'')/*end os Single quote*/
flag = 0;
HtmlFile.Write(&ch,sizeof(unsigned char));
}
}
szText = _T("</font>");
HtmlFile.Write(szText,szText.GetLength());
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
/*numbers Decimals, HExas and Octals*/
key_flag = 0;
flag = 1;
if (ch == '0')
{
/*Check for Hex. or Octals*/
nReadByte=CppFile.Read(&ch,sizeof(unsigned char));
if(nReadByte<1)
{
break;
}
if (ch == 'x' || ch == 'X' || ch == '0')
{
/*if Hexa || octal then till newline
|| space get char and write*/
flag = 0;
szText = _T("<font color=\"6e00aa\">0");
HtmlFile.Write(szText,szText.GetLength());
while (!(ch == ' ' || ch == '\n' || ch == ';'))
{
HtmlFile.Write(&ch,sizeof(unsigned char));
nReadByte=CppFile.Read(&ch,sizeof(unsigned char));
if(nReadByte < 1)
{
break;
}
}
key_flag = 1;/*dont get next character from file
it is already there in ch*/
szText = _T("</font>");
HtmlFile.Write(szText,szText.GetLength());
}
else
{
/*go one character back*/
CppFile.Seek(-1,CFile::current);
ch = '0';
}
}
if (flag == 1)
{
/*if number*/
szText = _T("<font color=\"c30000\"><b>");
HtmlFile.Write(szText,szText.GetLength());
HtmlFile.Write(&ch,sizeof(unsigned char));
while (flag)
{
nReadByte=CppFile.Read(&ch,sizeof(unsigned char));
if (nReadByte < 1 )
{
break;
}
/*if number*/
if (ch >= '0' && ch <= '9')
{
HtmlFile.Write(&ch,sizeof(unsigned char));
}
else
{
flag = 0;
key_flag = 1;/*dont get next character from file
it is already there in ch*/
}
}
szText = _T("</font></b>");
HtmlFile.Write(szText,szText.GetLength());
}
break;
case '#':
/*# include #define etc...*/
key_flag = 0;
flag = 1;
szText = _T("<font color=\"#c300c3\">");
HtmlFile.Write(szText,szText.GetLength());
HtmlFile.Write(&ch,sizeof(unsigned char));
nReadByte=CppFile.Read(&ch,sizeof(unsigned char));
if (nReadByte < 1)
{
break;
}
/*skip white spaces*/
while (ch == ' ' || ch == '\t')
{
HtmlFile.Write(&ch,sizeof(unsigned char));
nReadByte=CppFile.Read(&ch,sizeof(unsigned char));
if (nReadByte<1)
{
flag = 0;
break;
}
}
/*till white space is reached*/
while (!(ch == ' ' || ch == '\n' || ch == '\t'))
{
HtmlFile.Write(&ch,sizeof(unsigned char));
nReadByte=CppFile.Read(&ch,sizeof(unsigned char));
if (nReadByte < 1)
{
flag = 0;
break;
}
/*if < || > is found set key flag
so that next character is not
scanned as it is already present in ch */
if (ch == '<' || ch == '>')
{
flag = 0;
key_flag = 1;
break;
}
}
if (flag)
{
HtmlFile.Write(&ch,sizeof(unsigned char));
}
szText = _T("</font>");
HtmlFile.Write(szText,szText.GetLength());
break;
default:
/*default case checking for keywords is done here*/
key_flag = 1;
flag = 1;
char key[200];
int i = 0;
while (1)
{
/*till only Alphabet or number or underscore is found*/
if (ch <= '/' || ( ch >= '[' && ch <= '^' )
|| ch == '`'
|| ( ch >= ':' && ch <= '@' )
|| ch >= '{')
break;
else
key[i++] = ch;
nReadByte=CppFile.Read(&ch,sizeof(unsigned char));
if (nReadByte < 1)
{
break;
}
}
key[i] = '\0';
flag = 1;
/*Compare scanned string with array of Keyw||ds*/
for (i = 0; i < NUM_OF_KEYWORDS ; i++)
{
if (!(strcmp(keywords[i], key)))
{
/*if keyword match is found*/
szText = _T("<font color=\"#553399\"><b>");
HtmlFile.Write(szText,szText.GetLength());
szText.Format("%s",key);
HtmlFile.Write(szText,szText.GetLength());
szText = _T("</font></b>");
HtmlFile.Write(szText,szText.GetLength());
flag = 0;
break;
}
}
if (flag)
{
/*if keyword match is not found*/
szText.Format("%s",key);
HtmlFile.Write(szText,szText.GetLength());
}
/*if next character is not the one to be displayed
in other color*/
if (ch <= ' ' || ch >= 127 || ch == '`')
{
HtmlFile.Write(&ch,sizeof(unsigned char));
key_flag = 0;/*get next character*/
}
}
}
/*Complete HTML FILE structure*/
szText = _T(" \r\n</pre>\r\n</td></tr></table>\r\n</div>\r\n</center>\r\n</body>\r\n</html>");
HtmlFile.Write(szText,szText.GetLength());
HtmlFile.Close();
CppFile.Close();
}
catch(CFileException *e)
{
e->Delete();
}
}
void CCPP2HTMLDlg::OnAbout()
{
CAboutDlg dlg;
dlg.DoModal();
}
void CCPP2HTMLDlg::OnOK()
{
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -