|
cpp2htm.cpp - C++ to HTML conversion program |
// cpp2htm.cpp - formats a C++ source/header file as html
// Note: this version produces output files name as
// <inputfilename.cpp> <inputfilename_cpp.htm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <cctype>
using namespace std;
void replace_lt_gt_tab_sp(char * buf);
void cpp_comment(char * buf);
void c_comment_start(char * buf);
void c_comment_end(char * buf);
bool c_comment_on = false;
int main(int argc,char *argv[])
{
char infile[32], outfile[32];
char description[80];
char buf[1024];
char* ptr2char;
if (argc<2||argc>4)
{
cerr << "Invalid syntax\nUsage: cpptohtm <filename> [optional description]\n";
exit(-1);
}
strcpy(infile,argv[1]);
if (argc > 2) strcpy(description,argv[2]);
else strcpy(description,"");
ifstream fin(infile);
if (!fin) {
cerr << "Unable to find input file: " << infile << endl;
exit(-2);
}
/* create output filename:
first look for a . between name and extension, if found replace with "_"
else just add ".htm" */
strcpy(outfile,infile);
ptr2char = strchr(outfile,'.');
*ptr2char = '_';
// if (ptr2char) *ptr2char = 0;
strcat(outfile,".htm");
ofstream fout(outfile);
fout << "<HTML>\n";
fout << "<HEAD>\n";
fout << "<TITLE>" << description << "</TITLE>\n";
fout << "</HEAD>\n";
fout << "<BODY TEXT=\"#000033\" BGCOLOR=\"#FFFFCC\">\n";
fout << "<TABLE HEIGHT=30 WIDTH=\"100%\" BGCOLOR=\"#FF99FF\">\n";
fout << "<TR>\n";
fout << "<TD ALIGN=\"LEFT\">\n";
fout << "<FONT face=\"Arial,Helvetica\" size=+1>\n";
fout << "<B>" << infile << " - " << description <<"</B></TD>\n";
fout << "</TR></TABLE><BR><Pre>\n";
// Read each line of the cpp file
while (fin.getline(buf,sizeof(buf)))
{
// replace the < > tab and space characters
replace_lt_gt_tab_sp(buf);
// change font and color on C++ style comments
cpp_comment(buf);
// change font and color on C style comments
c_comment_start(buf);
c_comment_end(buf);
// write line out to HTML file
fout << buf << endl;
}
fout << "<BR>\n";
fout << "</Pre>\n";
fout << "<BR>\n";
// insert horizontal line in HTML file
fout << "<HR>\n";
// insert Examples Home button in HTML file
fout << "<TABLE BORDER=\"0\">\n";
fout << "<TD ALIGN=center><FORM METHOD=\"LINK\" ACTION=\"examples.htm\">\n";
fout << "<INPUT TYPE=\"submit\" VALUE=\"Examples Home\">\n";
fout << "</FORM></TABLE>\n";
// Last line in HTML page
fout << "<FONT face=\"Arial,Helvetica\"\n";
fout << "<BR><H6>CIS27: Programming in C++ Instructor: Joe Bentley</H6>";
fout << "</Font>";
fout << "</Body>\n";
fout << "</Html>\n";
return 0;
}
// replace the < > tab and space characters
void replace_lt_gt_tab_sp(char * buf)
{
char temp[1024];
temp [0] = 0;
char c2[2];
c2[1] = 0;
size_t i;
for (i = 0; i < strlen(buf) ; i++) {
if (buf[i] == '<') strcat(temp,"<");
else if (buf[i] == '>') strcat(temp,">");
else {
c2[0] = buf[i];
strcat(temp,c2);
}
}
*buf=0;
strcpy(buf,temp);
}
// change color and font for c++ style comment
void cpp_comment(char * buf)
{
char temp[1024];
strcpy(temp,buf);
char* p;
p = strstr(buf,"//");
if (!p||*(p-1)=='\"') return;
if (p)
{
temp[p-buf]=0;
strcat(temp,"<I><FONT face=\"Arial,Helvetica\" color=\"00CCFF\">");
strcat(temp,p);
strcat(temp,"</FONT></I>");
}
*buf=0;
strcpy(buf,temp);
}
/* change color and font for c style comment
this looks for the "slash-asterisk" */
void c_comment_start(char * buf)
{
char temp[1024];
strcpy(temp,buf);
char* p;
if (c_comment_on) return;
p = strstr(buf,"/*");
if (!p||*(p-1)=='"') return;
if (p)
{
temp[p-buf]=0;
strcat(temp,"<I><FONT face=\"Arial,Helvetica\" color=\"9999FF\">");
strcat(temp,p);
}
*buf=0;
strcpy(buf,temp);
c_comment_on = true;
}
/* change color and font back to default after c style comment
this looks for the "asterisk-slash" */
void c_comment_end(char * buf)
{
char temp[1024];
strcpy(temp,buf);
char* p;
if (!c_comment_on) return;
p = strstr(buf,"*/");
if (!p) return;
if (p)
{
temp[p-buf+2]=0;
strcat(temp,"</FONT></I>");
strcat(temp,p+2);
}
*buf=0;
strcpy(buf,temp);
c_comment_on = false;
}
CIS27: Programming in C++ Instructor: Joe Bentley