#include <stdio.h>
#include "functions.h"

void change_case(FILE *in_ptr, FILE *out_ptr) //Changes case of all char's in file and prints
{
	char ch;

	while(1)
	{
		ch = fgetc(in_ptr);
		
		if(feof(in_ptr)) //Check if end of file
		{
			break;
		}
		else
		if(ch >= 65 && ch <= 90) //Makes letters Lowercase
		{
			ch += 32;
			fputc(ch, out_ptr);
		}
		else
		if(ch >= 97 && ch <= 122) //Makes letters Uppercase
		{
			ch -= 32;
			fputc(ch, out_ptr);
		}
		else
		if(ch < 65 || (ch > 90 && ch < 97)) //Any non-char's
		{
			fputc(ch, out_ptr);
		}
		if(ch > 122)
		{
			fputc(ch, out_ptr); //Any non-char's
		}
	}
	
	fprintf(out_ptr, "\n");

}

int word_sent_count(FILE *in_ptr, FILE *out_ptr) //Counts words and sentences
{
	int w_count, s_count, index;
	//char ch;
	char ch[600];

	w_count = 0; //Initialize
	s_count = 0; //Initialize

	fprintf(out_ptr, "**************\n"); //Section Start
	
	fseek(in_ptr, 0, SEEK_SET); //Reset ch to begininng of in_ptr
	
	/*while(1)
	{
		ch = fgetc(in_ptr);

		if(feof(in_ptr)) //Check if end of file
		{
			break;
		}
		else
		if(ch == ' ') //Check for spaces
		{
			ch = fgetc(in_ptr); //Checks if next character is int
			if(ch < 47 || ch > 58)
			{
				//ch = fgetc(in_ptr);
				//if(ch < 47 || ch > 58)
				
					w_count++; //Adds one to word count	
				
				
			}			
		}
		else
		if(ch == '.' || ch == '!' || ch == '?') //Check for end of sentence
		{
			//w_count++; //Adds one to word count
			s_count++; //Adds one to sentence count
			ch = fgetc(in_ptr);
			if(ch == ' ')
				w_count++;
		}

		
	}*/

	for(index = 0; ;index++) //Initialize text array
	{
		ch[index] = fgetc(in_ptr);
		if(feof(in_ptr))
		{
			break;
		}
	}
	for(index = 0; ;index++)
	{
		if(ch[index] == 0)
		{
			break;
		}
		else
		if(ch[index] == ' ') //Checks for space
		{
			if(ch[index-1] < 47 || ch[index-1] > 58) //Checks if previous character is int
				w_count++;
			else
			if(ch[index-1] == '.' || ch[index-1] == '!' || ch[index-1] == '?')
				w_count++;

		}
		else
		if(ch[index] == '.' || ch[index] == '!' || ch[index] == '?')
		{
			s_count++;
		}
	}

	fprintf(out_ptr, "W_count is: %i\n", w_count);
	fprintf(out_ptr, "S_count is: %i\n", s_count);

	fprintf(out_ptr, "**************\n"); //Section End

	return 0;
}

void find_nums(FILE *in_ptr, FILE *out_ptr) //Distinguishes ints from chars
{
	char ch;
	int num;

	fprintf(out_ptr, "*********************\n");
	fprintf(out_ptr, "Below is a list of \nintegers in the file\n"); //Header
	fprintf(out_ptr, "*********************\n"); //Start of Section
	
	fseek(in_ptr, 0, SEEK_SET); //Reset ch to begininng of in_ptr
	while(1)
	{

		ch = fgetc(in_ptr);

		if(feof(in_ptr)) //Check if end of file
		{
			break;
		}
		if(ch > 47 && ch < 58)
		{
			fprintf(out_ptr, "%c\n", ch);
			//fscanf(out_ptr, "%i", &num);
			//fprintf(out_ptr, "\n");
		}	
		//if(ch == )
	}
		//fscanf(in_ptr, "hELLO uSER! tHIS IS JUST A TEXT %c", &ch);
		//fprintf(out_ptr, "%i\n", ch);
	
	fprintf(out_ptr, "****************\n"); //End of Section
}


void sum_avg(FILE *in_ptr, FILE *out_ptr)
{
	//Probably going to have to put this function inside of the find_nums function

	fprintf(out_ptr, "The sum is:    \n");
	fprintf(out_ptr, "The avg is:    \n");
	fprintf(out_ptr, "****************\n"); //End of Section
}





