Click here to Skip to main content
13,665,166 members
Click here to Skip to main content
Add your own
alternative version


43 bookmarked
Posted 24 May 2004

A C++ to HTML conversion utility

, 24 May 2004
Rate this:
Please Sign up or sign in to vote.
A utility which converts your C++ code to HTML.


Cpphtml is a utility to convert your C++ code to HTML. If you have a C++ file, say myprogram.cpp, and you want to put it on your website, you can run it through Cpphtml which will convert the code to HTML with all comments, keywords and preprocessor directives highlighted. Cpphtml will send all output to cout, so you have to redirect the output to a file if you want to create a HTML file:

C:\>cpphtml myprogram.cpp >myprogram.htm

Cpphtml will convert all tabs to 4 spaces. If you want the tab size to be 8 spaces, you can specify the tab size on the command line:

C:\>cpphtml myprogram.cpp 8 >myprogram.htm

The HTML code contains a <style> element which contains style rules for comments, keywords and preprocessor directives. So, you don't have to do a search-and-replace if you want to change, say, the color of keywords. For example, if you want all keywords in bold, just change the .keyword style rule: .keyword{color:rgb(0,0,255);font-weight:bold}. It's that easy.

I don't claim Cpphtml works perfectly. I tested it on the Dinkumware STL files, the source of Cpphtml, and a large Microsoft CPP file. The results are great. Cpphtml was compiled with the Borland C++ 5.5 command line compiler: bcc32 cpphtml.cpp.

A walk through the code


Cpphtml will replace all tabs by 4 spaces if no tab size is specified. Change _TABSIZE to 8 if you want the default tab size to be 8.

#define _TABSIZE    4

using namespace std;

int tabsize = _TABSIZE;

Token is a class which represents chunks of code. A token can have the type comment, pp (preprocessor directive), keyword or code. Code is everything which is not a comment, pp or keyword. Note that there are no getter and setter methods: because operator>> and operator<< are friends of class token, we don't need any.

