*** empty log message ***
[m17n/m17n-test.git] / imsim.c
diff --git a/imsim.c b/imsim.c
new file mode 100644 (file)
index 0000000..43d967a
--- /dev/null
+++ b/imsim.c
@@ -0,0 +1,84 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <m17n.h>
+
+int
+main (int argc, char **argv)
+{
+  MSymbol utf_8;
+  MInputMethod *im;
+  MInputContext *ic;
+  char buf[256];
+
+  if (argc < 3)
+    {
+      fprintf (stderr, "Usage: imsim LANG NAME\n");
+      exit (1);
+    }
+
+  M17N_INIT ();
+  utf_8 = msymbol ("utf-8");
+  im = minput_open_im (msymbol (argv[1]), msymbol (argv[2]), NULL);
+  if (! im)
+    {
+      fprintf (stderr, "Can't open input metdhod: (%s %s)\n",
+              argv[1], argv[2]);
+      exit (1);
+    }
+  ic = minput_create_ic (im, NULL);
+  if (! ic)
+    {
+      fprintf (stderr, "Can't create input context: (%s %s)\n",
+              argv[1], argv[2]);
+      exit (1);
+    }
+
+
+  while (fgets (buf, 256, stdin))
+    {
+      int len = strlen (buf);
+      char convbuf[1024];
+      int keylen, i;
+      char keyname[2];
+      MText *mt = mtext ();
+
+      if (buf[len - 1] == '\n')
+       buf[--len] = '\0';
+      for (keylen = 0; keylen < len; keylen++)
+       if (buf[keylen] == '\t')
+         break;
+      if (keylen == len)
+       {
+         fprintf (stderr, "Invalid input line: %s\n", buf);
+         exit (1);
+       }
+      keyname[1] = '\0';
+      for (i = 0; i < keylen; i++)
+       {
+         MSymbol key;
+
+         keyname[0] = buf[i];
+         key = msymbol (keyname);
+         if (minput_filter (ic, key, NULL))
+           continue;
+         if (minput_lookup (ic, key, NULL, mt) < 0)
+           mtext_cat_char (mt, buf[i]);
+       }
+      minput_filter (ic, Mnil, NULL);
+      minput_lookup (ic, Mnil, NULL, mt);
+      len = mconv_encode_buffer (utf_8, mt, (unsigned char *) convbuf, 1024);
+      convbuf[len] = '\0';
+      if (strcmp (buf + keylen + 1, convbuf))
+       {
+         buf[keylen] = '\0';
+         printf ("%s => %s (!= %s)\n", buf, convbuf, buf + keylen + 1);
+       }
+      m17n_object_unref (mt);
+    }
+
+  minput_destroy_ic (ic);
+  minput_close_im (im);
+  M17N_FINI ();
+  exit (0);
+}