c2html.c Source Code

/*
 * File 	- c2html.c
 * Version	- 1.0
 * Date 	- September 2004
 * Author 	- Jay Scott
 * Email 	- jay@jayscott.co.uk
 *
 *
 * Description
 * ------------
 *
 * C to HTML converts any ANSI C source code into
 * a syntax highlighted HTML file. The program will
 * highlight the following :
 *
 * - Standard 32 ANSI C keywords 
 * - Numerical values
 * - Symbols
 * - Include and Define tags
 * - Single and Double quotes
 * - Single line and Block code comment tags
 *
 *
 * TODO
 * -----
 *
 * - Improve elseif statments
 * - Cleaner HTML Output
 * - Input own CSS for output on HTML page
 *
 *
 * Example
 * --------
 *
 * gcc c2html.c -o c2html
 *
 * c2html  
 *
 */
 


/* Include files */
#include <stdio.h>												
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

/* Define some console colours */
#define WHITE		"\E[m\E[1m"
#define DEFAULT		"\E[m"

/* Local functions */
int checkSym(char symChar);
int writeHeader (char *fname);
int writeFooter ();

/* ANSI C keywords */
char* keywords[32] = {
	"auto", "double", "int","struct","break", "else",
	"long", "switch","case", "emum", "register", "typedef",
	"char", "extern", "return", "union", "const", "float",
	"short", "unsigned", "continue", "for", "signed", "void",
	"default", "goto", "sizeof", "volatile", "do", "if",
	"static", "while"
	};

FILE *outHTML;

