#include <stdio.h>
#include <stdlib.h>
#include "romaca.c"


typedef struct mapping  {
  const char * nick;
  const char * appendix_n;
} mapping;

/*
 * Modification by Chris: use knowledge of possible vowels to
 * analyse segment strings. Following is a NULL terminated list
 * of Roman representations of vowels. It matters that the ones
 * that have two letters in the orthography come first, since
 * we use the order of the list to resolve conflicts. 
 *
 * 
 */




static const mapping osu_korean_vowels[] = {
  {"eu","eu"},
  {"eo","eo"},
  {"ae","e"},  /* not in Kyuchul's dialect */
  {"oe","oe"}, /* not in the phone set, but is in appendix N */
  {"u","u"},
  {"e","e"},
  {"i","i"},
  {"o","o"},
  {"a","a"},
  {NULL,NULL}};


static const mapping osu_korean_consonants[] = {
  {"ph","p"},
  {"pp","bb"},  
  {"th","t"},
  {"tt","dd"},
  {"ch","c"},
  {"cc","jj"}, 
  {"kh","k"},
  {"kk","gg"},
  {"ng","ng"},
  {"ss","ss"},
  {"p","b"},
  {"t","d"},
  {"c","j"},
  {"k","g"},					       
  {"m","m"},
  {"n","n"},
  {"s","s"},
  {"w","w"},
  {"l","l"},
  {"r","l"},
  {"y","y"},
  {"h","h"},
  {NULL,NULL}
};






/*
 * find_vowel - return the first substring starting with a vowel 
 */ 

static const mapping * find_vowel(const char * target) {

  const mapping *p;


  for(p = osu_korean_vowels; p->nick != NULL; p++) {
      if(0 == strncmp(target,p->nick,strlen(p->nick)))
	return p;
  }


  return NULL;

}


static const mapping * find_consonant(const char * target) {

  const mapping *p;


  for(p = osu_korean_consonants; p->nick != NULL; p++) {
      if(0 == strncmp(target,p->nick,strlen(p->nick)))
	return p;
  }


  return NULL;

}


/*
 * single hangul need translation too
 */

static const mapping * find_any(const char * target) {
  const mapping * p;
  p = find_vowel(target);
  if(p) 
    return p;
  else
    return find_consonant(target);
}









static void print_syllable(const char * syllable,int hyphenate) {

  const mapping * m;


  if(hyphenate) 
    printf("-");
 
  while(( m = find_consonant(syllable))) {
    printf("%s",m->appendix_n);
    syllable += strlen(m->nick);
  }
  
  if((m=find_vowel(syllable))) {
    printf("%s",m->appendix_n);
    syllable += strlen(m->nick);
  } else {
    printf("__%s__",syllable);
    exit(-1);
  }


  while((m = find_consonant(syllable))) {
    printf("%s",m->appendix_n);
    syllable += strlen(m->nick);
  }


}


static void print_syllable_nospace(const char * syllable) {

  const mapping * m;


  while((m = find_consonant(syllable))) {
    printf("%s",m->appendix_n);
    syllable += strlen(m->nick);
  }
  if((m=find_vowel(syllable))) {
    printf("%s",m->appendix_n);
    syllable += strlen(m->nick);
  } else {
    printf("__%s__",syllable);
    exit(-1);
  }



  while((m = find_consonant(syllable))) {
    printf("%s",m->appendix_n);
    syllable += strlen(m->nick);
  }

}
  




int main(int argc,char**argv) 
{
  int i,j;

  for(i=0; i < 25; i++) {
    for(j=0; j < 94; j++) {
      print_syllable_nospace(romaca[i][j]);
      printf("\t%c%c\n",i+176,j+176-15);
    }
  }
}

