📄 cpptohtm.cpp
字号:
// FILE: CPPtoHTML.cpp
//
// DESCRIPTION:
// Create an HTML page from C or C++ code.
//
// Note that this program was written using Rogue Wave's Tools.h++ 7.0.3
// with Standard Template Library support. Converting this code to use
// the STL and String classes should not be difficult.
//
// REV: 1.0
// CREATED: 09/29/96 11:19:46
// AUTHOR: Mike Benzinger
// Disable warning about signed/unsigned mismatch.
#pragma warning( disable : 4018 )
// Disable warning about unary minus operator applied to unsigned type.
#pragma warning( disable : 4146 )
// Include the necessary header files.
#include <iostream>
#include <fstream>
#include <ctype.h>
#include <rw/cstring.h>
#include <rw/re.h>
#include <rw/tvordvec.h>
typedef RWTValOrderedVector<CString> StringVector;
// createKeywordList
//
// DESCRIPTION:
// Create a list of C and C++ keywords.
//
// PARAMETERS:
// keywordList - List of keywords to be populated
//
// RETURNS:
// Nothing
void
createKeywordList(StringVector& keywordList)
{
keywordList.insert("asm");
keywordList.insert("auto");
keywordList.insert("bool");
keywordList.insert("break");
keywordList.insert("case");
keywordList.insert("catch");
keywordList.insert("char");
keywordList.insert("class");
keywordList.insert("const");
keywordList.insert("const_cast");
keywordList.insert("continue");
keywordList.insert("default");
keywordList.insert("define");
keywordList.insert("delete");
keywordList.insert("disable");
keywordList.insert("do");
keywordList.insert("double");
keywordList.insert("dynamic_cast");
keywordList.insert("else");
keywordList.insert("enum");
keywordList.insert("error");
keywordList.insert("explicit");
keywordList.insert("extern");
keywordList.insert("false");
keywordList.insert("float");
keywordList.insert("for");
keywordList.insert("friend");
keywordList.insert("goto");
keywordList.insert("if");
keywordList.insert("ifdef");
keywordList.insert("ifndef");
keywordList.insert("include");
keywordList.insert("inline");
keywordList.insert("int");
keywordList.insert("long");
keywordList.insert("mutable");
keywordList.insert("namespace");
keywordList.insert("new");
keywordList.insert("NULL");
keywordList.insert("operator");
keywordList.insert("pragma");
keywordList.insert("private");
keywordList.insert("protected");
keywordList.insert("public");
keywordList.insert("register");
keywordList.insert("reinterpret_cast");
keywordList.insert("return");
keywordList.insert("short");
keywordList.insert("signed");
keywordList.insert("sizeof");
keywordList.insert("static");
keywordList.insert("static_cast");
keywordList.insert("struct");
keywordList.insert("switch");
keywordList.insert("template");
keywordList.insert("this");
keywordList.insert("throw");
keywordList.insert("true");
keywordList.insert("try");
keywordList.insert("typedef");
keywordList.insert("typeid");
keywordList.insert("uchar");
keywordList.insert("uint");
keywordList.insert("ulong");
keywordList.insert("union");
keywordList.insert("unsigned");
keywordList.insert("ushort");
keywordList.insert("using");
keywordList.insert("virtual");
keywordList.insert("void");
keywordList.insert("volatile");
keywordList.insert("warning");
keywordList.insert("wchar_t");
keywordList.insert("while");
}
// Define constants for the HTML colors.
const CString black = "\"#000000\"";
const CString blue = "\"#0000FF\"";
const CString cyan = "\"#00FFFF\"";
const CString green = "\"#00FF00\"";
const CString magenta = "\"#FF00FF\"";
const CString red = "\"#FF0000\"";
const CString yellow = "\"#FFFF00\"";
const CString white = "\"#FFFFFF\"";
const CString darkBlue = "\"#000080\"";
const CString darkCyan = "\"#008080\"";
const CString darkGreen = "\"#008000\"";
const CString darkMagenta = "\"#800080\"";
const CString darkRed = "\"#800000\"";
const CString darkYellow = "\"#808000\"";
const CString darkGray = "\"#808080\"";
const CString lightGray = "\"#C0C0C0\"";
// startColor
//
// DESCRIPTION:
// Compose an HTML tag to change the font color.
//
// PARAMETERS:
// color - Desired font color
//
// RETURNS:
// String defining the font color
CString
startColor(CString color)
{
return( CString("<FONT COLOR=" + color + ">") );
}
// endColor
//
// DESCRIPTION:
// Compose an HTML tag to revert back to the previous color.
//
// PARAMETERS:
// None
//
// RETURNS:
// String defining the end font HTML tag.
CString
endColor()
{
return( CString("</FONT>") );
}
// processToken
//
// DESCRIPTION:
// If a token is a keyword, bold it and change its color to blue.
//
// PARAMETERS:
// token - Token to be analyzed
// htmlLine - HTML output line
// keywordList - List of keywords
//
// RETURNS:
// Nothing
CString
processToken(CString& token, bool& tokenFound, StringVector& keywordList)
{
// If the keyword is a keyword, then bold it and change its color to blue.
// Otherwise, simply add the token to the HTML output line with no changes.
CString htmlString;
if ( keywordList.contains(token) )
htmlString = "<B>" + startColor(blue) + token + endColor() + "</B>";
else
htmlString = token;
// Reset the token to blank and indicate that no token has been found.
token = "";
tokenFound = false;
// Return the HTML string.
return( htmlString );
}
// colorInclude
//
// DESCRIPTION:
// Change the color of a system include file in the format <include.h> so
// that it is colored the same as a string.
//
// PARAMETERS:
// htmlLine - The HTML output line to be modified.
//
// RETURNS:
// Nothing
void
colorInclude(CString& htmlLine, StringVector& headerNamesList)
{
// Extract the header name from the HTML line with the format <headerFile.hpp>.
// Note that since the "<" symbol is a special character for HTML, they are all
// converted to "<".
CString headerName;
if ( htmlLine.contains("<") )
{
// Extract the header name to determine if it's in the list of
// headers to have hypertext links.
headerName = htmlLine;
headerName.replace(RWCRExpr(".*<</FONT>"), "");
headerName.replace(RWCRExpr("<FONT.*>.*"), "");
}
// Otherwise check for a quoted include file. Note, again, that since the
// '"' symbol is a special character for HTML, they are all converted to
// """.
else if (htmlLine.contains(""") )
{
// Extract the header name to determine if it's in the list of
// headers to have hypertext links.
headerName = htmlLine;
headerName.replace(RWCRExpr(".*>""), "");
headerName.replace(RWCRExpr(""<.*"), "");
}
// Force the header name to lower case to ensure that the compare will be
// done properly.
headerName.toLower();
// If the name is in the list, then create a hypertext link.
if ( headerNamesList.contains(headerName) )
{
// Create the hypertext reference name.
CString hyperTextHRef;
hyperTextHRef = headerName;
hyperTextHRef.replace(RWCRExpr("\\."), "_");
hyperTextHRef += ".htm";
// Create the hypertext link.
CString hyperTextLink;
hyperTextLink = "<A HREF=\"" + hyperTextHRef + "\">" + headerName + "</A>";
htmlLine.replace(RWCRExpr(headerName), hyperTextLink);
}
// Otherwise, if this is a include file in the form <headerFile.hpp> then
// change the color of the header name to red.
else if ( htmlLine.contains("<") )
{
// Convert the dark red color used for symbols and operators to red.
htmlLine.replace(RWCRExpr(darkRed), red, CString::all);
// Since by default, the less than symbol is colored and the coloring is
// turned off immediately afterwards, remove the </FONT> which halts the
// red coloring so that it will continue coloring the name of the file.
htmlLine.replace(RWCRExpr("lt;</FONT>"), "lt;");
// If there are slashes in the header name, then remove all coloring
// HTML flags. An example is #include <rw/cstring.h>.
RWCRExpr removeSlashColor("<FONT COLOR=" + red + ">/</FONT>");
htmlLine.replace(removeSlashColor, "/", CString::all);
// Remove the font coloring from before the greater than symbol.
htmlLine.replace(RWCRExpr("<FONT COLOR=" + red + ">>"), ">");
}
}
// main
//
// DESCRIPTION:
// Main routine for converting C or C++ code to HTML.
//
// PARAMETERS:
// argc - Number of input arguments.
//
// RETURNS:
// 0
int
main(int argc, char *argv[])
{
// Check to be sure that the input file name has been passed in.
if ( argc < 2 )
{
cout << "Usage: " << argv[0] << " sourceFile [headerNamesFile] [background]" << endl;
exit( 0 );
}
// Open the input file.
ifstream cfile(argv[1]);
if ( ! cfile )
{
cout << "Unable to open input file '" << argv[1] << "'" << endl;
exit( 1 );
}
// If a header names file is present, load the list of header files for
// which a hyper text link will be created. Note that header names should
// be specified one per line.
StringVector headerNamesList;
if ( argc >= 3 )
{
// Open the header names list file.
ifstream hdrFile(argv[2]);
if ( ! hdrFile )
{
cout << "Unable to open header names file '" << argv[2] << "'" << endl;
exit( 1 );
}
// Input all header names, convert them to lower case and add them to
// the list.
CString headerFileName;
while ( ! hdrFile.eof() )
{
hdrFile >> headerFileName;
headerFileName.toLower();
headerNamesList.insert(headerFileName);
}
}
// If the background name exists, then read it for output in the body statement.
CString background;
if ( argc >= 4 )
{
background = argv[3];
}
// Create an order vector of CStrings and add all C and C++ keywords to it.
StringVector keywordList;
createKeywordList(keywordList);
// Output the header HTML.
cout << "<HTML>" << endl;
// Strip the drive and directory off the file name. In other words, the
// following regular expression and replace function will transform the file
// name "C:\ADIR\BDIR\CDIR\CODEFILE.CPP" to "CODEFILE.CPP". Note that a
// back slash is used as an escape character in both C++ strings and regular
// expressions. Therefore, in order to have a back slash interpreted as a
// regular character, two back slashes are needed for the C++ string as well
// as the regular expression.
CString fileName = argv[1];
fileName.replace(RWCRExpr(".:"), "");
fileName.replace(RWCRExpr("\\\\(.+\\\\)*"), "");
// Create the title for the HTML document.
cout << "<HEAD>" << endl;
cout << "<TITLE>" << fileName << "</TITLE>" << endl;
cout << "</HEAD>" << endl;
// Output the body statement and the background if any.
cout << "<BODY ";
if ( background.length() > 0 )
{
cout << "BACKGROUND=\"" << background << "\">" << endl;
}
else
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -