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

void translate();
void options();
void loadfile();
void viewfile();
char quit();
void savefile();
void slanguage();
void dlanguage();
void filter();
char vfile(char fname[256]);
void convert(char word[256]);
void thesaurus();
char wordend(int test);
int decapitalize(int d);
int stripchars(int d);

/* global parameters variables */
char filename[256];
char sfilename[256];
char languages[256];
char languaged[256];
char ffilter[256];
char viewevents = 'y';
char schar = 'y';
char trword[256];

void main()

{
   
   /* /----------------------------------------------\ */
   /* | MAIN() doubles as the introduction procedure | */
   /* \----------------------------------------------/ */
   
   char hitkey;
   char option;
   char q;
         
   q = 'n';
   
   strcpy(filename, "English.txt");
   strcpy(sfilename, "Espanol.txt");
   strcpy(languages, "English.cat");
   strcpy(languaged, "Espanol.cat");
   strcpy(ffilter, "Ascii");

      
   printf("\nMulti-Lingual file transfer");
   printf("\n/---------\\\n");
   printf("|CAUTION!!|\n");
   printf("\\---------/\n");
   printf("This program will NOT necessarily translate foreign grammer \ncorrectly!!");
   printf("This program is also suceptable to typos in all \nlanguages!!\n");
   printf("\\__       __/\n");
   printf(" <>\\     /<>\n");
   printf("     . .\n");
   printf("  \\_______/\n");
   printf("   V     V\n");
   printf("Hit RETURN to continue.\n");
   do
   {
      hitkey=getchar();
   } while(hitkey != '\n');
   do
   {         
      printf("Main Menu\n");
      printf("Enter your choice below\n\n");
      printf(" ---> Current load file = \"%s\" <---\n", filename);
      printf(" ---> Current save file = \"%s\" <---\n\n", sfilename);
      printf("(O)ptions\n");
      printf("(T)ranslate an ASCII file\n");  
      printf("(V)iew current Load ASCII file\n");
      printf("(D)ictionary\n");
      printf("(Q)uit\n\n");
      printf("---> ");
      do
      {
         option = getchar(); 
      } while(option == '\n');
      
      if (option == 'o' || option == 'O')
      {
         options();
      };

      if (option == 'v' || option == 'V')
      {
         viewfile();
      };
   
      if (option == 't' || option == 'T')
      {
         translate();
      };

      if (option == 'd' || option == 'D')
      {
         thesaurus();
      };
      
      if (option == 'Q' || option == 'q')
      {
         q=quit('h');
      };

      option=' ';

   } while (q != 'N');
   printf("If you think the multiple idiot requesters followed by the\n");
   printf("especially obnoxious one at the end are stupid, send $1 million\n");
   printf("to me and you will recieve the registered version with only one\n");
   printf("idiot requester.\n");
   printf("\\__       __/\n");
   printf(" <>\\     /<>\n");
   printf("     . .\n");
   printf("  \\_______/\n");
   printf("   V     V\nHave a nice day!\n");   
}

void translate()
{
   int c;   
   char word[256];
   int pos = 0;
   int poss = 0;
   FILE*file_pointer;
   FILE*savefile; 
   file_pointer=fopen(filename,"r");
   savefile=fopen(sfilename,"w");  
   printf("\n");
   while((c=fgetc(file_pointer))!=EOF&&pos<255)
   {
      if(schar=='n')
      {
         word[pos]=stripchars(decapitalize(c));
      }
      else
      {
         word[pos]=decapitalize(c);
      };
      word[pos+1]=0;
      

      if(wordend(c)=='y')
      {
         word[pos]=0;
         convert(word);
         if(viewevents=='y')
         {
            if(strcmp(trword, "-")!=0)
            {
               printf("%s", trword);
            }
            else
            {
               printf("%s", word);
            };
         };
         
         if(strcmp(trword, "-")!=0)
         {
            while(trword[poss]!=0)
            {
               fputc(trword[poss], savefile);
               poss=poss+1;
            };
            poss=0;
            pos=-1;
         }
         else
         {
            while(word[poss]!=0)
            {
               fputc(word[poss], savefile);
               poss=poss+1;
            };
            poss=0;
            pos=-1;
         };
      };
      if(stripchars(decapitalize(c))<97||stripchars(decapitalize(c))>122)
      {
         fputc(c, savefile);
         if(viewevents=='y')
         {
            fputc(c, stdout);
         };
      };

      pos=pos+1;
   };
   fclose(savefile);
   fclose(file_pointer);
   printf("\n\n%s to %s translation complete\n", languages, languaged);
}


