|
0
|
1 |
// Copyright (C) 1999,2000 Bruce Guenter <bruceg@em.ca>
|
|
|
2 |
//
|
|
|
3 |
// This program is free software; you can redistribute it and/or modify
|
|
|
4 |
// it under the terms of the GNU General Public License as published by
|
|
|
5 |
// the Free Software Foundation; either version 2 of the License, or
|
|
|
6 |
// (at your option) any later version.
|
|
|
7 |
//
|
|
|
8 |
// This program is distributed in the hope that it will be useful,
|
|
|
9 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
// GNU General Public License for more details.
|
|
|
12 |
//
|
|
|
13 |
// You should have received a copy of the GNU General Public License
|
|
|
14 |
// along with this program; if not, write to the Free Software
|
|
|
15 |
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
16 |
|
|
|
17 |
#include "cdb++.h"
|
|
|
18 |
#include "internal.h"
|
|
|
19 |
|
|
2
|
20 |
char* cdb_reader::seek(const mystring& key, uint32& dlen)
|
|
0
|
21 |
{
|
|
|
22 |
if(failed)
|
|
2
|
23 |
return 0;
|
|
0
|
24 |
uint32 h = hash(key);
|
|
|
25 |
uint32 pos = 8 * (h & 255);
|
|
|
26 |
|
|
2
|
27 |
uint32 poshash = unpack(map+pos);
|
|
|
28 |
uint32 lenhash = unpack(map+pos+4);
|
|
0
|
29 |
|
|
2
|
30 |
if(!lenhash)
|
|
|
31 |
return 0;
|
|
0
|
32 |
uint32 h2 = (h >> 8) % lenhash;
|
|
|
33 |
|
|
|
34 |
for (uint32 loop = 0; loop < lenhash; ++loop) {
|
|
2
|
35 |
unsigned char* ptrhash = map + poshash + 8*h2;
|
|
|
36 |
uint32 poskd = unpack(ptrhash + 4);
|
|
|
37 |
if(!poskd)
|
|
|
38 |
return 0;
|
|
|
39 |
if(unpack(ptrhash) == h) {
|
|
|
40 |
unsigned char* ptrmatch = map + poskd;
|
|
|
41 |
if(unpack(ptrmatch) == key.length() &&
|
|
|
42 |
!memcmp(ptrmatch+8, key.c_str(), key.length())) {
|
|
|
43 |
dlen = unpack(ptrmatch + 4);
|
|
|
44 |
return (char*)ptrmatch + 8 + key.length();
|
|
|
45 |
}
|
|
0
|
46 |
}
|
|
|
47 |
if(++h2 == lenhash) h2 = 0;
|
|
|
48 |
}
|
|
|
49 |
return 0;
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
datum* cdb_reader::get(const mystring& key)
|
|
|
53 |
{
|
|
|
54 |
if(failed)
|
|
|
55 |
return 0;
|
|
2
|
56 |
uint32 datalen;
|
|
|
57 |
char* dataptr = seek(key, datalen);
|
|
|
58 |
if(!dataptr)
|
|
|
59 |
return 0;
|
|
|
60 |
return new datum(key, mystring(dataptr, datalen));
|
|
0
|
61 |
}
|