limit fstBC to 30bp in Python3 ver.
[GalaxyCodeBases.git] / c_cpp / aha / aha.c
blob65514edf6783751aaaf777e8d9a7ee670431ddf5
1 /*
2 The contents of this file are subject to the Mozilla Public License
3 Version 1.1 (the "License"); you may not use this file except in
4 compliance with the License. You may obtain a copy of the License at
5 http://www.mozilla.org/MPL/
7 Software distributed under the License is distributed on an "AS IS"
8 basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
9 License for the specific language governing rights and limitations
10 under the License.
12 Alternatively, the contents of this file may be used under the terms
13 of the GNU Lesser General Public license (the "LGPL License"), in which case the
14 provisions of LGPL License are applicable instead of those
15 above.
17 For feedback and questions about my Files and Projects please mail me,
18 Alexander Matthes (Ziz) , zizsdl_at_googlemail.com
20 #define AHA_VERSION "0.4.8.0"
21 #define AHA_YEAR "2015"
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
26 int future=0;
27 int future_char=0;
29 int getNextChar(register FILE* fp)
31 int c;
32 if (future)
34 future=0;
35 return future_char;
37 if ((c = fgetc(fp)) != EOF)
38 return c;
39 fprintf(stderr,"Unknown Error in File Parsing!\n");
40 exit(1);
43 typedef struct selem *pelem;
44 typedef struct selem {
45 unsigned char digit[8];
46 unsigned char digitcount;
47 pelem next;
48 } telem;
50 pelem parseInsert(char* s)
52 pelem firstelem=NULL;
53 pelem momelem=NULL;
54 unsigned char digit[8];
55 unsigned char digitcount=0;
56 int pos=0;
57 for (pos=0;pos<1024;pos++)
59 if (s[pos]=='[')
60 continue;
61 if (s[pos]==';' || s[pos]==0)
63 if (digitcount==0)
65 digit[0]=0;
66 digitcount=1;
69 pelem newelem=(pelem)malloc(sizeof(telem));
70 for (unsigned char a=0;a<8;a++)
71 newelem->digit[a]=digit[a];
72 newelem->digitcount=digitcount;
73 newelem->next=NULL;
74 if (momelem==NULL)
75 firstelem=newelem;
76 else
77 momelem->next=newelem;
78 momelem=newelem;
79 digitcount=0;
80 memset(digit,0,8);
81 if (s[pos]==0)
82 break;
84 else
85 if (digitcount<8)
87 digit[digitcount]=s[pos]-'0';
88 digitcount++;
91 return firstelem;
94 void deleteParse(pelem elem)
96 while (elem!=NULL)
98 pelem temp=elem->next;
99 free(elem);
100 elem=temp;
104 #define VERSION_PRINTF_MAKRO \
105 printf("\033[1;31mAnsi Html Adapter\033[0m Version "AHA_VERSION"\n");
107 int main(int argc,char* args[])
109 char* filename=NULL;
110 register FILE *fp = stdin;
111 int colorshema=0; //0:normal, 1:black, 2:pink
112 int iso=-1; //utf8
113 char stylesheet=0;
114 char htop_fix=0;
115 char line_break=0;
116 char* title=NULL;
117 char word_wrap=0;
118 char no_header=0;
119 //Searching Parameters
120 for (int p = 1;p<argc;p++)
122 if ((strcmp(args[p],(char*)"--help")==0) || (strcmp(args[p],(char*)"-h")==0) || (strcmp(args[p],(char*)"-?")==0))
124 VERSION_PRINTF_MAKRO
125 printf("\033[1maha\033[0m takes SGR-colored Input and prints W3C conform HTML-Code\n");
126 printf("use: \033[1maha\033[0m <\033[4moptions\033[0m> [\033[4m-f file\033[0m]\n");
127 printf(" \033[1maha\033[0m (\033[4m--help\033[0m|\033[4m-h\033[0m|\033[4m-?\033[0m)\n");
128 printf("\033[1maha\033[0m reads the Input from a file or stdin and writes HTML-Code to stdout\n");
129 printf("\033[4moptions\033[0m: --black, -b: \033[1;30m\033[1;47mBlack\033[0m Background and \033[1;37mWhite\033[0m \"standard color\"\n");
130 printf(" --pink, -p: \033[1;35mPink\033[0m Background\n");
131 printf(" --stylesheet, -s: Use a stylesheet instead of inline styles\n");
132 printf(" --iso X, -i X: Uses ISO 8859-X instead of utf-8. X must be 1..16\n");
133 printf(" --title X, -t X: Gives the html output the title \"X\" instead of\n");
134 printf(" \"stdin\" or the filename\n");
135 printf(" --line-fix, -l: Uses a fix for inputs using control sequences to\n");
136 printf(" change the cursor position like htop. It's a hot fix,\n");
137 printf(" it may not work with any program like htop. Example:\n");
138 printf(" \033[1mecho\033[0m q | \033[1mhtop\033[0m | \033[1maha\033[0m -l > htop.htm\n");
139 printf(" --word-wrap, -w: Wrap long lines in the html file. This works with\n");
140 printf(" CSS3 supporting browsers as well as many older ones.\n");
141 printf(" --no-header, -n: Don't include header into generated HTML,\n");
142 printf(" useful for inclusion in full HTML files.\n");
143 printf("Example: \033[1maha\033[0m --help | \033[1maha\033[0m --black > aha-help.htm\n");
144 printf(" Writes this help text to the file aha-help.htm\n\n");
145 printf("Copyleft \033[1;32mAlexander Matthes\033[0m aka \033[4mZiz\033[0m "AHA_YEAR"\n");
146 printf(" \033[5;36mzizsdl@googlemail.com\033[0m\n");
147 printf(" \033[5;36mhttp://ziz.delphigl.com/tool_aha.php\033[0m\n");
148 printf("This application is subject to the \033[1;34mMPL\033[0m or \033[1;34mLGPL\033[0m.\n");
149 return 0;
151 else
152 if ((strcmp(args[p],(char*)"--version")==0) || (strcmp(args[p],(char*)"-v")==0))
154 VERSION_PRINTF_MAKRO
155 return 0;
157 else
158 if ((strcmp(args[p],"--title")==0) || (strcmp(args[p],"-t")==0))
160 if (p+1>=argc)
162 fprintf(stderr,"No title given!\n");
163 return 0;
165 title=args[p+1];
166 p++;
168 else
169 if ((strcmp(args[p],"--line-fix")==0) || (strcmp(args[p],"-l")==0))
171 htop_fix=1;
173 else
174 if ((strcmp(args[p],"--no-header")==0) || (strcmp(args[p],"-n")==0))
176 no_header=1;
178 else
179 if ((strcmp(args[p],"--word-wrap")==0) || (strcmp(args[p],"-w")==0))
180 word_wrap=1;
181 else
182 if ((strcmp(args[p],"--black")==0) || (strcmp(args[p],"-b")==0))
183 colorshema=1;
184 else
185 if ((strcmp(args[p],"--pink")==0) || (strcmp(args[p],"-p")==0))
186 colorshema=2;
187 else
188 if ((strcmp(args[p],"--stylesheet")==0) || (strcmp(args[p],"-s")==0))
189 stylesheet=1;
190 else
191 if ((strcmp(args[p],"--iso")==0) || (strcmp(args[p],"-i")==0))
193 if (p+1>=argc)
195 fprintf(stderr,"No ISO code given!\n");
196 return 0;
198 iso = atoi(args[p+1]);
199 if (iso<1 || iso>16)
201 fprintf(stderr,"not a valid ISO code: ISO 8859-%s\n",args[p+1]);
202 return 0;
204 p++;
206 else
207 if (strcmp(args[p],"-f")==0)
209 if (p+1>=argc)
211 fprintf(stderr,"no file to read given after \"-f\"!\n");
212 return 0;
214 fp = fopen(args[p+1],"r");
215 if (fp==NULL)
217 fprintf(stderr,"file \"%s\" not found!\n",args[p+1]);
218 return 0;
220 p++;
221 filename=args[p];
223 else
225 fprintf(stderr,"Unknown parameter \"%s\"\n",args[p]);
226 return 0;
230 if (no_header == 0)
232 //Header:
233 if (iso<0)
234 printf("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n");
235 else
236 printf("<?xml version=\"1.0\" encoding=\"ISO-8859-%i\" ?><!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n",iso);
237 printf("<!-- This file was created with the aha Ansi HTML Adapter. http://ziz.delphigl.com/tool_aha.php -->\n");
238 printf("<html xmlns=\"http://www.w3.org/1999/xhtml\">\n");
239 printf("<head>\n<meta http-equiv=\"Content-Type\" content=\"application/xml+xhtml; charset=UTF-8\" />\n");
240 if (title)
241 printf("<title>%s</title>\n",title);
242 else
244 if (filename==NULL)
245 printf("<title>stdin</title>\n");
246 else
247 printf("<title>%s</title>\n",filename);
249 if (stylesheet)
251 printf("<style type=\"text/css\">\n");
252 switch (colorshema)
254 case 1: printf("body {color: white; background-color: black;}\n");
255 printf(".reset {color: white;}\n");
256 printf(".bg-reset {background-color: black;}\n");
257 break;
258 case 2: printf("body {background-color: pink;}\n");
259 printf(".reset {color: black;}\n");
260 printf(".bg-reset {background-color: pink;}\n");
261 break;
262 default: printf(".reset {color: black;}\n");
263 printf(".bg-reset {background-color: white;}\n");
265 if (colorshema!=1)
267 printf(".dimgray {color: dimgray;}\n");
268 printf(".red {color: red;}\n");
269 printf(".green {color: green;}\n");
270 printf(".yellow {color: olive;}\n");
271 printf(".blue {color: blue;}\n");
272 printf(".purple {color: purple;}\n");
273 printf(".cyan {color: teal;}\n");
274 printf(".white {color: gray;}\n");
275 printf(".bg-black {background-color: black;}\n");
276 printf(".bg-red {background-color: red;}\n");
277 printf(".bg-green {background-color: green;}\n");
278 printf(".bg-yellow {background-color: olive;}\n");
279 printf(".bg-blue {background-color: blue;}\n");
280 printf(".bg-purple {background-color: purple;}\n");
281 printf(".bg-cyan {background-color: teal;}\n");
282 printf(".bg-white {background-color: gray;}\n");
284 else
286 printf(".dimgray {color: dimgray;}\n");
287 printf(".red {color: red;}\n");
288 printf(".green {color: lime;}\n");
289 printf(".yellow {color: yellow;}\n");
290 printf(".blue {color: #3333FF;}\n");
291 printf(".purple {color: fuchsia;}\n");
292 printf(".cyan {color: aqua;}\n");
293 printf(".white {color: white;}\n");
294 printf(".bg-black {background-color: black;}\n");
295 printf(".bg-red {background-color: red;}\n");
296 printf(".bg-green {background-color: lime;}\n");
297 printf(".bg-yellow {background-color: yellow;}\n");
298 printf(".bg-blue {background-color: #3333FF;}\n");
299 printf(".bg-purple {background-color: fuchsia;}\n");
300 printf(".bg-cyan {background-color: aqua;}\n");
301 printf(".bg-white {background-color: white;}\n");
303 printf(".underline {text-decoration: underline;}\n");
304 printf(".bold {font-weight: bold;}\n");
305 printf(".blink {text-decoration: blink;}\n");
306 printf("</style>\n");
308 if (word_wrap)
310 printf("<style type=\"text/css\">pre {white-space: pre-wrap; white-space: -moz-pre-wrap !important;\n");
311 printf("white-space: -pre-wrap; white-space: -o-pre-wrap; word-wrap: break-word;}</style>\n");
313 printf("</head>\n");
314 if (stylesheet || ! colorshema)
315 printf("<body>\n");
316 else
318 switch (colorshema)
320 case 1: printf("<body style=\"color:white; background-color:black\">\n"); break;
321 case 2: printf("<body style=\"background-color:pink\">\n"); break;
325 //default values:
326 //printf("<div style=\"font-family:monospace; white-space:pre\">");
327 printf("<pre>\n");
330 //Begin of Conversion
331 unsigned int c;
332 int fc = -1; //Standard Foreground Color //IRC-Color+8
333 int bc = -1; //Standard Background Color //IRC-Color+8
334 int ul = 0; //Not underlined
335 int bo = 0; //Not bold
336 int bl = 0; //No Blinking
337 int ofc,obc,oul,obo,obl; //old values
338 int line=0;
339 int momline=0;
340 int newline=-1;
341 int temp;
342 while ((c=fgetc(fp)) != EOF)
344 if (c=='\033')
346 //Saving old values
347 ofc=fc;
348 obc=bc;
349 oul=ul;
350 obo=bo;
351 obl=bl;
352 //Searching the end (a letter) and safe the insert:
353 c='0';
354 char buffer[1024];
355 int counter=0;
356 while ((c<'A') || ((c>'Z') && (c<'a')) || (c>'z'))
358 c=getNextChar(fp);
359 buffer[counter]=c;
360 if (c=='>') //end of htop
361 break;
362 counter++;
363 if (counter>1022)
364 break;
366 buffer[counter-1]=0;
367 pelem elem;
368 switch (c)
370 case 'm':
371 //printf("\n%s\n",buffer); //DEBUG
372 elem=parseInsert(buffer);
373 pelem momelem=elem;
374 while (momelem!=NULL)
376 //jump over zeros
377 int mompos=0;
378 while (mompos<momelem->digitcount && momelem->digit[mompos]==0)
379 mompos++;
380 if (mompos==momelem->digitcount) //only zeros => delete all
382 bo=0;ul=0;bl=0;fc=-1;bc=-1;
384 else
386 switch (momelem->digit[mompos])
388 case 1: bo=1; break;
389 case 2: if (mompos+1<momelem->digitcount)
390 switch (momelem->digit[mompos+1])
392 case 1: //Reset blink and bold
393 bo=0;
394 bl=0;
395 break;
396 case 4: //Reset underline
397 ul=0;
398 break;
399 case 7: //Reset Inverted
400 temp = bc;
401 if (fc == -1 || fc == 9)
403 if (colorshema!=1)
404 bc = 0;
405 else
406 bc = 7;
408 else
409 bc = fc;
410 if (temp == -1 || temp == 9)
412 if (colorshema!=1)
413 fc = 7;
414 else
415 fc = 0;
417 else
418 fc = temp;
419 break;
421 break;
422 case 3: if (mompos+1<momelem->digitcount)
423 fc=momelem->digit[mompos+1];
424 break;
425 case 4: if (mompos+1==momelem->digitcount)
426 ul=1;
427 else
428 bc=momelem->digit[mompos+1];
429 break;
430 case 5: bl=1; break;
431 case 7: //TODO: Inverse
432 temp = bc;
433 if (fc == -1 || fc == 9)
435 if (colorshema!=1)
436 bc = 0;
437 else
438 bc = 7;
440 else
441 bc = fc;
442 if (temp == -1 || temp == 9)
444 if (colorshema!=1)
445 fc = 7;
446 else
447 fc = 0;
449 else
450 fc = temp;
451 break;
454 momelem=momelem->next;
456 deleteParse(elem);
457 break;
458 case 'H':
459 if (htop_fix) //a little dirty ...
461 elem=parseInsert(buffer);
462 pelem second=elem->next;
463 if (second==NULL)
464 second=elem;
465 newline=second->digit[0]-1;
466 if (second->digitcount>1)
467 newline=(newline+1)*10+second->digit[1]-1;
468 if (second->digitcount>2)
469 newline=(newline+1)*10+second->digit[2]-1;
470 deleteParse(elem);
471 if (newline<line)
472 line_break=1;
474 break;
476 if (htop_fix)
477 if (line_break)
479 for (;line<80;line++)
480 printf(" ");
482 //Checking the differences
483 if ((fc!=ofc) || (bc!=obc) || (ul!=oul) || (bo!=obo) || (bl!=obl)) //ANY Change
485 if ((ofc!=-1) || (obc!=-1) || (oul!=0) || (obo!=0) || (obl!=0))
486 printf("</span>");
487 if ((fc!=-1) || (bc!=-1) || (ul!=0) || (bo!=0) || (bl!=0))
489 if (stylesheet)
490 printf("<span class=\"");
491 else
492 printf("<span style=\"");
493 switch (fc)
495 case 0: if (stylesheet)
496 printf("dimgray ");
497 else
498 printf("color:dimgray;");
499 break; //Black
500 case 1: if (stylesheet)
501 printf("red ");
502 else
503 printf("color:red;");
504 break; //Red
505 case 2: if (stylesheet)
506 printf("green ");
507 else if (colorshema!=1)
508 printf("color:green;");
509 else
510 printf("color:lime;");
511 break; //Green
512 case 3: if (stylesheet)
513 printf("yellow ");
514 else if (colorshema!=1)
515 printf("color:olive;");
516 else
517 printf("color:yellow;");
518 break; //Yellow
519 case 4: if (stylesheet)
520 printf("blue ");
521 else if (colorshema!=1)
522 printf("color:blue;");
523 else
524 printf("color:#3333FF;");
525 break; //Blue
526 case 5: if (stylesheet)
527 printf("purple ");
528 else if (colorshema!=1)
529 printf("color:purple;");
530 else
531 printf("color:fuchsia;");
532 break; //Purple
533 case 6: if (stylesheet)
534 printf("cyan ");
535 else if (colorshema!=1)
536 printf("color:teal;");
537 else
538 printf("color:aqua;");
539 break; //Cyan
540 case 7: if (stylesheet)
541 printf("white ");
542 else if (colorshema!=1)
543 printf("color:gray;");
544 else
545 printf("color:white;");
546 break; //White
547 case 9: if (stylesheet)
548 printf("reset ");
549 else if (colorshema!=1)
550 printf("color:black;");
551 else
552 printf("color:white;");
553 break; //Reset
555 switch (bc)
557 case 0: if (stylesheet)
558 printf("bg-black ");
559 else
560 printf("background-color:black;");
561 break; //Black
562 case 1: if (stylesheet)
563 printf("bg-red ");
564 else
565 printf("background-color:red;");
566 break; //Red
567 case 2: if (stylesheet)
568 printf("bg-green ");
569 else if (colorshema!=1)
570 printf("background-color:green;");
571 else
572 printf("background-color:lime;");
573 break; //Green
574 case 3: if (stylesheet)
575 printf("bg-yellow ");
576 else if (colorshema!=1)
577 printf("background-color:olive;");
578 else
579 printf("background-color:yellow;");
580 break; //Yellow
581 case 4: if (stylesheet)
582 printf("bg-blue ");
583 else if (colorshema!=1)
584 printf("background-color:blue;");
585 else
586 printf("background-color:#3333FF;");
587 break; //Blue
588 case 5: if (stylesheet)
589 printf("bg-purple ");
590 else if (colorshema!=1)
591 printf("background-color:purple;");
592 else
593 printf("background-color:fuchsia;");
594 break; //Purple
595 case 6: if (stylesheet)
596 printf("bg-cyan ");
597 else if (colorshema!=1)
598 printf("background-color:teal;");
599 else
600 printf("background-color:aqua;");
601 break; //Cyan
602 case 7: if (stylesheet)
603 printf("bg-white ");
604 else if (colorshema!=1)
605 printf("background-color:gray;");
606 else
607 printf("background-color:white;");
608 break; //White
609 case 9: if (stylesheet)
610 printf("bg-reset ");
611 else if (colorshema==1)
612 printf("background-color:black;");
613 else if (colorshema==2)
614 printf("background-color:pink;");
615 else
616 printf("background-color:white;");
617 break; //Reset
619 if (ul)
621 if (stylesheet)
622 printf("underline ");
623 else
624 printf("text-decoration:underline;");
626 if (bo)
628 if (stylesheet)
629 printf("bold ");
630 else
631 printf("font-weight:bold;");
633 if (bl)
635 if (stylesheet)
636 printf("blink ");
637 else
638 printf("text-decoration:blink;");
641 printf("\">");
645 else
646 if (c==13 && htop_fix)
648 for (;line<80;line++)
649 printf(" ");
650 line=0;
651 momline++;
652 printf("\n");
654 else if (c!=8)
656 line++;
657 if (line_break)
659 printf("\n");
660 line=0;
661 line_break=0;
662 momline++;
664 if (newline>=0)
666 while (newline>line)
668 printf(" ");
669 line++;
671 newline=-1;
673 switch (c)
675 case '&': printf("&amp;"); break;
676 case '\"': printf("&quot;"); break;
677 case '<': printf("&lt;"); break;
678 case '>': printf("&gt;"); break;
679 case '\n':case 13: momline++;
680 line=0;
681 default: printf("%c",c);
683 if (iso>0) //only at ISOS
684 if ((c & 128)==128) //first bit set => there must be followbytes
686 int bits=2;
687 if ((c & 32)==32)
688 bits++;
689 if ((c & 16)==16)
690 bits++;
691 for (int meow=1;meow<bits;meow++)
692 printf("%c",getNextChar(fp));
698 //Footer
699 if ((fc!=-1) || (bc!=-1) || (ul!=0) || (bo!=0) || (bl!=0))
700 printf("</span>\n");
702 if (no_header == 0)
704 printf("</pre>\n");
705 printf("</body>\n");
706 printf("</html>\n");
709 if (fp!=stdin)
710 fclose(fp);
711 return 0;