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 if ( ((COS_Object)obj)->header.reference_count <= 0 )
140 return (*COS_Object_release_function_table
141 [((COS_Object)obj)->header.type
142 - COS_FAT_OBJECT_TYPE_MIN])(obj);
144 ((COS_Object)obj)->header.reference_count--;
151 cos_make_string (char* str, size_t size)
153 COS_String obj = COS_ALLOCATE_OBJECT (String);
159 obj->data = malloc (size + 1);
160 if (obj->data == NULL)
166 strncpy ((char*)obj->data, str, size);
167 obj->data[size] = '\0';
172 cos_build_string (char* str)
175 COS_String obj = COS_ALLOCATE_OBJECT (string);
180 obj->size = strlen (str);
181 obj->data = malloc (obj->size + 1);
182 if (obj->data == NULL)
188 strncpy ((char*)obj->data, str, obj->size);
189 obj->data[obj->size] = '\0';
192 return cos_make_string (str, strlen (str));
197 cos_retain_string (COS_Object obj)
203 cos_release_string (COS_Object obj)
208 if ( ((COS_String)obj)->data != NULL)
209 free (((COS_String)obj)->data);
214 int cos_string_p (COS_object obj)
216 return COS_OBJECT_STRING_P (obj);
220 cos_string_size (COS_String string)
226 cos_string_data (COS_String string)
228 return (char*)string->data;
233 cos_cons (COS_object car, COS_object cdr)
235 COS_Cons obj = COS_ALLOCATE_OBJECT (Cons);
242 cos_retain_object (car);
243 cos_retain_object (cdr);
249 cos_retain_cons (COS_Object obj)
251 //cos_retain_object (COS_CAR (obj));
252 //cos_retain_object (COS_CDR (obj));
257 cos_release_cons (COS_Object obj)
262 cos_release_object (COS_CAR (obj));
263 cos_release_object (COS_CDR (obj));
269 cos_cons_p (COS_object obj)
271 return COS_OBJECT_CONS_P (obj);
275 cos_car (COS_Cons pair)
277 if (COS_OBJECT_CONS_P (pair))
278 return COS_CAR (pair);
284 cos_cdr (COS_Cons pair)
286 if (COS_OBJECT_CONS_P (pair))
287 return COS_CDR (pair);
293 cos_assoc (COS_object key, COS_Cons alist)
295 COS_Cons rest = alist;
297 while ( COS_OBJECT_CONS_P (rest) )
299 COS_Cons cell = cos_car (rest);
301 if ( cos_car (cell) == key )
304 rest = cos_cdr (rest);
310 cos_alist_get (COS_Cons alist, COS_object key)
312 return cos_cdr (cos_assoc (key, alist));
317 cos_retain_container (COS_Object obj)
323 cos_release_container (COS_Object obj)
330 cos_retain_sexp (COS_Object obj)
336 cos_release_sexp (COS_Object obj)
343 cos_retain_binary (COS_Object obj)
349 cos_release_binary (COS_Object obj)
356 concord_open_env (COS_object ds)
358 if (COS_OBJECT_DS_P (ds))
359 concord_current_env = (COS_DS)ds;
364 if (COS_OBJECT_C_STRING_P (ds))
367 path = CONCORD_SI_DB_DIR;
371 concord_current_env = concord_open_ds (CONCORD_Backend_Berkeley_DB,
374 return concord_current_env;
378 cos_retain_ds (COS_Object obj)
384 cos_release_ds (COS_Object obj)
386 return concord_close_ds ((COS_DS)obj);
391 concord_get_genre (COS_object ds, COS_object genre)
393 if (COS_OBJECT_C_STRING_P (genre))
394 return concord_ds_get_genre (ds, (char*)genre);
395 else if (COS_OBJECT_SYMBOL_P (genre))
396 return concord_ds_get_genre (ds, (char*)((COS_Symbol)genre)->name->data);
397 else if (COS_OBJECT_GENRE_P (genre))
398 return (COS_Genre)genre;
403 cos_retain_genre (COS_Object obj)
409 cos_release_genre (COS_Object obj)
411 return concord_close_genre ((COS_Genre)obj);
416 concord_get_feature (COS_object ds,
417 COS_object genre, COS_object feature)
419 if (COS_OBJECT_FEATURE_P (feature))
423 COS_Genre gobj = concord_get_genre (ds, genre);
426 if (COS_OBJECT_C_STRING_P (feature))
427 feature_name = (char*)feature;
428 else if (COS_OBJECT_STRING_P (feature))
429 feature_name = (char*)((COS_String)feature)->data;
430 else if (COS_OBJECT_SYMBOL_P (feature))
431 feature_name = (char*)((COS_Symbol)feature)->name->data;
435 return concord_genre_get_feature (gobj, feature_name);
440 cos_retain_feature (COS_Object obj)
446 cos_release_feature (COS_Object obj)
448 return concord_close_feature ((COS_Feature)obj);
453 concord_get_feature_index (COS_object ds,
454 COS_object genre, COS_object feature)
456 if (COS_OBJECT_FEATURE_INDEX_P (feature))
460 COS_Feature fobj = concord_get_feature (ds, genre, feature);
467 gobj = concord_feature_get_genre (fobj);
468 feature_name = concord_feature_get_name (fobj);
469 return concord_genre_get_index (gobj, feature_name);
474 cos_retain_index (COS_Object obj)
476 return concord_close_index ((COS_Feature_INDEX)obj);
480 cos_release_index (COS_Object obj)
482 return concord_close_index ((COS_Feature_INDEX)obj);
487 cos_retain_db_object (COS_Object obj)
493 cos_release_db_object (COS_Object obj)
499 concord_decode_object (COS_object ds, COS_object genre,
500 COS_object feature, COS_object id)
502 COS_Feature_INDEX index = concord_get_feature_index (ds, genre, feature);
505 CONCORD_String_Tank obj_st;
512 if (COS_OBJECT_INT_P (id))
514 snprintf(buf, 256, "%ld", cos_int_value (id));
517 else if (COS_OBJECT_CHAR_P (id))
519 snprintf(buf, 256, "%d", cos_char_id (id));
522 else if (COS_OBJECT_SYMBOL_P (id))
524 id_str = cos_string_data (cos_symbol_name ((COS_Symbol)id));
526 else if (COS_OBJECT_C_STRING_P (id))
530 else if (COS_OBJECT_STRING_P (id))
532 id_str = cos_string_data ((COS_String)id);
537 if ( concord_index_strid_get_obj_string (index, id_str, &obj_st) )
540 cid = cos_read_char (CONCORD_String_data (&obj_st),
541 CONCORD_String_size (&obj_st),
545 return cos_make_char (cid);
552 concord_object_get_feature_value (COS_object object, COS_object feature)
556 CONCORD_Feature fobj;
557 CONCORD_String_Tank val_st;
560 if (COS_OBJECT_CHAR_P (object))
562 cos_utf8_print_char (object, (unsigned char*)id_buf, 256);
567 if (!COS_OBJECT_DS_P (concord_current_env))
569 concord_current_env = NULL;
573 gobj = concord_get_genre (concord_current_env, "character");
577 fobj = concord_get_feature (concord_current_env, gobj, feature);
581 if ( concord_obj_get_feature_value_string (id_buf, fobj, &val_st) )
584 return cos_read_object (CONCORD_String_data (&val_st),
585 CONCORD_String_size (&val_st),