(test_map_func): Modify for chise_char_feature_value_iterate.
[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_Char_ID cid, CHISE_Feature_Table *db,
7                CHISE_Value *valdatum)
8 {
9   unsigned char buf[256];
10
11   printf ("(flags = %x)\n", valdatum->flags);
12   if (chise_value_size (valdatum) < 256)
13     {
14       strncpy (buf, chise_value_data (valdatum),
15                chise_value_size (valdatum));
16       buf[chise_value_size (valdatum)] = '\0';
17       printf ("#x%04X\t(%c)\t%s\n", cid, cid, buf);
18     }
19   else
20     printf ("#x%04X\t(%c)\t%s\n",
21             cid, cid, chise_value_to_c_string (valdatum));
22   return 0;
23 }
24
25 int
26 main (int argc, char* argv[])
27 {
28   CHISE_DS *ds;
29   CHISE_CCS ccs_daikanwa;
30   CHISE_Feature_Table *ft_ideographic_structure;
31   CHISE_Feature_Table *ft_ascii;
32   int modemask;
33   int accessmask = 0;
34   DBTYPE real_subtype;
35   int status;
36   CHISE_Char_ID char_id;
37   CHISE_Value value;
38   unsigned char buf[1024];
39
40   /* open a data-source */
41   ds = chise_open_data_source (CHISE_DS_Berkeley_DB, db_dir);
42   if (ds == NULL)
43     {
44       printf ("Can't open data source\n");
45       return -1;
46     }
47
48
49   /*
50    * get a character corresponding with Daikanwa number 364
51    */
52
53   modemask = 0755;              /* rwxr-xr-x */
54   real_subtype = DB_HASH;
55   accessmask = DB_RDONLY;
56
57   /* setup a decoding-table */
58   ccs_daikanwa
59     = chise_ds_get_ccs (ds, "=daikanwa",
60                         real_subtype, accessmask, modemask);
61   if (ccs_daikanwa == NULL)
62     {
63       printf ("Can't open CCS =daikanwa\n");
64       chise_ds_close (ds);
65       return -1;
66     }
67   
68   /* get a character from the decoding-table */
69   char_id = chise_ccs_decode (ccs_daikanwa, 364);
70
71
72   /* get a feature-value of the character */
73   ft_ideographic_structure
74     = chise_ds_get_feature (ds, "ideographic-structure",
75                             real_subtype, accessmask, modemask);
76   if (chise_char_gets_feature_value
77       (char_id, ft_ideographic_structure, buf, sizeof (buf)) != NULL)
78     printf ("#x%X => %s\n", char_id, buf);
79   else
80     printf ("#x%X\n", char_id);
81
82   ft_ascii = chise_ds_get_feature (ds, "ascii",
83                                    real_subtype, accessmask, modemask);
84   chise_char_feature_value_iterate (ft_ascii, &test_map_func);
85   chise_char_feature_value_iterate (ft_ideographic_structure, &test_map_func);
86
87   /* close the data-source */
88   chise_ds_close (ds);
89 }