/***************************************
* Naam        : RLEncode.C			      *
* Auteur      : Joost Brugman	         *
* Omschrijving: Compressieprogramma    *
* Copyright   : Uitgeverij Divo 1994   *
****************************************
* Includes                             *
***************************************/
#include <stdio.h>
#include <stdlib.h>

/***************************************
** Prototypes                          *
***************************************/
long RLEncode(FILE *, FILE *);

long FileSize(FILE *);

/***************************************
* main()                               *
*                                      *
* Het eigenlijke hoofdprogramma. Dit   *
* wordt als eerste aangeroepen na het  *
* opstarten.                           *
***************************************/
int main(int argc, char *argv[])
{
 FILE *Input, *Output;
 long InputSize, OutputSize;

 if(argc == 3)
 {
  /*************************************
  * Open invoerbestand.                *
  *************************************/
  Input = fopen(argv[1], "r");
  if(Input)
  {
   /************************************
   * Open uitvoerbestand.              *
   ************************************/
   Output = fopen(argv[2], "w");
   if(Output)
   {
    /***********************************
    * Lees lengte van invoerbestand.   *
    ***********************************/
    InputSize = FileSize(Input);
    printf("Invoer:  %s, %i bytes.\n",
           argv[1],
	        InputSize);
	 puts("Een moment...");

    OutputSize = RLEncode(Input,
                          Output);

    printf("Uitvoer: %s, %i bytes.\n",
           argv[2],
    		  OutputSize);

    fclose(Output);
   }
   else
    puts("Uitvoer bestand kan niet \
         worden geopend.");
   fclose(Input);
  }
  else
   puts("Invoer bestand niet \
        gevonden.");
 }
 else
  puts("RLEncode <invoerbestand> \
       <uitvoerbestand>");
 return 0;
}

/***************************************
* FileSize()                           *
*                                      *
* Berekent de grootte van een bestand  *
* aan de hand van zijn handle. De      *
* grootte van het bestand wordt        *
* teruggegeven als een long.           *
***************************************/                                     
long FileSize(FILE *stream)
{
   long curpos, length;

   curpos = ftell(stream);
    /* Vraag huidige positie op       */
   fseek(stream, 0L, SEEK_END);
    /* Ga naar einde                  */
   length = ftell(stream);            
    /* Vraag positie laatste byte     */
   fseek(stream, curpos, SEEK_SET);
    /* En terug naar orginele positie */
   return length;
}

/***************************************
* RLEncode()                           *
*                                      *
* Compressed een bestand en schrijft   *
* hem naar diskette. Als invoer is een *
* pointer naar het invoerbestand en    *
* een uitvoer bestand vereist.         *
***************************************/
long RLEncode(FILE *InputFile,
              FILE *OutputFile)
{
 char CurrentChar = getc(InputFile),
      PreviousChar = CurrentChar,
      CharCount = 0x0;
 long OutputSize = 0;

 while(CurrentChar !=EOF)
 {
  if((CurrentChar == PreviousChar)
    &&(CharCount != 255))
   CharCount++;
  else
  {
   putc(PreviousChar, OutputFile);
   putc(CharCount, OutputFile);
   PreviousChar = CurrentChar;
   CharCount = 1;
   OutputSize += 2;
  }   
  CurrentChar = getc(InputFile);
 }
 if(CharCount)
 {
   putc(PreviousChar, OutputFile);
   putc(CharCount, OutputFile);
   OutputSize += 2;
 }
 return OutputSize;
}

