Listing 2: Filter program converted to HTML
<pre><b>
</b><i>/**** C/C++ to HTML Converter Filter ******/</i><b>
#include <stdio.h>
#include <string.h>
</b><i>/* Output chars and convert various
HTML-specific characters */</i><b>
void outchar (char ch)
{ switch (ch) {
case '<' : printf ("&lt;"); break;
case '>' : printf ("&gt;"); break;
case '&' : printf ("&amp;"); break;
case '\"' : printf ("&quot;"); break;
default : putchar (ch);
}
} </b><i>/* outchar */</i><b>
void main (void)
{ char line[255+1];
int i, </b><i>/* counter for each char in line */</i><b>
inEOLcomment=0, </b><i>/* flag to indicate in C++
"to EOL" comment */</i><b>
incomment=0, </b><i>/* flag to indicate if
currently in comment */</i><b>
inquote=0; </b><i>/* flag to indicate if currently
in quoted text */</i><b>
</b><i>/* start HTML output; indicate text is
preformatted and will begin in bold */</i><b>
printf ("<pre><b>");
</b><i>/* process each line from stdin */</i><b>
while (gets(line) != NULL) {
i = inEOLcomment = 0;
</b><i>/* look at each char on line */</i><b>
while (line[i]) {
if (!inEOLcomment) {
if (!incomment &&
(line[i] == '\'' || line[i] == '\"'))
if (line[i-1] != '\\' ||
(line[i-1] == '\\' && line[i-2] == '\\'))
inquote = !inquote; </b><i>/* toggle quote status */</i><b>
if (!inquote)
if (!incomment && strncmp(line+i,"//",2) == 0) {
</b><i>/* beginning of C++ line comment */</i><b>
inEOLcomment = 1;
printf ("</b><i>//"); i += 2;
}
else if (strncmp(line+i,"/*",2) == 0) {
</b><i>/* beginning of comment.. */</i><b>
if (!incomment) printf ("</b><i>");
incomment++;
}
else if (incomment && strncmp(line+i,"*/",2) == 0) {
</b><i>/* end of comment... */</i><b>
printf ("*/");
incomment--; i += 2;
if (!incomment) printf ("</i><b>");
continue;
}
}
outchar (line[i++]);
} </b><i>/* end of line */</i><b>
if (inEOLcomment) printf ("</i><b>");
putchar ('\n');
} </b><i>/* end of input */</i><b>
</b><i>/* finish up HTML output */</i><b>
printf ("</b></pre>\n");
}
</b></pre>