Merge b1-r0_2_0-pre10.
[chise/libchise.git] / sample.c
index b43b1e9..aba6940 100644 (file)
--- a/sample.c
+++ b/sample.c
-#include "chise.h"
+#include <string.h>
+#include <alloca.h>
+#include <chise.h>
 
-#define xzero(lvalue) ((void) memset (&(lvalue), '\0', sizeof (lvalue)))
 
-char db_dir[] = "/usr/local/lib/xemacs-21.4.10/i586-pc-linux/char-db";
+static int
+test_name_map_func (CHISE_DS *ds, unsigned char *name)
+{
+  printf ("feature : %s\n", name);
+  return 0;
+}
+
+static int
+test_map_func (CHISE_Char_ID cid, CHISE_Feature_Table *db,
+              CHISE_Value *valdatum)
+{
+  unsigned char buf[256];
+
+  printf ("(flags = %x)\n", valdatum->flags);
+  if (chise_value_size (valdatum) < 256)
+    {
+      strncpy (buf, chise_value_data (valdatum),
+              chise_value_size (valdatum));
+      buf[chise_value_size (valdatum)] = '\0';
+      printf ("#x%04X\t(%c)\t%s\n", cid, cid, buf);
+    }
+  else
+    printf ("#x%04X\t(%c)\t%s\n",
+           cid, cid, chise_value_to_c_string (valdatum));
+  return 0;
+}
 
 int
 main (int argc, char* argv[])
 {
-  CHISE_DS ds;
-  CHISE_Decoding_Table *dt;
-  CHISE_Feature_Table *ft;
-  int modemask;
-  int accessmask = 0;
-  DBTYPE real_subtype;
-  int status;
+  CHISE_DS *ds;
+#if 0
+  CHISE_CCS ccs_daikanwa;
+#endif
+  CHISE_Feature ft_ideographic_structure;
+  CHISE_Feature ft_ascii;
+  int modemask = 0755; /* rwxr-xr-x */
+  DBTYPE real_subtype = DB_HASH;
   CHISE_Char_ID char_id;
-  CHISE_Value value;
-  
-  status = chise_open_data_source (&ds, CHISE_DS_Berkeley_DB, db_dir);
-  if (status)
+  unsigned char buf[1024];
+  unsigned char *db_dir;
+
+  printf("chise_db_dir = %s\n", chise_db_dir);
+
+  db_dir = (unsigned char*)alloca (strlen (chise_db_dir) + 4);
+  if (db_dir == NULL)
     {
-      chise_close_data_source (&ds);
+      printf ("Can't open data source\n");
       return -1;
     }
+  strcpy (db_dir, chise_db_dir);
+  strcat (db_dir, "db/");
+  printf("db_dir = '%s'\n", db_dir);
 
-  modemask = 0755;             /* rwxr-xr-x */
-
-  real_subtype = DB_HASH;
-  accessmask = DB_RDONLY;
-
-  status = chise_open_decoding_table (&dt, &ds,
-                                     "ideograph-daikanwa",
-                                     real_subtype, accessmask, modemask);
-  if (status)
+  /* open a data-source */
+  ds = CHISE_DS_open (CHISE_DS_Berkeley_DB, db_dir,
+                     real_subtype, modemask);
+  if (ds == NULL)
     {
-      chise_close_decoding_table (dt);
-      chise_close_data_source (&ds);
+      printf ("Can't open data source\n");
       return -1;
     }
 
-  char_id = chise_dt_get_char (dt, 20);
-  chise_close_decoding_table (dt);
-      
-  status = chise_open_feature_table (&ft, &ds,
-                                    "ideographic-structure",
-                                    real_subtype, accessmask, modemask);
-  if (status)
+
+  /*
+   * get a character corresponding with Daikanwa number 364
+   */
+
+#if 1
+  char_id = chise_ds_decode_char (ds, "=daikanwa", 364);
+#else
+  /* setup a decoding-table */
+  ccs_daikanwa
+    = chise_ds_get_ccs (ds, "=daikanwa");
+  if (ccs_daikanwa == NULL)
     {
-      chise_close_feature_table (ft);
-      chise_close_data_source (&ds);
+      printf ("Can't open CCS =daikanwa\n");
+      CHISE_DS_close (ds);
       return -1;
     }
+  
+  /* get a character from the decoding-table */
+  char_id = chise_ccs_decode (ccs_daikanwa, 364);
+#endif
 
-  status = chise_ft_get_value (ft, char_id, &value);
-  if (!status)
-    printf ("#x%X => %s\n", char_id, chise_value_to_c_string(&value));
+  /* get a feature-value of the character */
+  ft_ideographic_structure
+    = chise_ds_get_feature (ds, "ideographic-structure");
+  if (chise_char_gets_feature_value
+      (char_id, ft_ideographic_structure, buf, sizeof (buf)) != NULL)
+    printf ("#x%X => %s\n", char_id, buf);
   else
-    printf ("#x%X (%d)\n", char_id, status);
+    printf ("#x%X\n", char_id);
+
+  chise_ds_foreach_char_feature_name (ds, &test_name_map_func);
+
+  ft_ascii = chise_ds_get_feature (ds, "ascii");
+  chise_feature_foreach_char_with_value (ft_ascii, &test_map_func);
+  chise_feature_foreach_char_with_value
+    (ft_ideographic_structure, &test_map_func);
+
+  /* close the data-source */
+  CHISE_DS_close (ds);
 
-  chise_close_feature_table (ft);
-  chise_close_data_source (&ds);
+  return 0;
 }