| #include #include #include #include
#define NORMAL_COLOR "#000000" #define STRING_COLOR "#ff0000" #define COMMENT_COLOR "#008000" #define PREPROC_COLOR "#0000FF" #define RESERVED_COLOR "#0000FF"
/* C / C++ to HTML converter. Converts C and C++ code to HTML with syntax hilighting... */
char *Reserved_Words[]={"_asm","asm","catch","double","cgoto","return","throw", "volatile","char","else","if","short", "true","while","and_eq","class","enum","inline","operator", "signed","try","auto","compl","explicit","int","or", "sizeof","typedef","xor_eq","bitand","const","extern", "long","or_eq","static","typename","bitor","continue", "false","mutable","private","struct","union","bool","default", "float","namespace","protected","switch","unsigned","break", "delete","for","new","public","template","virtual","case", "do","friend","register","this","void", "__int64","naked","__declspec","_cdecl","_stdcall","*"}; int IsReservedWord (char *word) { int i=0;
while(strcmp(Reserved_Words,"*")) { if (!strcmp(word,Reserved_Words)) return 1; i++; } return 0; }
int fpeek(FILE *f) { int ch;
ch = fgetc(f); ungetc(ch,f); return ch; }
void printchar(char inchar, FILE *fstream) { if ((inchar != '<') && (inchar != '>') && (inchar != '&')) fputc(inchar,fstream); else if (inchar == '&') fprintf(fstream,"&"); else if (inchar == '>') fprintf(fstream,">"); else fprintf(fstream,"<"); }
int Print_HTML(FILE* fstream, char *Title) { FILE *InFile; int inchar,i,newline = 1,Multi_Line_Comment = 0,WordIdx = 0,colorNormal=1; char word[280];
for (i=0; i<280; ++i) word = 0; // Print out the HTML header. fprintf(fstream,"nn %s nn",Title); fprintf(fstream,""); fprintf(fstream,""); fprintf(fstream," | "); fprintf(fstream,"n",NORMAL_COLOR);
// Now we open the file for reading in text mode. InFile = fopen(Title,"rt"); if (InFile == NULL) return -1;
// And start reading it in character by character. inchar = fgetc(InFile); fprintf(fstream,"",NORMAL_COLOR); while (!feof(InFile)) { if (inchar == 'n') { newline = 1; } else if (!isspace(inchar)) { // Handle all preproccessor directives. if ((inchar == '#') && (!Multi_Line_Comment)) { if (newline == 1) { fprintf(fstream,"",PREPROC_COLOR); while ((inchar != 'n') && (inchar != ' ') && (inchar > 0)) { printchar(inchar,fstream); inchar = fgetc(InFile); } fprintf(fstream,""); newline = 1; colorNormal=0; continue; } } // Handle single line comments. else if ((inchar == '/') && (fpeek(InFile) == '/') && (!Multi_Line_Comment)) { fprintf(fstream,"",COMMENT_COLOR); while ((inchar != 'n') && (inchar > 0)) { printchar(inchar,fstream); inchar = fgetc(InFile); } fprintf(fstream,"" ); newline = 1; colorNormal=0; continue; } // Handle Multi-line comments. else if ((inchar == '/') && (fpeek(InFile) == '*') && (!Multi_Line_Comment)) { Multi_Line_Comment = 1; fprintf(fstream,"/",COMMENT_COLOR); inchar = fgetc(InFile); colorNormal=0; } else if ((inchar == '*') && (fpeek(InFile) == '/') && (Multi_Line_Comment)) { fgetc(InFile); inchar = fgetc(InFile); Multi_Line_Comment = 0; fprintf(fstream,"*/",COMMENT_COLOR); } // Handle Double Quotes. else if ((inchar == 34 ) && (!Multi_Line_Comment)) { fprintf(fstream,"%c",STRING_COLOR,34); inchar = fgetc(InFile); while ((inchar != 34) && (inchar > 0)) { printchar(inchar,fstream); inchar = fgetc(InFile); } inchar = fgetc(InFile); fprintf(fstream,"%c",34); newline = 0; colorNormal=0; continue; } // Handle Single Quotes. else if ((inchar == 39 ) && (!Multi_Line_Comment)) { fprintf(fstream,"%c",STRING_COLOR,39); inchar = fgetc(InFile); while ((inchar != 39) && (inchar > 0)) { printchar(inchar,fstream); inchar = fgetc(InFile); } inchar = fgetc(InFile); fprintf(fstream,"%c",39); newline = 0; colorNormal=0; continue; } newline = 0; } // Everything else. if (!isalnum(inchar) && inchar!='_') { if (IsReservedWord(word) && !Multi_Line_Comment) { fprintf(fstream,"%s%c" ,RESERVED_COLOR,word,NORMAL_COLOR,inchar); } else { if(colorNormal==0 && !Multi_Line_Comment) { fprintf(fstream,"",NORMAL_COLOR); colorNormal=1; } for (i=0; i < WordIdx; ++i) printchar(word,fstream); if(inchar==0x9) fprintf(fstream," "); else printchar(inchar,fstream); } for (i=0; i < 280; ++i) word = 0; WordIdx = 0; } else { word[WordIdx++] = inchar; } inchar = fgetc(InFile); } fprintf(fstream,"n"); fprintf(fstream," | "); fprintf(fstream,"n"); fclose(InFile); return 1; }
int main(int argc,char *argv[]) { FILE *fich; char fichero[255]; if (argc < 2) { printf("Usage: %s [Filename]nn",argv[0]); return -1; } strcpy(fichero,argv[1]); strcat(fichero,".html"); fich=fopen(fichero,"w"); Print_HTML(fich,argv[1]); fclose(fich); return 0; }
|