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;
223 chise_open_data_source (CHISE_DS_Type type, char *location)
225 CHISE_DS *ds = (CHISE_DS*)malloc (sizeof (CHISE_DS));
226 size_t len = strlen (location);
232 ds->location = (unsigned char*)malloc (len + 1);
233 if (ds->location == NULL)
238 strcpy (ds->location, location);
243 chise_ds_close (CHISE_DS *ds)
245 if (ds->location != NULL)
252 CHISE_Decoding_Table*
253 chise_ds_open_decoding_table (CHISE_DS *ds, const char *ccs,
255 u_int32_t accessmask, int modemask)
258 chise_open_attribute_table (ds,
259 ccs, "system-char-id",
260 real_subtype, accessmask, modemask);
264 chise_dt_close (CHISE_Decoding_Table *table)
267 return chise_close_attribute_table (table);
272 chise_dt_get_char (CHISE_Decoding_Table *table, int code_point)
274 CHISE_Value valdatum;
278 sprintf(key_buf, "%d", code_point);
279 status = chise_get_attribute_table (table, key_buf, &valdatum);
283 = (unsigned char *)chise_value_data (&valdatum);
284 int len = strnlen (str, chise_value_size (&valdatum));
286 return chise_char_id_parse_c_string (str, len);
292 chise_dt_put_char (CHISE_Decoding_Table *table,
293 int code_point, CHISE_Char_ID cid)
295 CHISE_Value valdatum;
296 char key_buf[16], val_buf[8];
298 sprintf(key_buf, "%d", code_point);
299 chise_format_char_id (cid, val_buf, 8);
300 return chise_put_attribute_table (table, key_buf, val_buf);
306 chise_ds_open_feature_table (CHISE_DS *ds, const char *feature,
308 u_int32_t accessmask, int modemask)
311 chise_open_attribute_table (ds,
312 "system-char-id", feature,
313 real_subtype, accessmask, modemask);
317 chise_ft_close (CHISE_Feature_Table *table)
320 return chise_close_attribute_table (table);
325 chise_ft_get_value (CHISE_Feature_Table *table,
326 CHISE_Char_ID cid, CHISE_Value *valdatum)
328 unsigned char key_buf[8];
330 chise_format_char_id (cid, key_buf, 8);
331 return chise_get_attribute_table (table, key_buf, valdatum);
335 chise_ft_iterate (CHISE_Feature_Table *table,
336 int (*func) (CHISE_Feature_Table *table,
337 CHISE_Char_ID cid, CHISE_Value *valdatum))
339 DBT keydatum, valdatum;
346 status = table->db->cursor (table->db, NULL, &dbcp, 0);
347 for (status = dbcp->c_get (dbcp, &keydatum, &valdatum, DB_FIRST);
349 status = dbcp->c_get (dbcp, &keydatum, &valdatum, DB_NEXT))
351 unsigned char *key_str = (unsigned char *)keydatum.data;
352 int key_len = strnlen (key_str, keydatum.size);
353 CHISE_Char_ID key = chise_char_id_parse_c_string (key_str, key_len);
356 if (ret = func (table, key, &valdatum))
359 dbcp->c_close (dbcp);
362 CHISE_Attribute_Table*
363 chise_open_attribute_table (CHISE_DS *ds,
364 const char *encoding, const char *feature,
366 u_int32_t accessmask, int modemask)
368 CHISE_Attribute_Table* table;
374 char *db_file_name, *sp;
379 table = (CHISE_Attribute_Table*)malloc (sizeof (CHISE_Attribute_Table));
385 status = db_create (&dbase, NULL, 0);
392 db_dir = ds->location;
393 len = strlen (db_dir);
394 flen = strlen (feature);
395 size = len + strlen (encoding) + flen * 3 + 4;
396 db_file_name = alloca (size);
397 strcpy (db_file_name, db_dir);
398 if (db_file_name[len - 1] != '/')
400 db_file_name[len++] = '/';
401 db_file_name[len] = '\0';
403 strcat (db_file_name, encoding);
404 strcat (db_file_name, "/");
405 /* strcat (db_file_name, feature); */
406 sp = &db_file_name[strlen (db_file_name)];
407 for (i = 0; i < flen; i++)
411 if ( (c == '/') || (c == '%') )
413 sprintf (sp, "%%%02X", c);
420 status = dbase->open (dbase, db_file_name, NULL,
421 real_subtype, accessmask, modemask);
424 dbase->close (dbase, 0);
433 chise_close_attribute_table (CHISE_Attribute_Table *table)
437 table->db->sync (table->db, 0);
438 table->db->close (table->db, 0);
445 chise_get_attribute_table (CHISE_Attribute_Table *table,
446 char *key, CHISE_Value *valdatum)
451 /* DB Version 2 requires DBT's to be zeroed before use. */
456 keydatum.size = strlen (key);
458 status = table->db->get (table->db, NULL, &keydatum, valdatum, 0);
463 chise_put_attribute_table (CHISE_Attribute_Table *table,
464 char *key, char *value)
466 DBT keydatum, valdatum;
469 /* DB Version 2 requires DBT's to be zeroed before use. */
474 keydatum.size = strlen (key);
476 valdatum.data = value;
477 valdatum.size = strlen (value);
479 status = table->db->put (table->db, NULL, &keydatum, &valdatum, 0);