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,
36 cos_release_container,
48 cos_make_int (COS_C_Int num)
50 return (COS_object)((COS_INT)(num << 1) | 1);
54 cos_int_value (COS_object obj)
56 if (COS_OBJECT_INT_P (obj))
57 return ((COS_INT)obj) >> 1;
62 cos_int_p (COS_object obj)
64 return COS_OBJECT_INT_P (obj);
69 cos_make_char (int code)
71 return (COS_object)((COS_INT)(code << 2) | 2);
75 cos_char_id (COS_object obj)
77 if (COS_OBJECT_CHAR_P (obj))
78 return ((COS_INT)obj) >> 2;
83 cos_char_p (COS_object obj)
85 return COS_OBJECT_CHAR_P (obj);
90 cos_allocate_object_0 (enum COS_Object_Type type, size_t size)
92 COS_Object obj = (COS_Object)malloc (size);
97 obj->header.prefix = COS_OBJECT_PREFIX_OBJECT;
98 obj->header.type = type;
99 obj->header.reference_count = 0;
105 cos_retain_object (COS_Object obj)
107 obj->header.reference_count++;
113 cos_release_object (COS_object obj)
115 ((COS_Object)obj)->header.reference_count--;
117 if ( ((COS_Object)obj)->header.reference_count <= 0 )
118 return (*COS_Object_release_function_table
119 [((COS_Object)obj)->header.type
120 - COS_FAT_OBJECT_TYPE_MIN])(obj);
127 cos_make_string (char* str, size_t size)
129 COS_String obj = COS_ALLOCATE_OBJECT (String);
135 obj->data = malloc (size + 1);
136 if (obj->data == NULL)
142 strncpy ((char*)obj->data, str, size);
143 obj->data[size] = '\0';
148 cos_build_string (char* str)
151 COS_String obj = COS_ALLOCATE_OBJECT (string);
156 obj->size = strlen (str);
157 obj->data = malloc (obj->size + 1);
158 if (obj->data == NULL)
164 strncpy ((char*)obj->data, str, obj->size);
165 obj->data[obj->size] = '\0';
168 return cos_make_string (str, strlen (str));
173 cos_release_string (COS_Object obj)
178 if ( ((COS_String)obj)->data != NULL)
179 free (((COS_String)obj)->data);
185 cos_string_size (COS_String string)
191 cos_string_data (COS_String string)
193 return (char*)string->data;
198 cos_release_container (COS_Object obj)
205 cos_release_sexp (COS_Object obj)
212 cos_release_binary (COS_Object obj)
219 concord_open_env (COS_object ds)
221 if (COS_OBJECT_DS_P (ds))
222 concord_current_env = (COS_DS)ds;
227 if (COS_OBJECT_C_STRING_P (ds))
230 path = CONCORD_SI_DB_DIR;
234 concord_current_env = concord_open_ds (CONCORD_Backend_Berkeley_DB,
237 return concord_current_env;
241 cos_release_ds (COS_Object obj)
243 return concord_close_ds ((COS_DS)obj);
248 concord_get_genre (COS_object ds, COS_object genre)
250 if (COS_OBJECT_C_STRING_P (genre))
251 return concord_ds_get_genre (ds, (char*)genre);
252 else if (COS_OBJECT_SYMBOL_P (genre))
253 return concord_ds_get_genre (ds, (char*)((COS_Symbol)genre)->name->data);
254 else if (COS_OBJECT_GENRE_P (genre))
255 return (COS_Genre)genre;
260 cos_release_genre (COS_Object obj)
262 return concord_close_genre ((COS_Genre)obj);
267 concord_get_feature (COS_object ds,
268 COS_object genre, COS_object feature)
270 if (COS_OBJECT_FEATURE_P (feature))
274 COS_Genre gobj = concord_get_genre (ds, genre);
277 if (COS_OBJECT_C_STRING_P (feature))
278 feature_name = (char*)feature;
279 else if (COS_OBJECT_STRING_P (feature))
280 feature_name = (char*)((COS_String)feature)->data;
281 else if (COS_OBJECT_SYMBOL_P (feature))
282 feature_name = (char*)((COS_Symbol)feature)->name->data;
286 return concord_genre_get_feature (gobj, feature_name);
291 cos_release_feature (COS_Object obj)
293 return concord_close_feature ((COS_Feature)obj);
298 concord_get_feature_index (COS_object ds,
299 COS_object genre, COS_object feature)
301 if (COS_OBJECT_FEATURE_INDEX_P (feature))
305 COS_Feature fobj = concord_get_feature (ds, genre, feature);
312 gobj = concord_feature_get_genre (fobj);
313 feature_name = concord_feature_get_name (fobj);
314 return concord_genre_get_index (gobj, feature_name);
319 cos_release_index (COS_Object obj)
321 return concord_close_index ((COS_Feature_INDEX)obj);
326 cos_release_db_object (COS_Object obj)
332 concord_decode_object (COS_object ds, COS_object genre,
333 COS_object feature, COS_object id)
335 COS_Feature_INDEX index = concord_get_feature_index (ds, genre, feature);
338 CONCORD_String_Tank obj_st;
339 COS_String obj_string;
346 printf ("decoding id (%lX)...", id);
347 if (COS_OBJECT_INT_P (id))
349 printf ("(id is an int)...");
350 snprintf(buf, 256, "%ld", cos_int_value (id));
353 else if (COS_OBJECT_CHAR_P (id))
355 printf ("(id is a char)...");
356 snprintf(buf, 256, "%ld", cos_char_id (id));
359 else if (COS_OBJECT_SYMBOL_P (id))
361 printf ("(id is a symbol)....");
362 id_str = cos_string_data (cos_symbol_name ((COS_Symbol)id));
364 else if (COS_OBJECT_C_STRING_P (id))
366 printf ("(id is a C-string)....");
369 else if (COS_OBJECT_STRING_P (id))
371 printf ("(id is a string)....");
372 id_str = cos_string_data ((COS_String)id);
376 printf ("done (%s).\n", id_str);
378 if ( concord_index_strid_get_obj_string (index, id_str, &obj_st) )
381 obj_string = cos_make_string ((char*)CONCORD_String_data (&obj_st),
382 CONCORD_String_size (&obj_st));
384 cid = cos_read_char (CONCORD_String_data (&obj_st),
385 CONCORD_String_size (&obj_st),
389 printf ("obj = %s (%d, U+%04X), len = %d, end = %d\n",
390 cos_string_data (obj_string),
392 CONCORD_String_size (&obj_st), end);
393 cos_release_object (obj_string);
394 return cos_make_char (cid);
397 printf ("obj = %s\n", cos_string_data (obj_string));
399 cos_release_object (obj_string);
405 concord_object_get_feature_value (COS_object object, COS_object feature)
408 CONCORD_Feature fobj;
409 CONCORD_String_Tank val_st;
410 COS_String val_string;
412 if (COS_OBJECT_CHAR_P (object))
414 cos_utf8_print_char (object, id_buf, 256);
415 printf ("Object[char:0x%lX]'s id is %s.\n", object, id_buf);
420 fobj = concord_get_feature (concord_current_env,
421 "character", feature);
425 if ( concord_obj_get_feature_value_string (id_buf, fobj, &val_st) )
428 val_string = cos_make_string ((char*)CONCORD_String_data (&val_st),
429 CONCORD_String_size (&val_st));
430 printf ("obj[%s]'s %s = %s\n",
432 concord_feature_get_name (fobj),
433 cos_string_data (val_string));