(TAR): New variable.
[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_Table *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 int
61 main (int argc, char* argv[])
62 {
63 #if 0
64   CHISE_CCS ccs_daikanwa;
65 #endif
66   CHISE_Feature ft_ideographic_structure;
67   CHISE_Feature ft_numeric_value;
68   CHISE_Char_ID char_id;
69   unsigned char buf[1024];
70
71   printf("chise_system_db_dir = %s\n", chise_system_db_dir);
72
73   /* open a data-source */
74   ds = CHISE_DS_open (CHISE_DS_Berkeley_DB, chise_system_db_dir,
75                       0 /* DB_HASH */, 0755 /* rwxr-xr-x */);
76   if (ds == NULL)
77     {
78       printf ("Can't open data source\n");
79       return -1;
80     }
81
82
83   /*
84    * get a character corresponding with Daikanwa number 364
85    */
86
87 #if 1
88   char_id = chise_ds_decode_char (ds, "=daikanwa", 364);
89 #else
90   /* setup a decoding-table */
91   ccs_daikanwa
92     = chise_ds_get_ccs (ds, "=daikanwa");
93   if (ccs_daikanwa == NULL)
94     {
95       printf ("Can't open CCS =daikanwa\n");
96       CHISE_DS_close (ds);
97       return -1;
98     }
99   
100   /* get a character from the decoding-table */
101   char_id = chise_ccs_decode (ccs_daikanwa, 364);
102 #endif
103
104   /* get a feature-value of the character */
105   ft_ideographic_structure
106     = chise_ds_get_feature (ds, "ideographic-structure");
107   if (chise_char_gets_feature_value
108       (char_id, ft_ideographic_structure, buf, sizeof (buf)) != NULL)
109     printf ("#x%X => %s\n", char_id, buf);
110   else
111     printf ("#x%X\n", char_id);
112
113   chise_ds_foreach_char_feature_name (ds, &test_name_map_func);
114
115   ft_numeric_value = chise_ds_get_feature (ds, "numeric-value");
116   chise_feature_foreach_char_with_value (ft_numeric_value, &test_map_func);
117   chise_feature_foreach_char_with_value
118     (ft_ideographic_structure, &test_map_func);
119
120   /* close the data-source */
121   CHISE_DS_close (ds);
122
123   return 0;
124 }