update.
[chise/libchise.git] / sample.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <alloca.h>
4 #include <chise.h>
5
6 CHISE_DS *ds;
7
8 static int
9 test_name_map_func (CHISE_DS *ds, unsigned char *name)
10 {
11   printf ("feature : %s\n", name);
12   return 0;
13 }
14
15 static int
16 test_map_func (CHISE_Char_ID cid, CHISE_Feature db,
17                CHISE_Value *valdatum)
18 {
19   unsigned char buf[256];
20   unsigned char name[256];
21   int ucs = -1;
22
23   printf ("#x%08X ", cid);
24
25   if ( chise_char_gets_feature_value
26        (cid, chise_ds_get_feature (ds, "=ucs"),
27         buf, sizeof (buf)) != NULL )
28     {
29       ucs = atoi (buf);
30       printf ("[U-%08X]", ucs);
31     }
32   else if ( chise_char_gets_feature_value
33             (cid, chise_ds_get_feature (ds, "=>ucs"),
34              buf, sizeof (buf)) != NULL )
35     {
36       ucs = atoi (buf);
37       printf ("(U-%08X)", ucs);
38     }
39   else
40     printf ("            ");
41
42   if ( chise_char_gets_feature_value
43        (cid, chise_ds_get_feature (ds, "name"),
44         name, sizeof (name)) != NULL )
45     printf (" %s", name);
46
47   if (chise_value_size (valdatum) < 256)
48     {
49       strncpy (buf, chise_value_data (valdatum),
50                chise_value_size (valdatum));
51       buf[chise_value_size (valdatum)] = '\0';
52       printf ("\t%s\n", buf);
53     }
54   else
55     printf ("\t%s\n",
56             chise_value_to_c_string (valdatum));
57   return 0;
58 }
59
60 static int
61 test_radical_func (CHISE_Char_ID cid, CHISE_Feature db,
62                    CHISE_Value *valdatum)
63 {
64   unsigned char buf[256];
65   unsigned char name[256];
66   int ucs = -1;
67
68   if (chise_value_size (valdatum) < 256)
69     {
70       strncpy (buf, chise_value_data (valdatum),
71                chise_value_size (valdatum));
72       buf[chise_value_size (valdatum)] = '\0';
73       printf ("%s\t", buf);
74     }
75   else
76     printf ("%s\t",
77             chise_value_to_c_string (valdatum));
78
79   if (strcmp (buf, "75") == 0) /* Tree */
80     {
81       printf ("#x%08X ", cid);
82       if ( chise_char_gets_feature_value
83            (cid, chise_ds_get_feature (ds, "=ucs"),
84             buf, sizeof (buf)) != NULL )
85         {
86           ucs = atoi (buf);
87           printf ("[U-%08X]", ucs);
88         }
89       else if ( chise_char_gets_feature_value
90                 (cid, chise_ds_get_feature (ds, "=>ucs"),
91                  buf, sizeof (buf)) != NULL )
92         {
93           ucs = atoi (buf);
94           printf ("(U-%08X)", ucs);
95         }
96       else
97         printf ("            ");
98
99       printf ("\n");
100     }
101   else
102     printf ("\n");
103   return 0;
104 }
105
106 int
107 main (int argc, char* argv[])
108 {
109 #if 0
110   CHISE_CCS ccs_daikanwa;
111 #endif
112   CHISE_Feature ft_ideographic_structure;
113   CHISE_Feature ft_numeric_value;
114   CHISE_Feature ft_radicals;
115   CHISE_Char_ID char_id;
116   unsigned char buf[1024];
117
118   printf("chise_system_db_dir = %s\n", chise_system_db_dir);
119
120   /* open a data-source */
121   ds = CHISE_DS_open (CHISE_DS_Berkeley_DB, chise_system_db_dir,
122                       0 /* DB_HASH */, 0755 /* rwxr-xr-x */);
123   if (ds == NULL)
124     {
125       printf ("Can't open data source\n");
126       return -1;
127     }
128
129   printf("data source location = %s\n", chise_ds_location(ds));
130
131
132   /*
133    * get a character corresponding with Daikanwa number 364
134    */
135
136 #if 1
137   char_id = chise_ds_decode_char (ds, "=daikanwa", 364);
138 #else
139   /* setup a decoding-table */
140   ccs_daikanwa
141     = chise_ds_get_ccs (ds, "=daikanwa");
142   if (ccs_daikanwa == NULL)
143     {
144       printf ("Can't open CCS =daikanwa\n");
145       CHISE_DS_close (ds);
146       return -1;
147     }
148   
149   /* get a character from the decoding-table */
150   char_id = chise_ccs_decode (ccs_daikanwa, 364);
151 #endif
152
153   /* get a feature-value of the character */
154   ft_ideographic_structure
155     = chise_ds_get_feature (ds, "ideographic-structure");
156   if (chise_char_gets_feature_value
157       (char_id, ft_ideographic_structure, buf, sizeof (buf)) != NULL)
158     printf ("#x%X => %s\n", char_id, buf);
159   else
160     printf ("#x%X\n", char_id);
161
162   chise_ds_foreach_char_feature_name (ds, &test_name_map_func);
163
164   ft_radicals = chise_ds_get_feature (ds, "ideographic-radical");
165   chise_feature_foreach_char_with_value (ft_radicals, &test_radical_func);
166
167   ft_numeric_value = chise_ds_get_feature (ds, "numeric-value");
168   chise_feature_foreach_char_with_value (ft_numeric_value, &test_map_func);
169   chise_feature_foreach_char_with_value
170     (ft_ideographic_structure, &test_map_func);
171
172   /* close the data-source */
173   CHISE_DS_close (ds);
174
175   return 0;
176 }