void options()
{
   char o;
         
   do
   {
   
      printf("Options\n\n");
      printf("1) change output file          (%s)\n", sfilename);
      printf("2) change input file           (%s)\n", filename);
      printf("3) change source language      (%s)\n", languages);
      printf("4) change destination language (%s)\n", languaged);
      printf("5) change file type            (%s)\n", ffilter);
      printf("6) view events                 (%c)\n", viewevents);
      printf("7) special characters          (%c)\n", schar);
      printf("8) main menu\n\n---> ");
      do
      {   
         o = getchar();
      } while(o == '\n');
      if (o == '1')
      {
         savefile();
      };
   
      if (o == '2')
      {
         loadfile();
      };
   
      if (o == '3')
      {
         slanguage();
      };
   
      if (o == '4')
      {
         dlanguage();
      };
   
      if (o == '5')
      {
         filter();
      };
      
      if (o == '6'&&viewevents=='y')
      {
         viewevents='n';
      }
      else
      {
         if (o == '6'&&viewevents=='n')
         {
            viewevents='y';
         };
      };
      
      if (o == '7'&&schar=='y')
      {
         schar='n';
      }
      else
      {
         if (o == '7'&&schar=='n')
         {
            schar='y';
         };
      };
        
   } while (o != '8');
}           
void viewfile()
{
   char ignoreme;
   int c;
   int ver = 0;
   
   FILE *file_pointer_p;
   file_pointer_p=fopen(filename,"r");
   ignoreme=getchar();
   while((c=fgetc(file_pointer_p))!=EOF) 
   {
      if(schar=='n')
      {
         fputc(stripchars(decapitalize(c)), stdout);
      }
      else
      {
         fputc(decapitalize(c), stdout);
      };
      if(c=='\n')
      ver=ver+1;
      if(ver==20)
      {
         ver=0;
         printf("\n/----------------------\\\n|Hit return to continue|\n\\----------------------/\n");
         ignoreme=' ';
         while(ignoreme!='\n')
         {
            ignoreme = getchar();
         };
      };
   }
   fclose(file_pointer_p);
   printf("\n/----------------------\\\n|Hit return to continue|\n\\----------------------/\n");
   ignoreme = getchar(); 
   ignoreme = getchar();
   
}

char quit()
{                                                    
   char g;
   printf("Are you sure you want to quit?(Y/N)\n\n---> ");
   do
   {
      g = getchar();
   } while (g == '\n');
   
   if(g == 'n' || g =='N')
   {
      g = 'b';
   };
   
   if(g == 'Y' || g == 'y')
   {
      printf("Are you POSITIVE you want to quit?(Y/N)\n\n---> ");
      do
      {
         g = getchar();
      } while (g == '\n');
   };
   
   if(g == 'n' || g == 'N')
   {
      g = 'b';
   };
   
   if(g == 'Y' || g == 'y')
   {
      printf("Are you ABSOLUTELY POSITIVELY SURE you don't want to quit?(Y/N)\n\n---> ");
      do
      {
         g = getchar();
      } while (g == '\n');
   };
         
   if (g == 'n')
   {
      g='N';
   }
   return(g);
}

