home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD1.iso / Printer / add16.dms / in.adf / Free_Software / StripANSI_v1.2 / Source / source.lzh / prog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-15  |  3.6 KB  |  165 lines

  1. /*********************************************************************\
  2.  *                          StripANSI v1.2                           *
  3.  *                                      *
  4.  *                         CLI Only Version                          *
  5.  *                                                                   *
  6.  *                     Written by Syd L. Bolton                      *
  7.  *         Copyright ©1991 Legendary Design Technologies Inc.        *
  8.  *                                                                   *
  9.  *         Revision: 001  Date: July 28, 1991  Time: 19:50:34        *
  10.  *         Revision: 002  Date: Sept 18, 1992  Time: 20:01:00        *
  11. \*********************************************************************/
  12.  
  13. #include <stdio.h>
  14.  
  15. #define ENTRIES 26
  16. #define EXTENTRIES 11
  17.  
  18. FILE *rf,*wf;
  19. int count=0,iterations[ENTRIES+EXTENTRIES];
  20.  
  21. int codes[] = {
  22.     99,64,65,66,67,68,69,70,72,74,75,76,77,80,83,84,104,108,109,110,
  23.     112,113,116,117,120,121
  24.     };
  25.  
  26. char *cnames[] = {
  27.     "RESET TO INITIAL STATE",
  28.     "INSERT [N] CHARACTERS",
  29.     "CURSOR UP [N] CHARACTERS",
  30.     "CURSOR DOWN [N] CHARACTERS",
  31.     "CURSOR FWD [N] CHARACTERS",
  32.     "CURSOR BKWD [N] CHARACTERS",
  33.     "CURSOR NEXT LINE [N]",
  34.     "CURSOR PRECEDING LINE [N]",
  35.     "MOVE CURSOR TO ROW/COLUMN",
  36.     "ERASE TO END OF DISPLAY",
  37.     "ERASE TO END OF LINE",
  38.     "INSERT LINE",
  39.     "DELETE LINE",
  40.     "DELETE CHARACTER [N]",
  41.     "SCROLL UP [N] LINES",
  42.     "SCROLL DOWN [N] LINES",
  43.     "SET LINEFEED MODE",
  44.     "RESET NEWLINE MODE",
  45.     "SELECT GRAPHIC RENDITION",
  46.     "DEVICE STATUS REPORT",
  47.     "»SET CURSOR RENDITION",
  48.     "»WINDOW STATUS REQUEST",
  49.     "»SET PAGE LENGTH",
  50.     "»SET LINE LENGTH",
  51.     "»SET LEFT OFFSET",
  52.     "»SET TOP OFFSET"
  53. };
  54.  
  55. int extcodes[] = {
  56.     7,8,12,13,14,15,17,18,19,20,24
  57.     };
  58.  
  59. char *extcnames[] = {
  60.     "BELL",
  61.     "BACKSPACE",
  62.     "FORM FEED",
  63.     "CARRIAGE RETURN",
  64.     "DOUBLE WIDTH MODE",
  65.     "CONDENSED MODE",
  66.     "SELECT PRINTER",
  67.     "CANCEL CONDENSED",
  68.     "DESELECT PRINTER",
  69.     "CANCEL DOUBLE WIDTH",
  70.     "CANCEL LINE"
  71. };
  72.  
  73. main(argc,argv)
  74. int argc;
  75. char *argv[];
  76. {
  77. register short i,rv;
  78. int report=0,true=0;
  79. char c;
  80.  
  81. if (argc < 3) {
  82.     puts("StripANSI v1.2 ©1991,92 Legendary Design Technologies Inc.");
  83.     puts("Written by Syd L. Bolton");
  84.     puts(" ");
  85.     puts("Usage: StripANSI inputfile outputfile [REPORT,TRUE]");
  86.     exit(1);
  87.     }
  88.  
  89. if (!(rf=fopen(argv[1],"rb"))) {
  90.     puts("StripANSI: Couldn't open input file.");
  91.     exit(1);
  92.     }
  93.  
  94. if (!(wf=fopen(argv[2],"wb"))) {
  95.     puts("StripANSI: Couldn't open output file.");
  96.     exit(1);
  97.     }
  98.  
  99. if (argc > 3) {
  100.     for (i=3; i<argc; i++) {
  101.         if (!(strcmp("REPORT",argv[i])) || !(strcmp("report",argv[i]))) report=1;
  102.         if (!(strcmp("TRUE",argv[i])) || !(strcmp("true",argv[i]))) true=1;
  103.         }
  104.     }
  105.  
  106. do {
  107.     c=getc(rf);
  108.     if (!(feof(rf))) {
  109.         if (c=='\0x9b' || c==27) stripcode();  /* either CSI or ESC */
  110.             else {
  111.                 if (true==0) rv=checkextended(c); else rv=0;
  112.                 if (rv==0) fputc(c,wf);
  113.                 }
  114.         }
  115.     } while(!(feof(rf)));
  116.  
  117. fclose(rf);
  118. fclose(wf);    
  119.  
  120. if (report==1 && count > 0) {
  121.     puts("        ANSI CODE NAME             #    %");
  122.     puts("---------------------------------------------");
  123.     for (i=0; i<ENTRIES; i++) 
  124.         if (iterations[i]) printf("%30s: %6d %3d%%\n",cnames[i],iterations[i],iterations[i]*100/count);
  125.     if (true==0) {
  126.         for (i=0; i<EXTENTRIES; i++) 
  127.             if (iterations[ENTRIES+i]) printf("%30s: %6d %3d%%\n",extcnames[i],iterations[ENTRIES+i],iterations[ENTRIES+i]*100/count);
  128.         }    
  129.     puts("---------------------------------------------");
  130.     }
  131. }
  132.  
  133. stripcode()
  134. {
  135. int i,exit_flag=0;
  136. char c;
  137.  
  138. do {
  139.     c=getc(rf);
  140.     for (i=0; i<ENTRIES; i++) { 
  141.         if (c==codes[i]) {
  142.             exit_flag=1;
  143.             iterations[i]++;
  144.             count++;
  145.             }
  146.         }
  147.     } while (exit_flag==0) ;
  148. }
  149.  
  150. checkextended(c)
  151. char c;
  152. {
  153. register short i;
  154. int rv=0;
  155.  
  156. for (i=0; i<EXTENTRIES; i++) {
  157.     if (c==extcodes[i]) {
  158.         count++;
  159.         iterations[ENTRIES+i]++;
  160.         rv=1;
  161.         }
  162.     }
  163. return(rv);
  164. }
  165.