b6b567f0cf3a8946170cb2d538bc5ba2cb90a85d
[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   status = chise_open_data_source (&ds, CHISE_DS_Berkeley_DB, db_dir);
30   if (status)
31     {
32       chise_close_data_source (&ds);
33       return -1;
34     }
35
36
37   /*
38    * get a character corresponding with Daikanwa number 364
39    */
40
41   modemask = 0755;              /* rwxr-xr-x */
42   real_subtype = DB_HASH;
43   accessmask = DB_RDONLY;
44
45   /* setup a decoding-table */
46   status = chise_open_decoding_table (&dt_daikanwa, &ds,
47                                       "=daikanwa",
48                                       real_subtype,
49                                       accessmask, modemask);
50   if (status)
51     {
52       chise_close_decoding_table (dt_daikanwa);
53       chise_close_data_source (&ds);
54       return -1;
55     }
56   
57   /* get a character from the decoding-table */
58   char_id = chise_dt_get_char (dt_daikanwa, 364);
59
60   /* close the decoding-table */
61   chise_close_decoding_table (dt_daikanwa);
62
63
64   /*
65    * get a ideographic-structure feature of the character
66    */
67   
68   /* setup a feature-table */
69   status = chise_open_feature_table (&ft_ideographic_structure, &ds,
70                                      "ideographic-structure",
71                                      real_subtype, accessmask, modemask);
72   if (status)
73     {
74       chise_close_feature_table (ft_ideographic_structure);
75       chise_close_data_source (&ds);
76       return -1;
77     }
78   
79   /* setup a feature-table */
80   status = chise_open_feature_table (&ft_ascii, &ds,
81                                      "ascii",
82                                      real_subtype, accessmask, modemask);
83   if (status)
84     {
85       chise_close_feature_table (ft_ascii);
86       chise_close_data_source (&ds);
87       return -1;
88     }
89   
90   /* get a feature-value of the character */
91   status = chise_ft_get_value (ft_ideographic_structure, char_id, &value);
92
93   if (!status)
94     printf ("#x%X => %s\n", char_id, chise_value_to_c_string(&value));
95   else
96     printf ("#x%X (%d)\n", char_id, status);
97
98   chise_ft_iterate (ft_ascii, &test_map_func);
99   chise_ft_iterate (ft_ideographic_structure, &test_map_func);
100
101   /* close the feature-table */
102   chise_close_feature_table (ft_ideographic_structure);
103
104   chise_close_feature_table (ft_ascii);
105
106
107   /* close the data-source */
108   chise_close_data_source (&ds);
109 }