void loadfile()
/* /-----------------------------------------------\
   |load pathname modification procedure (filename)|
   \-----------------------------------------------/ */
{
   char c;
   do
   {
      printf("Enter the the full path of the new SOURCE file.\n\n---> ");
      scanf("%s", filename);
      c = vfile(filename);
      if (c=='y')
      {
         printf("File \"%s\" exists.\n", filename);
      }
      else
      {
         printf("Error finding file \"%s\". Please Re-Enter.\n", filename);
      };
   } while (c == 'n');
}

void savefile()
/* /------------------------------------------------\
   |save pathname modification procedure (sfilename)|
   \------------------------------------------------/ */
{
   char c;
   do
   {
      printf("Enter the the full path of the new DESTINATION file.\n\n---> ");
      scanf("%s", sfilename);
      if(vfile(sfilename)=='y')
      {
         printf("File \"%s\" already exists. Overwrite?(Y/N)\n", sfilename);
         do
         {
            c = getchar();
         } while(c == '\n');
      }
      else
      {
         c = 'Y';
      };
   } while(c=='n'||c=='N');
}
   
void slanguage()
{
   /* /-------------------------------------------------\
      |source catalog modification procedure (languages)|
      \-------------------------------------------------/ */
   char c;
   do
   {
   		printf("Enter the the full path of the SOURCE language.\n\n---> ");
   		scanf("%s", languages);
      c = vfile(languages);
      if (c=='y')
      {
         printf("Language catalog \"%s\" exists.\n", languages);
      }
      else
      {
         strcat(languages,".cat");
         if (vfile(languages) == 'n')
         {
         	printf("Error finding language catalog \"%s\b\b\b\b\". Please Re-Enter.\n", languages);
         	c = 'n'; 
         }
         else
         {
         	c = 'y';
         };
      };
   } while (c == 'n');

}
void dlanguage()
{
   /* /------------------------------------------------------\
      |destination catalog modification procedure (languaged)|
      \------------------------------------------------------/ */

   char c;
   do
   {
      printf("Enter the the full path of the DESTINATION language.\n\n---> ");
      scanf("%s", languaged);
      c = vfile(languaged);
      if (c=='y')
      {
         printf("Language catalog \"%s\" exists.\n", languaged);
      }
      else
      {
         strcat(languaged,".cat");
         if (vfile(languaged) == 'n')
         {
            printf("Error finding language catalog \"%s\". Please Re-Enter.\n",languaged);
            c = 'n';
         }
         else
         {
            c = 'y'; 
            printf("Language catalog \"%s\" exists.\n", languaged);
         };
      };
   } while (c == 'n');
}
void filter()
{
   /* /---------------------------------------------\
      |file filter modification procedure (ffilter) |
      \---------------------------------------------/ */
   printf("Enter the the full path of the filter catalog.\n\n---> ");
   scanf("%s", ffilter);
}     
char vfile(char fname[256])
{
   /* Stands for Verify File. Returns 'y' if file exists, 'n' if not */
   char c;
   FILE *file_pointer_p;
   if(file_pointer_p=fopen(fname,"r"))
   {
      c = 'y';
   }
   else
   {
      c = 'n';
   };
   return(c);
}

