1 /* Copyright (C) 2013 MORIOKA Tomohiko
2 This file is part of the CONCORD Library.
4 The CONCORD Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The CONCORD Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the CONCORD Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 #include "cos-print.h"
25 const char concord_db_format_version[] = CONCORD_DB_FORMAT_VERSION;
26 const char concord_db_dir[] = CONCORD_DB_DIR;
27 const char concord_system_db_dir[] = CONCORD_SI_DB_DIR;
29 CONCORD_DS concord_current_env = NULL;
31 int (*COS_Object_retain_function_table
32 [COS_OBJECT_TYPE_MAX - COS_Object_Type_char])
34 = { cos_retain_string,
47 int (*COS_Object_release_function_table
48 [COS_OBJECT_TYPE_MAX - COS_Object_Type_char])
50 = { cos_release_string,
53 cos_release_container,
65 cos_make_int (COS_C_Int num)
67 return (COS_object)((COS_INT)(num << 1) | 1);
71 cos_int_p (COS_object obj)
73 return COS_OBJECT_INT_P (obj);
77 cos_int_value (COS_object obj)
79 if (COS_OBJECT_INT_P (obj))
80 return ((COS_INT)obj) >> 1;
86 cos_make_char (int code)
88 return (COS_object)((COS_INT)(code << 2) | 2);
92 cos_char_p (COS_object obj)
94 return COS_OBJECT_CHAR_P (obj);
98 cos_char_id (COS_object obj)
100 if (COS_OBJECT_CHAR_P (obj))
101 return (((COS_INT)obj) >> 2) & 0x7FFFFFFF;
107 cos_allocate_object_0 (enum COS_Object_Type type, size_t size)
109 COS_Object obj = (COS_Object)malloc (size);
114 obj->header.prefix = COS_OBJECT_PREFIX_OBJECT;
115 obj->header.type = type;
116 obj->header.reference_count = 0;
122 cos_retain_object (COS_Object obj)
124 if (COS_OBJECT_P (obj))
126 obj->header.reference_count++;
127 (*COS_Object_retain_function_table
128 [((COS_Object)obj)->header.type
129 - COS_FAT_OBJECT_TYPE_MIN])(obj);
135 cos_release_object (COS_object obj)
137 if (COS_OBJECT_P (obj))
139 ((COS_Object)obj)->header.reference_count--;
140 if ( ((COS_Object)obj)->header.reference_count <= 0 )
141 return (*COS_Object_release_function_table
142 [((COS_Object)obj)->header.type
143 - COS_FAT_OBJECT_TYPE_MIN])(obj);
150 cos_make_string (char* str, size_t size)
152 COS_String obj = COS_ALLOCATE_OBJECT (String);
158 obj->data = malloc (size + 1);
159 if (obj->data == NULL)
165 strncpy ((char*)obj->data, str, size);
166 obj->data[size] = '\0';
171 cos_build_string (char* str)
174 COS_String obj = COS_ALLOCATE_OBJECT (string);
179 obj->size = strlen (str);
180 obj->data = malloc (obj->size + 1);
181 if (obj->data == NULL)
187 strncpy ((char*)obj->data, str, obj->size);
188 obj->data[obj->size] = '\0';
191 return cos_make_string (str, strlen (str));
196 cos_retain_string (COS_Object obj)
202 cos_release_string (COS_Object obj)
207 if ( ((COS_String)obj)->data != NULL)
208 free (((COS_String)obj)->data);
213 int cos_string_p (COS_object obj)
215 return COS_OBJECT_STRING_P (obj);
219 cos_string_size (COS_String string)
225 cos_string_data (COS_String string)
227 return (char*)string->data;
232 cos_cons (COS_object car, COS_object cdr)
234 COS_Cons obj = COS_ALLOCATE_OBJECT (Cons);
241 cos_retain_object (car);
242 cos_retain_object (cdr);
248 cos_retain_cons (COS_Object obj)
250 //cos_retain_object (COS_CAR (obj));
251 //cos_retain_object (COS_CDR (obj));
256 cos_release_cons (COS_Object obj)
261 cos_release_object (COS_CAR (obj));
262 cos_release_object (COS_CDR (obj));
268 cos_cons_p (COS_object obj)
270 return COS_OBJECT_CONS_P (obj);
274 cos_car (COS_Cons pair)
276 if (COS_OBJECT_CONS_P (pair))
277 return COS_CAR (pair);
283 cos_cdr (COS_Cons pair)
285 if (COS_OBJECT_CONS_P (pair))
286 return COS_CDR (pair);
292 cos_assoc (COS_object key, COS_Cons alist)
294 COS_Cons rest = alist;
296 while ( COS_OBJECT_CONS_P (rest) )
298 COS_Cons cell = cos_car (rest);
300 if ( cos_car (cell) == key )
303 rest = cos_cdr (rest);
309 cos_alist_get (COS_Cons alist, COS_object key)
311 return cos_cdr (cos_assoc (key, alist));
316 cos_retain_container (COS_Object obj)
322 cos_release_container (COS_Object obj)
329 cos_retain_sexp (COS_Object obj)
335 cos_release_sexp (COS_Object obj)
342 cos_retain_binary (COS_Object obj)
348 cos_release_binary (COS_Object obj)
355 concord_open_env (COS_object ds)
357 if (COS_OBJECT_DS_P (ds))
358 concord_current_env = (COS_DS)ds;
363 if (COS_OBJECT_C_STRING_P (ds))
366 path = CONCORD_SI_DB_DIR;
370 concord_current_env = concord_open_ds (CONCORD_Backend_Berkeley_DB,
373 return concord_current_env;
377 cos_retain_ds (COS_Object obj)
383 cos_release_ds (COS_Object obj)
385 return concord_close_ds ((COS_DS)obj);
389 concord_ds_p (COS_object obj)
391 return COS_OBJECT_DS_P (obj);
396 concord_get_genre (COS_object ds, COS_object genre)
398 if (COS_OBJECT_C_STRING_P (genre))
399 return concord_ds_get_genre (ds, (char*)genre);
400 else if (COS_OBJECT_SYMBOL_P (genre))
401 return concord_ds_get_genre (ds, (char*)((COS_Symbol)genre)->name->data);
402 else if (COS_OBJECT_GENRE_P (genre))
403 return (COS_Genre)genre;
408 cos_retain_genre (COS_Object obj)
414 cos_release_genre (COS_Object obj)
416 return concord_close_genre ((COS_Genre)obj);
421 concord_get_feature (COS_object ds,
422 COS_object genre, COS_object feature)
424 if (COS_OBJECT_FEATURE_P (feature))
428 COS_Genre gobj = concord_get_genre (ds, genre);
431 if (COS_OBJECT_C_STRING_P (feature))
432 feature_name = (char*)feature;
433 else if (COS_OBJECT_STRING_P (feature))
434 feature_name = (char*)((COS_String)feature)->data;
435 else if (COS_OBJECT_SYMBOL_P (feature))
436 feature_name = (char*)((COS_Symbol)feature)->name->data;
440 return concord_genre_get_feature (gobj, feature_name);
445 cos_retain_feature (COS_Object obj)
451 cos_release_feature (COS_Object obj)
453 return concord_close_feature ((COS_Feature)obj);
458 concord_get_feature_index (COS_object ds,
459 COS_object genre, COS_object feature)
461 if (COS_OBJECT_FEATURE_INDEX_P (feature))
465 COS_Feature fobj = concord_get_feature (ds, genre, feature);
472 gobj = concord_feature_get_genre (fobj);
473 feature_name = concord_feature_get_name (fobj);
474 return concord_genre_get_index (gobj, feature_name);
479 cos_retain_index (COS_Object obj)
481 return concord_close_index ((COS_Feature_INDEX)obj);
485 cos_release_index (COS_Object obj)
487 return concord_close_index ((COS_Feature_INDEX)obj);
492 cos_retain_db_object (COS_Object obj)
498 cos_release_db_object (COS_Object obj)
504 concord_decode_object (COS_object ds, COS_object genre,
505 COS_object feature, COS_object id)
507 COS_Feature_INDEX index = concord_get_feature_index (ds, genre, feature);
510 CONCORD_String_Tank obj_st;
517 if ( index->decoding_table != NULL )
519 COS_object ret = cos_hash_table_get (index->decoding_table, id);
524 index->decoding_table = cos_make_hash_table ();
526 if (COS_OBJECT_INT_P (id))
528 snprintf(buf, 256, "%ld", cos_int_value (id));
531 else if (COS_OBJECT_CHAR_P (id))
533 snprintf(buf, 256, "%d", cos_char_id (id));
536 else if (COS_OBJECT_SYMBOL_P (id))
538 id_str = cos_string_data (cos_symbol_name ((COS_Symbol)id));
540 else if (COS_OBJECT_C_STRING_P (id))
544 else if (COS_OBJECT_STRING_P (id))
546 id_str = cos_string_data ((COS_String)id);
551 if ( concord_index_strid_get_obj_string (index, id_str, &obj_st) )
557 cid = cos_read_char (CONCORD_String_data (&obj_st),
558 CONCORD_String_size (&obj_st),
562 obj = cos_make_char (cid);
563 cos_retain_object (id);
564 cos_retain_object (obj);
565 cos_hash_table_put (index->decoding_table, id, obj);
573 concord_object_get_attribute (COS_object object, COS_object feature)
576 CONCORD_Feature fobj;
579 if (COS_OBJECT_CHAR_P (object))
581 if (!COS_OBJECT_DS_P (concord_current_env))
583 concord_current_env = NULL;
586 gobj = concord_get_genre (concord_current_env, "character");
593 fobj = concord_get_feature (concord_current_env, gobj, feature);
597 if ( fobj->value_table != NULL )
599 ret = cos_hash_table_get (fobj->value_table, object);
604 fobj->value_table = cos_make_hash_table ();
608 CONCORD_String_Tank val_st;
611 if (COS_OBJECT_CHAR_P (object))
613 cos_utf8_print_char (object, (unsigned char*)id_buf, 256);
618 if ( concord_obj_get_feature_value_string (id_buf, fobj, &val_st) )
621 ret = cos_read_object (CONCORD_String_data (&val_st),
622 CONCORD_String_size (&val_st),
627 cos_retain_object (object);
628 cos_retain_object (ret);
629 cos_hash_table_put (fobj->value_table, object, ret);