Make source code HTML-compatible


Source code may contain characters that are treated as "special characters" by HTML. Examples of these characters are:

&   "   <   >   [   ]

These special characters need to be replaced with escape sequences such as:

&amp;   &quot;   &lt;   &gt;   &#91;   &#93;


The following program will read source and replace the special characters for you. The input is taken from stdin and the output is printed to stdout.

Here's an example usage:

cat my_source_code.c | ./htmlcompatible > my_source_code.html


In the above line, the contents of the file "my_source_code.c" is piped into the input of the htmlcompatible program, and from there the output is piped to a file.

Here's the source code for htmlcompatible:
#include <stdio.h>

typedef struct Couple {
    int c;
    char const *replacement;
} Couple;

Couple const couples[] =
{
    {'&', "&amp;"},
    {'\"', "&quot;"},
    {'<', "&lt;"},
    {'>', "&gt;"},
    {'[', "&#91;"},
    {']', "&#93;"},
    {0,0}  /* Null terminator required */
};

int main(void)
{
    int c;

    while (  EOF !=  (c = fgetc(stdin))  )
    {
        Couple const *p = couples;

        if (c == '\r') /* Discard the "\r" in line breaks "\r\n" */
            continue;

        for (;;) /* Loop through the list of special characters */
        {
            if (c == p->c)
            {
                /* The input contains a special character */

                fputs(p->replacement,stdout); /* Print the replacement */
                break;
            }

            ++p; /* Advance to the next special character in the list */

            if (!p->c)
            {
                /* We've reached the end of the special character list */

                fputc(c,stdout); /* Print the character unchanged */
                break;
            }
        }
    }

    return 0;
}
You can copy this code into a file named "htmlcompatible.c" and then compile it as follows:
gcc htmlcompatible.c -D NDEBUG -s -O3 -o htmlcompatible


Virjacode Home