int main(int argc, char **argv)
{
	char *HTMLFile, *CFile, tmpChar;
	char tmpWord[512];
	FILE *inHTML;
	int x, i, marked;

	/* Clear the screen */
	system("clear");

	/* Check amount of args */
	if (argc != 3) {
		printf("%sPlease supply a input and ouput file %s",WHITE, DEFAULT);
		exit(1);
	}

	CFile = argv[1];
	HTMLFile = argv[2];

	printf("%sConverting %s to %s%s\n\n",WHITE, CFile,HTMLFile, DEFAULT);

	/* Open C file for reading */
	if ((inHTML = fopen(CFile,"r")) == NULL) {
		printf("Error will opening C file\n");
		exit(1);
	}

	/* Open HTML file for writing */
	if ((outHTML = fopen(HTMLFile,"w")) == NULL) {
		printf("Error will opening HTML file\n");
		exit(1);
	}

	/* Write HTML headers to file */
	writeHeader(CFile);

	/* Get first Char */
	tmpChar = getc(inHTML);

	/* While not end-of-file */
	while(tmpChar != EOF) {
		/* Check for newline */
		if (tmpChar == '\n')
			fprintf(outHTML,"\n");

		/* Find comment tags */
		else if (tmpChar == '/') {
			tmpChar = getc(inHTML);
			/* Is it a block comment */
			if (tmpChar == '*') {
				fprintf(outHTML,"<font color=\"#E40000\">/");
				while (tmpChar != '/') {
					fprintf(outHTML,"%c", tmpChar);
					tmpChar = getc(inHTML);
				}
				fprintf(outHTML,"%c</font>", tmpChar);
			}
			/* Is it a single line comment */
			else if (tmpChar == '/') {
				fprintf(outHTML,"<font color=\"#E40000\">/");
				while (tmpChar != '\n') {
					fprintf(outHTML,"%c", tmpChar);
					tmpChar = getc(inHTML);
				}
				fprintf(outHTML,"%c</font>", tmpChar);
			} else
				fprintf(outHTML,"/%c", tmpChar);
		}

		/* Find double quotes */
		else if (tmpChar == '"') {
			fprintf(outHTML,"<font color=\"#777806\">%c",tmpChar);
			tmpChar = getc(inHTML);
			while (tmpChar != '"') {
				if (tmpChar == '<') {
					fprintf(outHTML,"<");
					tmpChar = getc(inHTML);
					continue;
				}
				if (tmpChar == '>') {
					fprintf(outHTML,">");
					tmpChar = getc(inHTML);
					continue;
				}
				if (tmpChar == '\\') {
					fprintf(outHTML,"%c", tmpChar);
					tmpChar = getc(inHTML);
					fprintf(outHTML,"%c", tmpChar);
					tmpChar = getc(inHTML);
				}
				else {
					fprintf(outHTML,"%c", tmpChar);
					tmpChar = getc(inHTML);
				}
			}
			fprintf(outHTML,"\"</font>");
		}

		/* Find single quotes */
		else if (tmpChar == '\'') {
			fprintf(outHTML,"<font color=\"#777806\">%c",tmpChar);
			tmpChar = getc(inHTML);
			while (tmpChar != '\'') {
				if (tmpChar == '<') {
					fprintf(outHTML,"<");
					tmpChar = getc(inHTML);
					continue;
				}
				if (tmpChar == '>') {
					fprintf(outHTML,">");
					tmpChar = getc(inHTML);
					continue;
				}
				if (tmpChar == '\\') {
					fprintf(outHTML,"%c", tmpChar);
					tmpChar = getc(inHTML);
					fprintf(outHTML,"%c", tmpChar);
					tmpChar = getc(inHTML);
				}
				else {
					fprintf(outHTML,"%c", tmpChar);
					tmpChar = getc(inHTML);
				}
			}
			fprintf(outHTML,"\'</font>");
		}

		/* Find define, include tags */
		else if (tmpChar == '#') {
			fprintf(outHTML,"<font color=\"#7F7F00\">");
			while(!isspace(tmpChar)) {
				fprintf(outHTML,"%c",tmpChar);
				tmpChar = getc(inHTML);
			}
			fprintf(outHTML,"</font> ");
		}

		/* Find any digits */
		else if (isdigit(tmpChar)) {
			fprintf(outHTML,"<font color=\"#009926\">%c",tmpChar);
			tmpChar = getc(inHTML);
			while (isdigit(tmpChar)) {
				fprintf(outHTML,"%c", tmpChar);
				tmpChar = getc(inHTML);
			}
			if (isspace(tmpChar)) {
				fprintf(outHTML,"</font>%c",tmpChar);
				tmpChar = getc(inHTML);
				continue;
			}

			/* Check incase last char is a symbol */
			if (checkSym(tmpChar)) {
				fprintf(outHTML,"</font><font color=\"#6145aa\">%c</font>",tmpChar);
				tmpChar = getc(inHTML);
				continue;
			}
			fprintf(outHTML,"</font>%c",tmpChar);
		}

		/* Find < and > tags */
		else if (tmpChar == '<')
			fprintf(outHTML,"<");
		else if (tmpChar == '>')
			fprintf(outHTML,">");

		/* Check for any keywords */
		else {
			x = 0;
			marked = 1;
			while (isalpha(tmpChar)){
				tmpWord[x++] = tmpChar;
				tmpChar = getc(inHTML);
			}

			tmpWord[x] = '\0';
			for (i = 0; i < 32 ;i++){
				if (!(strcmp(keywords[i],tmpWord))) {
					fprintf(outHTML,"<font color=\"#022ebc\">%s</font>",tmpWord);
					marked = 0;
					break;
				}
			}
			if(marked == 1)
					fprintf(outHTML,"%s",tmpWord);
			
			/* Check whitespace again if we dont
			 * alot of not needed font tags will
			 * be printed
			 */
			if (isspace(tmpChar)) {
				fprintf(outHTML,"%c",tmpChar);
				tmpChar = getc(inHTML);
				continue;
			}
			/* Check for Symbols */
			if (checkSym(tmpChar)) {
				fprintf(outHTML,"<font color=\"#6145aa\">%c</font>",tmpChar);
				tmpChar = getc(inHTML);
				continue;
			}
			fprintf(outHTML,"%c",tmpChar);

		}
		/* Get next char */
		tmpChar = getc(inHTML);
	}

	/* Write end HTML code */
	writeFooter();
	/* Close C file  */
	fclose(inHTML);
	/* Close HTML file  */
	fclose(outHTML);

	printf("%sCompleted!%s\n\n",WHITE, DEFAULT);

	return 0;
}

