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);
184 int cos_string_p (COS_object obj)
186 return COS_OBJECT_STRING_P (obj);
190 cos_string_size (COS_String string)
196 cos_string_data (COS_String string)
198 return (char*)string->data;
203 cos_release_container (COS_Object obj)
210 cos_release_sexp (COS_Object obj)
217 cos_release_binary (COS_Object obj)
224 concord_open_env (COS_object ds)
226 if (COS_OBJECT_DS_P (ds))
227 concord_current_env = (COS_DS)ds;
232 if (COS_OBJECT_C_STRING_P (ds))
235 path = CONCORD_SI_DB_DIR;
239 concord_current_env = concord_open_ds (CONCORD_Backend_Berkeley_DB,
242 return concord_current_env;
246 cos_release_ds (COS_Object obj)
248 return concord_close_ds ((COS_DS)obj);
253 concord_get_genre (COS_object ds, COS_object genre)
255 if (COS_OBJECT_C_STRING_P (genre))
256 return concord_ds_get_genre (ds, (char*)genre);
257 else if (COS_OBJECT_SYMBOL_P (genre))
258 return concord_ds_get_genre (ds, (char*)((COS_Symbol)genre)->name->data);
259 else if (COS_OBJECT_GENRE_P (genre))
260 return (COS_Genre)genre;
265 cos_release_genre (COS_Object obj)
267 return concord_close_genre ((COS_Genre)obj);
272 concord_get_feature (COS_object ds,
273 COS_object genre, COS_object feature)
275 if (COS_OBJECT_FEATURE_P (feature))
279 COS_Genre gobj = concord_get_genre (ds, genre);
282 if (COS_OBJECT_C_STRING_P (feature))
283 feature_name = (char*)feature;
284 else if (COS_OBJECT_STRING_P (feature))
285 feature_name = (char*)((COS_String)feature)->data;
286 else if (COS_OBJECT_SYMBOL_P (feature))
287 feature_name = (char*)((COS_Symbol)feature)->name->data;
291 return concord_genre_get_feature (gobj, feature_name);
296 cos_release_feature (COS_Object obj)
298 return concord_close_feature ((COS_Feature)obj);
303 concord_get_feature_index (COS_object ds,
304 COS_object genre, COS_object feature)
306 if (COS_OBJECT_FEATURE_INDEX_P (feature))
310 COS_Feature fobj = concord_get_feature (ds, genre, feature);
317 gobj = concord_feature_get_genre (fobj);
318 feature_name = concord_feature_get_name (fobj);
319 return concord_genre_get_index (gobj, feature_name);
324 cos_release_index (COS_Object obj)
326 return concord_close_index ((COS_Feature_INDEX)obj);
331 cos_release_db_object (COS_Object obj)
337 concord_decode_object (COS_object ds, COS_object genre,
338 COS_object feature, COS_object id)
340 COS_Feature_INDEX index = concord_get_feature_index (ds, genre, feature);
343 CONCORD_String_Tank obj_st;
344 COS_String obj_string;
351 printf ("decoding id (%lX)...", id);
352 if (COS_OBJECT_INT_P (id))
354 printf ("(id is an int)...");
355 snprintf(buf, 256, "%ld", cos_int_value (id));
358 else if (COS_OBJECT_CHAR_P (id))
360 printf ("(id is a char)...");
361 snprintf(buf, 256, "%ld", cos_char_id (id));
364 else if (COS_OBJECT_SYMBOL_P (id))
366 printf ("(id is a symbol)....");
367 id_str = cos_string_data (cos_symbol_name ((COS_Symbol)id));
369 else if (COS_OBJECT_C_STRING_P (id))
371 printf ("(id is a C-string)....");
374 else if (COS_OBJECT_STRING_P (id))
376 printf ("(id is a string)....");
377 id_str = cos_string_data ((COS_String)id);
381 printf ("done (%s).\n", id_str);
383 if ( concord_index_strid_get_obj_string (index, id_str, &obj_st) )
386 obj_string = cos_make_string ((char*)CONCORD_String_data (&obj_st),
387 CONCORD_String_size (&obj_st));
389 cid = cos_read_char (CONCORD_String_data (&obj_st),
390 CONCORD_String_size (&obj_st),
394 printf ("obj = %s (%d, U+%04X), len = %d, end = %d\n",
395 cos_string_data (obj_string),
397 CONCORD_String_size (&obj_st), end);
398 cos_release_object (obj_string);
399 return cos_make_char (cid);
402 printf ("obj = %s\n", cos_string_data (obj_string));
404 cos_release_object (obj_string);
410 concord_object_get_feature_value (COS_object object, COS_object feature)
413 CONCORD_Feature fobj;
414 CONCORD_String_Tank val_st;
415 COS_String val_string;
420 if (COS_OBJECT_CHAR_P (object))
422 cos_utf8_print_char (object, id_buf, 256);
423 printf ("Object[char:0x%lX]'s id is %s.\n", object, id_buf);
428 fobj = concord_get_feature (concord_current_env,
429 "character", feature);
433 if ( concord_obj_get_feature_value_string (id_buf, fobj, &val_st) )
436 val_string = cos_make_string ((char*)CONCORD_String_data (&val_st),
437 CONCORD_String_size (&val_st));
438 printf ("obj[%s]'s %s = %s\n",
440 concord_feature_get_name (fobj),
441 cos_string_data (val_string));
443 val_cid = cos_read_char (CONCORD_String_data (&val_st),
444 CONCORD_String_size (&val_st),
447 return cos_make_char (val_cid);
449 val_str = cos_read_string (CONCORD_String_data (&val_st),
450 CONCORD_String_size (&val_st),
452 if ( val_str != NULL )