#! /usr/bin/perl -w ##### # todo: # optimizations # full keyword list # special HTML characters ##### use strict; my $version = "0.6.0"; my $script_name = "c2html.pl"; # this must match the name of the script my $copy_years = "©2001-2003"; my $home = "http://energon.home.mindspring.com/"; my $company = "Energon Software"; # # replace these colors with your prefered # my $bg_color = '"#ffffff"'; my $text_color = '"#000000"'; my $multi = "false"; sub parse_line { # # replace these colors with your prefered # my $key_color = '"#0000cc"'; my $string_color = '"#cc0000"'; my $comment_color = '"#007700"'; my @preprocessor = ( "#if defined", "#if !defined", "#elif defined", "#include", "#define", "#undef", "#line", "#error", "#pragma", "#if", "#ifdef", "#ifndef", "#elif", "#else", "#endif" ); my @keywords = ( "and", "and_eq", "asm", "auto", "bitand", "bitor", "bool", "break", "case", "catch", "char", "class", "compl", "const", "const_cast", "continue", "default", "delete", "do", "double", "dynamic_cast", "else", "enum", "explicit", "export", "extern", "false", "float", "for", "friend", "goto", "if", "inline", "int", "long", "mutable", "namespace", "new", "not", "not_eq", "operator", "or", "or_eq", "private", "protected", "public", "register", "reinterpret_cast", "return", "short", "signed", "sizeof", "static", "static_cast", "struct", "switch", "template", "this", "throw", "true", "try", "typedef", "typeid", "typename", "union", "unsigned", "using", "virtual", "void", "volatile", "wchar_t", "while", "xor", "xor_eq", "_asm", "NULL" ); shift && chomp; s//>/g; # <> characters if((/\/\*/) || ($multi eq "true")) { # working w/ multiline comments $multi = "true"; if(/\/\*/) { $_ = "\n".$_; } if(/\*\//) { $_.=""; $multi = "false"; } } else { # not working w/ multiline comments s/(\".+?\")/$1<\/font>/g; # strings s/(\'.+?\')/$1<\/font>/g; # characters s/(<.+?>)/$1<\/font>/g; # standard headers foreach my $preproc (@preprocessor) { s/$preproc/$preproc<\/font>/g; } foreach my $keyword (@keywords) { s/\b$keyword\b/$keyword<\/font>/g; } # pull all font tags out of comments if(/\/\//) { s///g; s/<\/font>//g; } s/(\/\/.*)/$1<\/font>/g; } return; } sub optimize_line { chomp; return $_."\n"; } sub main { my $html = "\n\n\n \n \n\n "; my $copyright = "\n<hr>\n\nSource Converted by $script_name $copy_years <a href=\"$home\">$company</a>"; my $file = shift; unless(open INFILE, $file) { print "Could not open $file\n"; return; } $html.=" $file by $script_name\n\n\n\n\n
\n";

    print "Converting $file...\n";
    my $line;
    while() {
        &parse_line;
        $line = &optimize_line;
        $html.=$line;
    }
    close INFILE;

    # fix file extension
    $file =~ s/\./_/g;
    my $htmlfile = $file.".html";

    $file.=".html";
    print "Writing $htmlfile...\n";
    unless(open OUTFILE, ">$htmlfile") {
        print "Could not create $htmlfile\n";
        return;
    }
    print OUTFILE $html.$copyright."\n
\n\n\n"; close OUTFILE; print "\n"; } die "Usage: $script_name [file1] [file2] ...\n" unless @ARGV; foreach my $file(@ARGV) { &main($file) unless($file eq $script_name); } exit 0;