+#include <stdlib.h>
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
+#include "sysdep.h"
+#include "chise.h"
+#include "chise-name.h"
+
+
+CHISE_Feature_Table*
+chise_ds_open_feature_table (CHISE_DS *ds, const char *feature);
+
+int chise_ft_close (CHISE_Feature_Table *table);
+
+
+CHISE_CCS_Table*
+chise_ds_open_ccs_table (CHISE_DS *ds, const char *ccs);
+
+int chise_ccst_close (CHISE_CCS_Table *table);
+
+
+typedef DB CHISE_Attribute_Table;
+
+CHISE_Attribute_Table*
+CHISE_Attribute_Table_open (const unsigned char *db_dir,
+ const char *encoding, const char *feature,
+ DBTYPE real_subtype,
+ u_int32_t accessmask, int modemask);
+
+int CHISE_Attribute_Table_close (CHISE_Attribute_Table *db);
+
+int chise_attribute_table_get (CHISE_Attribute_Table *db,
+ char *key, CHISE_Value *valdatum);
+
+int chise_attribute_table_put (CHISE_Attribute_Table *db,
+ char *key, unsigned char *value);
-#ifndef HAVE_STRNLEN
-/* original in mysql, strings/strnlen.c.
-uint strnlen(register const char *s, register uint maxlen)
+
+#define xzero(lvalue) ((void) memset (&(lvalue), '\0', sizeof (lvalue)))
+
+CHISE_Char_ID
+chise_char_id_parse_c_string (unsigned char *str, size_t len);
+
+int
+chise_format_char_id (CHISE_Char_ID cid, unsigned char *dest, size_t len);
+
+
+struct CHISE_DS
+{
+ CHISE_DS_Type type;
+ unsigned char *location;
+ CHISE_NAME_TABLE* feature_names;
+ CHISE_NAME_TABLE* ccs_names;
+ DBTYPE subtype;
+ int modemask;
+};
+
+CHISE_DS*
+CHISE_DS_open (CHISE_DS_Type type, char *location,
+ DBTYPE subtype, int modemask)
{
- const char *end= (const char *)memchr(s, '\0', maxlen);
- return end ? (uint) (end - s) : maxlen;
+ CHISE_DS *ds = (CHISE_DS*)malloc (sizeof (CHISE_DS));
+ size_t len = strlen (location);
+
+ if (ds == NULL)
+ return NULL;
+
+ ds->type = type;
+ ds->subtype = subtype;
+ ds->modemask = modemask;
+ ds->location = (unsigned char*)malloc (len + 1);
+ if (ds->location == NULL)
+ {
+ free (ds);
+ return NULL;
+ }
+ strcpy (ds->location, location);
+
+ ds->feature_names = chise_make_name_table ();
+ if (ds->feature_names == NULL)
+ {
+ free (ds->location);
+ free (ds);
+ }
+
+ ds->ccs_names = chise_make_name_table ();
+ if (ds->ccs_names == NULL)
+ {
+ free (ds->feature_names);
+ free (ds->location);
+ free (ds);
+ }
+ return ds;
}
-*/
-static inline int
-strnlen (register const char *s, register int maxlen)
+
+int
+CHISE_DS_close (CHISE_DS *ds)
{
- const char *end= (const char *)memchr(s, '\0', maxlen);
- return end ? (int) (end - s) : maxlen;
+ if (ds->location != NULL)
+ free (ds->location);
+ if (ds->feature_names != NULL)
+ free (ds->feature_names);
+ if (ds->ccs_names != NULL)
+ free (ds->ccs_names);
+ free (ds);
+ return 0;
}
-#endif
-#include "chise.h"
+CHISE_Feature_Table*
+chise_ds_get_feature (CHISE_DS *ds, const unsigned char *feature)
+{
+ CHISE_Feature_Table* ft;
-#define xzero(lvalue) ((void) memset (&(lvalue), '\0', sizeof (lvalue)))
+ ft = chise_name_table_get (ds->feature_names, feature);
+ if (ft != NULL)
+ return ft;
+
+ ft = chise_ds_open_feature_table (ds, feature);
+ if (ft == NULL)
+ return NULL;
+
+ if (chise_name_table_put (ds->feature_names, feature, ft))
+ {
+ chise_ft_close (ft);
+ return NULL;
+ }
+ return ft;
+}
+
+CHISE_CCS
+chise_ds_get_ccs (CHISE_DS *ds, const unsigned char *ccs)
+{
+ CHISE_CCS_Table* ct;
+
+ ct = chise_name_table_get (ds->ccs_names, ccs);
+ if (ct != NULL)
+ return ct;
+
+ ct = chise_ds_open_ccs_table (ds, ccs);
+ if (ct == NULL)
+ return NULL;
+
+ if (chise_name_table_put (ds->ccs_names, ccs, ct))
+ {
+ chise_ccst_close (ct);
+ return NULL;
+ }
+ return ct;
+}
+
+
+struct CHISE_Feature_Table
+{
+ CHISE_DS *ds;
+ unsigned char *name;
+ CHISE_Attribute_Table *db;
+ u_int32_t access;
+};
+
+CHISE_Feature_Table*
+chise_ds_open_feature_table (CHISE_DS *ds, const char *feature)
+{
+ CHISE_Feature_Table* table;
+ size_t len = strlen (feature);
+
+ if (ds == NULL)
+ return NULL;
+
+ table = (CHISE_Feature_Table*)malloc (sizeof (CHISE_Feature_Table));
+ if (table == NULL)
+ return NULL;
+
+ table->ds = ds;
+ table->db = NULL;
+ table->access = 0;
+ table->name = (unsigned char*)malloc (len + 1);
+ if (table->name == NULL)
+ {
+ free (table);
+ return NULL;
+ }
+ strcpy (table->name, feature);
+ return table;
+}
+
+int
+chise_ft_close (CHISE_Feature_Table *table)
+{
+ int status;
+
+ if (table == NULL)
+ return -1;
+
+ if (table->db == NULL)
+ status = -1;
+ else
+ status = CHISE_Attribute_Table_close (table->db);
+
+ if (table->name == NULL)
+ status = -1;
+ else
+ {
+ free (table->name);
+ status = 0;
+ }
+ free (table);
+ return status;
+}
+
+static int
+chise_feature_setup_db (CHISE_Feature feature, int writable)
+{
+ u_int32_t access;
+
+ if (writable)
+ {
+ if ((feature->access & DB_CREATE) == 0)
+ {
+ if (feature->db != NULL)
+ {
+ CHISE_Attribute_Table_close (feature->db);
+ feature->db = NULL;
+ }
+ feature->access = 0;
+ }
+ access = DB_CREATE;
+ }
+ else
+ access = DB_RDONLY;
+
+ if (feature->db == NULL)
+ {
+ CHISE_DS *ds = feature->ds;
+
+ feature->db
+ = CHISE_Attribute_Table_open (ds->location,
+ "system-char-id", feature->name,
+ ds->subtype, access, ds->modemask);
+ if (feature->db == NULL)
+ return -1;
+ feature->access = access;
+ }
+ return 0;
+}
+
+int
+chise_char_load_feature_value (CHISE_Char_ID cid,
+ CHISE_Feature_Table *table,
+ CHISE_Value *valdatum)
+{
+ unsigned char key_buf[8];
+
+ if (chise_feature_setup_db (table, 0))
+ return -1;
+ chise_format_char_id (cid, key_buf, 8);
+ return chise_attribute_table_get (table->db, key_buf, valdatum);
+}
+
+unsigned char*
+chise_char_gets_feature_value (CHISE_Char_ID cid,
+ CHISE_Feature_Table *table,
+ unsigned char *buf, size_t size)
+{
+ CHISE_Value valdatum;
+ unsigned char key_buf[8];
+ int status;
+
+ if (chise_feature_setup_db (table, 0))
+ return NULL;
+ chise_format_char_id (cid, key_buf, 8);
+ status = chise_attribute_table_get (table->db,
+ key_buf, &valdatum);
+ if (status)
+ return NULL;
+ if (size < valdatum.size)
+ return NULL;
+ strncpy (buf, valdatum.data, valdatum.size);
+ buf[valdatum.size] = '\0';
+ return buf;
+}
+
+int
+chise_char_feature_value_iterate (CHISE_Feature feature,
+ int (*func) (CHISE_Char_ID cid,
+ CHISE_Feature feature,
+ CHISE_Value *valdatum))
+{
+ DBT keydatum, valdatum;
+ DBC *dbcp;
+ int status;
+
+ if (chise_feature_setup_db (feature, 0))
+ return -1;
+ xzero (keydatum);
+ xzero (valdatum);
+
+ status = feature->db->cursor (feature->db, NULL, &dbcp, 0);
+ for (status = dbcp->c_get (dbcp, &keydatum, &valdatum, DB_FIRST);
+ status == 0;
+ status = dbcp->c_get (dbcp, &keydatum, &valdatum, DB_NEXT))
+ {
+ unsigned char *key_str = (unsigned char *)keydatum.data;
+ int key_len = strnlen (key_str, keydatum.size);
+ CHISE_Char_ID key = chise_char_id_parse_c_string (key_str, key_len);
+ int ret = func (key, feature, &valdatum);
+
+ if (ret)
+ break;
+ }
+ dbcp->c_close (dbcp);
+ return 0;
+}
+
+
+struct CHISE_CCS_Table
+{
+ CHISE_DS *ds;
+ unsigned char *name;
+ CHISE_Attribute_Table *db;
+ u_int32_t access;
+};
+
+CHISE_CCS_Table*
+chise_ds_open_ccs_table (CHISE_DS *ds, const char *ccs)
+{
+ CHISE_CCS_Table* table;
+ size_t len = strlen (ccs);
+
+ if (ds == NULL)
+ return NULL;
+
+ table = (CHISE_CCS_Table*)malloc (sizeof (CHISE_CCS_Table));
+ if (table == NULL)
+ return NULL;
+
+ table->ds = ds;
+ table->db = NULL;
+ table->access = 0;
+ table->name = (unsigned char*)malloc (len + 1);
+ if (table->name == NULL)
+ {
+ free (table);
+ return NULL;
+ }
+ strcpy (table->name, ccs);
+ return table;
+}
+
+int
+chise_ccst_close (CHISE_CCS_Table *table)
+{
+ int status;
+
+ if (table == NULL)
+ return -1;
+
+ if (table->db == NULL)
+ status = 0;
+ else
+ status = CHISE_Attribute_Table_close (table->db);
+
+ if (table->name == NULL)
+ status = -1;
+ else
+ {
+ free (table->name);
+ status = 0;
+ }
+ free (table);
+ return status;
+}
+
+int
+chise_ccs_sync (CHISE_CCS ccs)
+{
+ int status;
+
+ if (ccs->db == NULL)
+ status = 0;
+ else
+ status = CHISE_Attribute_Table_close (ccs->db);
+ ccs->db = NULL;
+ ccs->access = 0;
+ return status;
+}
CHISE_Char_ID
-chise_char_id_parse_c_string (unsigned char *str, int len)
+chise_ccs_decode (CHISE_CCS ccs, int code_point)
+{
+ CHISE_Value valdatum;
+ int status = 0;
+ char key_buf[16];
+
+ if (ccs == NULL)
+ return -1;
+
+ if (ccs->db == NULL)
+ {
+ CHISE_DS *ds = ccs->ds;
+
+ ccs->db = CHISE_Attribute_Table_open (ds->location,
+ ccs->name, "system-char-id",
+ ds->subtype,
+ DB_RDONLY, ds->modemask);
+ if (ccs->db == NULL)
+ return -1;
+ ccs->access = DB_RDONLY;
+ }
+
+ sprintf(key_buf, "%d", code_point);
+ status = chise_attribute_table_get (ccs->db, key_buf, &valdatum);
+ if (!status)
+ {
+ unsigned char *str
+ = (unsigned char *)chise_value_data (&valdatum);
+ int len = strnlen (str, chise_value_size (&valdatum));
+
+ return chise_char_id_parse_c_string (str, len);
+ }
+ return -1;
+}
+
+int
+chise_ccs_set_decoded_char (CHISE_CCS ccs,
+ int code_point, CHISE_Char_ID cid)
+{
+ char key_buf[16], val_buf[8];
+
+ if (ccs == NULL)
+ return -1;
+
+ if ((ccs->access & DB_CREATE) == 0)
+ {
+ if (ccs->db != NULL)
+ {
+ CHISE_Attribute_Table_close (ccs->db);
+ ccs->db = NULL;
+ }
+ ccs->access = 0;
+ }
+ if (ccs->db == NULL)
+ {
+ CHISE_DS *ds = ccs->ds;
+
+ ccs->db
+ = CHISE_Attribute_Table_open (ds->location,
+ ccs->name, "system-char-id",
+ ds->subtype, DB_CREATE, ds->modemask);
+ if (ccs->db == NULL)
+ return -1;
+ ccs->access = DB_CREATE;
+ }
+
+ sprintf(key_buf, "%d", code_point);
+ chise_format_char_id (cid, val_buf, 8);
+ return chise_attribute_table_put (ccs->db, key_buf, val_buf);
+}
+
+
+CHISE_Attribute_Table*
+CHISE_Attribute_Table_open (const unsigned char *db_dir,
+ const char *encoding, const char *feature,
+ DBTYPE real_subtype,
+ u_int32_t accessmask, int modemask)
+{
+ DB* dbase;
+ int status;
+ int len, flen, i;
+ int size;
+ char *db_file_name, *sp;
+
+ status = db_create (&dbase, NULL, 0);
+ if (status)
+ return NULL;
+
+ len = strlen (db_dir);
+ flen = strlen (feature);
+ size = len + strlen (encoding) + flen * 3 + 4;
+ db_file_name = alloca (size);
+ strcpy (db_file_name, db_dir);
+ if (db_file_name[len - 1] != '/')
+ {
+ db_file_name[len++] = '/';
+ db_file_name[len] = '\0';
+ }
+ strcat (db_file_name, encoding);
+ strcat (db_file_name, "/");
+ /* strcat (db_file_name, feature); */
+ sp = &db_file_name[strlen (db_file_name)];
+ for (i = 0; i < flen; i++)
+ {
+ int c = feature[i];
+
+ if ( (c == '/') || (c == '%') )
+ {
+ sprintf (sp, "%%%02X", c);
+ sp += 3;
+ }
+ else
+ *sp++ = c;
+ }
+ *sp = '\0';
+ status = dbase->open (dbase, db_file_name, NULL,
+ real_subtype, accessmask, modemask);
+ if (status)
+ {
+ dbase->close (dbase, 0);
+ return NULL;
+ }
+ return dbase;
+}
+
+int
+CHISE_Attribute_Table_close (CHISE_Attribute_Table *db)
+{
+ if (db)
+ {
+ db->sync (db, 0);
+ db->close (db, 0);
+ }
+ return 0;
+}
+
+int
+chise_attribute_table_get (CHISE_Attribute_Table *db,
+ char *key, CHISE_Value *valdatum)
+{
+ DBT keydatum;
+ int status = 0;
+
+ /* DB Version 2 requires DBT's to be zeroed before use. */
+ xzero (keydatum);
+ xzero (*valdatum);
+
+ keydatum.data = key;
+ keydatum.size = strlen (key);
+
+ status = db->get (db, NULL, &keydatum, valdatum, 0);
+ return status;
+}
+
+int
+chise_attribute_table_put (CHISE_Attribute_Table *db,
+ char *key, unsigned char *value)
+{
+ DBT keydatum, valdatum;
+ int status = 0;
+
+ /* DB Version 2 requires DBT's to be zeroed before use. */
+ xzero (keydatum);
+ xzero (valdatum);
+
+ keydatum.data = key;
+ keydatum.size = strlen (key);
+
+ valdatum.data = value;
+ valdatum.size = strlen (value);
+
+ status = db->put (db, NULL, &keydatum, &valdatum, 0);
+ return status;
+}
+
+CHISE_Char_ID
+chise_char_id_parse_c_string (unsigned char *str, size_t len)
{
int i = 0;
}
int
-chise_format_char_id (CHISE_Char_ID cid, unsigned char *dest, int len)
+chise_format_char_id (CHISE_Char_ID cid, unsigned char *dest, size_t len)
{
int i = 0;
return i;
}
}
-
-int
-chise_open_data_source (CHISE_DS *ds, CHISE_DS_Type type, char *location)
-{
- ds->type = type;
- ds->location = location;
- return 0;
-}
-
-int
-chise_close_data_source (CHISE_DS *ds)
-{
- ds->type = CHISE_DS_NONE;
- ds->location = NULL;
- return 0;
-}
-
-
-int
-chise_open_decoding_table (CHISE_Decoding_Table **db,
- CHISE_DS *ds, const char *ccs,
- DBTYPE real_subtype,
- u_int32_t accessmask, int modemask)
-{
- return
- chise_open_attribute_table (db, ds->location,
- ccs, "system-char-id",
- real_subtype, accessmask, modemask);
-}
-
-int
-chise_close_decoding_table (CHISE_Decoding_Table *db)
-{
- if (db)
- return chise_close_attribute_table (db);
- return -1;
-}
-
-CHISE_Char_ID
-chise_dt_get_char (CHISE_Decoding_Table *db, int code_point)
-{
- CHISE_Value valdatum;
- int status = 0;
- char key_buf[16];
-
- sprintf(key_buf, "%d", code_point);
- status = chise_get_attribute_table (db, key_buf, &valdatum);
- if (!status)
- {
- unsigned char *str
- = (unsigned char *)chise_value_data (&valdatum);
- int len = strnlen (str, chise_value_size (&valdatum));
-
- return chise_char_id_parse_c_string (str, len);
- }
- return -1;
-}
-
-
-
-int
-chise_open_feature_table (CHISE_Feature_Table **db,
- CHISE_DS *ds, const char *feature,
- DBTYPE real_subtype,
- u_int32_t accessmask, int modemask)
-{
- return
- chise_open_attribute_table (db, ds->location,
- "system-char-id", feature,
- real_subtype, accessmask, modemask);
-}
-
-int
-chise_close_feature_table (CHISE_Feature_Table *db)
-{
- if (db)
- return chise_close_attribute_table (db);
- return -1;
-}
-
-int
-chise_ft_get_value (CHISE_Feature_Table *db,
- CHISE_Char_ID cid, CHISE_Value *valdatum)
-{
- unsigned char key_buf[8];
-
- chise_format_char_id (cid, key_buf, 8);
- return chise_get_attribute_table (db, key_buf, valdatum);
-}
-
-void
-chise_ft_iterate (CHISE_Feature_Table *dbp,
- int (*func) (CHISE_Feature_Table *db,
- CHISE_Char_ID cid, CHISE_Value *valdatum))
-{
- DBT keydatum, valdatum;
- DBC *dbcp;
- int status;
-
- xzero (keydatum);
- xzero (valdatum);
-
- status = dbp->cursor (dbp, NULL, &dbcp, 0);
- for (status = dbcp->c_get (dbcp, &keydatum, &valdatum, DB_FIRST);
- status == 0;
- status = dbcp->c_get (dbcp, &keydatum, &valdatum, DB_NEXT))
- {
- unsigned char *key_str = (unsigned char *)keydatum.data;
- int key_len = strnlen (key_str, keydatum.size);
- CHISE_Char_ID key = chise_char_id_parse_c_string (key_str, key_len);
- int ret;
-
- if (ret = func (dbp, key, &valdatum))
- break;
- }
- dbcp->c_close (dbcp);
-}
-
-int
-chise_open_attribute_table (CHISE_Attribute_Table **db,
- const char *db_dir,
- const char *encoding, const char *feature,
- DBTYPE real_subtype,
- u_int32_t accessmask, int modemask)
-{
- DB* dbase;
- int status;
- int len, flen, i;
- int size;
- char *db_file_name, *sp;
-
- status = db_create (&dbase, NULL, 0);
- if (status)
- return -1;
-
- len = strlen (db_dir);
- flen = strlen (feature);
- size = len + strlen (encoding) + flen * 3 + 4;
- db_file_name = alloca (size);
- strcpy (db_file_name, db_dir);
- if (db_file_name[len - 1] != '/')
- {
- db_file_name[len++] = '/';
- db_file_name[len] = '\0';
- }
- strcat (db_file_name, encoding);
- strcat (db_file_name, "/");
- /* strcat (db_file_name, feature); */
- sp = &db_file_name[strlen (db_file_name)];
- for (i = 0; i < flen; i++)
- {
- int c = feature[i];
-
- if ( (c == '/') || (c == '%') )
- {
- sprintf (sp, "%%%02X", c);
- sp += 3;
- }
- else
- *sp++ = c;
- }
- *sp = '\0';
- status = dbase->open (dbase, db_file_name, NULL,
- real_subtype, accessmask, modemask);
- if (status)
- {
- dbase->close (dbase, 0);
- return -1;
- }
- *db = dbase;
- return 0;
-}
-
-int
-chise_close_attribute_table (CHISE_Attribute_Table *db)
-{
- if (db)
- {
- db->sync (db, 0);
- db->close (db, 0);
- }
- return 0;
-}
-
-int
-chise_get_attribute_table (CHISE_Attribute_Table *db,
- char *key, CHISE_Value *valdatum)
-{
- DBT keydatum;
- int status = 0;
-
- /* DB Version 2 requires DBT's to be zeroed before use. */
- xzero (keydatum);
- xzero (*valdatum);
-
- keydatum.data = key;
- keydatum.size = strlen (key);
-
- status = db->get (db, NULL, &keydatum, valdatum, 0);
- return status;
-}