#include <stdio.h>
#include <stdlib.h>
/* #include <ctype.h> */
#include "getCharacter.h"

int 
getCharacters(FILE *source,int option)
{
  /* printf("%d\n",option); */
  
  unsigned int lines=0;
  unsigned int characters = 0;
  unsigned int words = 0; 
  
  char ch;
  char tmp = ' '; /* temp buffer, initialized to a known condition */

while((ch = fgetc(source)))
    {
      if (ch == EOF)
	{
	  if (tmp != ' ' && tmp != '\n')
	    {
	    words++;
	  /* printf("_WORDS++_"); */
	    }
	  break;
	}
      
      printf("%c", ch); /* prints the test as it reads is */
      
      characters++; 

      if (ch == '\n')
	{
	  lines++;
	  /* printf("_LINES++_");  */
	}
      
      if (ch == ' ' && (tmp != ' ' && tmp != '\n'))
	{
	  words++;
	  /* printf("_WORDS++_"); */
	}
      else if (ch == '\n' && (tmp != '\n' && tmp != ' '))
      	{
      	  words++;
      	  /* printf("_WORDS++_");    */
      	}
      
      tmp = ch; /* tmp receives the now outdated ch value */
    }
 
 switch (option)
   {
   case 0:
     fprintf(stdout,"%i %i %i\n", lines, words, characters);
     break;
   case 1:
     fprintf(stdout, "%i\n", characters);
     break;
   case 2:
     fprintf(stdout, "%i\n", lines);
     break;
   }
  /* fprintf(stdout ,"EOF Recieved\n"); */
  /* fprintf(stdout ,"No. of characters: %d\n", characters); */
  /* fprintf(stdout ,"No. of WORDS: %d\n", words); */
  /* fprintf(stdout ,"No. of lines: %d\n", lines); */
  return 0;
}

