|
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 <stdio.h>
|
|
|
18 |
#include <unistd.h>
|
|
|
19 |
#include "cdb++.h"
|
|
|
20 |
#include "internal.h"
|
|
|
21 |
|
|
|
22 |
#define safeadd(u,v) do{ uint32 newu = u+v; if(newu<u) FAIL; u=newu; }while(0)
|
|
|
23 |
#define FAIL do{ abort(); return false; }while(0)
|
|
|
24 |
|
|
|
25 |
void cdb_writer::abort()
|
|
|
26 |
{
|
|
|
27 |
if(!failed) {
|
|
|
28 |
unlink(fntemp.c_str());
|
|
|
29 |
out.close();
|
|
|
30 |
failed = true;
|
|
|
31 |
}
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
cdb_writer::cdb_writer(const mystring& filename, int mode)
|
|
|
35 |
: out(filename.c_str(), fdobuf::excl | fdobuf::create, mode),
|
|
|
36 |
fntemp(filename),
|
|
|
37 |
pos(sizeof cdbm.final),
|
|
|
38 |
failed(false)
|
|
|
39 |
{
|
|
|
40 |
if(!out)
|
|
|
41 |
failed = true;
|
|
|
42 |
else {
|
|
|
43 |
if(!out.write(cdbm.final, sizeof cdbm.final))
|
|
|
44 |
abort();
|
|
|
45 |
}
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
cdb_writer::~cdb_writer()
|
|
|
49 |
{
|
|
|
50 |
abort();
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
bool cdb_writer::put(const mystring& key, const mystring& data)
|
|
|
54 |
{
|
|
|
55 |
if(failed) return 0;
|
|
|
56 |
unsigned char packbuf[8];
|
|
|
57 |
pack(packbuf, key.length());
|
|
|
58 |
pack(packbuf+4, data.length());
|
|
|
59 |
if(!out.write(packbuf, sizeof packbuf)) FAIL;
|
|
|
60 |
|
|
|
61 |
uint32 h = hash(key);
|
|
|
62 |
if(!out.write(key.c_str(), key.length()) ||
|
|
|
63 |
!out.write(data.c_str(), data.length()) ||
|
|
|
64 |
!cdbm.add(h, pos))
|
|
|
65 |
FAIL;
|
|
|
66 |
safeadd(pos, 8);
|
|
|
67 |
safeadd(pos, key.length());
|
|
|
68 |
safeadd(pos, data.length());
|
|
|
69 |
return true;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
bool cdb_writer::end(const mystring& filename)
|
|
|
73 |
{
|
|
|
74 |
if(failed) return 0;
|
|
|
75 |
if(!cdbm.split_hashes()) FAIL;
|
|
|
76 |
for(unsigned i = 0; i < 256; i++) {
|
|
|
77 |
uint32 len = cdbm.throw_hash(pos, i);
|
|
|
78 |
for(uint32 u = 0; u < len; u++) {
|
|
|
79 |
unsigned char packbuf[8];
|
|
|
80 |
pack(packbuf, cdbm.hash[u].h);
|
|
|
81 |
pack(packbuf+4, cdbm.hash[u].p);
|
|
|
82 |
if(!out.write(packbuf, sizeof packbuf)) FAIL;
|
|
|
83 |
safeadd(pos, 8);
|
|
|
84 |
}
|
|
|
85 |
}
|
|
|
86 |
if(!out.rewind()) FAIL;
|
|
|
87 |
if(!out.write(cdbm.final, sizeof(cdbm.final))) FAIL;
|
|
|
88 |
if(!out.flush()) FAIL;
|
|
|
89 |
out.close();
|
|
|
90 |
if(rename(fntemp.c_str(), filename.c_str())) FAIL;
|
|
|
91 |
return true;
|
|
|
92 |
}
|