int checkSym(char symChar)
{
	char symbols[] = {
	'!', '$', '%', '&', '(', ')', '*', '+',
	',', '-', '.', ':', ';', '=', '?', '@',
	'[', ']', '^', '{', '|', '}', '~' };

	if (strchr(symbols,symChar))
		return 1;

	return 0;
}

int writeHeader (char *fname)
{
	/* Write basic HTML headers */
	fprintf(outHTML,"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN>\n");
	fprintf(outHTML,"\n<html>\n<head>\n<title>%s Source Code</title>\n<style>\n\n",fname);

	/* Body CSS tag */
	fprintf(outHTML,"\nbody\n{\n\tcolor: rgb(0,0,0);");
	fprintf(outHTML,"\n\tbackground-color: rgb(200,200,200);\n}\n\n");

	/* A CSS tags */
	fprintf(outHTML,"a\n{\n\tfont-size: 8pt;\n\ttext-decoration:none;\n}\n\n");
	fprintf(outHTML,"a:link,a:visited\n{\n\tcolor: rgb(60,72,90);\n");
	fprintf(outHTML,"\tbackground-color: transparent;\n}\n\n");
	fprintf(outHTML,"a:hover\n{\n\tcolor: rgb(128,0,0);\n");
	fprintf(outHTML,"\tbackground-color: transparent;\n}\n\n");

	/* Table CSS tag */
	fprintf(outHTML,"table\n{\n\t");
	fprintf(outHTML,"width: 800px;\n\t");
	fprintf(outHTML,"margin-left: auto;\n\t");
	fprintf(outHTML,"margin-right: auto;\n\t");
	fprintf(outHTML,"color: rgb(0,0,0);\n\t");
	fprintf(outHTML,"background-color: rgb(110,120,140);\n\t");
	fprintf(outHTML,"border: 1px solid rgb(60,72,90);");
	fprintf(outHTML,"\n\tmargin-bottom: 5px;\n}\n\n");

	/* Table CSS tag */
	fprintf(outHTML,"td.htmlContent\n{\n\t");
	fprintf(outHTML,"color: rgb(0,0,0);\n\t");
	fprintf(outHTML,"background-color: rgb(200,205,210);\n\t");
	fprintf(outHTML,"width: 560px;\n\t");
	fprintf(outHTML,"font-size: 8pt;\n\t");
	fprintf(outHTML,"margin-top: 3px;\n\t");
	fprintf(outHTML,"padding-left: 10px;\n\t");
	fprintf(outHTML,"padding-right: 10px;\n\t");
	fprintf(outHTML,"padding-top: 0px;\n\t");
	fprintf(outHTML,"padding-bottom: 0px;\n}\n");

	/* Start of table */
	fprintf(outHTML,"\n</style>\n</head>\n\n<body>\n<br>");
	fprintf(outHTML,"<center>%s Source Code</center>\n", fname);
	fprintf(outHTML,"<table cellspacing=\"0\">\n<tr>\t\n");	 
	fprintf(outHTML,"<td class=\"htmlContent\"><br>\n<p><pre>");

	return 0;
}

int writeFooter ()
{
	fprintf(outHTML,"</pre></p><p> </p></td></tr></table><center>");
	fprintf(outHTML,"<a href=\"http://www.jayscott.co.uk\">");
	fprintf(outHTML,"Generated by C2HTML</a></body</html></center>");
	return 0;
}

 

Generated by C2HTML