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>
26 #include "concord-bdb.h"
29 CONCORD_BDB_open (const char* db_dir,
34 u_int32_t accessmask, int modemask)
40 char *db_file_name, *sp;
43 status = db_create (&dbase, NULL, 0);
47 if ( (accessmask & DB_CREATE) && stat (db_dir, &statbuf) )
48 mkdir (db_dir, modemask);
50 len = strlen (db_dir);
51 name_len = strlen (name);
52 size = len + strlen (genre) + strlen (key_type) + name_len * 3 + 5;
53 db_file_name = alloca (size);
54 strcpy (db_file_name, db_dir);
55 if (db_file_name[len - 1] != '/')
57 db_file_name[len++] = '/';
58 db_file_name[len] = '\0';
61 strcat (db_file_name, genre);
62 if ( (accessmask & DB_CREATE) && stat (db_file_name, &statbuf) )
63 mkdir (db_file_name, modemask);
64 strcat (db_file_name, "/");
66 strcat (db_file_name, key_type);
67 if ( (accessmask & DB_CREATE) && stat (db_file_name, &statbuf) )
68 mkdir (db_file_name, modemask);
69 strcat (db_file_name, "/");
71 /* strcat (db_file_name, name); */
72 sp = &db_file_name[strlen (db_file_name)];
73 for (i = 0; i < name_len; i++)
77 if ( /* (c == '/') || (c == '%') */
78 strchr ("%/\\:*?\"<>|", c) != NULL )
80 sprintf (sp, "%%%02X", c);
87 #if DB_VERSION_MAJOR < 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR < 1)
88 status = dbase->open (dbase, db_file_name, NULL,
89 real_subtype, accessmask, modemask);
90 #else /* DB_VERSION >= 4.1 */
91 status = dbase->open (dbase, NULL, db_file_name, NULL,
93 accessmask /* | DB_AUTO_COMMIT */, modemask);
94 #endif /* DB_VERSION < 4.1 */
97 dbase->close (dbase, 0);
104 CONCORD_BDB_close (DB* db)
115 CONCORD_BDB_get (DB* db, const char* key, DBT* valdatum)
120 /* DB Version 2 requires DBT's to be zeroed before use. */
124 keydatum.data = (char*)key;
125 keydatum.size = strlen (key);
127 status = db->get (db, NULL, &keydatum, valdatum, 0);
132 CONCORD_BDB_put (DB* db, const char* key, unsigned char* value)
134 DBT keydatum, valdatum;
137 /* DB Version 2 requires DBT's to be zeroed before use. */
141 keydatum.data = (char*)key;
142 keydatum.size = strlen (key);
144 valdatum.data = value;
145 valdatum.size = strlen ((char*)value);
147 status = db->put (db, NULL, &keydatum, &valdatum, 0);