home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / ANSIREM2.ZIP / ANSI-REM.C next >
C/C++ Source or Header  |  1991-04-18  |  6KB  |  155 lines

  1. /*  ansi-rem - A program to remove ANSI escape sequences from a    */
  2. /*             text file.                                          */
  3.  
  4. /*  This program uses a very simple-minded method of identifying   */
  5. /*  an escape sequence.  By definition, an escape sequence begins  */
  6. /*  with the ESC character (hex 1B).  The program assumes that     */
  7. /*  the sequence ends with the first alphabetic character (case-   */
  8. /*  insensitive) that follows.                                     */
  9. /*  This identifies all escape sequences, but in fact, some        */
  10. /*  sequences end with a digit and some with just a bracket such   */
  11. /*  as '>'.  As a result, the above method will also chop off the  */
  12. /*  first letter of whatever text follows such an escape sequence  */
  13. /*  You may therefore find it necessary to edit the resulting file */
  14. /*  with a text editor to put some characters back.                */
  15.  
  16. /*  Compiled using Microsoft QuickC version 2.5 with the following */
  17. /*                                                                 */
  18. /*            qcl /Olt /Za /W4 ansi-rem.c                          */
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23.  
  24. /*  Global variable declarations.  */
  25. FILE *infile, *outfile;
  26.  
  27. int main (int argc, char *argv[])
  28. {
  29. /*  This is implemented as a two-state finite state machine    */
  30. /*  The states are START (found a starting delimiter) and STOP */
  31. /*  These enums are also used as the indices of the            */
  32. /*  array cnt[] which counts how many opening delimiters were  */
  33. /*  found when in the START state, not including the one which */
  34. /*  which put us there, and the number of closing delimiters   */
  35. /*  found in the STOP state, not including the one which put   */
  36. /*  us there.
  37.  
  38. /*  If either cnt[] is non-zero then a warning message is     */
  39. /*  output to stderr telling how many unbalanced escape       */
  40. /*  characters were found and we return with EXIT_FAILURE.    */
  41. /*  Otherwise we return with EXIT_SUCCESS.                    */
  42.  
  43.     enum States { START, STOP, STATECHNGE };
  44.     int c, state = STOP, cnt[3] = { 0, 0, 0 };
  45.     char sequence[20], *seqptr, *ptr;
  46.  
  47.     /*  Announce myself  */
  48.  
  49.     fprintf(stderr, "\nRemoves escape sequences from text files\n"
  50.                       "(The Cursor Forward sequence is translated to a single space.)\n"
  51.                       "(Cursor Position to column 1 is translated to a newline.)\n"
  52.                       "                        Gordon Torrie  April 1991\n\n");
  53.  
  54.     /*  Check that the input and output files were specified and
  55.         that they exist!                                         */
  56.  
  57.     if (argc < 2) {
  58.     fprintf(stderr, "Read from what file?\nWrite to what file?\n"  
  59.                         "Try:  %s <infile.ext> <outfile.ext>\n\n", argv[0]);
  60.         exit(2);
  61.         }
  62.     if (argc < 3) {
  63.     fprintf(stderr, "Write to what file?\n"
  64.                         "Try:  %s %s <outfile.ext>\n\n", argv[0], argv[1]);
  65.         exit(3);
  66.         }
  67.  
  68.     if ( (infile = fopen(argv[1],"rb")) == NULL) {
  69.         fprintf(stderr, "Unable to open file: %s\nDoes it exist?\n\n", argv[1]);
  70.         exit(4);
  71.         }
  72.  
  73.     if ( (outfile = fopen(argv[2],"wb")) == NULL) {
  74.         fprintf(stderr, "Unable to open file: %s\nDisk or directory full?\n\n", argv[2]);
  75.         exit(4);
  76.         }
  77.  
  78.     /*  Main part of program.                                     */
  79.     /*  Report what we are about to do.                           */
  80.     fprintf(stderr, "Removing escape sequences.\n");
  81.  
  82.     while ( (c = fgetc(infile)) != EOF)
  83.        {
  84.  
  85.        /* If the character just read is the start delimiter       */
  86.        if (c == '\x1B')
  87.           {
  88.           strcpy( sequence, "" );
  89.           seqptr = sequence;
  90.           if (state == STOP) 
  91.              {
  92.              state = START;
  93.              cnt[STATECHNGE]++;
  94.              }
  95.           else cnt[START]++;
  96.           }
  97.  
  98.        /* If the character just read is the stop delimiter       */
  99.        else if ( (state == START) &&
  100.                 (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", c) != NULL) )
  101.           {
  102.           state = STOP;
  103.           cnt[STATECHNGE]--;
  104.  
  105.           /*  Here's where we put in some intelligence.            */
  106.           /*  At this point the array sequence[] will contain the  */
  107.           /*  escape sequence string (except for the starting      */
  108.           /*  escape character and the terminating alpha character.*/ 
  109.  
  110.           switch( c )
  111.              {
  112.  
  113.              /*  If the sequence was a cursor forward sequence,    */
  114.              /*  then output one space.                            */
  115.  
  116.              case 'C': fputc(' ', outfile);     break;
  117.  
  118.              /*  If the sequence was a cursor position sequence    */
  119.              /*  and it positioned to column 1 of whatever line,   */
  120.              /*  then output a newline.                            */
  121.  
  122.              case 'H':
  123.              case 'f': if ( (ptr = strchr(sequence, ';')) != NULL &&
  124.                           strcmp(++ptr, "1") == 0 )
  125.                           fputc('\n', outfile); break;
  126.  
  127.              default:                           break;
  128.              }
  129.           }
  130.  
  131.        /* If the character just read is neither the start nor   */
  132.        /* the stop delimiter                                    */
  133.        else if (state == STOP) fputc(c, outfile);
  134.  
  135.             /*  If we are reading an escape sequence               */
  136.             /*  (state == START) then save the character in the    */
  137.             /*  array sequence[], increment the pointer and add a  */
  138.             /*  terminating null.                                  */
  139.  
  140.             else 
  141.                {
  142.                *seqptr++ = (char) c;
  143.                *seqptr = '\0';
  144.                }
  145.        }
  146.  
  147.    fclose(infile);
  148.    fclose(outfile);
  149.    cnt[START] += cnt[STATECHNGE];
  150.    if (cnt[START] != 0 )
  151.       fprintf(stderr, "\nThere %s %d unbalanced escape character%s\n", (cnt[START] == 1) ? "was" : "were", cnt[START], cnt[START]==1 ? "." : "s.");
  152.    if (cnt[START] != 0 || cnt[STOP] != 0) return EXIT_FAILURE;
  153.    else return EXIT_SUCCESS;
  154.    }
  155.