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 CHISE_Attribute_Table*
24 chise_open_attribute_table (CHISE_DS *ds,
25 const char *encoding, const char *feature,
27 u_int32_t accessmask, int modemask);
29 int chise_close_attribute_table (CHISE_Attribute_Table *at);
31 int chise_get_attribute_table (CHISE_Attribute_Table *at,
32 char *key, CHISE_Value *valdatum);
35 #define xzero(lvalue) ((void) memset (&(lvalue), '\0', sizeof (lvalue)))
38 chise_char_id_parse_c_string (unsigned char *str, int len)
42 if ( (len >= 2) && (str[i++] == '?') )
44 unsigned char c = str[i++];
61 return c & (0x80 | 0x1F);
95 if (counter + 2 <= len)
99 for (j = 0; j < counter; j++)
100 cid = (cid << 6) | (str[j + i] & 0x3F);
108 chise_format_char_id (CHISE_Char_ID cid, unsigned char *dest, int len)
120 else if (cid == '\n')
127 else if (cid == '\r')
134 else if (cid == 0x1C)
143 else if (cid <= 0x1F)
147 dest[i++] = '@' + cid;
151 else if ( (cid == ' ') || (cid == '"') ||
152 (cid == '#') || (cid == '\'') ||
153 (cid == '(') || (cid == ')') ||
154 (cid == ',') || (cid == '.') ||
155 (cid == ';') || (cid == '?') ||
156 (cid == '[') || (cid == '\\') ||
157 (cid == ']') || (cid == '`') )
164 else if (cid <= 0x7E)
170 else if (cid == 0x7F)
178 else if (cid <= 0x9F)
182 dest[i++] = ((cid + '@') >> 6) | 0xC0;
183 dest[i++] = ((cid + '@') & 0x3F) | 0x80;
187 else if (cid <= 0x7FF)
189 dest[i++] = (cid >> 6) | 0xC0;
190 dest[i++] = (cid & 0x3F) | 0x80;
194 else if (cid <= 0xFFFF)
196 dest[i++] = (cid >> 12) | 0xE0;
197 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
198 dest[i++]= (cid & 0x3F) | 0x80;
202 else if (cid <= 0x1FFFFF)
204 dest[i++]= (cid >> 18) | 0xF0;
205 dest[i++]= ((cid >> 12) & 0x3F) | 0x80;
206 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
207 dest[i++]= (cid & 0x3F) | 0x80;
211 else if (cid <= 0x3FFFFFF)
213 dest[i++]= (cid >> 24) | 0xF8;
214 dest[i++]= ((cid >> 18) & 0x3F) | 0x80;
215 dest[i++]= ((cid >> 12) & 0x3F) | 0x80;
216 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
217 dest[i++]= (cid & 0x3F) | 0x80;
223 dest[i++]= (cid >> 30) | 0xFC;
224 dest[i++]= ((cid >> 24) & 0x3F) | 0x80;
225 dest[i++]= ((cid >> 18) & 0x3F) | 0x80;
226 dest[i++]= ((cid >> 12) & 0x3F) | 0x80;
227 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
228 dest[i++]= (cid & 0x3F) | 0x80;
238 unsigned char *location;
242 chise_open_data_source (CHISE_DS_Type type, char *location)
244 CHISE_DS *ds = (CHISE_DS*)malloc (sizeof (CHISE_DS));
245 size_t len = strlen (location);
251 ds->location = (unsigned char*)malloc (len + 1);
252 if (ds->location == NULL)
257 strcpy (ds->location, location);
262 chise_ds_close (CHISE_DS *ds)
264 if (ds->location != NULL)
271 CHISE_Decoding_Table*
272 chise_ds_open_decoding_table (CHISE_DS *ds, const char *ccs,
274 u_int32_t accessmask, int modemask)
277 chise_open_attribute_table (ds,
278 ccs, "system-char-id",
279 real_subtype, accessmask, modemask);
283 chise_dt_close (CHISE_Decoding_Table *table)
286 return chise_close_attribute_table (table);
291 chise_dt_get_char (CHISE_Decoding_Table *table, int code_point)
293 CHISE_Value valdatum;
297 sprintf(key_buf, "%d", code_point);
298 status = chise_get_attribute_table (table, key_buf, &valdatum);
302 = (unsigned char *)chise_value_data (&valdatum);
303 int len = strnlen (str, chise_value_size (&valdatum));
305 return chise_char_id_parse_c_string (str, len);
311 chise_dt_put_char (CHISE_Decoding_Table *table,
312 int code_point, CHISE_Char_ID cid)
314 CHISE_Value valdatum;
315 char key_buf[16], val_buf[8];
317 sprintf(key_buf, "%d", code_point);
318 chise_format_char_id (cid, val_buf, 8);
319 return chise_put_attribute_table (table, key_buf, val_buf);
325 chise_ds_open_feature_table (CHISE_DS *ds, const char *feature,
327 u_int32_t accessmask, int modemask)
330 chise_open_attribute_table (ds,
331 "system-char-id", feature,
332 real_subtype, accessmask, modemask);
336 chise_ft_close (CHISE_Feature_Table *table)
339 return chise_close_attribute_table (table);
344 chise_ft_get_value (CHISE_Feature_Table *table,
345 CHISE_Char_ID cid, CHISE_Value *valdatum)
347 unsigned char key_buf[8];
349 chise_format_char_id (cid, key_buf, 8);
350 return chise_get_attribute_table (table, key_buf, valdatum);
354 chise_ft_iterate (CHISE_Feature_Table *table,
355 int (*func) (CHISE_Feature_Table *table,
356 CHISE_Char_ID cid, CHISE_Value *valdatum))
358 DBT keydatum, valdatum;
365 status = table->db->cursor (table->db, NULL, &dbcp, 0);
366 for (status = dbcp->c_get (dbcp, &keydatum, &valdatum, DB_FIRST);
368 status = dbcp->c_get (dbcp, &keydatum, &valdatum, DB_NEXT))
370 unsigned char *key_str = (unsigned char *)keydatum.data;
371 int key_len = strnlen (key_str, keydatum.size);
372 CHISE_Char_ID key = chise_char_id_parse_c_string (key_str, key_len);
375 if (ret = func (table, key, &valdatum))
378 dbcp->c_close (dbcp);
381 CHISE_Attribute_Table*
382 chise_open_attribute_table (CHISE_DS *ds,
383 const char *encoding, const char *feature,
385 u_int32_t accessmask, int modemask)
387 CHISE_Attribute_Table* table;
393 char *db_file_name, *sp;
398 table = (CHISE_Attribute_Table*)malloc (sizeof (CHISE_Attribute_Table));
404 status = db_create (&dbase, NULL, 0);
411 db_dir = ds->location;
412 len = strlen (db_dir);
413 flen = strlen (feature);
414 size = len + strlen (encoding) + flen * 3 + 4;
415 db_file_name = alloca (size);
416 strcpy (db_file_name, db_dir);
417 if (db_file_name[len - 1] != '/')
419 db_file_name[len++] = '/';
420 db_file_name[len] = '\0';
422 strcat (db_file_name, encoding);
423 strcat (db_file_name, "/");
424 /* strcat (db_file_name, feature); */
425 sp = &db_file_name[strlen (db_file_name)];
426 for (i = 0; i < flen; i++)
430 if ( (c == '/') || (c == '%') )
432 sprintf (sp, "%%%02X", c);
439 status = dbase->open (dbase, db_file_name, NULL,
440 real_subtype, accessmask, modemask);
443 dbase->close (dbase, 0);
452 chise_close_attribute_table (CHISE_Attribute_Table *table)
456 table->db->sync (table->db, 0);
457 table->db->close (table->db, 0);
464 chise_get_attribute_table (CHISE_Attribute_Table *table,
465 char *key, CHISE_Value *valdatum)
470 /* DB Version 2 requires DBT's to be zeroed before use. */
475 keydatum.size = strlen (key);
477 status = table->db->get (table->db, NULL, &keydatum, valdatum, 0);
482 chise_put_attribute_table (CHISE_Attribute_Table *table,
483 char *key, char *value)
485 DBT keydatum, valdatum;
488 /* DB Version 2 requires DBT's to be zeroed before use. */
493 keydatum.size = strlen (key);
495 valdatum.data = value;
496 valdatum.size = strlen (value);
498 status = table->db->put (table->db, NULL, &keydatum, &valdatum, 0);