void convert(char word[256])
{
   /* Convert word and initiate writefile */
   char word2[256];
   char endroutine = 'n';
   char foundword = 'n';
   int pos = 0;
   int c;
   int d = 0;
   int jump_input = 0;
   int jump_inputii = 0;
   int retcount = 0;
  
   FILE *input_file;
	FILE *language_transfer;
   input_file=fopen(languages,"r");          
   strcpy(word2, " ");
   
   /* get info from the source language catalog (default = "English.cat")
      and put it into the variable word2 
      ************************************************************ */
   while((c=fgetc(input_file))!=EOF&&pos<255)
   {
      if(schar=='n')
      {
         c=stripchars(decapitalize(c));
      }
      else
      {
         c=decapitalize(c);
      };
      
      word2[pos]=c;
      
      /* ignore "`" characters (routine on bottom)*/
      if(c!='`')
      {
         pos=pos+1;
      };
      
      /* terminate string.. temporary unless end of word found */
      word2[pos]=0;
      
      /* detect end-of-word. */
      if(wordend(c)=='y')
      {
         
         /* erase last character to get rid of word terminator at end of
            string. (space, number, etc...) */
         word2[pos-1]=0;

         /* detect if a matching word is found */
         if(strcmp(word,word2)==0)
         {
            foundword='y';
            
            /* this routine gets the number after the word, and puts it into
               jump_input. working on a way to accept the , as a continuation*/
            /* first index number */
            while((d=fgetc(input_file))!='|' && d !=',')
		      {
                 jump_input=jump_input*10;
                 jump_input=jump_input+d-48;
		      };
		      retcount=1;
		      /* second index number */
		      if(d==',')
		      {
		         while((d=fgetc(input_file))!='|')
		         {
		            printf("%c\n", d);
		            jump_inputii=jump_inputii*10;
		            jump_inputii=jump_inputii+d-48;
		         };
		         printf("jump_inputii=%d\n", d);
		      };
		    /* opens the destination language. default="Espanol.cat"*/
            language_transfer=fopen(languaged, "r");
            while((d=fgetc(language_transfer))!=EOF&&endroutine=='n')
		      {
               
               /* counts line feeds until retcount=jump_input */
               if(retcount<jump_input)
			      {
                     if (d=='\n')
			      	 {
				     	  retcount=retcount+1;
			      	 };
			      }
               else
               {  
                  /* tells procedure to end next time it hits the last } */
                  endroutine='y';
               };
		      };
		      pos=0;
            /* opens and reads word specified by jump_input into global
               variable trword. (still don't know how to pass strings) */
            while((d=fgetc(language_transfer))!=EOF&&pos<255)
		      {
			      if(schar=='n')
			      {
			         d=stripchars(decapitalize(d));
			      }
			      else
			      {
			         d=decapitalize(d);
			      };
			      trword[pos]=d;
			      pos=pos+1;
			      trword[pos]=0;
               if(wordend(d)=='y')
			      {
                      trword[pos-1]=0;
                      pos=256;
			      };
		      }
		      fclose(language_transfer);
         }
         else
         {
         	while((c=fgetc(input_file))!='\n')
         	{
         		pos=0;
         	};
         };
	  };
	};
   if(foundword=='n')
   {
      strcpy(trword, "-");
   };
   fclose(input_file);
}
char wordend(int test)
{
   /* Check to see if end of word has been reached */
   char revalue;
   if(stripchars(decapitalize(test))<96||stripchars(decapitalize(test))>122)
   {
      revalue = 'y';
   }
   else
   {
      revalue = 'n';
   };
   return(revalue);
}
int decapitalize(int d)
{
   if(d<91&&d>64)
   {
      d=d+32;
   }
   else
   {
      if(d<215&&d>191)
      {  
         d=d+32;
      };
   };
   return(d);
}

void thesaurus()
{
   char word[256];
   strcpy(word, " ");
   
   while(strcmp(word,"/quit")!=0)
   {
      printf("Enter word to translate (/quit to exit)\n\n---> ");
      scanf("%s", word);
      convert(word);
      if(word[0]!='/')
      {
         printf("\n%s word \"%s\" is \"%s\" in %s\n", languages, word, trword, languaged);
      };
   };
}                   

int stripchars(int d)
{
   if(d>223&&d<230)
   {
      d='a';
   };
   if(d>231&&d<236)
   {
      d='e';
   };
   if(d>235&&d<240)
   {
      d='i';
   };
   if(d==241)
   {
      d='n';
   };
   if(d>241&&d<247)
   {
      d='o';
   };
   if(d==248)
   {
      d='o';
   };
   if(d>248&&d<253)
   {
      d='u';
   };
   if(d==253||d==255)
   {
      d='y';
   };
   return(d);
}
