void pl1_color() { int j,x,R,G,B; if(player[local_player[1]] && player[local_player[1]]->islocal) { if (strlen(con->arg)>0) { x=0; for(j=x;(con->arg[j]!=',') && (j < strlen(con->arg));j++); R=atoi(strmid(con->arg,x,j-x)); x=j+1; for(j=x;(con->arg[j]!=',') && (j < strlen(con->arg));j++); G=atoi(strmid(con->arg,x,j-x)); x=j+1; for(j=x;(con->arg[j]!=',') && (j < strlen(con->arg));j++); B=atoi(strmid(con->arg,x,j-x)); x=j+1; player[local_player[1]]->color=makecol(R,G,B); }; }; };
void c2html(const char *filename, char *err, struct colors *c) { /* <- core function -> here the C source code is tranlated to an HTML web page. */ struct /* common bits */ { unsigned char color : 1; /* color the code */ unsigned char qt1 : 1; /* double quotes */ unsigned char qt2 : 1; /* single quotes */ unsigned char cm1 : 1; /* multi-line comments */ unsigned char cm2 : 1; /* single-line comments */ unsigned char num : 1; /* numerical constants */ unsigned char pre : 1; /* preprocessors */ unsigned char cont : 1; /* preprocessors */ } bits = { 1, 0, 0, 0, 0, 0, 0, 0 }; if(!strlen(filename)) { puts("\nPlease enter a filename."); return; } if(*filename==':') { bits.color = 0; filename++; } else if(*filename=='?') { userdef_colors(c); filename++; } FILE *cfile,*hfile; cfile = hfile = NULL; char htmlfile[STR_MAX]; strcpy(htmlfile,filename); strcat(htmlfile,".html"); if((cfile = fopen(filename,"r"))==NULL) { sprintf(err,"Could not open file: %s",filename); return; } unsigned short int line_length = 0; char input_str[STR_MAX]; printf("\nHow many letters per line? "); fgets(input_str,STR_MAX,stdin); string(input_str); line_length = atoi(input_str); if(line_length <= 1) line_length = 80; printf("\nAnalyzing file: %s ...\n\n",filename); register char move = 0; unsigned int strmax, strnow, lines, pages; unsigned long int charcount = 0; unsigned long int letters = 0; strmax = strnow = lines = pages = 0; for(;move!=EOF;strnow++, charcount++) { move = fgetc(cfile); if(move=='\n') { strmax = strnow>strmax ? strnow:strmax; strnow = 0; lines++; } if(move<128 && move!='\n' && move!= '\r') letters++; } pages = lines/50; pages += lines%50 < 10 ? 0 : 1; printf("No. of pages = %d\n",pages); printf("No. of lines = %d\n",lines); printf("No. of chars = %ld\n",charcount); printf("No. of letters = %ld\n",letters); if(charcount<=1) { strcpy(err,"Too few characters."); fclose(cfile); return; } if((hfile = fopen(htmlfile,"w"))==NULL) { sprintf(err,"Could not create file: %s",htmlfile); fclose(cfile); return; } printf("\nTranslating file: %s ... ",filename); fprintf(hfile,"<HTML><HEAD><TITLE>%s</TITLE></HEAD>\n",filename); fprintf(hfile,"<BODY TEXT=%s BGCOLOR=white><CENTER><H1>%s</H1></CENTER><HR><CODE>\n",c->text,filename); char *wstr = NULL, *wwstr = NULL; if((wstr = (char *) malloc (sizeof(char)*(strmax+1)))==NULL) { strcpy(err,"Out of memory."); fclose(hfile); fclose(cfile); return; } else *wstr = '\0'; unsigned int digits = 0; unsigned int _lines = lines; for(;_lines;digits++) _lines /= 10; char *nstr = NULL; if((nstr = (char *) malloc (sizeof(char)*(digits+1)))==NULL) { strcpy(err,"Out of memory."); free(wstr); fclose(hfile); fclose(cfile); return; } else *nstr = '\0'; fclose(cfile); unsigned int lnum = 0; unsigned register int cnum = 0; cfile = fopen(filename,"r"); /* C keywords */ char keys[33][9] = { "auto","break","case","char", "const","continue","default", "double","do","else","enum", "extern","float","for","goto", "if","int","long","register", "return","short","signed", "sizeof","static","struct", "switch","typedef","union", "unsigned","void","volatile", "while", "inline" }; for(bits.cont=0;fgets(wstr,strmax,cfile)!=NULL;lnum++) { unsigned int remaining = 0; unsigned int split_offset = 0; remainder: /* show translation progress */ char prog[4] = "00%"; double progn = (lnum*100)/lines; if(progn<100) numstr(prog,(int)progn,progn<10?1:2); strcat(prog,"%"); if(strlen(prog)==2) printf("\b\b%s",prog); if(strlen(prog)==3) printf("\b\b\b%s",prog); /* insert line numbers */ short int numlength = 0; if(remaining) goto so_go_on; numstr(nstr,lnum,digits); numlength = strlen(nstr); fprintf(hfile,"<B>%s</B> ",nstr); so_go_on: /* check if the line has been commented off */ if(bits.cm1&&lnum) fprintf(hfile,"<FONT COLOR=%s>",c->comment); /* reset the booleans in bits */ if(bits.cont && bits.pre) fprintf(hfile,"<B>"); else bits.pre = 0; if(bits.cont && bits.qt1) fprintf(hfile,"<FONT COLOR=%s>",c->string); else bits.qt1 = 0; bits.qt2 = 0; bits.cm2 = 0; bits.num = 0; unsigned int line_len = 0; line_len = line_length - numlength; if(remaining) { numstr(nstr,lnum,digits); numlength = strlen(nstr); strnull(nstr); strbluff(nstr," " ,numlength); fprintf(hfile,"%s ",nstr); char *t = allocs(line_len+1); strmid(t,wwstr,split_offset,line_len); strcpy(wstr,t); split_offset += strlen(wstr); remaining -= strlen(wstr); } if(strlen(wstr)>line_len && remaining <= 0) { split_offset = 0; char *t = allocs(line_len+1); strmid(t,wstr,split_offset,line_len); wwstr = clones(wstr); strcpy(wstr,t); split_offset += strlen(wstr); remaining = strlen(wwstr)-line_len; } for(cnum=0;cnum<strlen(wstr);cnum++) { /* color up the keywords */ int keyw; for(keyw=0;keyw<33;keyw++) if(!strncmp(&wstr[cnum],keys[keyw], strlen(keys[keyw]))&&!( wstr[cnum-1]>='A'&& wstr[cnum-1]<='Z')&&!( wstr[cnum-1]>='a'&& wstr[cnum-1]<='z')&&!( wstr[cnum-1]>='0'&& wstr[cnum-1]<='9')&& wstr[cnum-1]!='_' &&(!bits.cm1) &&(!bits.cm2) &&(!bits.qt1) &&(!bits.qt2) &&bits.color) { if(!(wstr[cnum+strlen(keys[keyw])]>='A'&& wstr[cnum+strlen(keys[keyw])]<='Z')&&!( wstr[cnum+strlen(keys[keyw])]>='a'&& wstr[cnum+strlen(keys[keyw])]<='z')&&!( wstr[cnum+strlen(keys[keyw])]>='0'&& wstr[cnum+strlen(keys[keyw])]<='9')&& wstr[cnum+strlen(keys[keyw])]!='_') { fprintf(hfile,"<FONT COLOR=%s>%s</FONT>",c->keyword,keys[keyw]); cnum += strlen(keys[keyw]); } } /* color up the comments */ if(!strncmp(&wstr[cnum],"/*",2) &&!bits.qt1 &&!bits.qt2 &&!bits.cm2 &&bits.color) { fprintf(hfile,"<FONT COLOR=%s>/",c->comment); bits.cm1 = 1; } else if(!strncmp(&wstr[cnum],"*/",2) &&!bits.qt1 &&!bits.qt2 &&!bits.cm2 &&(bits.cm1) &&bits.color) { fprintf(hfile,"*/</FONT>"); cnum++; bits.cm1 = 0; } else if((!strncmp(&wstr[cnum],"//",2)) &&!bits.qt1 &&!bits.qt2 &&!bits.cm2 &&!bits.cm1 &&bits.color) { fprintf(hfile,"<FONT COLOR=%s>/",c->comment); bits.cm2 = 1; } /* color up the strings */ else if(wstr[cnum]==34 &&(!bits.cm2) &&(!bits.cm1) &&(!bits.pre) &&bits.color) { if(wstr[cnum-1]!='\\') { if(!bits.qt1) { fprintf(hfile,"<FONT COLOR=%s>%c",c->string,34); bits.qt1 = 1; } else { fprintf(hfile,"%c</FONT>",34); bits.qt1 = 0; } } } /* color up the chars */ else if(wstr[cnum]==39 &&(!bits.qt1) &&(!bits.cm2) &&(!bits.cm1) &&(!bits.pre) &&bits.color) { if(wstr[cnum-1]!='\\') { if(!bits.qt2) { fprintf(hfile,"<FONT COLOR=%s>%c",c->chr,39); bits.qt2 = 1; } else { fprintf(hfile,"%c</FONT>",39); bits.qt2 = 0; } } } /* color up the numbers */ else if(wstr[cnum]>='0'&& wstr[cnum]<='9'&&!( wstr[cnum-1]>'A'&& wstr[cnum-1]<'Z')&&!( wstr[cnum-1]>'a'&& wstr[cnum-1]<'z')&& wstr[cnum-1]!='_' &&(!bits.cm2) &&(!bits.cm1) &&(!bits.qt1) &&(!bits.qt2) &&(!bits.num) &&(!bits.pre) &&bits.color) { fprintf(hfile,"<FONT COLOR=%s>%c",c->numbers,wstr[cnum]); bits.num = 1; } else if(bits.num /* to end number colouring */ &&((wstr[cnum]<'0') ||(wstr[cnum]>'9'))) { fprintf(hfile,"</FONT>%c",wstr[cnum]); bits.num = 0; } /* color up the preprocessors */ else if(wstr[cnum]=='#' &&(!bits.cm2) &&(!bits.cm1) &&(!bits.qt1) &&(!bits.qt2) &&(!bits.pre) &&(bits.color)) { fprintf(hfile,"<B>#"); bits.pre = 1; } /* replace the C symbols with their HTML equivalent */ else if(wstr[cnum-1]==' '&&wstr[cnum]==' ') fprintf(hfile," "); else if(wstr[cnum]=='\t') fprintf(hfile," "); else if(wstr[cnum]=='<') fprintf(hfile,"<"); else if(wstr[cnum]=='>') fprintf(hfile,">"); else if(wstr[cnum]=='\n') { fprintf(hfile,"</FONT>"); if(wstr[cnum-1]=='\\') bits.cont = 1; else bits.cont = 0; break; } else fprintf(hfile,"%c",wstr[cnum]); } if(bits.pre) fprintf(hfile,"</B>"); /* end font tag */ if(bits.cm1|| bits.cm2|| bits.qt1|| bits.qt2|| bits.num) fprintf(hfile,"</FONT>"); /* end the line */ fprintf(hfile,"<BR>\n"); if(remaining) goto remainder; } fputs("</CODE><HR NOSHADE><FONT FACE=Arial>\n",hfile); fprintf(hfile,"No. of pages = %d<BR>\n",pages); fprintf(hfile,"No. of lines = %d<BR>\n",lines); fprintf(hfile,"No. of chars = %ld<BR>\n",charcount); fprintf(hfile,"No. of letters = %ld\n",letters); fputs("</FONT></BODY></HTML>",hfile); puts("\b\b\b100%\n\nTranslation Complete."); printf("HTML File: %s stored.\n",htmlfile); free(wstr); fclose(hfile); fclose(cfile); }