Subject: gb2pinyin filter

From: [email-masked] (Tommi V Kaikkonen)
Date: 1 Nov 92 15:58:59 GMT

Ni hao! I have made one program which I would like to share with you all.
It is called gb2pinyin. This can be useful for producing for example
chinese GB (or hz2gb | gb2pinyin) text reading speech synthesizer
or learning how to pronounce some certain character.

You will need the TONEPY.tit input method dictionary from cxterm's
distribution package. If you don't have you, mail to me and I send it
to you.

A word of warning: This is a very alfa test release, I completly am
very beginner with chinese language and my program might be as well
worthless, but give it a try and send me comments!

Tommi Kaikkonen, [email-masked]

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

#define DICT "/usr/sbin/lib/cxterm/dicts/TONEPY.tit"

main()
{
  register int eka=0, toka=0, i=0;
  register char hz[4], **pipo=0;
  char **taulu=0, rivi[82];
  int rpit=0, tila=0, rraja=0;
  FILE *piffi=0;

  if ((piffi = fopen (DICT, "r")) == 0) {
    printf (stderr, "Error: unable to open pinyin dictionnary %s\n",
	    DICT);
    exit (-1);
  }
  if ((taulu = (char **)malloc (4096*sizeof(char *))) == 0) {
    printf (stderr, "Error: out of memory when allocating taulu\n");
    exit (-2);
  }
  pipo = taulu;
  while (1) {
    if (feof(piffi) || !fgets (rivi, 80, piffi)) {
      *pipo = 0;
      pipo[1] = 0;
      pipo[2] = 0;
      fclose (piffi);
      break;
    }
    rpit = strlen (rivi);
    rivi[rpit-1] = '\0';
    if (tila == 0) {
      if (!strncmp ("BEGINDICTIONARY", rivi, 9)) tila=1;
    } else {
      if (rivi[0] == '#') continue;
      if ((*pipo = (char *)malloc (rpit+8)) == 0) {
	printf (stderr, "Error: out of memory when allocating *pipo\n");
	exit (-3);
      }
      strcpy (*pipo, rivi);
      pipo++;
      if (++rraja >= 4096) {
	printf (stderr, "Error: out of memory when allocating *pipo...\n");
	exit (-4);
      }
    }
  }
  hz[2] = '\0';
  while (!feof (stdin)) {
    eka = getchar();
    if (eka < 0) break;
    if (eka & 128) {
      toka = getchar();
      if (toka < 0) break;
      hz[0] = (char)eka;
      hz[1] = (char)toka;
      for (pipo=taulu; pipo && *pipo; pipo++) {
	tila = 0;
	for (i=0; i<80 && (*pipo)[i] != '\t' && (*pipo)[i] != '\0'; i++);
	for (i++; i<80 && (*pipo)[i] && (*pipo)[i+1]; i+=2) {
	  if (hz[0] != (*pipo)[i] || hz[1] != (*pipo)[i+1]) continue;
	  else {tila=1; break;}
	}
	if (tila) {
	  for (i=0; i<80 && (*pipo)[i] != '\t'; i++) printf("%c", (*pipo)[i]);
	  printf (" ");
	  break;
	}
      }
    } else {
      printf ("%c", eka);
    }
  }
}