Update.
[chise/libchise.git] / sample.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <alloca.h>
4 #include <chise.h>
5
6 CHISE_DS *ds;
7
8 static int
9 test_name_map_func (CHISE_DS *ds, unsigned char *name)
10 {
11   printf ("feature : %s\n", name);
12   return 0;
13 }
14
15 static int
16 test_map_func (CHISE_Char_ID cid, CHISE_Feature_Table *db,
17                CHISE_Value *valdatum)
18 {
19   unsigned char buf[256];
20   unsigned char name[256];
21   int ucs = -1;
22
23   printf ("#x%08X ", cid);
24
25   if ( chise_char_gets_feature_value
26        (cid, chise_ds_get_feature (ds, "=ucs"),
27         buf, sizeof (buf)) != NULL )
28     {
29       ucs = atoi (buf);
30       printf ("[U-%08X]", ucs);
31     }
32   else if ( chise_char_gets_feature_value
33             (cid, chise_ds_get_feature (ds, "=>ucs"),
34              buf, sizeof (buf)) != NULL )
35     {
36       ucs = atoi (buf);
37       printf ("(U-%08X)", ucs);
38     }
39   else
40     printf ("            ");
41
42   if ( chise_char_gets_feature_value
43        (cid, chise_ds_get_feature (ds, "name"),
44         name, sizeof (name)) != NULL )
45     printf (" %s", name);
46
47   if (chise_value_size (valdatum) < 256)
48     {
49       strncpy (buf, chise_value_data (valdatum),
50                chise_value_size (valdatum));
51       buf[chise_value_size (valdatum)] = '\0';
52       printf ("\t%s\n", buf);
53     }
54   else
55     printf ("\t%s\n",
56             chise_value_to_c_string (valdatum));
57   return 0;
58 }
59
60 int
61 main (int argc, char* argv[])
62 {
63 #if 0
64   CHISE_CCS ccs_daikanwa;
65 #endif
66   CHISE_Feature ft_ideographic_structure;
67   CHISE_Feature ft_numeric_value;
68   CHISE_Char_ID char_id;
69   unsigned char buf[1024];
70
71   printf("chise_system_db_dir = %s\n", chise_system_db_dir);
72
73   /* open a data-source */
74   ds = CHISE_DS_open (CHISE_DS_Berkeley_DB, chise_system_db_dir,
75                       0 /* DB_HASH */, 0755 /* rwxr-xr-x */);
76   if (ds == NULL)
77     {
78       printf ("Can't open data source\n");
79       return -1;
80     }
81
82   printf("data source location = %s\n", chise_ds_location(ds));
83
84
85   /*
86    * get a character corresponding with Daikanwa number 364
87    */
88
89 #if 1
90   char_id = chise_ds_decode_char (ds, "=daikanwa", 364);
91 #else
92   /* setup a decoding-table */
93   ccs_daikanwa
94     = chise_ds_get_ccs (ds, "=daikanwa");
95   if (ccs_daikanwa == NULL)
96     {
97       printf ("Can't open CCS =daikanwa\n");
98       CHISE_DS_close (ds);
99       return -1;
100     }
101   
102   /* get a character from the decoding-table */
103   char_id = chise_ccs_decode (ccs_daikanwa, 364);
104 #endif
105
106   /* get a feature-value of the character */
107   ft_ideographic_structure
108     = chise_ds_get_feature (ds, "ideographic-structure");
109   if (chise_char_gets_feature_value
110       (char_id, ft_ideographic_structure, buf, sizeof (buf)) != NULL)
111     printf ("#x%X => %s\n", char_id, buf);
112   else
113     printf ("#x%X\n", char_id);
114
115   chise_ds_foreach_char_feature_name (ds, &test_name_map_func);
116
117   ft_numeric_value = chise_ds_get_feature (ds, "numeric-value");
118   chise_feature_foreach_char_with_value (ft_numeric_value, &test_map_func);
119   chise_feature_foreach_char_with_value
120     (ft_ideographic_structure, &test_map_func);
121
122   /* close the data-source */
123   CHISE_DS_close (ds);
124
125   return 0;
126 }