1 /* Copyright (C) 2003,2004,2005,2011 MORIOKA Tomohiko
2 This file is part of the CHISE Library.
4 The CHISE Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The CHISE Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the CHISE Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 #include <sys/types.h>
30 const unsigned char chise_db_format_version[] = CHISE_DB_FORMAT_VERSION;
31 const unsigned char chise_db_dir[] = CHISE_DB_DIR;
32 const unsigned char chise_system_db_dir[] = CHISE_SI_DB_DIR;
35 #define xzero(lvalue) ((void) memset (&(lvalue), '\0', sizeof (lvalue)))
39 chise_value_size (const CHISE_Value* s)
45 chise_value_data (const CHISE_Value* s)
51 chise_value_to_c_string (const CHISE_Value* s)
58 chise_char_id_parse_c_string (unsigned char *str, size_t len);
61 chise_format_char_id (CHISE_Char_ID cid, unsigned char *dest, size_t len);
65 CHISE_DS_open (CHISE_DS_Type type, const unsigned char* location,
66 int subtype, int modemask)
68 CONCORD_DS ds = concord_open_ds (type, location, subtype, modemask);
73 return concord_ds_get_genre (ds, "character");
77 CHISE_DS_close (CHISE_DS* ds)
79 return concord_close_ds (concord_genre_get_data_source (ds));
83 chise_ds_location (CHISE_DS* ds)
85 return concord_ds_location (concord_genre_get_data_source (ds));
89 chise_ds_get_feature (CHISE_DS* ds, const unsigned char* name)
91 return concord_genre_get_feature (ds, name);
95 chise_ds_get_ccs (CHISE_DS* ds, const unsigned char* name)
97 return concord_genre_get_index (ds, name);
101 chise_ds_get_property (CHISE_DS* ds, const unsigned char* name)
104 = concord_ds_get_genre (concord_genre_get_data_source (ds),
110 return concord_genre_get_feature (genre, name);
114 chise_ds_foreach_char_feature_name (CHISE_DS* ds,
115 int (*func) (CHISE_DS* ds,
116 unsigned char* name))
118 return concord_genre_foreach_feature_name (ds, func);
122 chise_feature_setup_db (CHISE_Feature feature, int writable)
124 return concord_feature_setup_db (feature, writable);
128 chise_feature_sync (CHISE_Feature feature)
130 return concord_feature_sync (feature);
134 chise_char_set_feature_value (CHISE_Char_ID cid,
135 CHISE_Feature feature,
136 unsigned char* value)
138 unsigned char key_buf[8];
140 chise_format_char_id (cid, key_buf, 8);
141 return concord_obj_put_feature_value_str (key_buf, feature, value);
145 chise_char_load_feature_value (CHISE_Char_ID cid,
146 CHISE_Feature feature,
147 CHISE_Value* valdatum)
149 unsigned char key_buf[8];
151 chise_format_char_id (cid, key_buf, 8);
152 return concord_obj_get_feature_value_string (key_buf, feature, valdatum);
156 chise_char_load_decomposition (CHISE_Char_ID cid,
157 CHISE_Feature feature,
158 CHISE_Char_ID* base_char, CHISE_Char_ID* comb_char)
160 unsigned char key_buf[8];
164 unsigned char* vp_ret;
167 chise_format_char_id (cid, key_buf, 8);
168 if ( status = concord_obj_get_feature_value_string (key_buf, feature, &value) )
170 len = chise_value_size (&value);
171 if ( len < (1 + 2 + 1 + 2 + 1) )
174 vp = chise_value_data (&value);
179 if ( ( vp_ret = memchr (vp, ' ', len) ) == NULL )
181 *base_char = chise_char_id_parse_c_string (vp, vp_ret - vp + 1);
182 if ( *base_char == -1 )
185 len -= vp - chise_value_data (&value);
189 *comb_char = chise_char_id_parse_c_string (vp, len);
190 if ( *comb_char == -1 )
196 chise_char_gets_feature_value (CHISE_Char_ID cid,
197 CHISE_Feature feature,
198 unsigned char* dst, size_t size)
200 unsigned char key_buf[8];
202 chise_format_char_id (cid, key_buf, 8);
203 return concord_obj_gets_feature_value (key_buf, feature, dst, size);
207 (*chise_feature_for_a_char_with_value_func) (CHISE_Char_ID cid,
208 CHISE_Feature feature,
209 CHISE_Value* valdatum);
212 chise_feature_foreach_char_with_value_wrapper (CONCORD_String object_id,
213 CONCORD_Feature feature,
214 CONCORD_String value);
216 chise_feature_foreach_char_with_value_wrapper (CONCORD_String object_id,
217 CONCORD_Feature feature,
218 CONCORD_String value)
220 unsigned char* cid_str = CONCORD_String_data (object_id);
221 int cid_len = strnlen (cid_str, CONCORD_String_size (object_id));
222 CHISE_Char_ID cid = chise_char_id_parse_c_string (cid_str, cid_len);
224 return chise_feature_for_a_char_with_value_func (cid, feature, value);
228 chise_feature_foreach_char_with_value (CHISE_Feature feature,
229 int (*func) (CHISE_Char_ID cid,
230 CHISE_Feature feature,
231 CHISE_Value* valdatum))
233 chise_feature_for_a_char_with_value_func = func;
235 concord_feature_foreach_obj_string
236 (feature, &chise_feature_foreach_char_with_value_wrapper);
241 chise_ccs_setup_db (CHISE_CCS ccs, int writable)
243 return concord_index_setup_db (ccs, writable);
247 chise_ccs_sync (CHISE_CCS ccs)
249 return concord_index_sync (ccs);
253 chise_ccs_decode (CHISE_CCS ccs, int code_point)
256 CONCORD_String_Tank value;
259 sprintf(key_buf, "%d", code_point);
260 status = concord_index_strid_get_obj_string (ccs, key_buf, &value);
264 = (unsigned char *)CONCORD_String_data (&value);
265 int len = strnlen (str, CONCORD_String_size (&value));
267 return chise_char_id_parse_c_string (str, len);
273 chise_ccs_set_decoded_char (CHISE_CCS ccs,
274 int code_point, CHISE_Char_ID cid)
276 char key_buf[16], val_buf[8];
278 sprintf(key_buf, "%d", code_point);
279 chise_format_char_id (cid, val_buf, 8);
280 return concord_index_strid_put_obj (ccs, key_buf, val_buf);
285 chise_property_setup_db (CHISE_Property property, int writable)
287 return concord_feature_setup_db (property, writable);
291 chise_property_sync (CHISE_Property property)
293 return concord_feature_sync (property);
297 chise_feature_set_property_value (CHISE_Feature feature,
298 CHISE_Property property,
299 unsigned char* value)
302 concord_obj_put_feature_value_str
303 (concord_feature_get_name (feature), property, value);
307 chise_feature_load_property_value (CHISE_Feature feature,
308 CHISE_Property property,
309 CHISE_Value* valdatum)
312 concord_obj_get_feature_value_string
313 (concord_feature_get_name (feature), property, valdatum);
317 chise_feature_gets_property_value (CHISE_Feature feature,
318 CHISE_Property property,
319 unsigned char* buf, size_t size)
322 concord_obj_gets_feature_value (concord_feature_get_name (feature),
323 property, buf, size);
328 chise_char_id_parse_c_string (unsigned char *str, size_t len)
332 if ( (len >= 2) && (str[i++] == '?') )
334 unsigned char c = str[i++];
351 return c & (0x80 | 0x1F);
385 if (counter + 2 <= len)
389 for (j = 0; j < counter; j++)
390 cid = (cid << 6) | (str[j + i] & 0x3F);
398 chise_format_char_id (CHISE_Char_ID cid, unsigned char *dest, size_t len)
410 else if (cid == '\n')
417 else if (cid == '\r')
424 else if (cid == 0x1C)
433 else if (cid <= 0x1F)
437 dest[i++] = '@' + cid;
441 else if ( (cid == ' ') || (cid == '"') ||
442 (cid == '#') || (cid == '\'') ||
443 (cid == '(') || (cid == ')') ||
444 (cid == ',') || (cid == '.') ||
445 (cid == ';') || (cid == '?') ||
446 (cid == '[') || (cid == '\\') ||
447 (cid == ']') || (cid == '`') )
454 else if (cid <= 0x7E)
460 else if (cid == 0x7F)
468 else if (cid <= 0x9F)
472 dest[i++] = ((cid + '@') >> 6) | 0xC0;
473 dest[i++] = ((cid + '@') & 0x3F) | 0x80;
477 else if (cid <= 0x7FF)
479 dest[i++] = (cid >> 6) | 0xC0;
480 dest[i++] = (cid & 0x3F) | 0x80;
484 else if (cid <= 0xFFFF)
486 dest[i++] = (cid >> 12) | 0xE0;
487 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
488 dest[i++]= (cid & 0x3F) | 0x80;
492 else if (cid <= 0x1FFFFF)
494 dest[i++]= (cid >> 18) | 0xF0;
495 dest[i++]= ((cid >> 12) & 0x3F) | 0x80;
496 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
497 dest[i++]= (cid & 0x3F) | 0x80;
501 else if (cid <= 0x3FFFFFF)
503 dest[i++]= (cid >> 24) | 0xF8;
504 dest[i++]= ((cid >> 18) & 0x3F) | 0x80;
505 dest[i++]= ((cid >> 12) & 0x3F) | 0x80;
506 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
507 dest[i++]= (cid & 0x3F) | 0x80;
513 dest[i++]= (cid >> 30) | 0xFC;
514 dest[i++]= ((cid >> 24) & 0x3F) | 0x80;
515 dest[i++]= ((cid >> 18) & 0x3F) | 0x80;
516 dest[i++]= ((cid >> 12) & 0x3F) | 0x80;
517 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
518 dest[i++]= (cid & 0x3F) | 0x80;