1 /* Copyright (C) 2003,2004,2005,2006 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
20 #include <sys/types.h>
29 #include "concord-name.h"
30 #include "concord-bdb.h"
34 CONCORD_String_size (const CONCORD_String s)
40 CONCORD_String_data (const CONCORD_String s)
45 CONCORD_Genre concord_ds_open_genre (CONCORD_DS ds, const char* name);
47 int concord_close_genre (CONCORD_Genre genre);
51 concord_genre_open_feature (CONCORD_Genre genre, const char* name);
53 int concord_close_feature (CONCORD_Feature feature);
57 concord_genre_open_index (CONCORD_Genre genre, const char* index);
60 concord_genre_get_feature_0 (CONCORD_Genre genre, const char* name);
62 int concord_close_index (CONCORD_INDEX table);
66 concord_default_read_object (const unsigned char* string, size_t length);
69 struct CONCORD_DS_Table
71 CONCORD_Backend_Type type;
73 CONCORD_NAME_TABLE* genre_names;
77 CONCORD_Object object_nil;
78 CONCORD_Object (*read_object) (const unsigned char* str, size_t length);
82 concord_default_read_object (const unsigned char* str, size_t length)
84 unsigned char* buf = malloc (length + 1);
88 strncpy ((char*)buf, (char*)str, length);
94 concord_open_ds (CONCORD_Backend_Type type, const char* location,
95 int subtype, int modemask)
97 CONCORD_DS ds = (CONCORD_DS)malloc (sizeof (CONCORD_DS_Table));
98 size_t len = strlen (location);
104 ds->subtype = ( (subtype != 0) ? subtype : DB_HASH );
105 ds->modemask = modemask;
106 ds->location = (char*)malloc (len + 1);
107 if (ds->location == NULL)
108 goto location_failure;
110 strcpy (ds->location, location);
112 ds->genre_names = concord_make_name_table ();
113 if (ds->genre_names == NULL)
121 ds->object_nil = NULL;
122 ds->read_object = &concord_default_read_object;
128 concord_close_ds (CONCORD_DS ds)
130 if (ds->location != NULL)
132 if (ds->genre_names != NULL)
133 concord_destroy_name_table (ds->genre_names);
139 concord_ds_location (CONCORD_DS ds)
145 concord_ds_set_object_failure (CONCORD_DS ds, CONCORD_Object object_nil)
147 ds->object_nil = object_nil;
152 concord_ds_set_read_object_function (CONCORD_DS ds,
153 CONCORD_Object (*read_object)
154 (const unsigned char* str,
157 ds->read_object = read_object;
162 concord_ds_get_genre (CONCORD_DS ds, const char* name)
166 genre = concord_name_table_get (ds->genre_names, name);
170 genre = concord_ds_open_genre (ds, name);
174 if (concord_name_table_put (ds->genre_names, name, genre))
176 concord_close_genre (genre);
183 concord_ds_foreach_genre_name (CONCORD_DS ds,
184 int (*func) (CONCORD_DS ds, char* name))
186 char* dname = ds->location;
190 if ( (dir = opendir (dname)) == NULL)
193 while ( (de = readdir (dir)) != NULL )
195 if ( (strcmp (de->d_name, ".") != 0) &&
196 (strcmp (de->d_name, "..") != 0) )
198 int i, need_to_decode = 0;
203 for (cp = (unsigned char*)de->d_name, i = 0; *cp != '\0'; i++)
214 name = (char*) alloca (i);
215 cp = (unsigned char*)de->d_name;
216 np = (unsigned char*)name;
218 while ( (ch = *cp++) != '\0')
234 if ( ('0' <= ch) && (ch <= '9') )
235 hex[index++] = ch - '0';
236 else if ( ('A' <= ch) && (ch <= 'F') )
237 hex[index++] = ch - 'A' + 10;
238 else if ( ('a' <= ch) && (ch <= 'f') )
239 hex[index++] = ch - 'a' + 10;
251 *np++ = (hex[0] << 4) | hex[1];
265 return closedir (dir);
268 return closedir (dir);
272 struct CONCORD_Genre_Table
276 CONCORD_NAME_TABLE* feature_names;
277 CONCORD_NAME_TABLE* index_names;
281 concord_ds_open_genre (CONCORD_DS ds, const char* name)
284 size_t len = strlen (name);
289 genre = (CONCORD_Genre)malloc (sizeof (CONCORD_Genre_Table));
294 genre->name = (char*)malloc (len + 1);
295 if (genre->name == NULL)
300 strcpy (genre->name, name);
302 genre->feature_names = concord_make_name_table ();
303 if (genre->feature_names == NULL)
310 genre->index_names = concord_make_name_table ();
311 if (genre->index_names == NULL)
313 free (genre->feature_names);
322 concord_close_genre (CONCORD_Genre genre)
329 if (genre->name == NULL)
337 if (genre->feature_names != NULL)
338 concord_destroy_name_table (genre->feature_names);
340 if (genre->index_names != NULL)
341 concord_destroy_name_table (genre->index_names);
348 concord_genre_get_name (CONCORD_Genre genre)
354 concord_genre_get_data_source (CONCORD_Genre genre)
360 concord_genre_foreach_feature_name (CONCORD_Genre genre,
361 int (*func) (CONCORD_Genre genre,
365 = alloca (strlen (genre->ds->location)
366 + 1 + strlen (genre->name) + sizeof ("/feature") + 1);
370 strcpy (dname, genre->ds->location);
372 strcat (dname, genre->name);
373 strcat (dname, "/feature");
375 if ( (dir = opendir (dname)) == NULL)
378 while ( (de = readdir (dir)) != NULL )
380 if ( (strcmp (de->d_name, ".") != 0) &&
381 (strcmp (de->d_name, "..") != 0) )
383 int i, need_to_decode = 0;
388 for (cp = (unsigned char*)de->d_name, i = 0; *cp != '\0'; i++)
399 name = (char*) alloca (i);
400 cp = (unsigned char*)de->d_name;
401 np = (unsigned char*)name;
403 while ( (ch = *cp++) != '\0')
419 if ( ('0' <= ch) && (ch <= '9') )
420 hex[index++] = ch - '0';
421 else if ( ('A' <= ch) && (ch <= 'F') )
422 hex[index++] = ch - 'A' + 10;
423 else if ( ('a' <= ch) && (ch <= 'f') )
424 hex[index++] = ch - 'a' + 10;
436 *np++ = (hex[0] << 4) | hex[1];
449 if (func (genre, name))
450 return closedir (dir);
453 return closedir (dir);
457 concord_genre_get_feature_0 (CONCORD_Genre genre, const char* name)
459 CONCORD_Feature feature;
461 feature = concord_name_table_get (genre->feature_names, name);
465 feature = concord_genre_open_feature (genre, name);
469 if (concord_name_table_put (genre->feature_names, name, feature))
471 concord_close_feature (feature);
478 concord_genre_get_feature (CONCORD_Genre genre, const char* name)
480 CONCORD_Genre g_feature
481 = concord_ds_get_genre (genre->ds, "feature");
483 if (g_feature != NULL)
485 CONCORD_Feature p_true_name
486 = concord_genre_get_feature_0 (g_feature, "true-name");
488 if (g_feature != NULL)
490 CONCORD_String_Tank s_true_name;
492 = concord_obj_get_feature_value_string (name,
497 char* t_name = alloca (s_true_name.size + 1);
499 strncpy (t_name, s_true_name.data, s_true_name.size);
500 t_name[s_true_name.size] = '\0';
501 return concord_genre_get_feature_0 (genre, t_name);
505 return concord_genre_get_feature_0 (genre, name);
509 concord_genre_get_index (CONCORD_Genre genre, const char* name)
513 index = concord_name_table_get (genre->index_names, name);
517 index = concord_genre_open_index (genre, name);
521 if (concord_name_table_put (genre->index_names, name, index))
523 concord_close_index (index);
530 struct CONCORD_Feature_Table
539 concord_genre_open_feature (CONCORD_Genre genre, const char* feature)
541 CONCORD_Feature table;
542 size_t len = strlen (feature);
547 table = (CONCORD_Feature)malloc (sizeof (CONCORD_Feature_Table));
551 table->genre = genre;
554 table->name = (char*)malloc (len + 1);
555 if (table->name == NULL)
560 strcpy (table->name, feature);
565 concord_close_feature (CONCORD_Feature feature)
572 if (feature->db == NULL)
575 status = CONCORD_BDB_close (feature->db);
577 if (feature->name == NULL)
581 free (feature->name);
589 concord_feature_get_name (CONCORD_Feature feature)
591 return feature->name;
595 concord_feature_get_genre (CONCORD_Feature feature)
597 return feature->genre;
601 concord_feature_setup_db (CONCORD_Feature feature, int writable)
610 if ((feature->access & DB_CREATE) == 0)
612 if (feature->db != NULL)
614 CONCORD_BDB_close (feature->db);
624 if (feature->db == NULL)
626 CONCORD_Genre genre = feature->genre;
629 = CONCORD_BDB_open (genre->ds->location, genre->name,
630 "feature", feature->name,
632 access, genre->ds->modemask);
633 if (feature->db == NULL)
635 feature->access = access;
641 concord_feature_sync (CONCORD_Feature feature)
645 if (feature->db == NULL)
648 status = CONCORD_BDB_close (feature->db);
655 concord_obj_put_feature_value_str (const char* object_id,
656 CONCORD_Feature feature,
657 unsigned char* value)
661 if (concord_feature_setup_db (feature, 1))
663 return CONCORD_BDB_put (feature->db, object_id, value);
667 concord_obj_get_feature_value_string (const char* object_id,
668 CONCORD_Feature feature,
669 CONCORD_String value)
673 if (concord_feature_setup_db (feature, 0))
675 status = CONCORD_BDB_get (feature->db, object_id, value);
680 concord_obj_get_feature_value (const char* object_id,
681 CONCORD_Feature feature)
686 if (concord_feature_setup_db (feature, 0))
687 return feature->genre->ds->object_nil;
688 status = CONCORD_BDB_get (feature->db, object_id, &valdatum);
690 return feature->genre->ds->object_nil;
691 return (*feature->genre->ds->read_object)(valdatum.data, valdatum.size);
695 concord_obj_gets_feature_value (const char* object_id,
696 CONCORD_Feature feature,
697 unsigned char* dst, size_t size)
702 if (concord_feature_setup_db (feature, 0))
704 status = CONCORD_BDB_get (feature->db, object_id, &valdatum);
707 if (size < valdatum.size)
709 strncpy ((char*)dst, valdatum.data, valdatum.size);
710 dst[valdatum.size] = '\0';
715 concord_feature_foreach_obj_string (CONCORD_Feature feature,
716 int (*func)(CONCORD_String object_id,
717 CONCORD_Feature feature,
718 CONCORD_String value))
720 CONCORD_String_Tank key, value;
724 if (concord_feature_setup_db (feature, 0))
729 status = feature->db->cursor (feature->db, NULL, &dbcp, 0);
730 for (status = dbcp->c_get (dbcp, &key, &value, DB_FIRST);
732 status = dbcp->c_get (dbcp, &key, &value, DB_NEXT))
734 int ret = func (&key, feature, &value);
739 dbcp->c_close (dbcp);
744 struct CONCORD_INDEX_Table
753 concord_genre_open_index (CONCORD_Genre genre, const char* index)
756 size_t len = strlen (index);
761 table = (CONCORD_INDEX)malloc (sizeof (CONCORD_INDEX_Table));
765 table->genre = genre;
768 table->name = (char*)malloc (len + 1);
769 if (table->name == NULL)
774 strcpy (table->name, index);
779 concord_close_index (CONCORD_INDEX table)
786 if (table->db == NULL)
789 status = CONCORD_BDB_close (table->db);
791 if (table->name == NULL)
803 concord_index_setup_db (CONCORD_INDEX index, int writable)
812 if ((index->access & DB_CREATE) == 0)
814 if (index->db != NULL)
816 CONCORD_BDB_close (index->db);
826 if (index->db == NULL)
828 CONCORD_Genre genre = index->genre;
831 = CONCORD_BDB_open (genre->ds->location, genre->name,
832 "index", index->name,
834 access, genre->ds->modemask);
835 if (index->db == NULL)
837 index->access = access;
843 concord_index_sync (CONCORD_INDEX index)
847 if (index->db == NULL)
850 status = CONCORD_BDB_close (index->db);
857 concord_index_strid_put_obj (CONCORD_INDEX index,
858 const char* strid, char* object_id)
863 if (concord_index_setup_db (index, 1))
866 return CONCORD_BDB_put (index->db, strid, (unsigned char*)object_id);
870 concord_index_strid_get_obj_string (CONCORD_INDEX index,
872 CONCORD_String object_id)
877 if (concord_index_setup_db (index, 0))
880 return CONCORD_BDB_get (index->db, strid, object_id);