class token {
    token() : _what(code) {}
    enum type {code, comment, pp, keyword};
    string _str;
    type _what;
    friend istream& operator>>(istream&, token&);
    friend ostream& operator<<(ostream&, const token&);

The function iskeyword() returns true if string s is a C++ keyword, false if not. It's possible you don't recognize some keywords, e.g. and. Those keywords can be used by programmers who don't have access to all ASCII characters. I've never seen code with such keywords though.

bool iskeyword(const string& s)
    static const char* keywords[] = {

    for (int i = 0; i < sizeof(keywords) / sizeof(char*); i++)
        if (string(keywords[i]) == s)
            return true;

    return false;

The function containspp() returns true if string s contains a substring which is a preprocessor directive. A token of type pp can contain a string of the form "#...define", therefore, we have to find a substring.

bool containspp(const string& s)
    static const char* pptokens[] = {

    for (int i = 0; i < sizeof(pptokens) / sizeof(char*); i++)
        if (s.find(pptokens[i]) != string::npos)
            return true;

    return false;

Operator>> extracts a token from an input stream. It recognizes "//" and "/*...*/" comments, preprocessor directives of the form "#...define", and keywords. String constants are also recognized to avoid keywords to be highlighted in strings.

istream& operator>>(istream& is, token& t)
    t._str = "", t._what = token::code;
    int c = is.get();
    switch (c) {
        case '/':
            c = is.get();
            if (c == '*') {
                t._str = "/*";
                t._what = token::comment;
                while (1) {
                    c = is.get();
                    if (c == EOF)
                        return is.unget(), is.clear(), is;
                    if (c == '/') {
                        if (t._str.length() > 2 &&
                            t._str[t._str.length() - 1] == '*') {
                            return t._str += '/', is;
                    t._str += (char)c;
            } else if (c == '/') {
                t._str = "//";
                t._what = token::comment;
                c = is.get();
                while (c != '\n' && c != EOF) {
                    t._str += (char)c;
                    c = is.get();
                if (c == '\n') {
                    t._str += '\n';
                return is;
            t._str = '/';
            return is.unget(), is.clear(), is;
        case '#':
            t._str = '#';
            c = is.get();
            while (strchr(" \r\n\t", c)) {
                t._str += (char)c;
                c = is.get();
            if (c == EOF)
                return is.unget(), is.clear(), is;
            while (strchr("abcdefghijklmnopqrstuvwxyz", c)) {
                t._str += (char)c;
                c = is.get();
            is.unget(), is.clear();
            if (containspp(t._str))
                t._what = token::pp;
            return is;
        case '\'':
        case '"': {
            char q = (char)c;
            t._str = q;
            while (1) {
                c = is.get();
                if (c == EOF)
                    return is.unget(), is.clear(), is;
                if (c == q) {
                    if (t._str.length() >= 2) {
                        if (!(t._str[t._str.length() - 1] == '\\' &&
                            t._str[t._str.length() - 2] != '\\'))
                            return t._str += q, is;
                    } else {
                        return t._str += q, is;
                t._str += (char)c;                
        case 'a':
        case 'b':
        case 'c':
        case 'd':
        case 'e':
        case 'f':
        case 'g':
        case 'i':
        case 'l':
        case 'm':
        case 'n':
        case 'o':
        case 'p':
        case 'r':
        case 's':
        case 't':
        case 'u':
        case 'v':
        case 'w':
        case 'x':
            t._str += (char)c;
            c = is.get();
            while (isalpha(c) || isdigit(c) || c == '_') {
                t._str += (char)c;
                c = is.get();
            is.unget(), is.clear();
            if (iskeyword(t._str))
                t._what = token::keyword;
            return is;
        case EOF:
            return is;
            t._str += (char)c;
            c = is.get();
            while (c != '/' && c != '#' && !strchr("abcdefgilmnoprstuvwx", c) &&
                c != '\'' && c != '"' && c != EOF) {
                t._str += (char)c;
                c = is.get();
            is.unget(), is.clear();
            return is;

The function html() replaces the characters '&', '<', '>' and '"' in string s by its HTML equivalents and replaces the tabs by spaces.

string html(const string& s)
    string s1;
    string::size_type i;
    for (i = 0; i < s.length(); i++) {
        switch (s[i]) {
            case '&':
                s1 += "&";
            case '<':
                s1 += "<";
            case '>':
                s1 += ">";
            case '"':
                s1 += """;
            case '\t':
                s1.append(tabsize, ' ');
                s1 += s[i];
    return s1;

Operator<< sends a token to an output stream. The code is straightforward.

ostream& operator<<(ostream& os, const token& t)
    if (t._what == token::code)
        cout << html(t._str);
    else if (t._what == token::comment)
        cout << "<span class=comment>" << html(t._str) << "</span>";
    else if (t._what == token::keyword)
        cout << "<span class=keyword>" << html(t._str) << "</span>";
    else if (t._what == token::pp)
        cout << "<span class=pp>" << html(t._str) << "</span>";
        cout << html(t._str);
    return os;

This is the entry point of Cpphtml. All code will be wrapped in a <pre> element. By overloading operator>> and operator<<, the while loop is very short and clean. All output is sent to cout.

int main(int argc, char **argv)
    if (argc != 2 && argc != 3) {
        cout << "usage: cpphtml file [tab size]" << endl;
        return 0;
    ifstream is(argv[1]);
    if (!is.good()) {
        cerr << "bad input file" << endl;
        return -1;
    if (argc == 3) {
        tabsize = atoi(argv[2]);
        if (tabsize <= 0)
            tabsize = _TABSIZE;
    cout << "<html>" << endl 
      << "<head>" << endl 
      << "<style>" << endl;
    cout << ".keyword{color:rgb(0,0,255);}" << endl;
    cout << ".comment{color:rgb(0,128,0);}" << endl;
    cout << ".pp{color:rgb(0,0,255);}" << endl;
    cout << "</style>" << endl << "<body>" << endl;
    cout << "<pre style=\"font-family:courier;font-size:10pt\">";
    token t;
    while (is >> t) {
        cout << t;
    cout << "</pre>" << "</body>" 
         << endl << "</html>" << endl;
    return 0;


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Mike Finnegan
Belgium Belgium
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionHelp | I converted my .cpp file but the code did not change to HTML Pin
Member 1390954014-Jul-18 7:00
memberMember 1390954014-Jul-18 7:00 
Questionnot working Pin
Member 1133381024-Dec-14 15:43
memberMember 1133381024-Dec-14 15:43 
GeneralMy vote of 5 Pin
manoj kumar choubey26-Feb-12 21:24
membermanoj kumar choubey26-Feb-12 21:24 
GeneralMy vote of 1 Pin
r4start5-Apr-10 19:37
memberr4start5-Apr-10 19:37 
GeneralWow. Thanks! Pin
CaptainMorganRawks26-Aug-07 19:50
memberCaptainMorganRawks26-Aug-07 19:50 
GeneralHeader Pin
Flame Alchemist20-Jul-07 1:50
memberFlame Alchemist20-Jul-07 1:50 
GeneralRe: Header Pin
q12345678921-Jul-07 22:56
memberq12345678921-Jul-07 22:56 
GeneralRe: Header Pin
Flame Alchemist24-Jul-07 2:34
memberFlame Alchemist24-Jul-07 2:34 
GeneralN00b needs help with this program Pin
Harrierfalcon2-May-06 17:39
memberHarrierfalcon2-May-06 17:39 
GeneralRe: N00b needs help with this program Pin
Wes Aday2-May-06 17:56
memberWes Aday2-May-06 17:56 
GeneralRe: N00b needs help with this program Pin
Harrierfalcon3-May-06 11:10
memberHarrierfalcon3-May-06 11:10 
GeneralRe: N00b needs help with this program Pin
Wes Aday3-May-06 11:19
memberWes Aday3-May-06 11:19 
Generaldigrams/trigrams Pin
toxcct31-Jan-06 22:53
membertoxcct31-Jan-06 22:53 
GeneralRe: digrams/trigrams Pin
Jörgen Sigvardsson4-Feb-06 8:14
memberJörgen Sigvardsson4-Feb-06 8:14 
GeneralI want Line Number Pin
onjo14-Nov-05 17:49
memberonjo14-Nov-05 17:49 
GeneralRe: I want Line Number Pin
q12345678914-Nov-05 22:11
memberq12345678914-Nov-05 22:11 
GeneralAnother suggestion... not to the author Pin
gcampbell14-Oct-05 14:26
membergcampbell14-Oct-05 14:26 
GeneralThank you (and suggestion) Pin
Pablo Aliskevicius1-May-05 4:29
memberPablo Aliskevicius1-May-05 4:29 
GeneralRe: Thank you (and suggestion) Pin
q1234567892-May-05 3:41
memberq1234567892-May-05 3:41 
GeneralThanks, It's very good Pin
lynhoo2-Jun-04 15:49
memberlynhoo2-Jun-04 15:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180810.1 | Last Updated 25 May 2004
Article Copyright 2004 by Mike Finnegan
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid