+2003-08-17 MORIOKA Tomohiko <tomo@mousai.as.wakwak.ne.jp>
+
+ * sample.c (main): Modify for new API.
+
+ * chise.c (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.
+
+ * chise.h (struct CHISE_DS): Change type of location from <char*>
+ to <unsigned char*>.
+ (chise_open_data_source): Change interface.
+ (chise_ds_close): Renamed from `chise_close_data_source'.
+ (struct CHISE_Attribute_Table): Add new member `ds'; rename `dbp'
+ to `db'.
+ (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): Change interface.
+
2003-08-12 MORIOKA Tomohiko <tomo@mousai.as.wakwak.ne.jp>
* configure.in: Update version to 0.2.0.
}
}
-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
}
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;
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;
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))
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;
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;
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;
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;
}
typedef struct CHISE_DS
{
CHISE_DS_Type type;
- char *location;
+ unsigned char *location;
} CHISE_DS;
-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);
-int chise_close_data_source (CHISE_DS *ds);
+int chise_ds_close (CHISE_DS *ds);
typedef int CHISE_Char_ID;
typedef struct CHISE_Attribute_Table
{
- DB *dbp;
+ CHISE_DS *ds;
+ DB *db;
} CHISE_Attribute_Table;
typedef CHISE_Attribute_Table CHISE_Decoding_Table;
-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);
-int chise_close_decoding_table (CHISE_Decoding_Table *db);
+int chise_dt_close (CHISE_Decoding_Table *table);
-CHISE_Char_ID chise_dt_get_char (CHISE_Decoding_Table *db, int code_point);
+CHISE_Char_ID chise_dt_get_char (CHISE_Decoding_Table *table, int code_point);
-int chise_dt_put_char (CHISE_Decoding_Table *db,
+int chise_dt_put_char (CHISE_Decoding_Table *table,
int code_point, CHISE_Char_ID cid);
+
typedef CHISE_Attribute_Table CHISE_Feature_Table;
-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);
-int chise_close_feature_table (CHISE_Feature_Table *db);
+int chise_ft_close (CHISE_Feature_Table *table);
-int chise_ft_get_value (CHISE_Feature_Table *db,
+int chise_ft_get_value (CHISE_Feature_Table *table,
CHISE_Char_ID cid, CHISE_Value *valdatum);
void
-chise_ft_iterate (CHISE_Feature_Table *dbp,
- int (*func) (CHISE_Feature_Table *db,
+chise_ft_iterate (CHISE_Feature_Table *table,
+ int (*func) (CHISE_Feature_Table *table,
CHISE_Char_ID cid, CHISE_Value *valdatum));
char *key, CHISE_Value *valdatum);
-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);
+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);
-int chise_close_attribute_table (CHISE_Attribute_Table *db);
+int chise_close_attribute_table (CHISE_Attribute_Table *at);
-int chise_get_attribute_table (CHISE_Attribute_Table *db,
+int chise_get_attribute_table (CHISE_Attribute_Table *at,
char *key, CHISE_Value *valdatum);
#endif /* !_CHISE_H */
int
main (int argc, char* argv[])
{
- CHISE_DS ds;
- CHISE_Decoding_Table dt_daikanwa;
- CHISE_Feature_Table ft_ideographic_structure;
- CHISE_Feature_Table ft_ascii;
+ CHISE_DS *ds;
+ CHISE_Decoding_Table *dt_daikanwa;
+ CHISE_Feature_Table *ft_ideographic_structure;
+ CHISE_Feature_Table *ft_ascii;
int modemask;
int accessmask = 0;
DBTYPE real_subtype;
CHISE_Value value;
/* open a data-source */
- status = chise_open_data_source (&ds, CHISE_DS_Berkeley_DB, db_dir);
- if (status)
+ ds = chise_open_data_source (CHISE_DS_Berkeley_DB, db_dir);
+ if (ds == NULL)
{
- chise_close_data_source (&ds);
return -1;
}
accessmask = DB_RDONLY;
/* setup a decoding-table */
- status = chise_open_decoding_table (&dt_daikanwa, &ds,
- "=daikanwa",
- real_subtype,
- accessmask, modemask);
- if (status)
+ dt_daikanwa
+ = chise_ds_open_decoding_table (ds, "=daikanwa",
+ real_subtype, accessmask, modemask);
+ if (dt_daikanwa == NULL)
{
- chise_close_decoding_table (&dt_daikanwa);
- chise_close_data_source (&ds);
+ chise_ds_close (ds);
return -1;
}
/* get a character from the decoding-table */
- char_id = chise_dt_get_char (&dt_daikanwa, 364);
+ char_id = chise_dt_get_char (dt_daikanwa, 364);
/* close the decoding-table */
- chise_close_decoding_table (&dt_daikanwa);
+ chise_dt_close (dt_daikanwa);
/*
*/
/* setup a feature-table */
- status = chise_open_feature_table (&ft_ideographic_structure, &ds,
- "ideographic-structure",
- real_subtype, accessmask, modemask);
- if (status)
+ ft_ideographic_structure
+ = chise_ds_open_feature_table (ds, "ideographic-structure",
+ real_subtype, accessmask, modemask);
+ if (ft_ideographic_structure == NULL)
{
- chise_close_feature_table (&ft_ideographic_structure);
- chise_close_data_source (&ds);
+ chise_ds_close (ds);
return -1;
}
/* setup a feature-table */
- status = chise_open_feature_table (&ft_ascii, &ds,
- "ascii",
- real_subtype, accessmask, modemask);
- if (status)
+ ft_ascii = chise_ds_open_feature_table (ds, "ascii",
+ real_subtype, accessmask, modemask);
+ if (ft_ascii == NULL)
{
- chise_close_feature_table (&ft_ascii);
- chise_close_data_source (&ds);
+ chise_ds_close (ds);
return -1;
}
/* get a feature-value of the character */
- status = chise_ft_get_value (&ft_ideographic_structure, char_id, &value);
+ status = chise_ft_get_value (ft_ideographic_structure, char_id, &value);
if (!status)
printf ("#x%X => %s\n", char_id, chise_value_to_c_string(&value));
else
printf ("#x%X (%d)\n", char_id, status);
- chise_ft_iterate (&ft_ascii, &test_map_func);
- chise_ft_iterate (&ft_ideographic_structure, &test_map_func);
+ chise_ft_iterate (ft_ascii, &test_map_func);
+ chise_ft_iterate (ft_ideographic_structure, &test_map_func);
/* close the feature-table */
- chise_close_feature_table (&ft_ideographic_structure);
-
- chise_close_feature_table (&ft_ascii);
-
+ chise_ft_close (ft_ideographic_structure);
+ chise_ft_close (ft_ascii);
/* close the data-source */
- chise_close_data_source (&ds);
+ chise_ds_close (ds);
}