]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/db++.h
Security-164.1.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / db++.h
1 /*
2 * Copyright (c) 2003 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 //
20 // dbm++ - generic C++ layer interface to [n]dbm
21 //
22 #ifndef _H_DBMPP
23 #define _H_DBMPP
24
25 #include <Security/utilities.h>
26 #include <Security/cssmdata.h>
27 #include <Security/unix++.h>
28 #include <string>
29 #include <db.h>
30
31
32 namespace Security {
33 namespace UnixPlusPlus {
34
35
36 class UnixDb : public FileDesc {
37 public:
38 UnixDb();
39 UnixDb(const char *path, int flags = O_RDWR, int mode = 0666, DBTYPE type = DB_HASH);
40 UnixDb(const std::string &path, int flags = O_RDWR, int mode = 0666, DBTYPE type = DB_HASH);
41
42 virtual ~UnixDb();
43
44 void open(const char *path, int flags = O_RDWR, int mode = 0666, DBTYPE type = DB_HASH);
45 void open(const std::string &path, int flags = O_RDWR, int mode = 0666, DBTYPE type = DB_HASH);
46 void close();
47
48 bool get(const CssmData &key, CssmData &value, int flags = 0) const;
49 bool get(const CssmData &key, CssmOwnedData &value, int flags = 0) const;
50 bool put(const CssmData &key, const CssmData &value, int flags = 0);
51 void erase(const CssmData &key, int flags = 0);
52 void flush(int flags = 0);
53
54 bool next(CssmData &key, CssmData &value, int flags = R_NEXT) const;
55 bool first(CssmData &key, CssmData &value) const
56 { return next(key, value, R_FIRST); }
57
58 operator bool () const
59 { return mDb; }
60
61 public:
62 struct Data : public PodWrapper<Data, DBT> {
63 template <class T>
64 Data(const T &src) { DBT::data = src.data(); DBT::size = src.length(); }
65
66 Data() { }
67 Data(void *data, size_t length) { DBT::data = data; DBT::size = length; }
68 Data(const DBT &dat) { DBT::data = dat.data; DBT::size = dat.size; }
69
70 void *data() const { return DBT::data; }
71 size_t length() const { return size; }
72 operator bool () const { return DBT::data != NULL; }
73 operator CssmData () const { return CssmData(data(), length()); }
74 };
75
76 private:
77 DB *mDb;
78 };
79
80
81 } // end namespace UnixPlusPlus
82 } // end namespace Security
83
84
85 #endif //_H_DBMPP