]>
Commit | Line | Data |
---|---|---|
12471842 PL |
1 | /* |
2 | * This file is part of PowerDNS or dnsdist. | |
3 | * Copyright -- PowerDNS.COM B.V. and its contributors | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of version 2 of the GNU General Public License as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * In addition, for the avoidance of any doubt, permission is granted to | |
10 | * link this program with OpenSSL and to (re)distribute the binaries | |
11 | * produced as the result of such linking. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
21 | */ | |
e8c59f2d | 22 | #pragma once |
ddee6d08 | 23 | #include <cdb.h> |
b4aac52a | 24 | |
90fe8ae6 | 25 | #include "misc.hh" |
ddee6d08 PD |
26 | |
27 | // This class is responsible for the reading of a CDB file. | |
28 | // The constructor opens the CDB file, the destructor closes it, so make sure you call that. | |
29 | class CDB | |
30 | { | |
31 | public: | |
9153c7cf AT |
32 | CDB(const string &cdbfile); |
33 | ~CDB(); | |
ddee6d08 | 34 | |
90fe8ae6 RG |
35 | /* Return negative value on error or non-negative value on success. |
36 | Values can be retrieved via readNext() */ | |
9153c7cf AT |
37 | int searchKey(const string &key); |
38 | bool searchSuffix(const string &key); | |
39 | void searchAll(); | |
40 | bool readNext(pair<string, string> &value); | |
41 | vector<string> findall(string &key); | |
73e1f0c5 | 42 | bool keyExists(const string& key); |
90fe8ae6 | 43 | bool findOne(const string& key, string& value); |
ddee6d08 PD |
44 | |
45 | private: | |
9153c7cf | 46 | bool moveToNext(); |
b4aac52a RG |
47 | |
48 | int d_fd{-1}; | |
9153c7cf AT |
49 | struct cdb d_cdb; |
50 | struct cdb_find d_cdbf; | |
b4aac52a RG |
51 | std::string d_key; |
52 | unsigned d_seqPtr{0}; | |
53 | enum SearchType { SearchSuffix, SearchKey, SearchAll } d_searchType{SearchKey}; | |
ddee6d08 PD |
54 | }; |
55 | ||
90fe8ae6 RG |
56 | class CDBWriter |
57 | { | |
58 | public: | |
59 | /* we own the fd after this call, don't ever touch it */ | |
60 | CDBWriter(int fd); | |
61 | ~CDBWriter(); | |
62 | ||
63 | bool addEntry(const std::string& key, const std::string& value); | |
64 | /* finalize the database and close the fd, the only thing you can do now is to call the destructor */ | |
65 | void close(); | |
66 | ||
67 | private: | |
68 | struct cdb_make d_cdbm; | |
69 | int d_fd{-1}; | |
70 | }; |