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