#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>


int komentari=0;


int broiredove(char *niz)
{
  int i,br=0;
  for (i=0;i<strlen(niz);i++)
   	{
	     if(niz[i]=='\n')
	     br++;
	}
  return br;
}


int brelse(char *niz)
{int i,br=0;
if (strlen(niz)>=4)
 for (i=0;i<strlen(niz);i++)
  {
  if (niz[i]=='/'&&niz[i+1]=='*') komentari=1;
  if (niz[i]=='*'&&niz[i+1]=='/') komentari=0;
  if (niz[i]=='"'&&niz[i-1]!=niz[i+1])
	{
	do
		i++;
	while (niz[i]!='"');
	}
	if (komentari==0)
	 {
		if (niz[i]=='/'&&niz[i+1]=='/') break;
		if (niz[i]=='e'&&niz[i+1]=='l'&&niz[i+2]=='s'&&niz[i+3]=='e')
		if (isspace(niz[i-1])==1||niz[i-1]==';'||i-1<0)
		if (isspace(niz[i+4])==1||niz[i+4]==';'||niz[i+4]=='('||niz[i+4]=='\n') br++;
	 }
  }
 return br;
}


int brif(char *niz)
{int i,br=0;
if (strlen(niz)>=2)
 for (i=0;i<strlen(niz);i++)
  {
  if (niz[i]=='/'&&niz[i+1]=='*') komentari=1;
  if (niz[i]=='*'&&niz[i+1]=='/') komentari=0;
  if (niz[i]=='"'&&niz[i-1]!=niz[i+1])
	{
	do
		i++;
	while (niz[i]!='"');
	}
	if (komentari==0)
	 {
		if (niz[i]=='/'&&niz[i+1]=='/') break;
		if (niz[i]=='i'&&niz[i+1]=='f')
		if (isspace(niz[i-1])==1||niz[i-1]==';'||i-1<0)
		if (isspace(niz[i+2])==1||niz[i+2]=='('||niz[i+2]=='\n') br++;
	 }
  }
 return br;
}


void temp(void)
{
  FILE *ftemp;
  int b=0;
  char text[81];
  ftemp = fopen("z","w+t");
  do{
	fgets(text,81,stdin);
	fputs(text,ftemp);
	if (text[0]=='\n'){
		fputs(text,ftemp);
		b++;}
     }
  while (b!=2);
  fclose(ftemp);
}



void sum(char *input,char *output)
{
  FILE *in;
  FILE *out;
  char text[255];
  int br=0,br2=0,br3=0;
  in = fopen(input,"rt");
  if (in==NULL)
	{
	printf("\nCan't open file '%s' to read!",input);
	getch();
	exit (0);
	}
  if (*output!='n')
  {out = fopen(output,"at");
  if (out==NULL)
	{
	printf("\nCan't open file '%s' to write result!",output);
	getch();
	exit (0);}
	
  }
  do
	 {
		fgets(text,254,in);
		br+=brif(text);
		br2+=brelse(text);
		br3+=broiredove(text);
	 }
  while (feof(in)==NULL);
  fclose(in);
  itoa(br-br2,text,10);
	if (*output!='n')
	   {
	   fputs("\nCount total of operators 'if' is:",out);
	   fputs(text,out);
	   }
        if (*output=='n')
	   {
	   fputs("\nCount total of operators 'if' is:",stdout);
	   puts(text);
	   }
  itoa(br2,text,10);
	if (*output!='n')
	  {
	  fputs("\nCount total of operators 'if/else' is:",out);
	  fputs(text,out);
	  }
	if (*output=='n')
	  {
	  fputs("\nCount total of operators 'if/else' is:",stdout);
	  puts(text);
	  }
  if(*input=='z')
	  {
	itoa(br3-5,text,10);
	  if (*output!='n')
	      {
	      fputs("\nCount total of rows is:",out);
	      fputs(text,out);
	      }
	  if (*output=='n')
            {
              fputs("\nCount total of rows is:",stdout);
	      puts(text);
	    }
  }
  if(*input!='z')
  {
	itoa(br3+1,text,10);
	  if (*output!='n')
	    {
	      fputs("\nCount total of rows is:",out);
	      fputs(text,out);
	    }
	  if (*output=='n')
	    {
	      fputs("\nCount total of rows is:",stdout);
              puts(text);
	    }
  }
  fclose(out);
}


void main (void)
{
	int x;
	char infile[13];
	char outfile[13];
	clrscr();
	printf ("\n---------------------------------------------------------------\n"
		"			      TU-Sofia             \n "
		"		           KURSOVA RABOTA          \n "
		"                                                  \n "
		"			Creatred by Teodor Peev     "
		"\n---------------------------------------------------------------\n"
		"	\n"
		"	\n"
		"	\n"
		"	 	       Pressany key to begin...			");
	getch();
	clrscr();
	printf ("\n-------------------------------------------------------------\n"
		"-- With this prgram you can find the count of rows and count --\n"
		"-- of operators 'IF' and 'IF - ELSE' in files for Borland ‘  --\n"
		"---------------------------------------------------------------\n"
		"Pleas select:\n"
		"1. For reading file and writing the result in new file\n \n"
		"2. For reading file and display the result on the screen\n \n"
		"3. For reading file from keyboard and writing the reult in file \n"
		"   created by user\n \n"
		"4. For reading file from keyboard and display result on the screen\n \n"

		"Select:");
	scanf ("%d", &x);
	switch (x)
	{
		case 1 :
		  {
			printf("\nOpen file:");
			scanf("%s",infile);
			printf("\nSave result as:");
			scanf("%s",outfile);
			sum(infile,outfile);
			printf("<<< Goodbye >>>");
			getch();
			break;
		  }

		case 2 :
		  {
			printf("\nOpen file:");
			scanf("%s",infile);
			sum(infile,"n");
			printf("<<< Goodbye >>>");
			getch();
			break;
		  }

		case 3 :
		  {
			printf("<<< Please eneter the C program code >>>\n");
	 		temp();
			printf("<<< End of the code >>>");
			printf("\nSave result as:");
			scanf("%s",outfile);
			sum("z",outfile);
			printf("<<< Goodbye >>>");
			getch();
			break;
		  }

		case 4 :
		  {
			printf("<<< Please eneter the C program code >>>\n");
			temp();
			printf("<<< End of the code >>>");
			sum("z","n");
			printf("<<< Goodbye >>>");
			getch();
			break;
		  }

		default: 
		  {
		printf("\nYour select is wrong. Goodbye ");
		getch();
		  }
	}


}