/**************************************************************************\
*     Written on November 30, 1988 by Christopher J. Dyl (GEnie: C.DYL)    *
*  If you are like me, you like to use ED all the time.  ED is a nice and  *
*  small editor and I use it a lot.  The problem with ED is that you will  *
*  constantly get a "File contains binary" message when it really only has *
*  a few control characters.  This program will do many things to modify a *
*  file in order to make it EDible.  First, it will remove all characters  *
*  that ED cannot handle.  It will also remove the CR/LFs of many files as *
*  when using ED, only LFs are needed and CR/LFs will leave lots of messy  *
*  double spacing in the file.  Another nice feature is the BS (BACKSPACE) *
*   handler.  If you have ever captured an online session from a BBS, you  *
*  will appreciate this function.  It will take out the backspaces in the  *
*  file and move back 1 space in the file so it can be replaced with the   *
*  next character.  If the next character is a backspace, it will continue *
*  to go back as many spaces as it takes until it starts printing forward  *
*  again.  Another feature is replacing a TAB with 8 spaces.  Since TABs   *
*  are many times 5 spaces, I just thought this would be easier and since  *
*  this is my first C program, I figured I would keep it simple.  To get   *
*  usage of the program, just type EDible.  An optional third comment may  *
*  be added if you like.  This can be a number from 0-255.  It will tell   *
*   the program the maximum line length you want.  Leaving it blank will   *
*  disable this function.  If you really like this program and would like  *
*  to make any sort of donation, please send it to:                        *
*    Christopher Dyl                 GEnie Address:  C.DYL                 *
*    191 Agricultural Avenue          Phone Number:  (508)/222-8320        *
*    Rehoboth, MA  02769             Compiled using Manx Aztec 3.6a        *
*  Any amout will be greatly appreciated and will convince me to write yet *
*  more programs!  If you have any ideas of programs you have always have  *
*  liked to have, please send me a letter at the same address.  I am a new *
*  programmer and and looking forward to programming ideas from the people *
*  themselves so I can know what they would like!  Thank you for any money *
*  you donate.  I am currently rather broke and ANY amount of money will   *
*  help!                                                                   *
\**************************************************************************/

#include <stdio.h>

#define CR  0x0D            /* Carriage Return */
#define LF  0x0A            /* Line Feeds */
#define SP  0x20            /* SPace */
#define BS  0x08            /* BackSpace */
#define DEL 0x7F            /* DELete */
#define TAB 0x09            /* TAB */
#define LOW 0xA0            /* LOWest alternate character */
#define HI  0xFF            /* HIghest alternate character */

main(argc, argv)
  int argc;
  char *argv[];
{
int max = 0, len = 0, chr = 0, lc = 0;
int c, x, lines = 0, tchr = 0;

FILE *fp1, *fp2, *fopen();

  if (argc < 3)
  { printf("Usage: EDible input output [Max Line Length]\n");
    exit(1);
  }
  else if ((fp1 = fopen(argv[1], "r")) == NULL)
  { printf("Cannot open file %s\n", argv[1]);
    exit(1);
  }
  else if ((fp2 = fopen(argv[2], "x+")) == NULL)
  { printf("Cannot create file %s\n", argv[2]);
    exit(1);
  }
  else
    printf("Making file %s usable with ED as file %s\n", argv[1], argv[2]);
  if (argc > 3)
    sscanf(argv[3], "%d", &max);         /* Convert argv[3] to a decimal */

{
 while ((c = getc(fp1)) != EOF)
 { if ((c == CR) ||
       (c == LF && lc != CR) ||          /* Eliminates CR/LFs together */
       (c >= SP && c < DEL) ||
       (c >= LOW && c <= HI))            /* Check weeding conditions */
   { putc(c, fp2);                       /* put character in file */
     chr++;
     if (++len == max)                   /* force a linefeed if the line */
     { len = 0;                          /* is greater than the maximum */
       lines++;
       putc(LF, fp2);
     }
     else if (len > 512)                 /* got an overflow so this is to */
       len = 0;                          /* prevent that */
   }
     if (c == LF || c == CR)             /* set length to 0 if a LF or CR */
     { len = 0;                          /* is in the file */
       lines++;
     }
   else if (c == TAB)
   { for (x=0; x < 8; x++)               /* extremely cheap tab handler */
       putc(SP, fp2);
     tchr++;
     chr =+ 5;                           /* count total characters weeded */
   }
   else if (c == BS)
   { fseek(fp2, -1L, 1);                 /* very good backspace handler */
     tchr++;
   }
   lc = c;                               /* put character to variable */
   tchr++;
 }
}

fclose(fp1);                 /* Close all files and then print results */
fclose(fp2);
printf("\nA total of %d characters were replaced.", chr);
printf("\nA total of %d characters were weeded.", tchr);
printf("\nThere are %d lines in the file.\n\n", lines);
}
