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 #define xzero(lvalue) ((void) memset (&(lvalue), '\0', sizeof (lvalue)))
26 chise_char_id_parse_c_string (unsigned char *str, int len)
30 if ( (len >= 2) && (str[i++] == '?') )
32 unsigned char c = str[i++];
49 return c & (0x80 | 0x1F);
83 if (counter + 2 <= len)
87 for (j = 0; j < counter; j++)
88 cid = (cid << 6) | (str[j + i] & 0x3F);
96 chise_format_char_id (CHISE_Char_ID cid, unsigned char *dest, int len)
108 else if (cid == '\n')
115 else if (cid == '\r')
122 else if (cid == 0x1C)
131 else if (cid <= 0x1F)
135 dest[i++] = '@' + cid;
139 else if ( (cid == ' ') || (cid == '"') ||
140 (cid == '#') || (cid == '\'') ||
141 (cid == '(') || (cid == ')') ||
142 (cid == ',') || (cid == '.') ||
143 (cid == ';') || (cid == '?') ||
144 (cid == '[') || (cid == '\\') ||
145 (cid == ']') || (cid == '`') )
152 else if (cid <= 0x7E)
158 else if (cid == 0x7F)
166 else if (cid <= 0x9F)
170 dest[i++] = ((cid + '@') >> 6) | 0xC0;
171 dest[i++] = ((cid + '@') & 0x3F) | 0x80;
175 else if (cid <= 0x7FF)
177 dest[i++] = (cid >> 6) | 0xC0;
178 dest[i++] = (cid & 0x3F) | 0x80;
182 else if (cid <= 0xFFFF)
184 dest[i++] = (cid >> 12) | 0xE0;
185 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
186 dest[i++]= (cid & 0x3F) | 0x80;
190 else if (cid <= 0x1FFFFF)
192 dest[i++]= (cid >> 18) | 0xF0;
193 dest[i++]= ((cid >> 12) & 0x3F) | 0x80;
194 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
195 dest[i++]= (cid & 0x3F) | 0x80;
199 else if (cid <= 0x3FFFFFF)
201 dest[i++]= (cid >> 24) | 0xF8;
202 dest[i++]= ((cid >> 18) & 0x3F) | 0x80;
203 dest[i++]= ((cid >> 12) & 0x3F) | 0x80;
204 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
205 dest[i++]= (cid & 0x3F) | 0x80;
211 dest[i++]= (cid >> 30) | 0xFC;
212 dest[i++]= ((cid >> 24) & 0x3F) | 0x80;
213 dest[i++]= ((cid >> 18) & 0x3F) | 0x80;
214 dest[i++]= ((cid >> 12) & 0x3F) | 0x80;
215 dest[i++]= ((cid >> 6) & 0x3F) | 0x80;
216 dest[i++]= (cid & 0x3F) | 0x80;
226 unsigned char *location;
230 chise_open_data_source (CHISE_DS_Type type, char *location)
232 CHISE_DS *ds = (CHISE_DS*)malloc (sizeof (CHISE_DS));
233 size_t len = strlen (location);
239 ds->location = (unsigned char*)malloc (len + 1);
240 if (ds->location == NULL)
245 strcpy (ds->location, location);
250 chise_ds_close (CHISE_DS *ds)
252 if (ds->location != NULL)
259 CHISE_Decoding_Table*
260 chise_ds_open_decoding_table (CHISE_DS *ds, const char *ccs,
262 u_int32_t accessmask, int modemask)
265 chise_open_attribute_table (ds,
266 ccs, "system-char-id",
267 real_subtype, accessmask, modemask);
271 chise_dt_close (CHISE_Decoding_Table *table)
274 return chise_close_attribute_table (table);
279 chise_dt_get_char (CHISE_Decoding_Table *table, int code_point)
281 CHISE_Value valdatum;
285 sprintf(key_buf, "%d", code_point);
286 status = chise_get_attribute_table (table, key_buf, &valdatum);
290 = (unsigned char *)chise_value_data (&valdatum);
291 int len = strnlen (str, chise_value_size (&valdatum));
293 return chise_char_id_parse_c_string (str, len);
299 chise_dt_put_char (CHISE_Decoding_Table *table,
300 int code_point, CHISE_Char_ID cid)
302 CHISE_Value valdatum;
303 char key_buf[16], val_buf[8];
305 sprintf(key_buf, "%d", code_point);
306 chise_format_char_id (cid, val_buf, 8);
307 return chise_put_attribute_table (table, key_buf, val_buf);
313 chise_ds_open_feature_table (CHISE_DS *ds, const char *feature,
315 u_int32_t accessmask, int modemask)
318 chise_open_attribute_table (ds,
319 "system-char-id", feature,
320 real_subtype, accessmask, modemask);
324 chise_ft_close (CHISE_Feature_Table *table)
327 return chise_close_attribute_table (table);
332 chise_ft_get_value (CHISE_Feature_Table *table,
333 CHISE_Char_ID cid, CHISE_Value *valdatum)
335 unsigned char key_buf[8];
337 chise_format_char_id (cid, key_buf, 8);
338 return chise_get_attribute_table (table, key_buf, valdatum);
342 chise_ft_iterate (CHISE_Feature_Table *table,
343 int (*func) (CHISE_Feature_Table *table,
344 CHISE_Char_ID cid, CHISE_Value *valdatum))
346 DBT keydatum, valdatum;
353 status = table->db->cursor (table->db, NULL, &dbcp, 0);
354 for (status = dbcp->c_get (dbcp, &keydatum, &valdatum, DB_FIRST);
356 status = dbcp->c_get (dbcp, &keydatum, &valdatum, DB_NEXT))
358 unsigned char *key_str = (unsigned char *)keydatum.data;
359 int key_len = strnlen (key_str, keydatum.size);
360 CHISE_Char_ID key = chise_char_id_parse_c_string (key_str, key_len);
363 if (ret = func (table, key, &valdatum))
366 dbcp->c_close (dbcp);
369 CHISE_Attribute_Table*
370 chise_open_attribute_table (CHISE_DS *ds,
371 const char *encoding, const char *feature,
373 u_int32_t accessmask, int modemask)
375 CHISE_Attribute_Table* table;
381 char *db_file_name, *sp;
386 table = (CHISE_Attribute_Table*)malloc (sizeof (CHISE_Attribute_Table));
392 status = db_create (&dbase, NULL, 0);
399 db_dir = ds->location;
400 len = strlen (db_dir);
401 flen = strlen (feature);
402 size = len + strlen (encoding) + flen * 3 + 4;
403 db_file_name = alloca (size);
404 strcpy (db_file_name, db_dir);
405 if (db_file_name[len - 1] != '/')
407 db_file_name[len++] = '/';
408 db_file_name[len] = '\0';
410 strcat (db_file_name, encoding);
411 strcat (db_file_name, "/");
412 /* strcat (db_file_name, feature); */
413 sp = &db_file_name[strlen (db_file_name)];
414 for (i = 0; i < flen; i++)
418 if ( (c == '/') || (c == '%') )
420 sprintf (sp, "%%%02X", c);
427 status = dbase->open (dbase, db_file_name, NULL,
428 real_subtype, accessmask, modemask);
431 dbase->close (dbase, 0);
440 chise_close_attribute_table (CHISE_Attribute_Table *table)
444 table->db->sync (table->db, 0);
445 table->db->close (table->db, 0);
452 chise_get_attribute_table (CHISE_Attribute_Table *table,
453 char *key, CHISE_Value *valdatum)
458 /* DB Version 2 requires DBT's to be zeroed before use. */
463 keydatum.size = strlen (key);
465 status = table->db->get (table->db, NULL, &keydatum, valdatum, 0);
470 chise_put_attribute_table (CHISE_Attribute_Table *table,
471 char *key, char *value)
473 DBT keydatum, valdatum;
476 /* DB Version 2 requires DBT's to be zeroed before use. */
481 keydatum.size = strlen (key);
483 valdatum.data = value;
484 valdatum.size = strlen (value);
486 status = table->db->put (table->db, NULL, &keydatum, &valdatum, 0);