?

Log in

No account? Create an account
penrose orange

stephenw32768


/var/log/stephen

cat /var/log/stephen >/dev/eyes


albhed.c
penrose orange
stephenw32768
/*
 * @(#) $Id: albhed.c,v 1.1 2003/08/27 16:59:04 stephen Exp $
 *
 * Translates between English and Al Bhed.
 * Copyright (c) 2003 Stephen Williams.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  For more
 * details, see the GNU General Public License.
 * (http://www.fsf.org/licenses/gpl.txt)
 *
 */
#include <ctype.h>
#include <stdio.h>
#include <unistd.h>


/* Takes a lower-case English letter and returns the
   corresponding Al Bhed letter. Incomplete, as I have
   yet to determine the Al Bhed equivalents of "j", "q"
   and "z". */
char english_to_albhed(char e)
{
  char a;

  switch(e) {

    case 'a': a = 'y'; break;
    case 'b': a = 'p'; break;
    case 'c': a = 'l'; break;
    case 'd': a = 't'; break;
    case 'e': a = 'a'; break;
    case 'f': a = 'v'; break;
    case 'g': a = 'k'; break;
    case 'h': a = 'r'; break;
    case 'i': a = 'e'; break;
    case 'j': a = '?'; break;
    case 'k': a = 'g'; break;
    case 'l': a = 'm'; break;
    case 'm': a = 's'; break;
    case 'n': a = 'h'; break;
    case 'o': a = 'u'; break;
    case 'p': a = 'b'; break;
    case 'q': a = '?'; break;
    case 'r': a = 'n'; break;
    case 's': a = 'c'; break;
    case 't': a = 'd'; break;
    case 'u': a = 'i'; break;
    case 'v': a = 'j'; break;
    case 'w': a = 'f'; break;
    case 'x': a = 'q'; break;
    case 'y': a = 'o'; break;
    case 'z': a = '?'; break;
    default: a = e;
  }

  return a;
}


/* Takes a lower-case Al Bhed letter and returns the
   corresponding English letter.  Incomplete, as I have
   yet to determine the English equivalents of "x" and
   "z". */
char albhed_to_english(char a)
{
  char e;

  switch(a) {

    case 'a': e = 'e'; break;
    case 'b': e = 'p'; break;
    case 'c': e = 's'; break;
    case 'd': e = 't'; break;
    case 'e': e = 'i'; break;
    case 'f': e = 'w'; break;
    case 'g': e = 'k'; break;
    case 'h': e = 'n'; break;
    case 'i': e = 'u'; break;
    case 'j': e = 'v'; break;
    case 'k': e = 'g'; break;
    case 'l': e = 'c'; break;
    case 'm': e = 'l'; break;
    case 'n': e = 'r'; break;
    case 'o': e = 'y'; break;
    case 'p': e = 'b'; break;
    case 'q': e = 'x'; break;
    case 'r': e = 'h'; break;
    case 's': e = 'm'; break;
    case 't': e = 'd'; break;
    case 'u': e = 'o'; break;
    case 'v': e = 'f'; break;
    case 'w': e = 't'; break;
    case 'x': e = '?'; break;
    case 'y': e = 'a'; break;
    case 'z': e = '?'; break;
    default: e = a;
  }

  return e;
}


/* Entry point */
int main(int argc, char * argv[])
{
  int  doit             = 1;
  char (*convert)(char) = english_to_albhed;
  int  option;

  /* Parse arguments */
  while((option = getopt(argc, argv, "aeh?")) != -1) {

    switch(option) {

    case 'a':
      convert = albhed_to_english;
      break;

    case 'e':
      convert = english_to_albhed;
      break;

    case 'h':
    case '?':
    default:
      fprintf(stderr,
              "Filters stdin to stdout, "
              "converting between English and Al Bhed.\n\n"
              "Options:\n"
              "  -a      Translate Al Bhed into English\n"
              "  -e      Translate English into Al Bhed (default)\n"
              "  -h, -?  Display this help text\n");
      doit = 0;
      break;
    }

  }

  if(doit) {

    char c;

    /* Main loop */
    while((c = getchar()) != EOF) {

      if(isupper(c)) {
        c = toupper(convert(tolower(c)));
      } else if(islower(c)) {
        c = convert(c);
      }

      putchar(c);
    }

  }

  return 0;
}