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

/***************************************
** Prototypes                          *
***************************************/
long RLDecode(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 = RLDecode(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;
}

/***************************************
* RLDecode()                           *
*                                      *
* Compressed een bestand en schrijft   *
* hem naar diskette. Als invoer is een *
* pointer naar het invoerbestand en    *
* een uitvoer bestand vereist.         *
***************************************/
long RLDecode(FILE *InputFile,
              FILE *OutputFile)
{
	char	ActualChar;
	int	InputChar,
			CharCount;
	long	OutputSize = 0;

	while((InputChar = getc(InputFile)) != EOF)
	{
		ActualChar = (char) InputChar;
		CharCount = getc(InputFile);
		OutputSize += CharCount;
		while(CharCount--)
			putc(ActualChar, OutputFile);
   }
   return OutputSize;
}

