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_release_function_table
32 [COS_OBJECT_TYPE_MAX - COS_Object_Type_char])
34 = { cos_release_string,
37 cos_release_container,
49 cos_make_int (COS_C_Int num)
51 return (COS_object)((COS_INT)(num << 1) | 1);
55 cos_int_value (COS_object obj)
57 if (COS_OBJECT_INT_P (obj))
58 return ((COS_INT)obj) >> 1;
63 cos_int_p (COS_object obj)
65 return COS_OBJECT_INT_P (obj);
70 cos_make_char (int code)
72 return (COS_object)((COS_INT)(code << 2) | 2);
76 cos_char_id (COS_object obj)
78 if (COS_OBJECT_CHAR_P (obj))
79 return ((COS_INT)obj) >> 2;
84 cos_char_p (COS_object obj)
86 return COS_OBJECT_CHAR_P (obj);
91 cos_allocate_object_0 (enum COS_Object_Type type, size_t size)
93 COS_Object obj = (COS_Object)malloc (size);
98 obj->header.prefix = COS_OBJECT_PREFIX_OBJECT;
99 obj->header.type = type;
100 obj->header.reference_count = 0;
106 cos_retain_object (COS_Object obj)
108 if (COS_OBJECT_P (obj))
109 obj->header.reference_count++;
114 cos_release_object (COS_object obj)
116 if (COS_OBJECT_P (obj))
118 ((COS_Object)obj)->header.reference_count--;
120 if ( ((COS_Object)obj)->header.reference_count <= 0 )
121 return (*COS_Object_release_function_table
122 [((COS_Object)obj)->header.type
123 - COS_FAT_OBJECT_TYPE_MIN])(obj);
130 cos_make_string (char* str, size_t size)
132 COS_String obj = COS_ALLOCATE_OBJECT (String);
138 obj->data = malloc (size + 1);
139 if (obj->data == NULL)
145 strncpy ((char*)obj->data, str, size);
146 obj->data[size] = '\0';
151 cos_build_string (char* str)
154 COS_String obj = COS_ALLOCATE_OBJECT (string);
159 obj->size = strlen (str);
160 obj->data = malloc (obj->size + 1);
161 if (obj->data == NULL)
167 strncpy ((char*)obj->data, str, obj->size);
168 obj->data[obj->size] = '\0';
171 return cos_make_string (str, strlen (str));
176 cos_release_string (COS_Object obj)
181 if ( ((COS_String)obj)->data != NULL)
182 free (((COS_String)obj)->data);
187 int cos_string_p (COS_object obj)
189 return COS_OBJECT_STRING_P (obj);
193 cos_string_size (COS_String string)
199 cos_string_data (COS_String string)
201 return (char*)string->data;
206 cos_cons (COS_object car, COS_object cdr)
208 COS_Cons obj = COS_ALLOCATE_OBJECT (Cons);
215 cos_retain_object (car);
216 cos_retain_object (cdr);
222 cos_release_cons (COS_Object obj)
227 cos_release_object (COS_CAR (obj));
228 cos_release_object (COS_CDR (obj));
234 cos_car (COS_Cons pair)
236 if (COS_OBJECT_CONS_P (pair))
237 return COS_CAR (pair);
243 cos_cdr (COS_Cons pair)
245 if (COS_OBJECT_CONS_P (pair))
246 return COS_CDR (pair);
253 cos_release_container (COS_Object obj)
260 cos_release_sexp (COS_Object obj)
267 cos_release_binary (COS_Object obj)
274 concord_open_env (COS_object ds)
276 if (COS_OBJECT_DS_P (ds))
277 concord_current_env = (COS_DS)ds;
282 if (COS_OBJECT_C_STRING_P (ds))
285 path = CONCORD_SI_DB_DIR;
289 concord_current_env = concord_open_ds (CONCORD_Backend_Berkeley_DB,
292 return concord_current_env;
296 cos_release_ds (COS_Object obj)
298 return concord_close_ds ((COS_DS)obj);
303 concord_get_genre (COS_object ds, COS_object genre)
305 if (COS_OBJECT_C_STRING_P (genre))
306 return concord_ds_get_genre (ds, (char*)genre);
307 else if (COS_OBJECT_SYMBOL_P (genre))
308 return concord_ds_get_genre (ds, (char*)((COS_Symbol)genre)->name->data);
309 else if (COS_OBJECT_GENRE_P (genre))
310 return (COS_Genre)genre;
315 cos_release_genre (COS_Object obj)
317 return concord_close_genre ((COS_Genre)obj);
322 concord_get_feature (COS_object ds,
323 COS_object genre, COS_object feature)
325 if (COS_OBJECT_FEATURE_P (feature))
329 COS_Genre gobj = concord_get_genre (ds, genre);
332 if (COS_OBJECT_C_STRING_P (feature))
333 feature_name = (char*)feature;
334 else if (COS_OBJECT_STRING_P (feature))
335 feature_name = (char*)((COS_String)feature)->data;
336 else if (COS_OBJECT_SYMBOL_P (feature))
337 feature_name = (char*)((COS_Symbol)feature)->name->data;
341 return concord_genre_get_feature (gobj, feature_name);
346 cos_release_feature (COS_Object obj)
348 return concord_close_feature ((COS_Feature)obj);
353 concord_get_feature_index (COS_object ds,
354 COS_object genre, COS_object feature)
356 if (COS_OBJECT_FEATURE_INDEX_P (feature))
360 COS_Feature fobj = concord_get_feature (ds, genre, feature);
367 gobj = concord_feature_get_genre (fobj);
368 feature_name = concord_feature_get_name (fobj);
369 return concord_genre_get_index (gobj, feature_name);
374 cos_release_index (COS_Object obj)
376 return concord_close_index ((COS_Feature_INDEX)obj);
381 cos_release_db_object (COS_Object obj)
387 concord_decode_object (COS_object ds, COS_object genre,
388 COS_object feature, COS_object id)
390 COS_Feature_INDEX index = concord_get_feature_index (ds, genre, feature);
393 CONCORD_String_Tank obj_st;
394 COS_String obj_string;
401 printf ("decoding id (%lX)...", id);
402 if (COS_OBJECT_INT_P (id))
404 printf ("(id is an int)...");
405 snprintf(buf, 256, "%ld", cos_int_value (id));
408 else if (COS_OBJECT_CHAR_P (id))
410 printf ("(id is a char)...");
411 snprintf(buf, 256, "%ld", cos_char_id (id));
414 else if (COS_OBJECT_SYMBOL_P (id))
416 printf ("(id is a symbol)....");
417 id_str = cos_string_data (cos_symbol_name ((COS_Symbol)id));
419 else if (COS_OBJECT_C_STRING_P (id))
421 printf ("(id is a C-string)....");
424 else if (COS_OBJECT_STRING_P (id))
426 printf ("(id is a string)....");
427 id_str = cos_string_data ((COS_String)id);
431 printf ("done (%s).\n", id_str);
433 if ( concord_index_strid_get_obj_string (index, id_str, &obj_st) )
436 obj_string = cos_make_string ((char*)CONCORD_String_data (&obj_st),
437 CONCORD_String_size (&obj_st));
439 cid = cos_read_char (CONCORD_String_data (&obj_st),
440 CONCORD_String_size (&obj_st),
444 printf ("obj = %s (%d, U+%04X), len = %d, end = %d\n",
445 cos_string_data (obj_string),
447 CONCORD_String_size (&obj_st), end);
448 cos_release_object (obj_string);
449 return cos_make_char (cid);
452 printf ("obj = %s\n", cos_string_data (obj_string));
454 cos_release_object (obj_string);
460 concord_object_get_feature_value (COS_object object, COS_object feature)
463 CONCORD_Feature fobj;
464 CONCORD_String_Tank val_st;
465 COS_String val_string;
471 if (COS_OBJECT_CHAR_P (object))
473 cos_utf8_print_char (object, id_buf, 256);
474 printf ("Object[char:0x%lX]'s id is %s.\n", object, id_buf);
479 fobj = concord_get_feature (concord_current_env,
480 "character", feature);
484 if ( concord_obj_get_feature_value_string (id_buf, fobj, &val_st) )
487 val_string = cos_make_string ((char*)CONCORD_String_data (&val_st),
488 CONCORD_String_size (&val_st));
489 printf ("obj[%s]'s %s = %s\n",
491 concord_feature_get_name (fobj),
492 cos_string_data (val_string));
494 val_obj = cos_read_int (CONCORD_String_data (&val_st),
495 CONCORD_String_size (&val_st),
497 if ( val_obj != NULL )
500 val_cid = cos_read_char (CONCORD_String_data (&val_st),
501 CONCORD_String_size (&val_st),
504 return cos_make_char (val_cid);
506 val_str = cos_read_string (CONCORD_String_data (&val_st),
507 CONCORD_String_size (&val_st),
509 if ( val_str != NULL )