update.
[chise/libchise.git] / sample.c
1 #include <chise.h>
2
3 char db_dir[] = "/usr/local/lib/chise/char-db";
4
5 int
6 test_map_func (CHISE_Feature_Table *db,
7                CHISE_Char_ID cid, CHISE_Value *valdatum)
8 {
9   printf ("#x%04X\t(%c)\t%s\n",
10           cid, cid, chise_value_to_c_string (valdatum));
11   return 0;
12 }
13
14 int
15 main (int argc, char* argv[])
16 {
17   CHISE_DS *ds;
18   CHISE_Decoding_Table *dt_daikanwa;
19   CHISE_Feature_Table *ft_ideographic_structure;
20   CHISE_Feature_Table *ft_ascii;
21   int modemask;
22   int accessmask = 0;
23   DBTYPE real_subtype;
24   int status;
25   CHISE_Char_ID char_id;
26   CHISE_Value value;
27   
28   /* open a data-source */
29   ds = chise_open_data_source (CHISE_DS_Berkeley_DB, db_dir);
30   if (ds == NULL)
31     {
32       return -1;
33     }
34
35
36   /*
37    * get a character corresponding with Daikanwa number 364
38    */
39
40   modemask = 0755;              /* rwxr-xr-x */
41   real_subtype = DB_HASH;
42   accessmask = DB_RDONLY;
43
44   /* setup a decoding-table */
45   dt_daikanwa
46     = chise_ds_open_decoding_table (ds, "=daikanwa",
47                                     real_subtype, accessmask, modemask);
48   if (dt_daikanwa == NULL)
49     {
50       chise_ds_close (ds);
51       return -1;
52     }
53   
54   /* get a character from the decoding-table */
55   char_id = chise_dt_get_char (dt_daikanwa, 364);
56
57   /* close the decoding-table */
58   chise_dt_close (dt_daikanwa);
59
60
61   /*
62    * get a ideographic-structure feature of the character
63    */
64   
65   /* setup a feature-table */
66   ft_ideographic_structure
67     = chise_ds_open_feature_table (ds, "ideographic-structure",
68                                    real_subtype, accessmask, modemask);
69   if (ft_ideographic_structure == NULL)
70     {
71       chise_ds_close (ds);
72       return -1;
73     }
74   
75   /* setup a feature-table */
76   ft_ascii = chise_ds_open_feature_table (ds, "ascii",
77                                           real_subtype, accessmask, modemask);
78   if (ft_ascii == NULL)
79     {
80       chise_ds_close (ds);
81       return -1;
82     }
83   
84   /* get a feature-value of the character */
85   status = chise_ft_get_value (ft_ideographic_structure, char_id, &value);
86
87   if (!status)
88     printf ("#x%X => %s\n", char_id, chise_value_to_c_string(&value));
89   else
90     printf ("#x%X (%d)\n", char_id, status);
91
92   chise_ft_iterate (ft_ascii, &test_map_func);
93   chise_ft_iterate (ft_ideographic_structure, &test_map_func);
94
95   /* close the feature-table */
96   chise_ft_close (ft_ideographic_structure);
97   chise_ft_close (ft_ascii);
98
99   /* close the data-source */
100   chise_ds_close (ds);
101 }