From: tomo Date: Sun, 17 Aug 2003 13:49:01 +0000 (+0000) Subject: (chise_open_data_source): Interface change; allocate a CHISE_DS object X-Git-Tag: b1c-r0_2_0-pre2~2 X-Git-Url: http://git.chise.org/gitweb/?a=commitdiff_plain;h=877ee8ab4278a2a2a5ea51b9349d7d24fa2ac153;p=chise%2Flibchise.git (chise_open_data_source): Interface change; allocate a CHISE_DS object and return it. (chise_ds_close): Renamed from `chise_close_data_source'; destroy the specified CHISE_DS object. (chise_ds_open_decoding_table): Renamed from `chise_open_decoding_table' and changed the interface. (chise_dt_close): Renamed from `chise_close_decoding_table'. (chise_ds_open_feature_table): Renamed from `chise_open_feature_table' and changed the interface. (chise_ft_close): Renamed from `chise_close_feature_table'. (chise_open_attribute_table): Interface change; allocate a CHISE_Attribute_Table object and return it; require pointer for data source object instead of db_dir. (chise_close_attribute_table): Destroy the specified CHISE_Attribute_Table object. --- diff --git a/chise.c b/chise.c index 5d382c1..c37a61a 100644 --- a/chise.c +++ b/chise.c @@ -219,52 +219,64 @@ chise_format_char_id (CHISE_Char_ID cid, unsigned char *dest, int len) } } -int -chise_open_data_source (CHISE_DS *ds, CHISE_DS_Type type, char *location) +CHISE_DS* +chise_open_data_source (CHISE_DS_Type type, char *location) { + CHISE_DS *ds = (CHISE_DS*)malloc (sizeof (CHISE_DS)); + size_t len = strlen (location); + + if (ds == NULL) + return NULL; + ds->type = type; - ds->location = location; - return 0; + ds->location = (unsigned char*)malloc (len + 1); + if (ds->location == NULL) + { + free (ds); + return NULL; + } + strcpy (ds->location, location); + return ds; } int -chise_close_data_source (CHISE_DS *ds) +chise_ds_close (CHISE_DS *ds) { - ds->type = CHISE_DS_NONE; - ds->location = NULL; + if (ds->location != NULL) + free (ds->location); + free (ds); 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) +CHISE_Decoding_Table* +chise_ds_open_decoding_table (CHISE_DS *ds, const char *ccs, + DBTYPE real_subtype, + u_int32_t accessmask, int modemask) { return - chise_open_attribute_table (db, ds->location, + chise_open_attribute_table (ds, ccs, "system-char-id", real_subtype, accessmask, modemask); } int -chise_close_decoding_table (CHISE_Decoding_Table *db) +chise_dt_close (CHISE_Decoding_Table *table) { - if (db) - return chise_close_attribute_table (db); + if (table) + return chise_close_attribute_table (table); return -1; } CHISE_Char_ID -chise_dt_get_char (CHISE_Decoding_Table *db, int code_point) +chise_dt_get_char (CHISE_Decoding_Table *table, 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); + status = chise_get_attribute_table (table, key_buf, &valdatum); if (!status) { unsigned char *str @@ -277,7 +289,7 @@ chise_dt_get_char (CHISE_Decoding_Table *db, int code_point) } int -chise_dt_put_char (CHISE_Decoding_Table *db, +chise_dt_put_char (CHISE_Decoding_Table *table, int code_point, CHISE_Char_ID cid) { CHISE_Value valdatum; @@ -285,44 +297,43 @@ chise_dt_put_char (CHISE_Decoding_Table *db, sprintf(key_buf, "%d", code_point); chise_format_char_id (cid, val_buf, 8); - return chise_put_attribute_table (db, key_buf, val_buf); + return chise_put_attribute_table (table, key_buf, val_buf); } -int -chise_open_feature_table (CHISE_Feature_Table *db, - CHISE_DS *ds, const char *feature, - DBTYPE real_subtype, - u_int32_t accessmask, int modemask) +CHISE_Feature_Table* +chise_ds_open_feature_table (CHISE_DS *ds, const char *feature, + DBTYPE real_subtype, + u_int32_t accessmask, int modemask) { return - chise_open_attribute_table (db, ds->location, + chise_open_attribute_table (ds, "system-char-id", feature, real_subtype, accessmask, modemask); } int -chise_close_feature_table (CHISE_Feature_Table *db) +chise_ft_close (CHISE_Feature_Table *table) { - if (db) - return chise_close_attribute_table (db); + if (table) + return chise_close_attribute_table (table); return -1; } int -chise_ft_get_value (CHISE_Feature_Table *db, +chise_ft_get_value (CHISE_Feature_Table *table, 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); + return chise_get_attribute_table (table, key_buf, valdatum); } void -chise_ft_iterate (CHISE_Feature_Table *ft, - int (*func) (CHISE_Feature_Table *ft, +chise_ft_iterate (CHISE_Feature_Table *table, + int (*func) (CHISE_Feature_Table *table, CHISE_Char_ID cid, CHISE_Value *valdatum)) { DBT keydatum, valdatum; @@ -332,7 +343,7 @@ chise_ft_iterate (CHISE_Feature_Table *ft, xzero (keydatum); xzero (valdatum); - status = ft->dbp->cursor (ft->dbp, NULL, &dbcp, 0); + status = table->db->cursor (table->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)) @@ -342,29 +353,43 @@ chise_ft_iterate (CHISE_Feature_Table *ft, CHISE_Char_ID key = chise_char_id_parse_c_string (key_str, key_len); int ret; - if (ret = func (ft, key, &valdatum)) + if (ret = func (table, key, &valdatum)) break; } dbcp->c_close (dbcp); } -int -chise_open_attribute_table (CHISE_Attribute_Table *ft, - const char *db_dir, +CHISE_Attribute_Table* +chise_open_attribute_table (CHISE_DS *ds, const char *encoding, const char *feature, DBTYPE real_subtype, u_int32_t accessmask, int modemask) { + CHISE_Attribute_Table* table; + char *db_dir; DB* dbase; int status; int len, flen, i; int size; char *db_file_name, *sp; + if (ds == NULL) + return NULL; + + table = (CHISE_Attribute_Table*)malloc (sizeof (CHISE_Attribute_Table)); + if (table == NULL) + return NULL; + + table->ds = ds; + status = db_create (&dbase, NULL, 0); if (status) - return -1; + { + free (table); + return NULL; + } + db_dir = ds->location; len = strlen (db_dir); flen = strlen (feature); size = len + strlen (encoding) + flen * 3 + 4; @@ -397,25 +422,27 @@ chise_open_attribute_table (CHISE_Attribute_Table *ft, if (status) { dbase->close (dbase, 0); - return -1; + free (table); + return NULL; } - ft->dbp = dbase; - return 0; + table->db = dbase; + return table; } int -chise_close_attribute_table (CHISE_Attribute_Table *ft) +chise_close_attribute_table (CHISE_Attribute_Table *table) { - if (ft->dbp) + if (table->db) { - ft->dbp->sync (ft->dbp, 0); - ft->dbp->close (ft->dbp, 0); + table->db->sync (table->db, 0); + table->db->close (table->db, 0); } + free (table); return 0; } int -chise_get_attribute_table (CHISE_Attribute_Table *ft, +chise_get_attribute_table (CHISE_Attribute_Table *table, char *key, CHISE_Value *valdatum) { DBT keydatum; @@ -428,12 +455,12 @@ chise_get_attribute_table (CHISE_Attribute_Table *ft, keydatum.data = key; keydatum.size = strlen (key); - status = ft->dbp->get (ft->dbp, NULL, &keydatum, valdatum, 0); + status = table->db->get (table->db, NULL, &keydatum, valdatum, 0); return status; } int -chise_put_attribute_table (CHISE_Attribute_Table *ft, +chise_put_attribute_table (CHISE_Attribute_Table *table, char *key, char *value) { DBT keydatum, valdatum; @@ -449,6 +476,6 @@ chise_put_attribute_table (CHISE_Attribute_Table *ft, valdatum.data = value; valdatum.size = strlen (value); - status = ft->dbp->put (ft->dbp, NULL, &keydatum, &valdatum, 0); + status = table->db->put (table->db, NULL, &keydatum, &valdatum, 0); return status; }