(chise_ds_foreach_char_feature_name): New prototype.
[chise/libchise.git] / sample.c
1 #include <alloca.h>
2 #include <chise.h>
3
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 ft_ideographic_structure;
31   CHISE_Feature ft_ascii;
32   int modemask = 0755; /* rwxr-xr-x */
33   DBTYPE real_subtype = DB_HASH;
34   int status;
35   CHISE_Char_ID char_id;
36   CHISE_Value value;
37   unsigned char buf[1024];
38   unsigned char *db_dir;
39
40   printf("chise_db_dir = %s\n", chise_db_dir);
41
42   db_dir = (unsigned char*)alloca (strlen (chise_db_dir) + 4);
43   if (db_dir == NULL)
44     {
45       printf ("Can't open data source\n");
46       return -1;
47     }
48   strcpy (db_dir, chise_db_dir);
49   strcat (db_dir, "db/");
50   printf("db_dir = '%s'\n", db_dir);
51
52   /* open a data-source */
53   ds = CHISE_DS_open (CHISE_DS_Berkeley_DB, db_dir,
54                       real_subtype, modemask);
55   if (ds == NULL)
56     {
57       printf ("Can't open data source\n");
58       return -1;
59     }
60
61
62   /*
63    * get a character corresponding with Daikanwa number 364
64    */
65
66 #if 1
67   char_id = chise_ds_decode_char (ds, "=daikanwa", 364);
68 #else
69   /* setup a decoding-table */
70   ccs_daikanwa
71     = chise_ds_get_ccs (ds, "=daikanwa");
72   if (ccs_daikanwa == NULL)
73     {
74       printf ("Can't open CCS =daikanwa\n");
75       CHISE_DS_close (ds);
76       return -1;
77     }
78   
79   /* get a character from the decoding-table */
80   char_id = chise_ccs_decode (ccs_daikanwa, 364);
81 #endif
82
83   /* get a feature-value of the character */
84   ft_ideographic_structure
85     = chise_ds_get_feature (ds, "ideographic-structure");
86   if (chise_char_gets_feature_value
87       (char_id, ft_ideographic_structure, buf, sizeof (buf)) != NULL)
88     printf ("#x%X => %s\n", char_id, buf);
89   else
90     printf ("#x%X\n", char_id);
91
92   ft_ascii = chise_ds_get_feature (ds, "ascii");
93   chise_char_feature_value_iterate (ft_ascii, &test_map_func);
94   chise_char_feature_value_iterate (ft_ideographic_structure, &test_map_func);
95
96   /* close the data-source */
97   CHISE_DS_close (ds);
98 }