6 /* original in mysql, strings/strnlen.c.
7 uint strnlen(register const char *s, register uint maxlen)
9 const char *end= (const char *)memchr(s, '\0', maxlen);
10 return end ? (uint) (end - s) : maxlen;
14 strnlen (register const char *s, register int maxlen)
16 const char *end= (const char *)memchr(s, '\0', maxlen);
17 return end ? (int) (end - s) : maxlen;
23 typedef struct CHISE_Attribute_Table
27 } CHISE_Attribute_Table;
29 CHISE_Attribute_Table*
30 chise_open_attribute_table (CHISE_DS *ds,
31 const char *encoding, const char *feature,
33 u_int32_t accessmask, int modemask);
35 int chise_close_attribute_table (CHISE_Attribute_Table *at);
37 int chise_get_attribute_table (CHISE_Attribute_Table *at,
38 char *key, CHISE_Value *valdatum);
41 #define xzero(lvalue) ((void) memset (&(lvalue), '\0', sizeof (lvalue)))
44 chise_char_id_parse_c_string (unsigned char *str, int len)
48 if ( (len >= 2) && (str[i++] == '?') )
50 unsigned char c = str[i++];
67 return c & (0x80 | 0x1F);
101 if (counter + 2 <= len)
105 for (j = 0; j < counter; j++)
106 cid = (cid << 6) | (str[j + i] & 0x3F);
114 chise_format_char_id (CHISE_Char_ID cid, unsigned char *dest, int len)
126 else if (cid == '\n')
133 else if (cid == '\r')
140 else if (cid == 0x1C)
149 else if (cid <= 0x1F)
153 dest[i++] = '@' + cid;
157 else if ( (cid == ' ') || (cid == '"') ||
158 (cid == '#') || (cid == '\'') ||
159 (cid == '(') || (cid == ')') ||
160 (cid == ',') || (cid == '.') ||
161 (cid == ';') || (cid == '?') ||
162 (cid == '[') || (cid == '\\') ||
163 (cid == ']') || (cid == '`') )
170 else if (cid <= 0x7E)
176 else if (cid == 0x7F)
184 else if (cid <= 0x9F)
188 dest[i++] = ((cid + '@') >> 6) | 0xC0;
189 dest[i++] = ((cid + '@') & 0x3F) | 0x80;
193 else if (cid <= 0x7FF)
195 dest[i++] = (cid >> 6) | 0xC0;
196 dest[i++] = (cid & 0x3F) | 0x80;
200 else if (cid <= 0xFFFF)
202 dest[i++] = (cid >> 12) | 0xE0;
203 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
204 dest[i++]= (cid & 0x3F) | 0x80;
208 else if (cid <= 0x1FFFFF)
210 dest[i++]= (cid >> 18) | 0xF0;
211 dest[i++]= ((cid >> 12) & 0x3F) | 0x80;
212 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
213 dest[i++]= (cid & 0x3F) | 0x80;
217 else if (cid <= 0x3FFFFFF)
219 dest[i++]= (cid >> 24) | 0xF8;
220 dest[i++]= ((cid >> 18) & 0x3F) | 0x80;
221 dest[i++]= ((cid >> 12) & 0x3F) | 0x80;
222 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
223 dest[i++]= (cid & 0x3F) | 0x80;
229 dest[i++]= (cid >> 30) | 0xFC;
230 dest[i++]= ((cid >> 24) & 0x3F) | 0x80;
231 dest[i++]= ((cid >> 18) & 0x3F) | 0x80;
232 dest[i++]= ((cid >> 12) & 0x3F) | 0x80;
233 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
234 dest[i++]= (cid & 0x3F) | 0x80;
244 unsigned char *location;
248 chise_open_data_source (CHISE_DS_Type type, char *location)
250 CHISE_DS *ds = (CHISE_DS*)malloc (sizeof (CHISE_DS));
251 size_t len = strlen (location);
257 ds->location = (unsigned char*)malloc (len + 1);
258 if (ds->location == NULL)
263 strcpy (ds->location, location);
268 chise_ds_close (CHISE_DS *ds)
270 if (ds->location != NULL)
277 struct CHISE_Decoding_Table
283 CHISE_Decoding_Table*
284 chise_ds_open_decoding_table (CHISE_DS *ds, const char *ccs,
286 u_int32_t accessmask, int modemask)
289 (CHISE_Decoding_Table*)
290 chise_open_attribute_table (ds,
291 ccs, "system-char-id",
292 real_subtype, accessmask, modemask);
296 chise_dt_close (CHISE_Decoding_Table *table)
299 return chise_close_attribute_table ((CHISE_Attribute_Table*)table);
304 chise_dt_get_char (CHISE_Decoding_Table *table, int code_point)
306 CHISE_Value valdatum;
310 sprintf(key_buf, "%d", code_point);
311 status = chise_get_attribute_table ((CHISE_Attribute_Table*)table,
316 = (unsigned char *)chise_value_data (&valdatum);
317 int len = strnlen (str, chise_value_size (&valdatum));
319 return chise_char_id_parse_c_string (str, len);
325 chise_dt_put_char (CHISE_Decoding_Table *table,
326 int code_point, CHISE_Char_ID cid)
328 CHISE_Value valdatum;
329 char key_buf[16], val_buf[8];
331 sprintf(key_buf, "%d", code_point);
332 chise_format_char_id (cid, val_buf, 8);
333 return chise_put_attribute_table (table, key_buf, val_buf);
337 struct CHISE_Feature_Table
344 chise_ds_open_feature_table (CHISE_DS *ds, const char *feature,
346 u_int32_t accessmask, int modemask)
349 (CHISE_Feature_Table*)
350 chise_open_attribute_table (ds,
351 "system-char-id", feature,
352 real_subtype, accessmask, modemask);
356 chise_ft_close (CHISE_Feature_Table *table)
359 return chise_close_attribute_table ((CHISE_Attribute_Table*)table);
364 chise_ft_get_value (CHISE_Feature_Table *table,
365 CHISE_Char_ID cid, CHISE_Value *valdatum)
367 unsigned char key_buf[8];
369 chise_format_char_id (cid, key_buf, 8);
370 return chise_get_attribute_table ((CHISE_Attribute_Table*)table,
375 chise_ft_iterate (CHISE_Feature_Table *table,
376 int (*func) (CHISE_Feature_Table *table,
377 CHISE_Char_ID cid, CHISE_Value *valdatum))
379 DBT keydatum, valdatum;
386 status = table->db->cursor (table->db, NULL, &dbcp, 0);
387 for (status = dbcp->c_get (dbcp, &keydatum, &valdatum, DB_FIRST);
389 status = dbcp->c_get (dbcp, &keydatum, &valdatum, DB_NEXT))
391 unsigned char *key_str = (unsigned char *)keydatum.data;
392 int key_len = strnlen (key_str, keydatum.size);
393 CHISE_Char_ID key = chise_char_id_parse_c_string (key_str, key_len);
396 if (ret = func (table, key, &valdatum))
399 dbcp->c_close (dbcp);
402 CHISE_Attribute_Table*
403 chise_open_attribute_table (CHISE_DS *ds,
404 const char *encoding, const char *feature,
406 u_int32_t accessmask, int modemask)
408 CHISE_Attribute_Table* table;
414 char *db_file_name, *sp;
419 table = (CHISE_Attribute_Table*)malloc (sizeof (CHISE_Attribute_Table));
425 status = db_create (&dbase, NULL, 0);
432 db_dir = ds->location;
433 len = strlen (db_dir);
434 flen = strlen (feature);
435 size = len + strlen (encoding) + flen * 3 + 4;
436 db_file_name = alloca (size);
437 strcpy (db_file_name, db_dir);
438 if (db_file_name[len - 1] != '/')
440 db_file_name[len++] = '/';
441 db_file_name[len] = '\0';
443 strcat (db_file_name, encoding);
444 strcat (db_file_name, "/");
445 /* strcat (db_file_name, feature); */
446 sp = &db_file_name[strlen (db_file_name)];
447 for (i = 0; i < flen; i++)
451 if ( (c == '/') || (c == '%') )
453 sprintf (sp, "%%%02X", c);
460 status = dbase->open (dbase, db_file_name, NULL,
461 real_subtype, accessmask, modemask);
464 dbase->close (dbase, 0);
473 chise_close_attribute_table (CHISE_Attribute_Table *table)
477 table->db->sync (table->db, 0);
478 table->db->close (table->db, 0);
485 chise_get_attribute_table (CHISE_Attribute_Table *table,
486 char *key, CHISE_Value *valdatum)
491 /* DB Version 2 requires DBT's to be zeroed before use. */
496 keydatum.size = strlen (key);
498 status = table->db->get (table->db, NULL, &keydatum, valdatum, 0);
503 chise_put_attribute_table (CHISE_Attribute_Table *table,
504 char *key, char *value)
506 DBT keydatum, valdatum;
509 /* DB Version 2 requires DBT's to be zeroed before use. */
514 keydatum.size = strlen (key);
516 valdatum.data = value;
517 valdatum.size = strlen (value);
519 status = table->db->put (table->db, NULL, &keydatum, &valdatum, 0);