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
24 const char concord_db_format_version[] = CONCORD_DB_FORMAT_VERSION;
25 const char concord_db_dir[] = CONCORD_DB_DIR;
26 const char concord_system_db_dir[] = CONCORD_SI_DB_DIR;
28 int (*COS_Object_release_function_table
29 [COS_OBJECT_TYPE_MAX - COS_Object_Type_char])
31 = { cos_release_string,
33 cos_release_container,
45 cos_make_int (COS_C_Int num)
47 return (COS_object)((COS_INT)(num << 1) | 1);
51 cos_int_value (COS_object obj)
53 if (COS_OBJECT_INT_P (obj))
54 return ((COS_INT)obj) >> 1;
59 cos_int_p (COS_object obj)
61 return COS_OBJECT_INT_P (obj);
66 cos_make_char (int code)
68 return (COS_object)((COS_INT)(code << 2) | 2);
72 cos_char_id (COS_object obj)
74 if (COS_OBJECT_CHAR_P (obj))
75 return ((COS_INT)obj) >> 2;
80 cos_char_p (COS_object obj)
82 return COS_OBJECT_CHAR_P (obj);
87 cos_allocate_object_0 (enum COS_Object_Type type, size_t size)
89 COS_Object obj = (COS_Object)malloc (size);
94 obj->header.prefix = COS_OBJECT_PREFIX_OBJECT;
95 obj->header.type = type;
96 obj->header.reference_count = 0;
102 cos_retain_object (COS_Object obj)
104 obj->header.reference_count++;
110 cos_release_object (COS_object obj)
112 ((COS_Object)obj)->header.reference_count--;
114 if ( ((COS_Object)obj)->header.reference_count <= 0 )
115 return (*COS_Object_release_function_table
116 [((COS_Object)obj)->header.type
117 - COS_FAT_OBJECT_TYPE_MIN])(obj);
124 cos_make_string (char* str, size_t size)
126 COS_String obj = COS_ALLOCATE_OBJECT (String);
132 obj->data = malloc (size + 1);
133 if (obj->data == NULL)
139 strncpy ((char*)obj->data, str, size);
140 obj->data[size] = '\0';
145 cos_build_string (char* str)
148 COS_String obj = COS_ALLOCATE_OBJECT (string);
153 obj->size = strlen (str);
154 obj->data = malloc (obj->size + 1);
155 if (obj->data == NULL)
161 strncpy ((char*)obj->data, str, obj->size);
162 obj->data[obj->size] = '\0';
165 return cos_make_string (str, strlen (str));
170 cos_release_string (COS_Object obj)
175 if ( ((COS_String)obj)->data != NULL)
176 free (((COS_String)obj)->data);
182 cos_string_size (COS_String string)
188 cos_string_data (COS_String string)
190 return (char*)string->data;
195 cos_release_container (COS_Object obj)
202 cos_release_sexp (COS_Object obj)
209 cos_release_binary (COS_Object obj)
216 concord_open_env (COS_object ds)
218 if (COS_OBJECT_DS_P (ds))
224 if (COS_OBJECT_C_STRING_P (ds))
227 path = CONCORD_SI_DB_DIR;
231 return concord_open_ds (CONCORD_Backend_Berkeley_DB,
237 cos_release_ds (COS_Object obj)
239 return concord_close_ds ((COS_DS)obj);
244 concord_get_genre (COS_object ds, COS_object genre)
246 if (COS_OBJECT_C_STRING_P (genre))
247 return concord_ds_get_genre (ds, (char*)genre);
248 else if (COS_OBJECT_SYMBOL_P (genre))
249 return concord_ds_get_genre (ds, (char*)((COS_Symbol)genre)->name->data);
250 else if (COS_OBJECT_GENRE_P (genre))
251 return (COS_Genre)genre;
256 cos_release_genre (COS_Object obj)
258 return concord_close_genre ((COS_Genre)obj);
263 concord_get_feature (COS_object ds,
264 COS_object genre, COS_object feature)
266 if (COS_OBJECT_FEATURE_P (feature))
270 COS_Genre gobj = concord_get_genre (ds, genre);
273 if (COS_OBJECT_C_STRING_P (feature))
274 feature_name = (char*)feature;
275 else if (COS_OBJECT_STRING_P (feature))
276 feature_name = (char*)((COS_String)feature)->data;
277 else if (COS_OBJECT_SYMBOL_P (feature))
278 feature_name = (char*)((COS_Symbol)feature)->name->data;
282 return concord_genre_get_feature (gobj, feature_name);
287 cos_release_feature (COS_Object obj)
289 return concord_close_feature ((COS_Feature)obj);
294 concord_get_feature_index (COS_object ds,
295 COS_object genre, COS_object feature)
297 if (COS_OBJECT_FEATURE_INDEX_P (feature))
301 COS_Feature fobj = concord_get_feature (ds, genre, feature);
308 gobj = concord_feature_get_genre (fobj);
309 feature_name = concord_feature_get_name (fobj);
310 return concord_genre_get_index (gobj, feature_name);
315 cos_release_index (COS_Object obj)
317 return concord_close_index ((COS_Feature_INDEX)obj);
322 cos_release_db_object (COS_Object obj)
328 concord_decode_object (COS_object ds, COS_object genre,
329 COS_object feature, COS_object id)
331 COS_Feature_INDEX index = concord_get_feature_index (ds, genre, feature);
334 CONCORD_String_Tank obj_st;
335 COS_String obj_string;
342 printf ("decoding id (%lX)...", id);
343 if (COS_OBJECT_INT_P (id))
345 printf ("(id is an int)...");
346 snprintf(buf, 256, "%ld", cos_int_value (id));
349 else if (COS_OBJECT_CHAR_P (id))
351 printf ("(id is a char)...");
352 snprintf(buf, 256, "%ld", cos_char_id (id));
355 else if (COS_OBJECT_SYMBOL_P (id))
357 printf ("(id is a symbol)....");
358 id_str = cos_string_data (cos_symbol_name ((COS_Symbol)id));
360 else if (COS_OBJECT_C_STRING_P (id))
362 printf ("(id is a C-string)....");
365 else if (COS_OBJECT_STRING_P (id))
367 printf ("(id is a string)....");
368 id_str = cos_string_data ((COS_String)id);
372 printf ("done (%s).\n", id_str);
374 if ( concord_index_strid_get_obj_string (index, id_str, &obj_st) )
377 obj_string = cos_make_string ((char*)CONCORD_String_data (&obj_st),
378 CONCORD_String_size (&obj_st));
380 cid = cos_read_char (CONCORD_String_data (&obj_st),
381 CONCORD_String_size (&obj_st),
385 printf ("obj = %s (%d, U+%04X), len = %d, end = %d\n",
386 cos_string_data (obj_string),
388 CONCORD_String_size (&obj_st), end);
389 cos_release_object (obj_string);
390 return cos_make_char (cid);
393 printf ("obj = %s\n", cos_string_data (obj_string));
395 cos_release_object (obj_string);