home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 602b.lha / StripANSI_v1.0 / Source / source.lzh / prog.c < prev    next >
C/C++ Source or Header  |  1991-08-03  |  3KB  |  133 lines

  1. /*********************************************************************\
  2.  *                          StripANSI v1.0                           *
  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. \*********************************************************************/
  11.  
  12. #include <stdio.h>
  13.  
  14. #define ENTRIES 26
  15. #define BACKSPACE ENTRIES
  16. #define FORM_FEED ENTRIES+1
  17. #define CR_ONLY   ENTRIES+2
  18.  
  19. FILE *rf,*wf;
  20. int count=0,iterations[ENTRIES+3];
  21.  
  22. int codes[] = {
  23.     99,64,65,66,67,68,69,70,72,74,75,76,77,80,83,84,104,108,109,110,
  24.     112,113,116,117,120,121
  25.     };
  26.  
  27. char *cnames[] = {
  28.     "RESET TO INITIAL STATE",
  29.     "INSERT [N] CHARACTERS",
  30.     "CURSOR UP [N] CHARACTERS",
  31.     "CURSOR DOWN [N] CHARACTERS",
  32.     "CURSOR FWD [N] CHARACTERS",
  33.     "CURSOR BKWD [N] CHARACTERS",
  34.     "CURSOR NEXT LINE [N]",
  35.     "CURSOR PRECEDING LINE [N]",
  36.     "MOVE CURSOR TO ROW/COLUMN",
  37.     "ERASE TO END OF DISPLAY",
  38.     "ERASE TO END OF LINE",
  39.     "INSERT LINE",
  40.     "DELETE LINE",
  41.     "DELETE CHARACTER [N]",
  42.     "SCROLL UP [N] LINES",
  43.     "SCROLL DOWN [N] LINES",
  44.     "SET LINEFEED MODE",
  45.     "RESET NEWLINE MODE",
  46.     "SELECT GRAPHIC RENDITION",
  47.     "DEVICE STATUS REPORT",
  48.     "»SET CURSOR RENDITION",
  49.     "»WINDOW STATUS REQUEST",
  50.     "»SET PAGE LENGTH",
  51.     "»SET LINE LENGTH",
  52.     "»SET LEFT OFFSET",
  53.     "»SET TOP OFFSET",
  54.     "BACKSPACE",
  55.     "FORM FEED",
  56.     "CARRIAGE RETURN"
  57. };
  58.  
  59. main(argc,argv)
  60. int argc;
  61. char *argv[];
  62. {
  63. int i;
  64. char c;
  65.  
  66. if (argc < 3) {
  67.     puts("StripANSI v1.0 ©1991 Legendary Design Technologies Inc.");
  68.     puts("Written by Syd L. Bolton");
  69.     puts(" ");
  70.     puts("Usage: StripANSI inputfile outputfile [REPORT]");
  71.     exit(1);
  72.     }
  73.  
  74. if (!(rf=fopen(argv[1],"rb"))) {
  75.     puts("StripANSI: Couldn't open input file.");
  76.     exit(1);
  77.     }
  78.  
  79. if (!(wf=fopen(argv[2],"wb"))) {
  80.     puts("StripANSI: Couldn't open output file.");
  81.     exit(1);
  82.     }
  83.  
  84. do {
  85.     c=getc(rf);
  86.     if(!(feof(rf))) {
  87.     if (c=='\0x9b' || c==27) stripcode();  /* either CSI or ESC */
  88.     else if (c == 8) {
  89.         iterations[BACKSPACE]++;    
  90.         count++;
  91.         }
  92.     else if (c == 12) {
  93.         iterations[FORM_FEED]++;
  94.         count++;
  95.         }
  96.     else if (c == 13) {
  97.         iterations[CR_ONLY]++;
  98.         count++;
  99.         }
  100.     else fputc(c,wf);
  101.         }
  102.     } while(!(feof(rf)));
  103.  
  104. fclose(rf);
  105. fclose(wf);    
  106.  
  107. if (argc > 3 && count > 0) {
  108.     puts("        ANSI CODE NAME             #    %");
  109.     puts("---------------------------------------------");
  110.     for (i=0; i<ENTRIES+3; i++) 
  111.         if (iterations[i]) printf("%30s: %6d %3d%%\n",cnames[i],iterations[i],iterations[i]*100/count);
  112.     puts("---------------------------------------------");
  113.     }
  114. }
  115.  
  116. stripcode()
  117. {
  118. int i,exit_flag=0;
  119. char c;
  120.  
  121. do {
  122.     c=getc(rf);
  123.     for (i=0; i<ENTRIES; i++) { 
  124.         if (c==codes[i]) {
  125.             exit_flag=1;
  126.             iterations[i]++;
  127.             count++;
  128.             }
  129.         }
  130.     } while (exit_flag==0) ;
  131. }
  132.  
  133.