From Tim's website
C++ Programs - c2html.cpp
<source lang="c"> // To prevent browse information warnings with Microsoft compiler
- pragma warning( disable : 4786 )
- include <iostream>
- include <fstream>
- include <string>
- include "keywords.h"
- include "symbols.h"
void write_html_char( ofstream& stream, const unsigned char character );
void main( int argc, char *argv[] ) {
using namespace std;
// Get the name of the input source file string source_filename; if( argc != 2 ) { cout << "C to HTML converter.\nEnter source file name: "; cin >> source_filename; } else { source_filename = argv[1]; }
// Create a name for the output html file const int last_dot = source_filename.find_last_of( '.' ); string html_filename = source_filename.substr( 0, last_dot ) + ".html";
cout << "Converting '" << source_filename << "' to '" << html_filename << "'" << endl;
// Open the source and html file streams ifstream source_ifs( source_filename.c_str() ); ofstream html_ofs( html_filename.c_str() );
if( !source_ifs.is_open() ) { cout << "Failed to open source file '" << source_filename << "'" << endl; return; } if( !html_ofs.is_open() ) { cout << "Failed to open html file '" << source_filename << "'" << endl; return; }
KEYWORDS keywords; SYMBOLS symbols;
html_ofs << "<HTML>\n"; html_ofs << "<HEAD><LINK href=\"code.css\" rel=stylesheet type=\"text/css\"></HEAD>\n"; html_ofs << "<BODY>\n";
// Copy strings from the input file to the output file while( !source_ifs.eof() ) { string word; bool star_found;
// Get the next character from the file unsigned char character = source_ifs.get();
switch( character ) { case '\n': html_ofs << "
\n"; break;
case '"': // Start string - read until next quote html_ofs << "";
do { write_html_char( html_ofs, character ); character = source_ifs.get(); if( character == '\\' ) { // Control character - ignore the next character write_html_char( html_ofs, character ); character = source_ifs.get(); write_html_char( html_ofs, character ); character = source_ifs.get(); } } while( character != '"' && !source_ifs.eof() );
write_html_char( html_ofs, character ); html_ofs << ""; break;
case '#': // Start directive - read until space html_ofs << ""; write_html_char( html_ofs, character ); source_ifs >> word; html_ofs << word;
html_ofs << ""; break;
case '/': // May start comment character = source_ifs.get(); switch( character ) { case '/': // Line comment - read to end of line html_ofs << ""; write_html_char( html_ofs, '/' );
while( character != '\n' && !source_ifs.eof() ) { write_html_char( html_ofs, character ); character = source_ifs.get(); }
html_ofs << ""; html_ofs << "
\n"; break;
case '*': // Limited comment - read until '*/' html_ofs << ""; write_html_char( html_ofs, '/' ); write_html_char( html_ofs, '*' );
character = source_ifs.get(); star_found = character == '*';
while( !(star_found && character == '/') && !source_ifs.eof() ) { write_html_char( html_ofs, character ); character = source_ifs.get(); if( character == '*' ) { star_found = true; } }
write_html_char( html_ofs, '/' ); html_ofs << ""; break;
default: write_html_char( html_ofs, '/' ); write_html_char( html_ofs, character ); break; } break;
default: // If the character is not a symbol read the rest of the word if( !symbols.is( character ) ) { // Read in characters up to the next symbol to make a word string word; while( !symbols.is( character ) && !source_ifs.eof() ) { word += character; character = source_ifs.get(); }
// Write out the word if( keywords.is( word ) ) { html_ofs << ""; html_ofs << word; html_ofs << ""; } else { html_ofs << word; } }
write_html_char( html_ofs, character );
break; } }
html_ofs << "</BODY>\n"; html_ofs << "</HTML>\n";
cout << "Done!" << endl;
}
void write_html_char( ofstream& stream, const unsigned char character ) {
switch( character ) { case '\n': stream << "
\n"; break; case '\t': stream << " "; break; case '"': stream << """; break; case ' ': stream << " "; break; case '&': stream << "&"; break; case '<': stream << "<"; break; case '>': stream << ">"; break; case 255: break; default: stream.put( character ); break; }
} </source>