/*
 *  gl2gr -- greeklish to greek
 *
 *  Copyright (c) 2000 Patroklos Argyroudis <argp at domain cs.tcd.ie>
 *  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
 
/*
 * gl2gr -- greeklish to greek
 * Patroklos Argyroudis <argp at domain cs.tcd.ie>
 *
 * A simple program that translates greeklish to greek.  Since there is no
 * standard way to write greeklish, and I hope that will never be one, this
 * program translates according to my own way and several other peoples'.
 * Your console driver or terminal must support greek characters or you will
 * lose.  Obviously this was coded just for fun..  If you find a use for this
 * or change it to make it more efficient, please mail me.
 *
 * $Id: gl2gr.c,v 1.3 2005/11/08 22:45:42 argp Exp $
 */

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

#define	VERSION	"1.0"

#define	SMALBUF	256
#define	BUFSIZE	1024

void	xprintf(char *msg, ...);
void	translate(FILE *in, FILE *out);

int
main(int argc, char *argv[])
{
 FILE *input, *output;
 
 if(argc == 3)		/* input from file, output to file */
 {
  if((input = fopen(argv[1], "r")) == NULL)
   xprintf("error opening file %s\n", argv[1]);
  if((output = fopen(argv[2], "w")) == NULL)
   xprintf("error opening file %s\n", argv[2]);
 }
 else if(argc == 2)	/* input from file, output to stdout */
 {
  output = stdout;
  if((input = fopen(argv[1], "r")) == NULL)
   xprintf("error opening file %s\n", argv[1]);
 }
 else			/* input from stdin, output to stdout */
 {
  input = stdin;
  output = stdout;
 }
 
 translate(input, output);
 
 if((fclose(input)) == EOF)
  xprintf("fclose error\n");
 if((fclose(output)) == EOF)
  xprintf("fclose error\n");
  
 return(EXIT_SUCCESS);
}

void
xprintf(char *msg, ...)
{
 char buffer[SMALBUF];
 va_list args;
   
 va_start(args, msg);
 vsnprintf(buffer, SMALBUF, msg, args);
      
 fprintf(stderr, "%s", buffer);
 va_end(args);
 exit(EXIT_FAILURE);
}
         
void
translate(FILE *in, FILE *out)
{
 int i, n, buflen;
 u_char buffer[BUFSIZE];
 
 while((fgets(buffer, BUFSIZE, in)) != NULL)
 {
  buflen = strlen(buffer);
  for(i = 0; i < buflen; i++)
  {
   /* first deal with upper case letters */
   if((buffer[i] == 'P') && (buffer[i + 1] == 'S' || buffer[i + 1] == 's'))
   {
    buffer[i] = 'Ø';
    n = (i + 1);
    while(buffer[n] != ' ')
    {
     buffer[n] = buffer[n + 1];
     n++;
    }
   }
   else if((buffer[i] == 'K') && (buffer[i + 1] == 'S' || buffer[i + 1] == 's'))
   {
    buffer[i] = 'Î';
    n = (i + 1);
    while(buffer[n] != ' ')
    {
     buffer[n] = buffer[n + 1];
     n++;
    }
   }
   else if(buffer[i] == 'D')
    buffer[i] = 'Ä';
   else if(buffer[i] == 'F')
    buffer[i] = 'Ö';
   else if(buffer[i] == 'G')
    buffer[i] = 'Ã';
   else if(buffer[i] == 'J')
    buffer[i] = 'Î';
   else if(buffer[i] == 'L')
    buffer[i] = 'Ë';
   else if(buffer[i] == 'P')
    buffer[i] = 'Ð';
   else if(buffer[i] == 'R')
    buffer[i] = 'Ñ';
   else if(buffer[i] == 'S')
    buffer[i] = 'Ó';
   else if(buffer[i] == 'U')
    buffer[i] = 'Õ';
   else if(buffer[i] == 'V')
    buffer[i] = 'Â';
   else if(buffer[i] == 'W')
    buffer[i] = 'Ù';
   else if(buffer[i] == '8')
    buffer[i] = 'È';
   /* now deal with lower case letters */
   else if((buffer[i] == 's') && (buffer[i + 1] == ' ' || buffer[i + 1] == ',' || buffer[i + 1] == '.' || buffer[i + 1] == '!' || buffer[i + 1] == '"' || buffer[i + 1] == '\n' || buffer[i + 1] == ')' || buffer[i + 1] == ']' || buffer[i + 1] == '}'))
    buffer[i] = 'ò';
   else if((buffer[i] == 'k') && (buffer[i + 1] == 's'))
   {
    buffer[i] = 'î';
    n = (i + 1);
    while(buffer[n] != ' ')
    {
     buffer[n] = buffer[n + 1];
     n++;
    }
   }
   else if((buffer[i] == 'p') && (buffer[i + 1] == 's'))
   {
    buffer[i] = 'ø';
    n = (i + 1);
    while(buffer[n] != ' ')
    {
     buffer[n] = buffer[n + 1];
     n++;
    }
   }
   else if(buffer[i] == 'a')
    buffer[i] = 'á';
   else if(buffer[i] == 'b')
    buffer[i] = 'â';
   else if(buffer[i] == 'd')
    buffer[i] = 'ä';
   else if(buffer[i] == 'e')
    buffer[i] = 'å';
   else if(buffer[i] == 'f')
    buffer[i] = 'ö';
   else if(buffer[i] == 'g')
    buffer[i] = 'ã';
   else if(buffer[i] == 'h')
    buffer[i] = 'ç';
   else if(buffer[i] == 'i')
    buffer[i] = 'é';
   else if(buffer[i] == 'j')
    buffer[i] = 'î';
   else if(buffer[i] == 'k')
    buffer[i] = 'ê';
   else if(buffer[i] == 'l')
    buffer[i] = 'ë';
   else if(buffer[i] == 'm')
    buffer[i] = 'ì';
   else if(buffer[i] == 'n')
    buffer[i] = 'í';
   else if(buffer[i] == 'o')
    buffer[i] = 'ï';
   else if(buffer[i] == 'p')
    buffer[i] = 'ð';
   else if(buffer[i] == 'r')
    buffer[i] = 'ñ';
   else if(buffer[i] == 's')
    buffer[i] = 'ó';
   else if(buffer[i] == 't')
    buffer[i] = 'ô';
   else if(buffer[i] == 'u')
    buffer[i] = 'õ';
   else if(buffer[i] == 'v')
    buffer[i] = 'â';
   else if(buffer[i] == 'w')
    buffer[i] = 'ù';
   else if(buffer[i] == 'x')
    buffer[i] = '÷';
   else if(buffer[i] == 'y')
    buffer[i] = 'õ';
   else if(buffer[i] == 'z')
    buffer[i] = 'æ';
  } /* for */
  
  fprintf(out, buffer);
 } /* while */
 
 return;
}

/* EOF */

