From: tomo Date: Fri, 3 Oct 2003 03:34:42 +0000 (+0000) Subject: (strnlen): Separated to sysdep.h; include sysdep.h instead. X-Git-Tag: b1-r0_2_0-pre6~5 X-Git-Url: http://git.chise.org/gitweb/?a=commitdiff_plain;h=54b6a03917df95f5ffa15c2acb7e433527a917fa;p=chise%2Flibchise.git (strnlen): Separated to sysdep.h; include sysdep.h instead. (CHISE_Attribute_Table_open): Renamed from `chise_open_attribute_table'. (CHISE_Attribute_Table_close): Renamed from `chise_close_attribute_table'. (chise_attribute_table_get): Renamed from `chise_get_attribute_table'. (chise_attribute_table_put): Renamed from `chise_put_attribute_table'. (CHISE_DS_open): Renamed from `chise_open_data_source'. (CHISE_DS_close): Renamed from `chise_ds_close'. (chise_feature_setup_db): New function. (chise_char_load_feature_value): Use `chise_feature_setup_db'. (chise_char_gets_feature_value): Likewise. (chise_char_feature_value_iterate): Likewise. --- diff --git a/chise.c b/chise.c index 3da6fd3..43c60e7 100644 --- a/chise.c +++ b/chise.c @@ -1,23 +1,8 @@ +#include #ifdef HAVE_CONFIG_H # include "config.h" #endif - -#ifndef HAVE_STRNLEN -/* original in mysql, strings/strnlen.c. -uint strnlen(register const char *s, register uint maxlen) -{ - const char *end= (const char *)memchr(s, '\0', maxlen); - return end ? (uint) (end - s) : maxlen; -} -*/ -static inline int -strnlen (register const char *s, register int maxlen) -{ - const char *end= (const char *)memchr(s, '\0', maxlen); - return end ? (int) (end - s) : maxlen; -} -#endif - +#include "sysdep.h" #include "chise.h" #include "chise-name.h" @@ -27,6 +12,7 @@ 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); @@ -36,218 +22,27 @@ int chise_ccst_close (CHISE_CCS_Table *table); typedef DB CHISE_Attribute_Table; CHISE_Attribute_Table* -chise_open_attribute_table (const unsigned char *db_dir, +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_close_attribute_table (CHISE_Attribute_Table *db); +int CHISE_Attribute_Table_close (CHISE_Attribute_Table *db); -int chise_get_attribute_table (CHISE_Attribute_Table *db, +int chise_attribute_table_get (CHISE_Attribute_Table *db, char *key, CHISE_Value *valdatum); -int chise_put_attribute_table (CHISE_Attribute_Table *db, +int chise_attribute_table_put (CHISE_Attribute_Table *db, char *key, unsigned char *value); #define xzero(lvalue) ((void) memset (&(lvalue), '\0', sizeof (lvalue))) CHISE_Char_ID -chise_char_id_parse_c_string (unsigned char *str, int len) -{ - int i = 0; - - if ( (len >= 2) && (str[i++] == '?') ) - { - unsigned char c = str[i++]; - int counter; - CHISE_Char_ID cid; - - if (c == '\\') - { - if (len < 3) - return -1; - c = str[i++]; - if (c == '^') - { - if (len < 4) - return -1; - c = str[i++]; - if (c == '?') - return 0x7F; - else - return c & (0x80 | 0x1F); - } - } - if ( c < 0xC0 ) - { - cid = c; - counter = 0; - } - else if ( c < 0xE0 ) - { - cid = c & 0x1f; - counter = 1; - } - else if ( c < 0xF0 ) - { - cid = c & 0x0f; - counter = 2; - } - else if ( c < 0xF8 ) - { - cid = c & 0x07; - counter = 3; - } - else if ( c < 0xFC ) - { - cid = c & 0x03; - counter = 4; - } - else - { - cid = c & 0x01; - counter = 5; - } - - if (counter + 2 <= len) - { - int j; - - for (j = 0; j < counter; j++) - cid = (cid << 6) | (str[j + i] & 0x3F); - return cid; - } - } - return -1; -} +chise_char_id_parse_c_string (unsigned char *str, size_t len); int -chise_format_char_id (CHISE_Char_ID cid, unsigned char *dest, int len) -{ - int i = 0; - - dest[i++] = '?'; - if (cid == '\t') - { - dest[i++] = '\\'; - dest[i++] = 't'; - dest[i] = '\0'; - return i; - } - else if (cid == '\n') - { - dest[i++] = '\\'; - dest[i++] = 'n'; - dest[i] = '\0'; - return i; - } - else if (cid == '\r') - { - dest[i++] = '\\'; - dest[i++] = 'r'; - dest[i] = '\0'; - return i; - } - else if (cid == 0x1C) - { - dest[i++] = '\\'; - dest[i++] = '^'; - dest[i++] = '\\'; - dest[i++] = '\\'; - dest[i] = '\0'; - return i; - } - else if (cid <= 0x1F) - { - dest[i++] = '\\'; - dest[i++] = '^'; - dest[i++] = '@' + cid; - dest[i] = '\0'; - return i; - } - else if ( (cid == ' ') || (cid == '"') || - (cid == '#') || (cid == '\'') || - (cid == '(') || (cid == ')') || - (cid == ',') || (cid == '.') || - (cid == ';') || (cid == '?') || - (cid == '[') || (cid == '\\') || - (cid == ']') || (cid == '`') ) - { - dest[i++] = '\\'; - dest[i++] = cid; - dest[i] = '\0'; - return i; - } - else if (cid <= 0x7E) - { - dest[i++] = cid; - dest[i] = '\0'; - return i; - } - else if (cid == 0x7F) - { - dest[i++] = '\\'; - dest[i++] = '^'; - dest[i++] = '?'; - dest[i] = '\0'; - return i; - } - else if (cid <= 0x9F) - { - dest[i++] = '\\'; - dest[i++] = '^'; - dest[i++] = ((cid + '@') >> 6) | 0xC0; - dest[i++] = ((cid + '@') & 0x3F) | 0x80; - dest[i] = '\0'; - return i; - } - else if (cid <= 0x7FF) - { - dest[i++] = (cid >> 6) | 0xC0; - dest[i++] = (cid & 0x3F) | 0x80; - dest[i] = '\0'; - return i; - } - else if (cid <= 0xFFFF) - { - dest[i++] = (cid >> 12) | 0xE0; - dest[i++]= ((cid >> 6) & 0x3F) | 0x80; - dest[i++]= (cid & 0x3F) | 0x80; - dest[i] = '\0'; - return i; - } - else if (cid <= 0x1FFFFF) - { - dest[i++]= (cid >> 18) | 0xF0; - dest[i++]= ((cid >> 12) & 0x3F) | 0x80; - dest[i++]= ((cid >> 6) & 0x3F) | 0x80; - dest[i++]= (cid & 0x3F) | 0x80; - dest[i] = '\0'; - return i; - } - else if (cid <= 0x3FFFFFF) - { - dest[i++]= (cid >> 24) | 0xF8; - dest[i++]= ((cid >> 18) & 0x3F) | 0x80; - dest[i++]= ((cid >> 12) & 0x3F) | 0x80; - dest[i++]= ((cid >> 6) & 0x3F) | 0x80; - dest[i++]= (cid & 0x3F) | 0x80; - dest[i] = '\0'; - return i; - } - else - { - dest[i++]= (cid >> 30) | 0xFC; - dest[i++]= ((cid >> 24) & 0x3F) | 0x80; - dest[i++]= ((cid >> 18) & 0x3F) | 0x80; - dest[i++]= ((cid >> 12) & 0x3F) | 0x80; - dest[i++]= ((cid >> 6) & 0x3F) | 0x80; - dest[i++]= (cid & 0x3F) | 0x80; - dest[i] = '\0'; - return i; - } -} +chise_format_char_id (CHISE_Char_ID cid, unsigned char *dest, size_t len); struct CHISE_DS @@ -261,8 +56,8 @@ struct CHISE_DS }; CHISE_DS* -chise_open_data_source (CHISE_DS_Type type, char *location, - DBTYPE subtype, int modemask) +CHISE_DS_open (CHISE_DS_Type type, char *location, + DBTYPE subtype, int modemask) { CHISE_DS *ds = (CHISE_DS*)malloc (sizeof (CHISE_DS)); size_t len = strlen (location); @@ -299,7 +94,7 @@ chise_open_data_source (CHISE_DS_Type type, char *location, } int -chise_ds_close (CHISE_DS *ds) +CHISE_DS_close (CHISE_DS *ds) { if (ds->location != NULL) free (ds->location); @@ -399,7 +194,7 @@ chise_ft_close (CHISE_Feature_Table *table) if (table->db == NULL) status = -1; else - status = chise_close_attribute_table (table->db); + status = CHISE_Attribute_Table_close (table->db); if (table->name == NULL) status = -1; @@ -412,6 +207,42 @@ chise_ft_close (CHISE_Feature_Table *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, @@ -419,21 +250,10 @@ chise_char_load_feature_value (CHISE_Char_ID cid, { unsigned char key_buf[8]; - if (table->db == NULL) - { - CHISE_DS *ds = table->ds; - - table->db = chise_open_attribute_table (ds->location, - "system-char-id", table->name, - ds->subtype, - DB_RDONLY, ds->modemask); - if (table->db == NULL) - return -1; - table->access = DB_RDONLY; - } + if (chise_feature_setup_db (table, 0)) + return -1; chise_format_char_id (cid, key_buf, 8); - return chise_get_attribute_table (table->db, - key_buf, valdatum); + return chise_attribute_table_get (table->db, key_buf, valdatum); } unsigned char* @@ -445,20 +265,10 @@ chise_char_gets_feature_value (CHISE_Char_ID cid, unsigned char key_buf[8]; int status; - if (table->db == NULL) - { - CHISE_DS *ds = table->ds; - - table->db = chise_open_attribute_table (ds->location, - "system-char-id", table->name, - ds->subtype, - DB_RDONLY, ds->modemask); - if (table->db == NULL) - return NULL; - table->access = DB_RDONLY; - } + if (chise_feature_setup_db (table, 0)) + return NULL; chise_format_char_id (cid, key_buf, 8); - status = chise_get_attribute_table (table->db, + status = chise_attribute_table_get (table->db, key_buf, &valdatum); if (status) return NULL; @@ -479,19 +289,8 @@ chise_char_feature_value_iterate (CHISE_Feature feature, DBC *dbcp; int status; - if (feature->db == NULL) - { - CHISE_DS *ds = feature->ds; - - feature->db - = chise_open_attribute_table (ds->location, - "system-char-id", feature->name, - ds->subtype, - DB_RDONLY, ds->modemask); - if (feature->db == NULL) - return -1; - feature->access = DB_RDONLY; - } + if (chise_feature_setup_db (feature, 0)) + return -1; xzero (keydatum); xzero (valdatum); @@ -503,9 +302,9 @@ chise_char_feature_value_iterate (CHISE_Feature feature, 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; + int ret = func (key, feature, &valdatum); - if (ret = func (key, feature, &valdatum)) + if (ret) break; } dbcp->c_close (dbcp); @@ -558,7 +357,7 @@ chise_ccst_close (CHISE_CCS_Table *table) if (table->db == NULL) status = 0; else - status = chise_close_attribute_table (table->db); + status = CHISE_Attribute_Table_close (table->db); if (table->name == NULL) status = -1; @@ -579,7 +378,7 @@ chise_ccs_sync (CHISE_CCS ccs) if (ccs->db == NULL) status = 0; else - status = chise_close_attribute_table (ccs->db); + status = CHISE_Attribute_Table_close (ccs->db); ccs->db = NULL; ccs->access = 0; return status; @@ -596,7 +395,7 @@ chise_ccs_decode (CHISE_CCS ccs, int code_point) { CHISE_DS *ds = ccs->ds; - ccs->db = chise_open_attribute_table (ds->location, + ccs->db = CHISE_Attribute_Table_open (ds->location, ccs->name, "system-char-id", ds->subtype, DB_RDONLY, ds->modemask); @@ -606,7 +405,7 @@ chise_ccs_decode (CHISE_CCS ccs, int code_point) } sprintf(key_buf, "%d", code_point); - status = chise_get_attribute_table (ccs->db, key_buf, &valdatum); + status = chise_attribute_table_get (ccs->db, key_buf, &valdatum); if (!status) { unsigned char *str @@ -622,14 +421,13 @@ int chise_ccs_set_decoded_char (CHISE_CCS ccs, int code_point, CHISE_Char_ID cid) { - CHISE_Value valdatum; char key_buf[16], val_buf[8]; if ((ccs->access & DB_CREATE) == 0) { if (ccs->db != NULL) { - chise_close_attribute_table (ccs->db); + CHISE_Attribute_Table_close (ccs->db); ccs->db = NULL; } ccs->access = 0; @@ -638,10 +436,10 @@ chise_ccs_set_decoded_char (CHISE_CCS ccs, { CHISE_DS *ds = ccs->ds; - ccs->db = chise_open_attribute_table (ds->location, - ccs->name, "system-char-id", - ds->subtype, - DB_CREATE, ds->modemask); + 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; @@ -649,12 +447,12 @@ chise_ccs_set_decoded_char (CHISE_CCS ccs, sprintf(key_buf, "%d", code_point); chise_format_char_id (cid, val_buf, 8); - return chise_put_attribute_table (ccs->db, key_buf, val_buf); + return chise_attribute_table_put (ccs->db, key_buf, val_buf); } CHISE_Attribute_Table* -chise_open_attribute_table (const unsigned char *db_dir, +CHISE_Attribute_Table_open (const unsigned char *db_dir, const char *encoding, const char *feature, DBTYPE real_subtype, u_int32_t accessmask, int modemask) @@ -707,7 +505,7 @@ chise_open_attribute_table (const unsigned char *db_dir, } int -chise_close_attribute_table (CHISE_Attribute_Table *db) +CHISE_Attribute_Table_close (CHISE_Attribute_Table *db) { if (db) { @@ -718,7 +516,7 @@ chise_close_attribute_table (CHISE_Attribute_Table *db) } int -chise_get_attribute_table (CHISE_Attribute_Table *db, +chise_attribute_table_get (CHISE_Attribute_Table *db, char *key, CHISE_Value *valdatum) { DBT keydatum; @@ -736,7 +534,7 @@ chise_get_attribute_table (CHISE_Attribute_Table *db, } int -chise_put_attribute_table (CHISE_Attribute_Table *db, +chise_attribute_table_put (CHISE_Attribute_Table *db, char *key, unsigned char *value) { DBT keydatum, valdatum; @@ -755,3 +553,200 @@ chise_put_attribute_table (CHISE_Attribute_Table *db, 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; + + if ( (len >= 2) && (str[i++] == '?') ) + { + unsigned char c = str[i++]; + int counter; + CHISE_Char_ID cid; + + if (c == '\\') + { + if (len < 3) + return -1; + c = str[i++]; + if (c == '^') + { + if (len < 4) + return -1; + c = str[i++]; + if (c == '?') + return 0x7F; + else + return c & (0x80 | 0x1F); + } + } + if ( c < 0xC0 ) + { + cid = c; + counter = 0; + } + else if ( c < 0xE0 ) + { + cid = c & 0x1f; + counter = 1; + } + else if ( c < 0xF0 ) + { + cid = c & 0x0f; + counter = 2; + } + else if ( c < 0xF8 ) + { + cid = c & 0x07; + counter = 3; + } + else if ( c < 0xFC ) + { + cid = c & 0x03; + counter = 4; + } + else + { + cid = c & 0x01; + counter = 5; + } + + if (counter + 2 <= len) + { + int j; + + for (j = 0; j < counter; j++) + cid = (cid << 6) | (str[j + i] & 0x3F); + return cid; + } + } + return -1; +} + +int +chise_format_char_id (CHISE_Char_ID cid, unsigned char *dest, size_t len) +{ + int i = 0; + + dest[i++] = '?'; + if (cid == '\t') + { + dest[i++] = '\\'; + dest[i++] = 't'; + dest[i] = '\0'; + return i; + } + else if (cid == '\n') + { + dest[i++] = '\\'; + dest[i++] = 'n'; + dest[i] = '\0'; + return i; + } + else if (cid == '\r') + { + dest[i++] = '\\'; + dest[i++] = 'r'; + dest[i] = '\0'; + return i; + } + else if (cid == 0x1C) + { + dest[i++] = '\\'; + dest[i++] = '^'; + dest[i++] = '\\'; + dest[i++] = '\\'; + dest[i] = '\0'; + return i; + } + else if (cid <= 0x1F) + { + dest[i++] = '\\'; + dest[i++] = '^'; + dest[i++] = '@' + cid; + dest[i] = '\0'; + return i; + } + else if ( (cid == ' ') || (cid == '"') || + (cid == '#') || (cid == '\'') || + (cid == '(') || (cid == ')') || + (cid == ',') || (cid == '.') || + (cid == ';') || (cid == '?') || + (cid == '[') || (cid == '\\') || + (cid == ']') || (cid == '`') ) + { + dest[i++] = '\\'; + dest[i++] = cid; + dest[i] = '\0'; + return i; + } + else if (cid <= 0x7E) + { + dest[i++] = cid; + dest[i] = '\0'; + return i; + } + else if (cid == 0x7F) + { + dest[i++] = '\\'; + dest[i++] = '^'; + dest[i++] = '?'; + dest[i] = '\0'; + return i; + } + else if (cid <= 0x9F) + { + dest[i++] = '\\'; + dest[i++] = '^'; + dest[i++] = ((cid + '@') >> 6) | 0xC0; + dest[i++] = ((cid + '@') & 0x3F) | 0x80; + dest[i] = '\0'; + return i; + } + else if (cid <= 0x7FF) + { + dest[i++] = (cid >> 6) | 0xC0; + dest[i++] = (cid & 0x3F) | 0x80; + dest[i] = '\0'; + return i; + } + else if (cid <= 0xFFFF) + { + dest[i++] = (cid >> 12) | 0xE0; + dest[i++]= ((cid >> 6) & 0x3F) | 0x80; + dest[i++]= (cid & 0x3F) | 0x80; + dest[i] = '\0'; + return i; + } + else if (cid <= 0x1FFFFF) + { + dest[i++]= (cid >> 18) | 0xF0; + dest[i++]= ((cid >> 12) & 0x3F) | 0x80; + dest[i++]= ((cid >> 6) & 0x3F) | 0x80; + dest[i++]= (cid & 0x3F) | 0x80; + dest[i] = '\0'; + return i; + } + else if (cid <= 0x3FFFFFF) + { + dest[i++]= (cid >> 24) | 0xF8; + dest[i++]= ((cid >> 18) & 0x3F) | 0x80; + dest[i++]= ((cid >> 12) & 0x3F) | 0x80; + dest[i++]= ((cid >> 6) & 0x3F) | 0x80; + dest[i++]= (cid & 0x3F) | 0x80; + dest[i] = '\0'; + return i; + } + else + { + dest[i++]= (cid >> 30) | 0xFC; + dest[i++]= ((cid >> 24) & 0x3F) | 0x80; + dest[i++]= ((cid >> 18) & 0x3F) | 0x80; + dest[i++]= ((cid >> 12) & 0x3F) | 0x80; + dest[i++]= ((cid >> 6) & 0x3F) | 0x80; + dest[i++]= (cid & 0x3F) | 0x80; + dest[i] = '\0'; + return i; + } +}