Copy the code on this page for the C++2HTML converter.
To run it you obviously have to compile it...
Usage : c2html filename.cpp
/****************************************************************************
* C++ TO HTML CONVERTER *
****************************************************************************/
#include <iostream.h> //include header files
#include <fstream.h>
#include <string.h>
#include <process.h>
//---------------------------------------------------------------------------
ifstream fin; //fin is input file stream
ofstream fout; //fout is output file stream
char *argv1[2];
int df_cl=0; //1=definition, 0=calls
int df_cl_no=0; //count for defns+calls
//*--------------------------------------------------------------------------
class TOKENS
{ int fn_len [20]; //function definition lengths
char fn_name [20][20]; //function definition names
int str_cur [20]; //start location of definition
int end_cur [20]; //end location of definition
int comma_no[20]; //no.of arguments
int x;
int defns; //local counter for no. of defns
int d1;
static int y; //count for total defns
static int no;
static int calls; //count for total calls
static int strcur [100]; //universal arrays for all token names
static int dfcl [100]; //defn or call
static int defno [100]; //if defn, then defn no.
static int comno [100]; //no.of arguments
static int clno; //global counter for definitions
char *t;
int invalid_flag; //for any invalid token or function name
public :
TOKENS(){ x = 1; calls = 1; defns = 1; y = 1; clno=0; no=1; }
void get_token(char *t,int m,int n,int dfno);
int get_defn(int n);
void get_calls(int c,int d);
void sort();
void adjust_clno();
void adjust_classdef();
int check_tag(int cursol);
};
/*-------------------------------------------------------------------------*/
class TAGS
{
public :
void write_str_tag(char *t,int l); //write start tag
void write_end_tag(char *t,int i); //write end tag
void write_ref_tag(int l); //write reference tag
void write_link_tag(int l); //write link tag
void write_comment_tag();
void enter(void){ fout << "\n"; }; //write new line
};
/*-------------------------------------------------------------------------*/
/*----------------------TAG WRITING FUNCTIONS------------------------------*/
void TAGS :: write_str_tag(char *t,int l)
{ fout << "<"; //writes start html tag
for(int i=0;i!=l;i++) fout << *(t+i); //with *t as tag body
fout << ">"; //and l as it's length
}
void TAGS :: write_end_tag(char *t,int l)
{ fout << "</""; //writes end html tag
for(int i=0;i!=l;i++) fout << *(t+i); //with *t as tag body
fout << ">"; //and l as it's length
}
void TAGS :: write_ref_tag(int l)
{ char ch; //l = df_cl_no
fout << "<font color=\"ef3830\"><A NAME=\""; //<A NAME="#2"> </A>
fout << l;
fout << "\"> </AA>";
while (ch != '\n')
{ fin.get(ch); fout << ch; }
fout << "</ffont>";
}
void TAGS :: write_link_tag(int l) //l = df_cl_no
{ char ch; //puts link to lth
fout << "<A HREF=\"#"; fout << l; fout << "\">"; //definition
while(ch!=')') //<A HREF="2"> </A>
{ fin.get(ch);
if (ch == '"') fout << """;
else fout << ch;
}
fout << "</AA>";
}
/*-------------------------------------------------------------------------*/
/*------------------------CHECK TYPE OF TAG TO INSERT----------------------*/
int TOKENS:: check_tag(int cursor)
{
if (strcur[no]==cursor) //is it location to insert tag?
{
if (dfcl[no]==1) df_cl=1; //definition
else df_cl=0; //call
df_cl_no=defno[no];
no++;
return (1); //1 - valid insertion point
}
else
return (0); //0 - invalid cursor location
}
/*-------------------------------------------------------------------------*/
void TOKENS:: adjust_clno(){ clno--; }
/*-------------------------------------------------------------------------*/
void TOKENS:: adjust_classdef(){ d1 = defns; d1--; }
/*-------------------------------------------------------------------------*/
/*--------------------SORT ALL NAMES IN ASCENDING ORDER--------------------*/
void TOKENS:: sort()
{ int j, k, tmp;
y--;
calls--;
cout << "\n";
while(y != 0)
{ cout << "," << strcur[y]; y--; }
for (j=0;j<calls;j++)
{ for(k=j+1;k<calls+1;k++)
{ if (strcur[j] > strcur[k])
{ tmp = strcur[j]; strcur[j] = strcur[k]; strcur[k] = tmp;
tmp = dfcl[j]; dfcl[j] = dfcl[k]; dfcl[k] = tmp;
tmp = defno[j]; defno[j] = defno[k]; defno[k] = tmp;
tmp = comno[j]; comno[j] = comno[k]; comno[k] = tmp;
}
}
}
cout <<"\nall fns names in ascending order :\n"; //test display the sorted names
for(y=1;y!=calls+1;y++)
{
cout << "/"" << strcur[y] << "," << dfcl[y] << "," << defno[y] ;
cout << "," << comno[y];
}
cout << "\ny=" <<y << " ,calls=" << calls << "clno =" << clno;
}
/*-------------------------------------------------------------------------*/
/*-----------------scans for token name and calls get_name-----------------*/
void TOKENS:: get_token(char *t,int len,int n,int dfno)
{ //char*t = pointer to token name,"void"
//len = length of token,4
//n = defn=0 or call=1
//dfno = definition no.,local voidx,intx
int i = 0; //reset array
int comment_flag = 0; //comment_flag
char ch = ' ';
char prev_ch = ' '; //to check if there's space before 'void'
while (fin) //xxxvoid, void fn, voidxxx
{ i = 0;
int illegal = 0; //not a valid token xxxvoid,voidxxx,//void
invalid_flag = 0;
int comma=0; //to count no of argumants
if (comment_flag == 1) //if it is a comment skip line
{
while(ch != '\n')
fin.get(ch);
}
comment_flag = 0; //reset comment_flag
do
{
prev_ch = ch; //to get first char 'v'oid or fin whichever
//comes first
fin.get(ch);
if ((ch == '/'') && (prev_ch == '/'')) //for comment line
comment_flag = 1;
}
while ((fin) && ch != *(t+i) && (comment_flag != 1));
if ((prev_ch != ' ')&&(prev_ch != '\n')&&(prev_ch != '\t')&&(comment_flag == 1))
{
illegal = 1;
}
int tmpcur = fin.tellg();
while( ch == *(t+i) && illegal != 1 )
{
fin.get(ch);
if( i==len-1) //for void len=4
{
if ( n==1) //called by get_calls
{ strcur[y] = tmpcur; //
if (strcur[y]==str_cur[dfno]) //is it call or defn
{
dfcl[y]=1;
defno[y]=clno; //if defn attach
comno[y]=comma_no[dfno]; //attributes
y++; calls++; //total no. of calls
}
else
{ while(ch!=')')
{ fin.get(ch);
if (ch==',') comma++; //count args
}
if (comma==comma_no[dfno]) //check if args match
{ dfcl[y]=0; //if yes then only it is a
defno[y]=clno; //valid call for the fn_defn
comno[y]=comma_no[dfno];
y++; calls++;
}
}
}
else
if (ch == ' ') //display function defn name
{
if (get_defn(n)==0)
{ cout << "\nx=" << x;
cout << ">" << str_cur[x] << "," << end_cur[x] << "," << fn_len[x] << "," << comma_no[x] << "--";
for (int j=0;j != fn_len[x];j++)
cout << *(fn_name[x]+j);
x++; //counts
defns++; //no. of defns
}
}
else
cout << " "; //not a valid definition
}
i++;
} //end of while
} //end of while(fin)
} //end of get_token
/*-------------------------------------------------------------------------*/
/*-------------------GETTING FUNCTION DEFN --------------------------------*/
int TOKENS :: get_defn(int n) //n=0-call/n=1-defn/n=2-class
{ int invalid_flag = 0; //reset fn_len,invalid_flag,class_flag
int class_flag = 0;
comma_no[x] = 0; //comma_no,no. of args
fn_len [x] = 0;
char tmp[20];
char ch = ' ';
fin.get(ch);
while(ch == ' ') // int ijk ()
{ fin.get(ch); }
if (ch == ':') //for object return type
invalid_flag=1; //TAGS TOKENS :: retobj() { }
int save_cur=fin.tellg()-1; //save cursor before scanning line for ::
//cursor - 1 position
while (ch != '\n') //search line for :
{ //and set class_flag if : found
fin.get(ch);
if (ch == ':')
class_flag = 1;
}
fin.seekg(save_cur); //restore cursor
switch(class_flag) //if class or no class defn
{
case 1: //void class_name :: putdata();
if (invalid_flag==1)
break;
while(ch != ':') //get first :
fin.get(ch);
ch = 'x'; //reset ch coz we have to search ':' again
while(ch != ':') //second : of ::
fin.get(ch);
fin.get(ch); //character after ::
while(ch == ' ') //ignore ' '
fin.get(ch);
str_cur[x] = fin.tellg();
while(ch != ' ' && ch != '(' && invalid_flag != 1)
{
tmp[fn_len[x]] = ch; //read in the fn name
fin.get(ch);
if (ch == ';'||ch==')'||ch=='\n')
invalid_flag = 1; //disallow any invalid char
fn_len[x]++;
}
if (invalid_flag==1)
break;
tmp[fn_len[x]]='\0'; //insert string terminator
strcpy(fn_name[x],tmp); //update fn_name
if (ch != '(') // int name = 0;
{ // int name ();
fin.get(ch); //ignore spaces after name till (
while (ch == ' ' && ch != '(')
fin.get(ch);
if ((ch == '\n')||(ch=='['))
invalid_flag = 1; //disallow absence of '('
if (ch == '(')
break;
}
if (invalid_flag==1)
break;
while(ch != ')') //count the no. of arguments
{
fin.get(ch);
if (ch == ',')
comma_no[x]++;
}
end_cur[x] = fin.tellg(); //save end of fn_name
break;
case 0: //not class definition
//void fn_name (arg1,arg2)
fin.get(ch);
str_cur[x] = fin.tellg();
if (n==2) //for class names
{
while(ch != ' ' && ch != '\n' && invalid_flag != 1)
{
tmp[fn_len[x]] = ch; //read in fn_name
fin.get(ch);
if (ch == ';') invalid_flag = 1;
fn_len[x]++;
}
}
else //for function names
{
while(ch != ' ' && ch != '(' && invalid_flag != 1)
{
tmp[fn_len[x]] = ch; //read in fn_name
fin.get(ch);
if (ch == ';'||ch==')'||ch=='\n') invalid_flag = 1;
fn_len[x]++;
}
}
if (invalid_flag==1)
break;
tmp[fn_len[x]]='\0';
strcpy(fn_name[x],tmp);
if(n==2) //validity check for class
{ //names
while(ch==' '||ch=='\n'||ch=='\t')
fin.get(ch);
if (ch!='{')
invalid_flag=1;
}
if (n==2) //if it's class defn
break; //no need for validity check
if (ch != '(') // int name = 0;
{ // ignore spcaes till '('
while (ch == ' ') // if no '(' set invalid_flag
fin.get(ch);
if (ch != '(')
invalid_flag = 1;
} //void get(arg1,arg2)
if (invalid_flag==1)
break;
while(ch != ')') // {,ignore blanks
{
fin.get(ch);
if (ch == ',') //count arguments
comma_no[x]++;
}
end_cur[x] = fin.tellg(); //save end of fn_name
do
{
fin.get(ch);
}
while((ch == ' ')||(ch == '\n')||(ch == '\t'));
//waiting for start of fn.
// i.e {
//if anything else betn ) and
if (ch != '{') //{ then invalid @#
invalid_flag = 1; // ...arg2)
// // { - cancelled
break; // { - put again
} //end of switch
return(invalid_flag);
} //end of get_name
/*-------------------------------------------------------------------------*/
/*-----------GETTING FUNCTION CALLS AND LOCATIONS--------------------------*/
void TOKENS:: get_calls(int c,int d)
{ cout << "\n--------";
if (d==1)
d=d1; //start from fn_names
for(int dfno=d;dfno!=defns;dfno++) //and not class names
{ fin.open(argv1[1],ios::in); //d is for neglecting
//the return types in
//class
get_token(fn_name[dfno],fn_len[dfno],c,dfno); //c=1 for get_calls
fin.close(); //c=0 for get defns
if (c!=0) //for class defns
clno++; //do not incr. clno
}
}
//clno - which defn is being operated --global counter
//dfno - (or defns) is local counter to voidx,intx
/****************************************************************************
* MAIN PROGRAM *
****************************************************************************/
int TOKENS :: y; //static declarations
int TOKENS :: no;
int TOKENS :: calls;
int TOKENS :: strcur [100];
int TOKENS :: dfcl [100];
int TOKENS :: defno [100];
int TOKENS :: comno [100];
int TOKENS :: clno;
void main(int argc, char* argv[])
{
/*-------------------------------------------------------------------------*/
cout << "testing";
if (argc != 2)
{ cerr << "\n Format: c2html filename.cpp";
exit(-1);
}
cout << "\nargv[1] : " << argv[1];
argv1[1]=argv[1];
cout << "\nargv[1] : " << argv[1];
/*-------------------------------------------------------------------------*/
char c, ch;
//@ int i = 0; int j = 0;
//@ int n = 20;
int comment=0;
// cout << "\nPlease enter the name of the file :";
// cin.getline (argv[1],n);
/*-------------------------------------------------------------------------*/
TOKENS voidx, intx, classx, includex; //token names as objects
fin.open(argv[1],ios::in);
if (!fin)
{ cerr << "\n Cannot open " << argv[1];
exit(-1);
}
cout << "\nvoid :- ";
voidx.get_token("void",4,0,0); //get all void defns
fin.close();
fin.open(argv[1],ios::in);
voidx.get_calls(1,0); //get all void calls
fin.close();
//---------------------------------------------------------------------------
fin.open(argv[1],ios::in);
cout << "\nint :- ";
intx.get_token("int",3,0,0); //get all int defns
fin.close();
fin.open(argv[1],ios::in);
intx.adjust_clno();
intx.get_calls(1,0); //get all int calls
fin.close();
/*-------------------------------------------------------------------------*/
fin.open(argv[1],ios::in);
cout << "\nclass :- "; //get all class names
classx.get_token("class",5,2,0); //n=2->class defn
//class complex
fin.close();
classx.adjust_classdef();
fin.open(argv[1],ios::in);
//using names as return types
classx.get_calls(0,0); //get defns : complex fn1()
fin.close();
fin.open(argv[1],ios::in);
classx.adjust_clno();
classx.get_calls(1,1); //get calls to fns with class
//names as return types
fin.close(); //i.e returning objects
//d=1 to start from class defns
//& not from class names
//---------------------------------------------------------------------------
/*-------------------------------------------------------------------------*/
// fin.open(argv[1],ios::in);
//
// cout << "\n#include :- ";
// includex.get_token("#include",8,0,0); //get all #include defns
//
// fin.close();
// fin.open(argv[1],ios::in);
//
// includex.get_calls(1,0);
//
// fin.close();
//---------------------------------------------------------------------------
//sort all the token names
voidx.sort(); //in a single array as they
//appear in CPP file
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
TOKENS t1;
TAGS s1;
fin.open(argv[1],ios::in);
fout.open("test1.htm"); //open o/p file for writing
/*---------------WRITE BASIC HTML TAGS-------------------------------------*/
s1.write_str_tag("html",4); s1.enter();
s1.write_str_tag("head",4); s1.enter();
s1.write_str_tag("title",5); fout << (argv[1]);
s1.write_end_tag("title",5); s1.enter();
s1.write_end_tag("head",4); s1.enter();
fout << "<META NAME=\"generator\" CONTENT=\"C++2HTML converter\"><br>";
fout << "<META NAME=\"keywords\" CONTENT=\"C++,HTML,converter,c2html,c\"><br>";
fout << "<LINK REV=\"ashish\" HREF=\"mailto:ashfreddie@hotmail.com\"><br>";
fout << "<Body bgcolor=\"#afafbf\" text=\"#000000\" link=\"#ff3828\" vlink=\"#ff3828\"><br>";
s1.enter();
s1.write_str_tag("pre",3); s1.enter();
fout << "<Font Face = \"courier new\" >";
while (fin) //while not EOF
{
int tag = t1.check_tag(fin.tellg()+1); //cursor matched
if (tag == 1) //insert tag
{ if (df_cl==1) s1.write_ref_tag (df_cl_no);
else
if (df_cl==0) s1.write_link_tag(df_cl_no);
}
fin.get(ch); //read item0 to screen one line at a time, ignores breaks
if (ch == '\n')
{ // fout << "\n";
if (comment==1)
{ fout << "</FFONT>";
comment=0;
}
}
else
if (ch == '/'') //if /*or // comment
{
if (comment==0)
{
//@ char prev_ch = ch;
fin.get(ch);
if ((ch == '/'')||(ch=='*')&&(comment==0))
{ fout << "<FONT color=\"0000ff\">" << "/"";
comment=1;
}
else
fout << "/"" << ch ;
}
}
if (ch == '"')
{
fout << """; //add line breaks if "\n"
}
else
if (ch == '<')
{
fout << "<"; //add line breaks if "\n"
}
else
if (ch == '>')
{
fout << ">"; //add line breaks if "\n"
}
else
fout << ch; //write to file item1
}
s1.write_end_tag("font",4);
s1.write_end_tag("pre",3);
s1.write_end_tag("body",4);
s1.enter();
fout.close();
fin.close();
/*-------------------------------------------------------------------------*/
} // END OF MAIN
//---------------------------------------------------------------------------