#include "my_f.h"

#define TMP_FILE "temp.tmp"

int fk(char *f_name)
{

	char err;
	int p=1,i=0;
	clrscr();
	err=0;
	printf("File name: ");
	gets(f_name);


	while (f_name[i])
	{
	   switch (p)
	   {
		case 1: if (isalpha(f_name[i]))
				{
				 i++;
				 p=2;
				}
				else err=1;
		case 2: if (f_name[i]==':')
				{
				 i++;
				 p=3;
				}
				else err=1;
		case 3: if (f_name[i]==92)
				{
				 i++;
				 p=4;
				}
				else err=1;
		case 4: if (f_name[i]!=92)
		{
		 i++;
		 p=5;
		}
		else err=1;
		case 5: if (!isalpha(f_name[i])&&f_name[i]!=92&&f_name[i]!='.'&&f_name[i]!='_'&&!isdigit(f_name[i]))
		 err=1;
	   }
		   i++;

	}

 if (err==1)
 {
  printf("Greshen pyt!");
  return 1;
 }

 return 0;
} //zatvarq6ta za funkciqta

int kk(char *f_name)
{
	FILE *out;
	char input;
	clrscr();

	my_strcpy(f_name,TMP_FILE);
	if ((out = fopen(f_name, "wt")) == NULL)
	{
	 printf("Cannot open file.\n");
	 return 1;
	}

	while (input!='+')
	{
	 input=getche();
	 if (input==13)
	 {
	  printf("\n");
	  input='\n';
	 }
	 if (input!='0')
	 putc(input,out);
	}

	fclose(out);
	clrscr();
	return 0;
}

int wc(char *pp)
{
	FILE *in,*out;
	int ch,sim[255];
	int i=0;
	for (i=0;i<255;i++) sim[i]=0;
	clrscr();

	if ((in = fopen(pp, "rt")) == NULL)
	{
	 printf("Cannot open file.\n");
	 return 1;
	}

	if ((out = fopen("res.txt", "wt")) == NULL)
	{
	 printf("Cannot open file.\n");
	 return 1;
	}


	while (!feof(in))
	{
	 ch=getc(in);
	 if (ch>0&&ch<255) sim[ch]++;
	}

	for (i=0;i<255;i++)
	{
	 if (i%25==0)
	 getch();
	 if(i!=26)
	 {
	  printf("%c: %d\n",i,sim[i]);
	  fprintf(out,"%c: %d\n",i,sim[i]);
	 }
	}

	/*printf("Done! -> res.txt");*/
	fclose(out);
	fclose(in);
	return 0;
}


int fwc(char *pp,int wh)
{
	FILE *in,*out;
	int ch,i=0,tos=0,k=1,ccn=0,ppp,*mm=malloc(20*sizeof(int));
	char word[100],**sort1;
	char **m=malloc(20*sizeof(char *));

	for(i=0;i<20;i++) mm[i]=0;
	for(i=0;i<20;i++) my_strcpy(m[i]," ");

	my_strcpy(word," ");
	i=0;
	clrscr();

	if ((in = fopen(pp, "rt")) == NULL)
	{
	 printf("Cannot open file.\n");
	 return 1;
	}

	if ((out = fopen("res.txt", "w")) == NULL)
	{
	 printf("Cannot open file.\n");
	 return 1;
	}

	while (!feof(in))
	{
	 ch=getc(in);

	 if (ch!=' '&&ch!='.'&&ch!='!'&&ch!='?'&&ch!='\n'&&ch!=';'&&ch!=',')
	 {
	  word[i]=ch;i++;
	 }

	 else if(my_strcmp(word," "))
	 {
	  word[i]=0;
	  for(i=0;i<tos;i++)
	  if (!my_strcmp(m[i],word)) ccn=i+1;

	  if (!ccn)
	  {
	   if(tos>=20*k)
	   {
		k++;
		m=realloc(m,20*k*sizeof(char *));
		mm=realloc(mm,20*k*sizeof(int));

		for(i=tos;i<20*k;i++)
		mm[i]=0;

		for(i=tos;i<20*k;i++)
		my_strcpy(m[i]," ");
	   }

		m[tos]=malloc(100*sizeof(char));
		my_strcpy(m[tos],word);
		mm[tos]++;
		tos++;
	  }

		else
		{
		 mm[ccn-1]++;
		 ccn=0;
		}

		my_strcpy(word," ");
		i=0;
		}

	}


 if (wh==0)
 {
  for (i=0;i<tos-1;i++)
  {
	if(my_strcmp(m[i+1],m[i])<0)
	{
	 sort1=m[i];ppp=mm[i];
	 m[i]=m[i+1];mm[i]=mm[i+1];
	 m[i+1]=sort1;mm[i+1]=ppp;
	 i=0;
	}
  }
 }


 for(i=1;i<tos;i++)
 {
  if(wh!=4)
  fprintf(out,"%s: %d\n",m[i],mm[i]);

  else
  fprintf(out,"%d: %d\n",i,mm[i]);

  if(wh!=4)
  printf("%s: %d\n",m[i],mm[i]);

  else
  printf("%d: %d\n",i,mm[i]);
 }

 /*printf("Done! -> res.txt\n");*/
 free(m);
 free(mm);
 fclose(in);
 fclose(out);
 return 0;
}


int sc(char *pp)
{
	FILE *in,*out;
	int ch,sim=0;
	clrscr();

	if ((in = fopen(pp, "rt")) == NULL)
	{
	 printf("Cannot open file.\n");
	 return 1;
	}

	if ((out = fopen("res.txt", "wt")) == NULL)
	{
	 printf("Cannot open file.\n");
	 return 1;
	}

	while (!feof(in))
	{
	 ch=getc(in);
	 if (ch=='!'||ch=='?'||ch=='.')
	 sim++;
	}

	fprintf(out,"Broj izrecheniq: %d\n",sim);
	printf("Broj izrecheniq: %d\n",sim);
	/*printf("Done! -> res.txt");*/
	fclose(in);
	fclose(out);
	return 0;
}

int vf()
{
	FILE *in;
	char ch;
	int i;
	clrscr();

	if ((in = fopen("res.txt", "rt")) == NULL)
	{
	 printf("Cannot open file.\n");
	 return 1;
	}

	while(!feof(in))
	{
	 ch=getc(in);
	 putchar(ch);
	}

	return 0;
}

int wcnt(char *pp)
{
	FILE *out,*in;
	int wcount=0,i;
	char ch,word[20];
	clrscr();

	if ((in = fopen(pp, "rt")) == NULL)
	{
	 printf("Cannot open file.\n");
	 return 1;
	}

	if ((out = fopen("res.txt", "w")) == NULL)
	{
	 printf("Cannot open file.\n");
	 return 1;
	}

	while (!feof(in))
	{
	 ch=getc(in);

	 if (ch!=' '&&ch!='.'&&ch!='!'&&ch!='?'&&ch!='\n'&&ch!=';'&&ch!=',')
	 {
	  word[i]=ch;i++;
	 }

	 else if(my_strcmp(word," "))
	 {
	  word[i]=0;
	  wcount++;
	 }

	  i=0;
	}

	printf("Broj dumi: %d",wcount);
	fprintf(out,"Broj dumi: %d",wcount);
	fclose(out);
	return 0;
}