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)
69 CONCORD_DS ds = concord_open_ds (type, location, subtype, modemask);
71 CONCORD_DS ds = concord_open_env (location);
77 return concord_ds_get_genre (ds, "character");
81 CHISE_DS_close (CHISE_DS* ds)
83 return concord_close_ds (concord_genre_get_data_source (ds));
87 chise_ds_location (CHISE_DS* ds)
89 return concord_ds_location (concord_genre_get_data_source (ds));
93 chise_ds_get_feature (CHISE_DS* ds, const unsigned char* name)
95 return concord_genre_get_feature (ds, name);
99 chise_ds_get_ccs (CHISE_DS* ds, const unsigned char* name)
101 return concord_genre_get_index (ds, name);
105 chise_ds_get_property (CHISE_DS* ds, const unsigned char* name)
108 = concord_ds_get_genre (concord_genre_get_data_source (ds),
114 return concord_genre_get_feature (genre, name);
118 chise_ds_foreach_char_feature_name (CHISE_DS* ds,
119 int (*func) (CHISE_DS* ds,
120 unsigned char* name))
122 return concord_genre_foreach_feature_name (ds, func);
126 chise_feature_setup_db (CHISE_Feature feature, int writable)
128 return concord_feature_setup_db (feature, writable);
132 chise_feature_sync (CHISE_Feature feature)
134 return concord_feature_sync (feature);
138 chise_char_set_feature_value (CHISE_Char_ID cid,
139 CHISE_Feature feature,
140 unsigned char* value)
142 unsigned char key_buf[8];
144 chise_format_char_id (cid, key_buf, 8);
145 return concord_obj_put_feature_value_str (key_buf, feature, value);
149 chise_char_load_feature_value (CHISE_Char_ID cid,
150 CHISE_Feature feature,
151 CHISE_Value* valdatum)
153 unsigned char key_buf[8];
155 chise_format_char_id (cid, key_buf, 8);
156 return concord_obj_get_feature_value_string (key_buf, feature, valdatum);
160 chise_char_load_decomposition (CHISE_Char_ID cid,
161 CHISE_Feature feature,
162 CHISE_Char_ID* base_char, CHISE_Char_ID* comb_char)
164 unsigned char key_buf[8];
168 unsigned char* vp_ret;
171 chise_format_char_id (cid, key_buf, 8);
172 if ( status = concord_obj_get_feature_value_string (key_buf, feature, &value) )
174 len = chise_value_size (&value);
175 if ( len < (1 + 2 + 1 + 2 + 1) )
178 vp = chise_value_data (&value);
183 if ( ( vp_ret = memchr (vp, ' ', len) ) == NULL )
185 *base_char = chise_char_id_parse_c_string (vp, vp_ret - vp + 1);
186 if ( *base_char == -1 )
189 len -= vp - chise_value_data (&value);
193 *comb_char = chise_char_id_parse_c_string (vp, len);
194 if ( *comb_char == -1 )
200 chise_char_gets_feature_value (CHISE_Char_ID cid,
201 CHISE_Feature feature,
202 unsigned char* dst, size_t size)
204 unsigned char key_buf[8];
206 chise_format_char_id (cid, key_buf, 8);
207 return concord_obj_gets_feature_value (key_buf, feature, dst, size);
211 (*chise_feature_for_a_char_with_value_func) (CHISE_Char_ID cid,
212 CHISE_Feature feature,
213 CHISE_Value* valdatum);
216 chise_feature_foreach_char_with_value_wrapper (CONCORD_String object_id,
217 CONCORD_Feature feature,
218 CONCORD_String value);
220 chise_feature_foreach_char_with_value_wrapper (CONCORD_String object_id,
221 CONCORD_Feature feature,
222 CONCORD_String value)
224 unsigned char* cid_str = CONCORD_String_data (object_id);
225 int cid_len = strnlen (cid_str, CONCORD_String_size (object_id));
226 CHISE_Char_ID cid = chise_char_id_parse_c_string (cid_str, cid_len);
228 return chise_feature_for_a_char_with_value_func (cid, feature, value);
232 chise_feature_foreach_char_with_value (CHISE_Feature feature,
233 int (*func) (CHISE_Char_ID cid,
234 CHISE_Feature feature,
235 CHISE_Value* valdatum))
237 chise_feature_for_a_char_with_value_func = func;
239 concord_feature_foreach_obj_string
240 (feature, &chise_feature_foreach_char_with_value_wrapper);
245 chise_ccs_setup_db (CHISE_CCS ccs, int writable)
247 return concord_index_setup_db (ccs, writable);
251 chise_ccs_sync (CHISE_CCS ccs)
253 return concord_index_sync (ccs);
257 chise_ccs_decode (CHISE_CCS ccs, int code_point)
260 CONCORD_String_Tank value;
263 sprintf(key_buf, "%d", code_point);
264 status = concord_index_strid_get_obj_string (ccs, key_buf, &value);
268 = (unsigned char *)CONCORD_String_data (&value);
269 int len = strnlen (str, CONCORD_String_size (&value));
271 return chise_char_id_parse_c_string (str, len);
277 chise_ccs_set_decoded_char (CHISE_CCS ccs,
278 int code_point, CHISE_Char_ID cid)
280 char key_buf[16], val_buf[8];
282 sprintf(key_buf, "%d", code_point);
283 chise_format_char_id (cid, val_buf, 8);
284 return concord_index_strid_put_obj (ccs, key_buf, val_buf);
289 chise_property_setup_db (CHISE_Property property, int writable)
291 return concord_feature_setup_db (property, writable);
295 chise_property_sync (CHISE_Property property)
297 return concord_feature_sync (property);
301 chise_feature_set_property_value (CHISE_Feature feature,
302 CHISE_Property property,
303 unsigned char* value)
306 concord_obj_put_feature_value_str
307 (concord_feature_get_name (feature), property, value);
311 chise_feature_load_property_value (CHISE_Feature feature,
312 CHISE_Property property,
313 CHISE_Value* valdatum)
316 concord_obj_get_feature_value_string
317 (concord_feature_get_name (feature), property, valdatum);
321 chise_feature_gets_property_value (CHISE_Feature feature,
322 CHISE_Property property,
323 unsigned char* buf, size_t size)
326 concord_obj_gets_feature_value (concord_feature_get_name (feature),
327 property, buf, size);
332 chise_char_id_parse_c_string (unsigned char *str, size_t len)
336 if ( (len >= 2) && (str[i++] == '?') )
338 unsigned char c = str[i++];
355 return c & (0x80 | 0x1F);
389 if (counter + 2 <= len)
393 for (j = 0; j < counter; j++)
394 cid = (cid << 6) | (str[j + i] & 0x3F);
402 chise_format_char_id (CHISE_Char_ID cid, unsigned char *dest, size_t len)
414 else if (cid == '\n')
421 else if (cid == '\r')
428 else if (cid == 0x1C)
437 else if (cid <= 0x1F)
441 dest[i++] = '@' + cid;
445 else if ( (cid == ' ') || (cid == '"') ||
446 (cid == '#') || (cid == '\'') ||
447 (cid == '(') || (cid == ')') ||
448 (cid == ',') || (cid == '.') ||
449 (cid == ';') || (cid == '?') ||
450 (cid == '[') || (cid == '\\') ||
451 (cid == ']') || (cid == '`') )
458 else if (cid <= 0x7E)
464 else if (cid == 0x7F)
472 else if (cid <= 0x9F)
476 dest[i++] = ((cid + '@') >> 6) | 0xC0;
477 dest[i++] = ((cid + '@') & 0x3F) | 0x80;
481 else if (cid <= 0x7FF)
483 dest[i++] = (cid >> 6) | 0xC0;
484 dest[i++] = (cid & 0x3F) | 0x80;
488 else if (cid <= 0xFFFF)
490 dest[i++] = (cid >> 12) | 0xE0;
491 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
492 dest[i++]= (cid & 0x3F) | 0x80;
496 else if (cid <= 0x1FFFFF)
498 dest[i++]= (cid >> 18) | 0xF0;
499 dest[i++]= ((cid >> 12) & 0x3F) | 0x80;
500 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
501 dest[i++]= (cid & 0x3F) | 0x80;
505 else if (cid <= 0x3FFFFFF)
507 dest[i++]= (cid >> 24) | 0xF8;
508 dest[i++]= ((cid >> 18) & 0x3F) | 0x80;
509 dest[i++]= ((cid >> 12) & 0x3F) | 0x80;
510 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
511 dest[i++]= (cid & 0x3F) | 0x80;
517 dest[i++]= (cid >> 30) | 0xFC;
518 dest[i++]= ((cid >> 24) & 0x3F) | 0x80;
519 dest[i++]= ((cid >> 18) & 0x3F) | 0x80;
520 dest[i++]= ((cid >> 12) & 0x3F) | 0x80;
521 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
522 dest[i++]= (cid & 0x3F) | 0x80;