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
21 #include <sys/types.h>
29 #include "concord-bdb.h"
32 CONCORD_BDB_open (const char* db_dir,
37 u_int32_t accessmask, int modemask)
43 char *db_file_name, *sp;
46 status = db_create (&dbase, NULL, 0);
50 if ( (accessmask & DB_CREATE) && stat (db_dir, &statbuf) )
51 mkdir (db_dir, modemask);
53 len = strlen (db_dir);
54 name_len = strlen (name);
55 size = len + strlen (genre) + strlen (key_type) + name_len * 3 + 5;
56 db_file_name = alloca (size);
57 strcpy (db_file_name, db_dir);
58 if (db_file_name[len - 1] != '/')
60 db_file_name[len++] = '/';
61 db_file_name[len] = '\0';
64 strcat (db_file_name, genre);
65 if ( (accessmask & DB_CREATE) && stat (db_file_name, &statbuf) )
66 mkdir (db_file_name, modemask);
67 strcat (db_file_name, "/");
69 strcat (db_file_name, key_type);
70 if ( (accessmask & DB_CREATE) && stat (db_file_name, &statbuf) )
71 mkdir (db_file_name, modemask);
72 strcat (db_file_name, "/");
74 /* strcat (db_file_name, name); */
75 sp = &db_file_name[strlen (db_file_name)];
76 for (i = 0; i < name_len; i++)
80 if ( /* (c == '/') || (c == '%') */
81 strchr ("%/\\:*?\"<>|", c) != NULL )
83 sprintf (sp, "%%%02X", c);
90 #if DB_VERSION_MAJOR < 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR < 1)
91 status = dbase->open (dbase, db_file_name, NULL,
92 real_subtype, accessmask, modemask);
93 #else /* DB_VERSION >= 4.1 */
94 status = dbase->open (dbase, NULL, db_file_name, NULL,
96 accessmask /* | DB_AUTO_COMMIT */, modemask);
97 #endif /* DB_VERSION < 4.1 */
100 dbase->close (dbase, 0);
107 CONCORD_BDB_close (DB* db)
118 CONCORD_BDB_get (DB* db, const char* key, DBT* valdatum)
123 /* DB Version 2 requires DBT's to be zeroed before use. */
127 keydatum.data = (char*)key;
128 keydatum.size = strlen (key);
130 status = db->get (db, NULL, &keydatum, valdatum, 0);
135 CONCORD_BDB_put (DB* db, const char* key, unsigned char* value)
137 DBT keydatum, valdatum;
140 /* DB Version 2 requires DBT's to be zeroed before use. */
144 keydatum.data = (char*)key;
145 keydatum.size = strlen (key);
147 valdatum.data = value;
148 valdatum.size = strlen ((char*)value);
150 status = db->put (db, NULL, &keydatum, &valdatum, 0);