10 #include "chise-name.h"
14 chise_ds_open_feature_table (CHISE_DS *ds, const char *feature);
16 int chise_ft_close (CHISE_Feature_Table *table);
20 chise_ds_open_ccs_table (CHISE_DS *ds, const char *ccs);
22 int chise_ccst_close (CHISE_CCS_Table *table);
25 typedef DB CHISE_Attribute_Table;
27 CHISE_Attribute_Table*
28 CHISE_Attribute_Table_open (const unsigned char *db_dir,
29 const char *encoding, const char *feature,
31 u_int32_t accessmask, int modemask);
33 int CHISE_Attribute_Table_close (CHISE_Attribute_Table *db);
35 int chise_attribute_table_get (CHISE_Attribute_Table *db,
36 char *key, CHISE_Value *valdatum);
38 int chise_attribute_table_put (CHISE_Attribute_Table *db,
39 char *key, unsigned char *value);
42 #define xzero(lvalue) ((void) memset (&(lvalue), '\0', sizeof (lvalue)))
45 chise_char_id_parse_c_string (unsigned char *str, size_t len);
48 chise_format_char_id (CHISE_Char_ID cid, unsigned char *dest, size_t len);
54 unsigned char *location;
55 CHISE_NAME_TABLE* feature_names;
56 CHISE_NAME_TABLE* ccs_names;
62 CHISE_DS_open (CHISE_DS_Type type, char *location,
63 DBTYPE subtype, int modemask)
65 CHISE_DS *ds = (CHISE_DS*)malloc (sizeof (CHISE_DS));
66 size_t len = strlen (location);
72 ds->subtype = subtype;
73 ds->modemask = modemask;
74 ds->location = (unsigned char*)malloc (len + 1);
75 if (ds->location == NULL)
80 strcpy (ds->location, location);
82 ds->feature_names = chise_make_name_table ();
83 if (ds->feature_names == NULL)
89 ds->ccs_names = chise_make_name_table ();
90 if (ds->ccs_names == NULL)
92 free (ds->feature_names);
100 CHISE_DS_close (CHISE_DS *ds)
102 if (ds->location != NULL)
104 if (ds->feature_names != NULL)
105 free (ds->feature_names);
106 if (ds->ccs_names != NULL)
107 free (ds->ccs_names);
113 chise_ds_get_feature (CHISE_DS *ds, const unsigned char *feature)
115 CHISE_Feature_Table* ft;
117 ft = chise_name_table_get (ds->feature_names, feature);
121 ft = chise_ds_open_feature_table (ds, feature);
125 if (chise_name_table_put (ds->feature_names, feature, ft))
134 chise_ds_get_ccs (CHISE_DS *ds, const unsigned char *ccs)
138 ct = chise_name_table_get (ds->ccs_names, ccs);
142 ct = chise_ds_open_ccs_table (ds, ccs);
146 if (chise_name_table_put (ds->ccs_names, ccs, ct))
148 chise_ccst_close (ct);
155 struct CHISE_Feature_Table
159 CHISE_Attribute_Table *db;
164 chise_ds_open_feature_table (CHISE_DS *ds, const char *feature)
166 CHISE_Feature_Table* table;
167 size_t len = strlen (feature);
172 table = (CHISE_Feature_Table*)malloc (sizeof (CHISE_Feature_Table));
179 table->name = (unsigned char*)malloc (len + 1);
180 if (table->name == NULL)
185 strcpy (table->name, feature);
190 chise_ft_close (CHISE_Feature_Table *table)
197 if (table->db == NULL)
200 status = CHISE_Attribute_Table_close (table->db);
202 if (table->name == NULL)
214 chise_feature_setup_db (CHISE_Feature feature, int writable)
223 if ((feature->access & DB_CREATE) == 0)
225 if (feature->db != NULL)
227 CHISE_Attribute_Table_close (feature->db);
237 if (feature->db == NULL)
239 CHISE_DS *ds = feature->ds;
242 = CHISE_Attribute_Table_open (ds->location,
243 "system-char-id", feature->name,
244 ds->subtype, access, ds->modemask);
245 if (feature->db == NULL)
247 feature->access = access;
253 chise_feature_sync (CHISE_Feature feature)
257 if (feature->db == NULL)
260 status = CHISE_Attribute_Table_close (feature->db);
267 chise_char_set_feature_value (CHISE_Char_ID cid,
268 CHISE_Feature feature,
269 unsigned char *value)
271 unsigned char key_buf[8];
275 if (chise_feature_setup_db (feature, 1))
277 chise_format_char_id (cid, key_buf, 8);
278 return chise_attribute_table_put (feature->db, key_buf, value);
282 chise_char_load_feature_value (CHISE_Char_ID cid,
283 CHISE_Feature_Table *table,
284 CHISE_Value *valdatum)
286 unsigned char key_buf[8];
288 if (chise_feature_setup_db (table, 0))
290 chise_format_char_id (cid, key_buf, 8);
291 return chise_attribute_table_get (table->db, key_buf, valdatum);
295 chise_char_gets_feature_value (CHISE_Char_ID cid,
296 CHISE_Feature_Table *table,
297 unsigned char *buf, size_t size)
299 CHISE_Value valdatum;
300 unsigned char key_buf[8];
303 if (chise_feature_setup_db (table, 0))
305 chise_format_char_id (cid, key_buf, 8);
306 status = chise_attribute_table_get (table->db,
310 if (size < valdatum.size)
312 strncpy (buf, valdatum.data, valdatum.size);
313 buf[valdatum.size] = '\0';
318 chise_char_feature_value_iterate (CHISE_Feature feature,
319 int (*func) (CHISE_Char_ID cid,
320 CHISE_Feature feature,
321 CHISE_Value *valdatum))
323 DBT keydatum, valdatum;
327 if (chise_feature_setup_db (feature, 0))
332 status = feature->db->cursor (feature->db, NULL, &dbcp, 0);
333 for (status = dbcp->c_get (dbcp, &keydatum, &valdatum, DB_FIRST);
335 status = dbcp->c_get (dbcp, &keydatum, &valdatum, DB_NEXT))
337 unsigned char *key_str = (unsigned char *)keydatum.data;
338 int key_len = strnlen (key_str, keydatum.size);
339 CHISE_Char_ID key = chise_char_id_parse_c_string (key_str, key_len);
340 int ret = func (key, feature, &valdatum);
345 dbcp->c_close (dbcp);
350 struct CHISE_CCS_Table
354 CHISE_Attribute_Table *db;
359 chise_ds_open_ccs_table (CHISE_DS *ds, const char *ccs)
361 CHISE_CCS_Table* table;
362 size_t len = strlen (ccs);
367 table = (CHISE_CCS_Table*)malloc (sizeof (CHISE_CCS_Table));
374 table->name = (unsigned char*)malloc (len + 1);
375 if (table->name == NULL)
380 strcpy (table->name, ccs);
385 chise_ccst_close (CHISE_CCS_Table *table)
392 if (table->db == NULL)
395 status = CHISE_Attribute_Table_close (table->db);
397 if (table->name == NULL)
409 chise_ccs_setup_db (CHISE_CCS ccs, int writable)
418 if ((ccs->access & DB_CREATE) == 0)
422 CHISE_Attribute_Table_close (ccs->db);
434 CHISE_DS *ds = ccs->ds;
437 = CHISE_Attribute_Table_open (ds->location,
438 ccs->name, "system-char-id",
439 ds->subtype, access, ds->modemask);
442 ccs->access = access;
448 chise_ccs_sync (CHISE_CCS ccs)
455 status = CHISE_Attribute_Table_close (ccs->db);
462 chise_ccs_decode (CHISE_CCS ccs, int code_point)
464 CHISE_Value valdatum;
471 if (chise_ccs_setup_db (ccs, 0))
474 sprintf(key_buf, "%d", code_point);
475 status = chise_attribute_table_get (ccs->db, key_buf, &valdatum);
479 = (unsigned char *)chise_value_data (&valdatum);
480 int len = strnlen (str, chise_value_size (&valdatum));
482 return chise_char_id_parse_c_string (str, len);
488 chise_ccs_set_decoded_char (CHISE_CCS ccs,
489 int code_point, CHISE_Char_ID cid)
491 char key_buf[16], val_buf[8];
496 if (chise_ccs_setup_db (ccs, 1))
499 sprintf(key_buf, "%d", code_point);
500 chise_format_char_id (cid, val_buf, 8);
501 return chise_attribute_table_put (ccs->db, key_buf, val_buf);
505 CHISE_Attribute_Table*
506 CHISE_Attribute_Table_open (const unsigned char *db_dir,
507 const char *encoding, const char *feature,
509 u_int32_t accessmask, int modemask)
515 char *db_file_name, *sp;
518 status = db_create (&dbase, NULL, 0);
522 if ( (accessmask & DB_CREATE) && stat (db_dir, &statbuf) )
523 mkdir (db_dir, modemask);
525 len = strlen (db_dir);
526 flen = strlen (feature);
527 size = len + strlen (encoding) + flen * 3 + 4;
528 db_file_name = alloca (size);
529 strcpy (db_file_name, db_dir);
530 if (db_file_name[len - 1] != '/')
532 db_file_name[len++] = '/';
533 db_file_name[len] = '\0';
535 strcat (db_file_name, encoding);
537 if ( (accessmask & DB_CREATE) && stat (db_file_name, &statbuf) )
538 mkdir (db_file_name, modemask);
540 strcat (db_file_name, "/");
541 /* strcat (db_file_name, feature); */
542 sp = &db_file_name[strlen (db_file_name)];
543 for (i = 0; i < flen; i++)
547 if ( (c == '/') || (c == '%') )
549 sprintf (sp, "%%%02X", c);
556 status = dbase->open (dbase, db_file_name, NULL,
557 real_subtype, accessmask, modemask);
560 dbase->close (dbase, 0);
567 CHISE_Attribute_Table_close (CHISE_Attribute_Table *db)
578 chise_attribute_table_get (CHISE_Attribute_Table *db,
579 char *key, CHISE_Value *valdatum)
584 /* DB Version 2 requires DBT's to be zeroed before use. */
589 keydatum.size = strlen (key);
591 status = db->get (db, NULL, &keydatum, valdatum, 0);
596 chise_attribute_table_put (CHISE_Attribute_Table *db,
597 char *key, unsigned char *value)
599 DBT keydatum, valdatum;
602 /* DB Version 2 requires DBT's to be zeroed before use. */
607 keydatum.size = strlen (key);
609 valdatum.data = value;
610 valdatum.size = strlen (value);
612 status = db->put (db, NULL, &keydatum, &valdatum, 0);
617 chise_char_id_parse_c_string (unsigned char *str, size_t len)
621 if ( (len >= 2) && (str[i++] == '?') )
623 unsigned char c = str[i++];
640 return c & (0x80 | 0x1F);
674 if (counter + 2 <= len)
678 for (j = 0; j < counter; j++)
679 cid = (cid << 6) | (str[j + i] & 0x3F);
687 chise_format_char_id (CHISE_Char_ID cid, unsigned char *dest, size_t len)
699 else if (cid == '\n')
706 else if (cid == '\r')
713 else if (cid == 0x1C)
722 else if (cid <= 0x1F)
726 dest[i++] = '@' + cid;
730 else if ( (cid == ' ') || (cid == '"') ||
731 (cid == '#') || (cid == '\'') ||
732 (cid == '(') || (cid == ')') ||
733 (cid == ',') || (cid == '.') ||
734 (cid == ';') || (cid == '?') ||
735 (cid == '[') || (cid == '\\') ||
736 (cid == ']') || (cid == '`') )
743 else if (cid <= 0x7E)
749 else if (cid == 0x7F)
757 else if (cid <= 0x9F)
761 dest[i++] = ((cid + '@') >> 6) | 0xC0;
762 dest[i++] = ((cid + '@') & 0x3F) | 0x80;
766 else if (cid <= 0x7FF)
768 dest[i++] = (cid >> 6) | 0xC0;
769 dest[i++] = (cid & 0x3F) | 0x80;
773 else if (cid <= 0xFFFF)
775 dest[i++] = (cid >> 12) | 0xE0;
776 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
777 dest[i++]= (cid & 0x3F) | 0x80;
781 else if (cid <= 0x1FFFFF)
783 dest[i++]= (cid >> 18) | 0xF0;
784 dest[i++]= ((cid >> 12) & 0x3F) | 0x80;
785 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
786 dest[i++]= (cid & 0x3F) | 0x80;
790 else if (cid <= 0x3FFFFFF)
792 dest[i++]= (cid >> 24) | 0xF8;
793 dest[i++]= ((cid >> 18) & 0x3F) | 0x80;
794 dest[i++]= ((cid >> 12) & 0x3F) | 0x80;
795 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
796 dest[i++]= (cid & 0x3F) | 0x80;
802 dest[i++]= (cid >> 30) | 0xFC;
803 dest[i++]= ((cid >> 24) & 0x3F) | 0x80;
804 dest[i++]= ((cid >> 18) & 0x3F) | 0x80;
805 dest[i++]= ((cid >> 12) & 0x3F) | 0x80;
806 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
807 dest[i++]= (cid & 0x3F) | 0x80;