update.
[chise/concord.git] / cos.c
1 /* Copyright (C) 2013 MORIOKA Tomohiko
2    This file is part of the CONCORD Library.
3
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.
8
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.
13
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
17    02111-1307 USA.  */
18
19 #include <stdlib.h>
20 #include "sysdep.h"
21 #include "cos-i.h"
22 #include "cos-read.h"
23 #include "cos-print.h"
24
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;
28
29 CONCORD_DS concord_current_env = NULL;
30
31 int (*COS_Object_release_function_table
32      [COS_OBJECT_TYPE_MAX - COS_Object_Type_char])
33   (COS_Object)
34   = { cos_release_string,
35       cos_release_symbol,
36       cos_release_cons,
37       cos_release_container,
38       cos_release_sexp,
39       cos_release_binary,
40       cos_release_ds,
41       cos_release_genre,
42       cos_release_feature,
43       cos_release_index,
44       cos_release_db_object
45 };
46
47
48 COS_object
49 cos_make_int (COS_C_Int num)
50 {
51   return (COS_object)((COS_INT)(num << 1) | 1);
52 }
53
54 COS_C_Int
55 cos_int_value (COS_object obj)
56 {
57   if (COS_OBJECT_INT_P (obj))
58     return ((COS_INT)obj) >> 1;
59   return INTPTR_MIN;
60 }
61
62 int
63 cos_int_p (COS_object obj)
64 {
65   return COS_OBJECT_INT_P (obj);
66 }
67
68
69 COS_object
70 cos_make_char (int code)
71 {
72   return (COS_object)((COS_INT)(code << 2) | 2);
73 }
74
75 int
76 cos_char_id (COS_object obj)
77 {
78   if (COS_OBJECT_CHAR_P (obj))
79     return ((COS_INT)obj) >> 2;
80   return -1;
81 }
82
83 int
84 cos_char_p (COS_object obj)
85 {
86   return COS_OBJECT_CHAR_P (obj);
87 }
88
89
90 COS_Object
91 cos_allocate_object_0 (enum COS_Object_Type type, size_t size)
92 {
93   COS_Object obj = (COS_Object)malloc (size);
94
95   if (obj == NULL)
96     return NULL;
97
98   obj->header.prefix = COS_OBJECT_PREFIX_OBJECT;
99   obj->header.type = type;
100   obj->header.reference_count = 0;
101
102   return obj;
103 }
104
105 COS_Object
106 cos_retain_object (COS_Object obj)
107 {
108   if (COS_OBJECT_P (obj))
109     obj->header.reference_count++;
110   return obj;
111 }
112
113 int
114 cos_release_object (COS_object obj)
115 {
116   if (COS_OBJECT_P (obj))
117     {
118       ((COS_Object)obj)->header.reference_count--;
119
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);
124     }
125   return 0;
126 }
127
128
129 COS_String
130 cos_make_string (char* str, size_t size)
131 {
132   COS_String obj = COS_ALLOCATE_OBJECT (String);
133
134   if (obj == NULL)
135     return NULL;
136
137   obj->size = size;
138   obj->data = malloc (size + 1);
139   if (obj->data == NULL)
140     {
141       free (obj);
142       return NULL;
143     }
144
145   strncpy ((char*)obj->data, str, size);
146   obj->data[size] = '\0';
147   return obj;
148 }
149
150 COS_String
151 cos_build_string (char* str)
152 {
153 #if 0
154   COS_String obj = COS_ALLOCATE_OBJECT (string);
155
156   if (obj == NULL)
157     return NULL;
158
159   obj->size = strlen (str);
160   obj->data = malloc (obj->size + 1);
161   if (obj->data == NULL)
162     {
163       free (obj);
164       return NULL;
165     }
166
167   strncpy ((char*)obj->data, str, obj->size);
168   obj->data[obj->size] = '\0';
169   return obj;
170 #else
171   return cos_make_string (str, strlen (str));
172 #endif
173 }
174
175 int
176 cos_release_string (COS_Object obj)
177 {
178   if (obj == NULL)
179     return 0;
180
181   if ( ((COS_String)obj)->data != NULL)
182     free (((COS_String)obj)->data);
183   free (obj);
184   return 0;
185 }
186
187 int cos_string_p (COS_object obj)
188 {
189   return COS_OBJECT_STRING_P (obj);
190 }
191
192 size_t
193 cos_string_size (COS_String string)
194 {
195   return string->size;
196 }
197
198 char*
199 cos_string_data (COS_String string)
200 {
201   return (char*)string->data;
202 }
203
204
205 COS_Cons
206 cos_cons (COS_object car, COS_object cdr)
207 {
208   COS_Cons obj = COS_ALLOCATE_OBJECT (Cons);
209
210   if (obj == NULL)
211     return NULL;
212
213   obj->car = car;
214   obj->cdr = cdr;
215   cos_retain_object (car);
216   cos_retain_object (cdr);
217
218   return obj;
219 }
220
221 int
222 cos_release_cons (COS_Object obj)
223 {
224   if (obj == NULL)
225     return 0;
226
227   cos_release_object (COS_CAR (obj));
228   cos_release_object (COS_CDR (obj));
229   free (obj);
230   return 0;
231 }
232
233 COS_object
234 cos_car (COS_Cons pair)
235 {
236   if (COS_OBJECT_CONS_P (pair))
237     return COS_CAR (pair);
238   else
239     return NULL;
240 }
241
242 COS_object
243 cos_cdr (COS_Cons pair)
244 {
245   if (COS_OBJECT_CONS_P (pair))
246     return COS_CDR (pair);
247   else
248     return NULL;
249 }
250
251
252 int
253 cos_release_container (COS_Object obj)
254 {
255   return 0;
256 }
257
258
259 int
260 cos_release_sexp (COS_Object obj)
261 {
262   return 0;
263 }
264
265
266 int
267 cos_release_binary (COS_Object obj)
268 {
269   return 0;
270 }
271
272
273 COS_DS
274 concord_open_env (COS_object ds)
275 {
276   if (COS_OBJECT_DS_P (ds))
277     concord_current_env = (COS_DS)ds;
278   else
279     {
280       char* path;
281
282       if (COS_OBJECT_C_STRING_P (ds))
283         path = (char*)ds;
284       else if (ds == NULL)
285         path = CONCORD_SI_DB_DIR;
286       else
287         return NULL;
288
289       concord_current_env = concord_open_ds (CONCORD_Backend_Berkeley_DB,
290                                              path, 0, 0755);
291     }
292   return concord_current_env;
293 }
294
295 int
296 cos_release_ds (COS_Object obj)
297 {
298   return concord_close_ds ((COS_DS)obj);
299 }
300
301
302 COS_Genre
303 concord_get_genre (COS_object ds, COS_object genre)
304 {
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;
311   return NULL;
312 }
313
314 int
315 cos_release_genre (COS_Object obj)
316 {
317   return concord_close_genre ((COS_Genre)obj);
318 }
319
320
321 COS_Feature
322 concord_get_feature (COS_object ds,
323                      COS_object genre, COS_object feature)
324 {
325   if (COS_OBJECT_FEATURE_P (feature))
326     return feature;
327   else
328     {
329       COS_Genre gobj = concord_get_genre (ds, genre);
330       char* feature_name;
331
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;
338       else
339         return NULL;
340
341       return concord_genre_get_feature (gobj, feature_name);
342     }
343 }
344
345 int
346 cos_release_feature (COS_Object obj)
347 {
348   return concord_close_feature ((COS_Feature)obj);
349 }
350
351
352 COS_Feature_INDEX
353 concord_get_feature_index (COS_object ds,
354                            COS_object genre, COS_object feature)
355 {
356   if (COS_OBJECT_FEATURE_INDEX_P (feature))
357     return feature;
358   else
359     {
360       COS_Feature fobj = concord_get_feature (ds, genre, feature);
361       COS_Genre gobj;
362       char* feature_name;
363
364       if (fobj == NULL)
365         return NULL;
366
367       gobj = concord_feature_get_genre (fobj);
368       feature_name = concord_feature_get_name (fobj);
369       return concord_genre_get_index (gobj, feature_name);
370     }
371 }
372
373 int
374 cos_release_index (COS_Object obj)
375 {
376   return concord_close_index ((COS_Feature_INDEX)obj);
377 }
378
379
380 int
381 cos_release_db_object (COS_Object obj)
382 {
383   return 0;
384 }
385
386 COS_object
387 concord_decode_object (COS_object ds, COS_object genre,
388                        COS_object feature, COS_object id)
389 {
390   COS_Feature_INDEX index = concord_get_feature_index (ds, genre, feature);
391   char* id_str;
392   char buf[256];
393   CONCORD_String_Tank obj_st;
394   COS_String obj_string;
395   int cid;
396   size_t end;
397
398   if (index == NULL)
399     return NULL;
400
401   printf ("decoding id (%lX)...", id);
402   if (COS_OBJECT_INT_P (id))
403     {
404       printf ("(id is an int)...");
405       snprintf(buf, 256, "%ld", cos_int_value (id));
406       id_str = buf;
407     }
408   else if (COS_OBJECT_CHAR_P (id))
409     {
410       printf ("(id is a char)...");
411       snprintf(buf, 256, "%ld", cos_char_id (id));
412       id_str = buf;
413     }
414   else if (COS_OBJECT_SYMBOL_P (id))
415     {
416       printf ("(id is a symbol)....");
417       id_str = cos_string_data (cos_symbol_name ((COS_Symbol)id));
418     }
419   else if (COS_OBJECT_C_STRING_P (id))
420     {
421       printf ("(id is a C-string)....");
422       id_str = (char*)id;
423     }
424   else if (COS_OBJECT_STRING_P (id))
425     {
426       printf ("(id is a string)....");
427       id_str = cos_string_data ((COS_String)id);
428     }
429   else
430     return NULL;
431   printf ("done (%s).\n", id_str);
432
433   if ( concord_index_strid_get_obj_string (index, id_str, &obj_st) )
434     return NULL;
435
436   obj_string = cos_make_string ((char*)CONCORD_String_data (&obj_st),
437                                 CONCORD_String_size (&obj_st));
438
439   cid = cos_read_char (CONCORD_String_data (&obj_st),
440                        CONCORD_String_size (&obj_st),
441                        0, &end);
442   if ( cid >= 0 )
443     {
444       printf ("obj = %s (%d, U+%04X), len = %d, end = %d\n",
445               cos_string_data (obj_string),
446               cid, cid,
447               CONCORD_String_size (&obj_st), end);
448       cos_release_object (obj_string);
449       return cos_make_char (cid);
450     }
451   else
452     printf ("obj = %s\n", cos_string_data (obj_string));
453
454   cos_release_object (obj_string);
455
456   return NULL;
457 }
458
459 COS_object
460 concord_object_get_feature_value (COS_object object, COS_object feature)
461 {
462   char id_buf[256];
463   CONCORD_Feature fobj;
464   CONCORD_String_Tank val_st;
465   COS_String val_string;
466   size_t end;
467   int val_cid;
468   COS_String val_str;
469   COS_object val_obj;
470
471   if (COS_OBJECT_CHAR_P (object))
472     {
473       cos_utf8_print_char (object, id_buf, 256);
474       printf ("Object[char:0x%lX]'s id is %s.\n", object, id_buf);
475     }
476   else
477     return NULL;
478
479   fobj = concord_get_feature (concord_current_env,
480                               "character", feature);
481   if (fobj == NULL)
482     return NULL;
483
484   if ( concord_obj_get_feature_value_string (id_buf, fobj, &val_st) )
485     return NULL;
486
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",
490           id_buf,
491           concord_feature_get_name (fobj),
492           cos_string_data (val_string));
493
494   val_obj = cos_read_int (CONCORD_String_data (&val_st),
495                           CONCORD_String_size (&val_st),
496                           0, &end);
497   if ( val_obj != NULL )
498     return val_obj;
499
500   val_cid = cos_read_char (CONCORD_String_data (&val_st),
501                            CONCORD_String_size (&val_st),
502                            0, &end);
503   if ( val_cid >= 0 )
504     return cos_make_char (val_cid);
505
506   val_str = cos_read_string (CONCORD_String_data (&val_st),
507                              CONCORD_String_size (&val_st),
508                              0, &end);
509   if ( val_str != NULL )
510     return val_str;
511
512   return NULL;
513 }