(cos_char_id): Fixed.
[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_retain_function_table
32      [COS_OBJECT_TYPE_MAX - COS_Object_Type_char])
33   (COS_Object)
34   = { cos_retain_string,
35       cos_retain_symbol,
36       cos_retain_cons,
37       cos_retain_container,
38       cos_retain_sexp,
39       cos_retain_binary,
40       cos_retain_ds,
41       cos_retain_genre,
42       cos_retain_feature,
43       cos_retain_index,
44       cos_retain_db_object
45 };
46
47 int (*COS_Object_release_function_table
48      [COS_OBJECT_TYPE_MAX - COS_Object_Type_char])
49   (COS_Object)
50   = { cos_release_string,
51       cos_release_symbol,
52       cos_release_cons,
53       cos_release_container,
54       cos_release_sexp,
55       cos_release_binary,
56       cos_release_ds,
57       cos_release_genre,
58       cos_release_feature,
59       cos_release_index,
60       cos_release_db_object
61 };
62
63
64 COS_object
65 cos_make_int (COS_C_Int num)
66 {
67   return (COS_object)((COS_INT)(num << 1) | 1);
68 }
69
70 int
71 cos_int_p (COS_object obj)
72 {
73   return COS_OBJECT_INT_P (obj);
74 }
75
76 COS_C_Int
77 cos_int_value (COS_object obj)
78 {
79   if (COS_OBJECT_INT_P (obj))
80     return ((COS_INT)obj) >> 1;
81   return INTPTR_MIN;
82 }
83
84
85 COS_object
86 cos_make_char (int code)
87 {
88   return (COS_object)((COS_INT)(code << 2) | 2);
89 }
90
91 int
92 cos_char_p (COS_object obj)
93 {
94   return COS_OBJECT_CHAR_P (obj);
95 }
96
97 int
98 cos_char_id (COS_object obj)
99 {
100   if (COS_OBJECT_CHAR_P (obj))
101     return (((COS_INT)obj) >> 2) & 0x7FFFFFFF;
102   return -1;
103 }
104
105
106 COS_Object
107 cos_allocate_object_0 (enum COS_Object_Type type, size_t size)
108 {
109   COS_Object obj = (COS_Object)malloc (size);
110
111   if (obj == NULL)
112     return NULL;
113
114   obj->header.prefix = COS_OBJECT_PREFIX_OBJECT;
115   obj->header.type = type;
116   obj->header.reference_count = 0;
117
118   return obj;
119 }
120
121 COS_Object
122 cos_retain_object (COS_Object obj)
123 {
124   if (COS_OBJECT_P (obj))
125     {
126       obj->header.reference_count++;
127       (*COS_Object_retain_function_table
128        [((COS_Object)obj)->header.type
129         - COS_FAT_OBJECT_TYPE_MIN])(obj);
130     }
131   return obj;
132 }
133
134 int
135 cos_release_object (COS_object obj)
136 {
137   if (COS_OBJECT_P (obj))
138     {
139       if ( ((COS_Object)obj)->header.reference_count <= 0 )
140         return (*COS_Object_release_function_table
141                 [((COS_Object)obj)->header.type
142                  - COS_FAT_OBJECT_TYPE_MIN])(obj);
143       else
144         ((COS_Object)obj)->header.reference_count--;
145     }
146   return 0;
147 }
148
149
150 COS_String
151 cos_make_string (char* str, size_t size)
152 {
153   COS_String obj = COS_ALLOCATE_OBJECT (String);
154
155   if (obj == NULL)
156     return NULL;
157
158   obj->size = size;
159   obj->data = malloc (size + 1);
160   if (obj->data == NULL)
161     {
162       free (obj);
163       return NULL;
164     }
165
166   strncpy ((char*)obj->data, str, size);
167   obj->data[size] = '\0';
168   return obj;
169 }
170
171 COS_String
172 cos_build_string (char* str)
173 {
174 #if 0
175   COS_String obj = COS_ALLOCATE_OBJECT (string);
176
177   if (obj == NULL)
178     return NULL;
179
180   obj->size = strlen (str);
181   obj->data = malloc (obj->size + 1);
182   if (obj->data == NULL)
183     {
184       free (obj);
185       return NULL;
186     }
187
188   strncpy ((char*)obj->data, str, obj->size);
189   obj->data[obj->size] = '\0';
190   return obj;
191 #else
192   return cos_make_string (str, strlen (str));
193 #endif
194 }
195
196 int
197 cos_retain_string (COS_Object obj)
198 {
199   return 0;
200 }
201
202 int
203 cos_release_string (COS_Object obj)
204 {
205   if (obj == NULL)
206     return 0;
207
208   if ( ((COS_String)obj)->data != NULL)
209     free (((COS_String)obj)->data);
210   free (obj);
211   return 0;
212 }
213
214 int cos_string_p (COS_object obj)
215 {
216   return COS_OBJECT_STRING_P (obj);
217 }
218
219 size_t
220 cos_string_size (COS_String string)
221 {
222   return string->size;
223 }
224
225 char*
226 cos_string_data (COS_String string)
227 {
228   return (char*)string->data;
229 }
230
231
232 COS_Cons
233 cos_cons (COS_object car, COS_object cdr)
234 {
235   COS_Cons obj = COS_ALLOCATE_OBJECT (Cons);
236
237   if (obj == NULL)
238     return NULL;
239
240   obj->car = car;
241   obj->cdr = cdr;
242   cos_retain_object (car);
243   cos_retain_object (cdr);
244
245   return obj;
246 }
247
248 int
249 cos_retain_cons (COS_Object obj)
250 {
251   //cos_retain_object (COS_CAR (obj));
252   //cos_retain_object (COS_CDR (obj));
253   return 0;
254 }
255
256 int
257 cos_release_cons (COS_Object obj)
258 {
259   if (obj == NULL)
260     return 0;
261
262   cos_release_object (COS_CAR (obj));
263   cos_release_object (COS_CDR (obj));
264   free (obj);
265   return 0;
266 }
267
268 int
269 cos_cons_p (COS_object obj)
270 {
271   return COS_OBJECT_CONS_P (obj);
272 }
273
274 COS_object
275 cos_car (COS_Cons pair)
276 {
277   if (COS_OBJECT_CONS_P (pair))
278     return COS_CAR (pair);
279   else
280     return NULL;
281 }
282
283 COS_object
284 cos_cdr (COS_Cons pair)
285 {
286   if (COS_OBJECT_CONS_P (pair))
287     return COS_CDR (pair);
288   else
289     return NULL;
290 }
291
292 COS_Cons
293 cos_assoc (COS_object key, COS_Cons alist)
294 {
295   COS_Cons rest = alist;
296
297   while ( COS_OBJECT_CONS_P (rest) )
298     {
299       COS_Cons cell = cos_car (rest);
300
301       if ( cos_car (cell) == key )
302         return cell;
303
304       rest = cos_cdr (rest);
305     }
306   return NULL;
307 }
308
309 COS_object
310 cos_alist_get (COS_Cons alist, COS_object key)
311 {
312   return cos_cdr (cos_assoc (key, alist));
313 }
314
315
316 int
317 cos_retain_container (COS_Object obj)
318 {
319   return 0;
320 }
321
322 int
323 cos_release_container (COS_Object obj)
324 {
325   return 0;
326 }
327
328
329 int
330 cos_retain_sexp (COS_Object obj)
331 {
332   return 0;
333 }
334
335 int
336 cos_release_sexp (COS_Object obj)
337 {
338   return 0;
339 }
340
341
342 int
343 cos_retain_binary (COS_Object obj)
344 {
345   return 0;
346 }
347
348 int
349 cos_release_binary (COS_Object obj)
350 {
351   return 0;
352 }
353
354
355 COS_DS
356 concord_open_env (COS_object ds)
357 {
358   if (COS_OBJECT_DS_P (ds))
359     concord_current_env = (COS_DS)ds;
360   else
361     {
362       char* path;
363
364       if (COS_OBJECT_C_STRING_P (ds))
365         path = (char*)ds;
366       else if (ds == NULL)
367         path = CONCORD_SI_DB_DIR;
368       else
369         return NULL;
370
371       concord_current_env = concord_open_ds (CONCORD_Backend_Berkeley_DB,
372                                              path, 0, 0755);
373     }
374   return concord_current_env;
375 }
376
377 int
378 cos_retain_ds (COS_Object obj)
379 {
380   return 0;
381 }
382
383 int
384 cos_release_ds (COS_Object obj)
385 {
386   return concord_close_ds ((COS_DS)obj);
387 }
388
389
390 COS_Genre
391 concord_get_genre (COS_object ds, COS_object genre)
392 {
393   if (COS_OBJECT_C_STRING_P (genre))
394     return concord_ds_get_genre (ds, (char*)genre);
395   else if (COS_OBJECT_SYMBOL_P (genre))
396     return concord_ds_get_genre (ds, (char*)((COS_Symbol)genre)->name->data);
397   else if (COS_OBJECT_GENRE_P (genre))
398     return (COS_Genre)genre;
399   return NULL;
400 }
401
402 int
403 cos_retain_genre (COS_Object obj)
404 {
405   return 0;
406 }
407
408 int
409 cos_release_genre (COS_Object obj)
410 {
411   return concord_close_genre ((COS_Genre)obj);
412 }
413
414
415 COS_Feature
416 concord_get_feature (COS_object ds,
417                      COS_object genre, COS_object feature)
418 {
419   if (COS_OBJECT_FEATURE_P (feature))
420     return feature;
421   else
422     {
423       COS_Genre gobj = concord_get_genre (ds, genre);
424       char* feature_name;
425
426       if (COS_OBJECT_C_STRING_P (feature))
427         feature_name = (char*)feature;
428       else if (COS_OBJECT_STRING_P (feature))
429         feature_name = (char*)((COS_String)feature)->data;
430       else if (COS_OBJECT_SYMBOL_P (feature))
431         feature_name = (char*)((COS_Symbol)feature)->name->data;
432       else
433         return NULL;
434
435       return concord_genre_get_feature (gobj, feature_name);
436     }
437 }
438
439 int
440 cos_retain_feature (COS_Object obj)
441 {
442   return 0;
443 }
444
445 int
446 cos_release_feature (COS_Object obj)
447 {
448   return concord_close_feature ((COS_Feature)obj);
449 }
450
451
452 COS_Feature_INDEX
453 concord_get_feature_index (COS_object ds,
454                            COS_object genre, COS_object feature)
455 {
456   if (COS_OBJECT_FEATURE_INDEX_P (feature))
457     return feature;
458   else
459     {
460       COS_Feature fobj = concord_get_feature (ds, genre, feature);
461       COS_Genre gobj;
462       char* feature_name;
463
464       if (fobj == NULL)
465         return NULL;
466
467       gobj = concord_feature_get_genre (fobj);
468       feature_name = concord_feature_get_name (fobj);
469       return concord_genre_get_index (gobj, feature_name);
470     }
471 }
472
473 int
474 cos_retain_index (COS_Object obj)
475 {
476   return concord_close_index ((COS_Feature_INDEX)obj);
477 }
478
479 int
480 cos_release_index (COS_Object obj)
481 {
482   return concord_close_index ((COS_Feature_INDEX)obj);
483 }
484
485
486 int
487 cos_retain_db_object (COS_Object obj)
488 {
489   return 0;
490 }
491
492 int
493 cos_release_db_object (COS_Object obj)
494 {
495   return 0;
496 }
497
498 COS_object
499 concord_decode_object (COS_object ds, COS_object genre,
500                        COS_object feature, COS_object id)
501 {
502   COS_Feature_INDEX index = concord_get_feature_index (ds, genre, feature);
503   char* id_str;
504   char buf[256];
505   CONCORD_String_Tank obj_st;
506   COS_String obj_string;
507   int cid;
508   size_t end;
509
510   if (index == NULL)
511     return NULL;
512
513   printf ("decoding id (%lX)...", id);
514   if (COS_OBJECT_INT_P (id))
515     {
516       printf ("(id is an int)...");
517       snprintf(buf, 256, "%ld", cos_int_value (id));
518       id_str = buf;
519     }
520   else if (COS_OBJECT_CHAR_P (id))
521     {
522       printf ("(id is a char)...");
523       snprintf(buf, 256, "%ld", cos_char_id (id));
524       id_str = buf;
525     }
526   else if (COS_OBJECT_SYMBOL_P (id))
527     {
528       printf ("(id is a symbol)....");
529       id_str = cos_string_data (cos_symbol_name ((COS_Symbol)id));
530     }
531   else if (COS_OBJECT_C_STRING_P (id))
532     {
533       printf ("(id is a C-string)....");
534       id_str = (char*)id;
535     }
536   else if (COS_OBJECT_STRING_P (id))
537     {
538       printf ("(id is a string)....");
539       id_str = cos_string_data ((COS_String)id);
540     }
541   else
542     return NULL;
543   printf ("done (%s).\n", id_str);
544
545   if ( concord_index_strid_get_obj_string (index, id_str, &obj_st) )
546     return NULL;
547
548   obj_string = cos_make_string ((char*)CONCORD_String_data (&obj_st),
549                                 CONCORD_String_size (&obj_st));
550
551   cid = cos_read_char (CONCORD_String_data (&obj_st),
552                        CONCORD_String_size (&obj_st),
553                        0, &end);
554   if ( cid >= 0 )
555     {
556       printf ("obj = %s (%d, U+%04X), len = %d, end = %d\n",
557               cos_string_data (obj_string),
558               cid, cid,
559               CONCORD_String_size (&obj_st), end);
560       cos_release_object (obj_string);
561       return cos_make_char (cid);
562     }
563   else
564     printf ("obj = %s\n", cos_string_data (obj_string));
565
566   cos_release_object (obj_string);
567
568   return NULL;
569 }
570
571 COS_object
572 concord_object_get_feature_value (COS_object object, COS_object feature)
573 {
574   char id_buf[256];
575   CONCORD_Feature fobj;
576   CONCORD_String_Tank val_st;
577   COS_String val_string;
578   size_t end;
579
580   if (COS_OBJECT_CHAR_P (object))
581     {
582       cos_utf8_print_char (object, id_buf, 256);
583       printf ("Object[char:0x%lX]'s id is %s.\n", object, id_buf);
584     }
585   else
586     return NULL;
587
588   fobj = concord_get_feature (concord_current_env,
589                               "character", feature);
590   if (fobj == NULL)
591     return NULL;
592
593   if ( concord_obj_get_feature_value_string (id_buf, fobj, &val_st) )
594     return NULL;
595
596   val_string = cos_make_string ((char*)CONCORD_String_data (&val_st),
597                                 CONCORD_String_size (&val_st));
598   printf ("obj[%s]'s %s = %s\n",
599           id_buf,
600           concord_feature_get_name (fobj),
601           cos_string_data (val_string));
602
603   return cos_read_object (CONCORD_String_data (&val_st),
604                           CONCORD_String_size (&val_st),
605                           0, &end);
606 }