mirror of
https://codeberg.org/scip/dbtool.git
synced 2025-12-16 19:00:58 +01:00
move to meson&ninja (#1)
This commit is contained in:
758
engine.cc
758
engine.cc
@@ -45,8 +45,6 @@
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
#include "dbtool.h"
|
||||
#include "engine.h"
|
||||
#include "platform.h"
|
||||
@@ -85,15 +83,15 @@ void Engine::init() {
|
||||
cerr << "Failed to open " + config.filename << "(" << strerror(err) << ")" << endl;
|
||||
exit(1);
|
||||
}
|
||||
#else
|
||||
#else
|
||||
|
||||
db = gdbm_open(
|
||||
(char *)config.filename.c_str(),
|
||||
BLOCKSIZE,
|
||||
mode,
|
||||
FILEMODE,
|
||||
0
|
||||
);
|
||||
(char *)config.filename.c_str(),
|
||||
BLOCKSIZE,
|
||||
mode,
|
||||
FILEMODE,
|
||||
0
|
||||
);
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
@@ -104,12 +102,12 @@ void Engine::init() {
|
||||
}
|
||||
#else
|
||||
db = gdbm_open(
|
||||
(char *)config.filename.c_str(),
|
||||
BLOCKSIZE,
|
||||
mode,
|
||||
FILEMODE,
|
||||
0
|
||||
);
|
||||
(char *)config.filename.c_str(),
|
||||
BLOCKSIZE,
|
||||
mode,
|
||||
FILEMODE,
|
||||
0
|
||||
);
|
||||
#endif
|
||||
}
|
||||
#ifndef HAVE_BERKELEY
|
||||
@@ -150,10 +148,10 @@ void Engine::dump() {
|
||||
string K((char *)key.get_data(), key.get_size());
|
||||
string V((char *)data.get_data(), data.get_size());
|
||||
if(config.reverse == 1) {
|
||||
cout << decode(V) << config.separator << K << endl;
|
||||
cout << decode(V) << config.separator << K << endl;
|
||||
}
|
||||
else {
|
||||
cout << K << config.separator << decode(V) << endl;
|
||||
cout << K << config.separator << decode(V) << endl;
|
||||
}
|
||||
}
|
||||
dbcp->close();
|
||||
@@ -168,12 +166,12 @@ void Engine::dump() {
|
||||
key = gdbm_firstkey(db);
|
||||
while ( key.dptr != NULL ) {
|
||||
/* iterate over every tuple and dump it out */
|
||||
value = gdbm_fetch(db, key);
|
||||
value = gdbm_fetch(db, key);
|
||||
string K(key.dptr, key.dsize);
|
||||
string V(value.dptr, value.dsize);
|
||||
if(config.reverse == 1) {
|
||||
cout << decode(V) << config.separator << K << endl;
|
||||
}
|
||||
}
|
||||
else {
|
||||
cout << K << config.separator << decode(V) << endl;
|
||||
}
|
||||
@@ -184,31 +182,43 @@ void Engine::dump() {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* search for regexp given in config.key
|
||||
*/
|
||||
void Engine::regexp() {
|
||||
init();
|
||||
int num;
|
||||
pcre *p_pcre;
|
||||
pcre_extra *p_pcre_extra;
|
||||
char *err_str;
|
||||
int sub_len = 9;
|
||||
int *sub_vec;
|
||||
int erroffset;
|
||||
p_pcre_extra = NULL;
|
||||
p_pcre = pcre_compile((char *)config.key.c_str(), 0,
|
||||
(const char **)(&err_str), &erroffset, NULL);
|
||||
int errnumber;
|
||||
PCRE2_SIZE erroffset;
|
||||
pcre2_match_data *match_data;
|
||||
|
||||
pcre2_code *p_pcre = pcre2_compile(
|
||||
(PCRE2_SPTR)config.key.c_str(), /* the pattern */
|
||||
PCRE2_ZERO_TERMINATED, /* indicates pattern is zero-terminated */
|
||||
0, /* default options */
|
||||
&errnumber, /* for error number */
|
||||
&erroffset, /* for error offset */
|
||||
NULL); /* use default compile context */
|
||||
|
||||
if (p_pcre == NULL) {
|
||||
PCRE2_UCHAR buffer[256];
|
||||
pcre2_get_error_message(errnumber, buffer, sizeof(buffer));
|
||||
cerr << "PCRE2 compilation failed at offset: " << erroffset << " with: " << buffer << endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
match_data = pcre2_match_data_create_from_pattern(p_pcre, NULL);
|
||||
|
||||
#ifdef HAVE_BERKELEY
|
||||
Dbc *dbcp;
|
||||
db->cursor(NULL, &dbcp, 0);
|
||||
Dbt key;
|
||||
Dbt data;
|
||||
while (dbcp->get(&key, &data, DB_NEXT) == 0) {
|
||||
string K((char *)key.get_data(), key.get_size());
|
||||
string V((char *)data.get_data(), data.get_size());
|
||||
Dbc *dbcp;
|
||||
db->cursor(NULL, &dbcp, 0);
|
||||
Dbt key;
|
||||
Dbt data;
|
||||
while (dbcp->get(&key, &data, DB_NEXT) == 0) {
|
||||
string K((char *)key.get_data(), key.get_size());
|
||||
string V((char *)data.get_data(), data.get_size());
|
||||
#else
|
||||
datum key;
|
||||
datum value;
|
||||
@@ -218,23 +228,24 @@ void Engine::regexp() {
|
||||
string K(key.dptr, key.dsize);
|
||||
string V(value.dptr, value.dsize);
|
||||
#endif
|
||||
|
||||
sub_vec = new int(sub_len);
|
||||
num = pcre_exec(p_pcre, p_pcre_extra, (char *)K.c_str(),
|
||||
(int)K.length(), 0, 0, (int *)sub_vec, sub_len);
|
||||
num = pcre2_match(p_pcre, (PCRE2_SPTR)K.c_str(), (PCRE2_SIZE)K.length(), 0, 0, match_data, NULL);
|
||||
|
||||
if(num == 1){
|
||||
if(config.reverse == 1) {
|
||||
cout << decode(V);
|
||||
if(config.with == 1) {
|
||||
cout << config.separator << K;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
else {
|
||||
if(config.with == 1) {
|
||||
cout << K << config.separator;
|
||||
}
|
||||
cout << decode(V) << endl;
|
||||
}
|
||||
if(config.reverse == 1) {
|
||||
cout << decode(V);
|
||||
if(config.with == 1) {
|
||||
cout << config.separator << K;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
else {
|
||||
if(config.with == 1) {
|
||||
cout << K << config.separator;
|
||||
}
|
||||
cout << decode(V) << endl;
|
||||
}
|
||||
}
|
||||
K = "";
|
||||
V = "";
|
||||
@@ -243,365 +254,392 @@ void Engine::regexp() {
|
||||
key = gdbm_nextkey(db,key);
|
||||
#endif
|
||||
}
|
||||
pcre_free(p_pcre);
|
||||
|
||||
pcre2_match_data_free(match_data);
|
||||
pcre2_code_free(p_pcre);
|
||||
|
||||
#ifdef HAVE_BERKELEY
|
||||
dbcp->close();
|
||||
db->close(0);
|
||||
#else
|
||||
gdbm_close(db);
|
||||
gdbm_close(db);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Insert data into the db
|
||||
*/
|
||||
void Engine::from_input() {
|
||||
init();
|
||||
/*
|
||||
* Insert data into the db
|
||||
*/
|
||||
void Engine::from_input() {
|
||||
init();
|
||||
#ifdef HAVE_BERKELEY
|
||||
int err;
|
||||
int err;
|
||||
#else
|
||||
int ret;
|
||||
int ret;
|
||||
#endif
|
||||
|
||||
FILE *stream;
|
||||
stream = fdopen(0, "r");
|
||||
char c;
|
||||
string mode = "key";
|
||||
string key = "";
|
||||
string value = "";
|
||||
string line = "";
|
||||
FILE *stream;
|
||||
stream = fdopen(0, "r");
|
||||
char c;
|
||||
string mode = "key";
|
||||
string key = "";
|
||||
string value = "";
|
||||
string line = "";
|
||||
|
||||
int num;
|
||||
|
||||
int errnumber;
|
||||
PCRE2_SIZE erroffset;
|
||||
pcre2_match_data *match_data;
|
||||
PCRE2_SIZE *ovector;
|
||||
|
||||
pcre2_code *p_pcre = pcre2_compile(
|
||||
(PCRE2_SPTR)config.token.c_str(),
|
||||
PCRE2_ZERO_TERMINATED,
|
||||
0,
|
||||
&errnumber,
|
||||
&erroffset,
|
||||
NULL);
|
||||
|
||||
if (p_pcre == NULL) {
|
||||
PCRE2_UCHAR buffer[256];
|
||||
pcre2_get_error_message(errnumber, buffer, sizeof(buffer));
|
||||
cerr << "PCRE2 compilation failed at offset: " << erroffset << " with: " << buffer << endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
match_data = pcre2_match_data_create_from_pattern(p_pcre, NULL);
|
||||
|
||||
int num, match;
|
||||
pcre *p_pcre;
|
||||
pcre_extra *p_pcre_extra;
|
||||
char *err_str;
|
||||
int sub_len = 9;
|
||||
int *sub_vec;
|
||||
char *v1;
|
||||
char *v2;
|
||||
int erroffset;
|
||||
p_pcre_extra = NULL;
|
||||
p_pcre = pcre_compile((char *)config.token.c_str(), 0,
|
||||
(const char **)(&err_str), &erroffset, NULL);
|
||||
while((c = fgetc(stream)) != EOF) {
|
||||
if(c == '\n') {
|
||||
// record end
|
||||
mode = "key";
|
||||
if(config.token != "") {
|
||||
v1 = new char[line.length()];
|
||||
v2 = new char[line.length()];
|
||||
sub_vec = new int[sub_len];
|
||||
num = pcre_exec(p_pcre, p_pcre_extra, (char *)line.c_str(),
|
||||
(int)line.length(), 0, 0, (int *)sub_vec, sub_len);
|
||||
if(num < 0)
|
||||
cerr << "Token \"" << config.token << "\" did not match on \"" << line << "\"!\n";
|
||||
else if(num == 1)
|
||||
cerr << "Token " << config.token << " did not produce sub strings!\n";
|
||||
else {
|
||||
match = pcre_copy_substring((char *)line.c_str(), sub_vec, num, 1, v1, line.length());
|
||||
match = pcre_copy_substring((char *)line.c_str(), sub_vec, num, 2, v2, line.length());
|
||||
if(config.reverse) {
|
||||
value = v1;
|
||||
key = v2;
|
||||
}
|
||||
else {
|
||||
value = v2;
|
||||
key = v1;
|
||||
}
|
||||
}
|
||||
delete(sub_vec);
|
||||
pcre_free((void *)v1);
|
||||
pcre_free((void *)v2);
|
||||
line = "";
|
||||
}
|
||||
value = encode(value);
|
||||
if(c == '\n') {
|
||||
// record end
|
||||
mode = "key";
|
||||
if(config.token != "") {
|
||||
//char **subs = new char*[2];
|
||||
char *subs[2];
|
||||
|
||||
num = pcre2_match(p_pcre, (PCRE2_SPTR)line.c_str(), (PCRE2_SIZE)line.length(), 0, 0, match_data, NULL);
|
||||
|
||||
if(num < 0)
|
||||
cerr << "Token \"" << config.token << "\" did not match on \"" << line << "\"!\n";
|
||||
else if(num == 1)
|
||||
cerr << "Token " << config.token << " did not produce sub strings!\n";
|
||||
else {
|
||||
ovector = pcre2_get_ovector_pointer(match_data);
|
||||
|
||||
for (int i = 0; i < num; i++) {
|
||||
char * substring_start = const_cast<char*>(line.c_str()) + ovector[2*i];
|
||||
|
||||
if (i == 2) {
|
||||
break;
|
||||
}
|
||||
|
||||
subs[i] = substring_start;
|
||||
}
|
||||
|
||||
free(ovector);
|
||||
|
||||
if(config.reverse) {
|
||||
value = subs[0];
|
||||
key = subs[1];
|
||||
}
|
||||
else {
|
||||
value = subs[1];
|
||||
key = subs[0];
|
||||
}
|
||||
}
|
||||
|
||||
//delete(subs);
|
||||
|
||||
|
||||
line = "";
|
||||
}
|
||||
value = encode(value);
|
||||
#ifdef HAVE_BERKELEY
|
||||
Dbt d_key((char *)key.c_str(), key.length());
|
||||
Dbt d_value((char *)value.c_str(), value.length());
|
||||
Dbt d_key((char *)key.c_str(), key.length());
|
||||
Dbt d_value((char *)value.c_str(), value.length());
|
||||
#else
|
||||
datum d_key = {(char *)key.c_str(), key.length()};
|
||||
datum d_value = {(char *)value.c_str(), value.length()};
|
||||
datum d_key = {(char *)key.c_str(), key.length()};
|
||||
datum d_value = {(char *)value.c_str(), value.length()};
|
||||
#endif
|
||||
if(config.force == 1) {
|
||||
if(config.force == 1) {
|
||||
#ifdef HAVE_BERKELEY
|
||||
if((err = db->put(0, &d_key, &d_value, 0)) != 0) {
|
||||
cerr << "Database error" << "(" << strerror(err) << ")" << endl;
|
||||
exit(1);
|
||||
}
|
||||
if((err = db->put(0, &d_key, &d_value, 0)) != 0) {
|
||||
cerr << "Database error" << "(" << strerror(err) << ")" << endl;
|
||||
exit(1);
|
||||
}
|
||||
#else
|
||||
ret = gdbm_store(db, d_key, d_value, GDBM_REPLACE);
|
||||
ret = gdbm_store(db, d_key, d_value, GDBM_REPLACE);
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
}
|
||||
else {
|
||||
#ifdef HAVE_BERKELEY
|
||||
if((err = db->put(NULL, &d_key, &d_value, DB_NOOVERWRITE)) != 0) {
|
||||
if(err == DB_KEYEXIST) {
|
||||
cerr << "Key " + config.key + " already exists" << "(" << strerror(err) << ")" << endl;
|
||||
exit(1);
|
||||
}
|
||||
else {
|
||||
cerr << "Database error" << "(" << strerror(err) << ")" << endl;
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
if((err = db->put(NULL, &d_key, &d_value, DB_NOOVERWRITE)) != 0) {
|
||||
if(err == DB_KEYEXIST) {
|
||||
cerr << "Key " + config.key + " already exists" << "(" << strerror(err) << ")" << endl;
|
||||
exit(1);
|
||||
}
|
||||
else {
|
||||
cerr << "Database error" << "(" << strerror(err) << ")" << endl;
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
#else
|
||||
ret = gdbm_store(db, d_key, d_value, GDBM_INSERT);
|
||||
ret = gdbm_store(db, d_key, d_value, GDBM_INSERT);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#ifndef HAVE_BERKELEY
|
||||
if(ret != 0) {
|
||||
cerr << pkg << ": " << gdbm_strerror(gdbm_errno) << endl;
|
||||
exit(1);
|
||||
if(ret != 0) {
|
||||
cerr << pkg << ": " << gdbm_strerror(gdbm_errno) << endl;
|
||||
exit(1);
|
||||
}
|
||||
#endif
|
||||
key = "";
|
||||
value = "";
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
key = "";
|
||||
value = "";
|
||||
continue;
|
||||
}
|
||||
else if(c == config.separator && mode != "value" && config.token == "") {
|
||||
// key ready, the following stuff is the data!
|
||||
mode = "value";
|
||||
continue;
|
||||
}
|
||||
if(config.token != "") {
|
||||
/* we have a split token */
|
||||
line += char(c);
|
||||
}
|
||||
else {
|
||||
if(mode == "key")
|
||||
key += char(c);
|
||||
else
|
||||
value += char(c);
|
||||
}
|
||||
}
|
||||
pcre_free(p_pcre);
|
||||
#ifdef HAVE_BERKELEY
|
||||
db->close(0);
|
||||
#else
|
||||
gdbm_close(db);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Insert data into the db
|
||||
*/
|
||||
void Engine::insert() {
|
||||
init();
|
||||
string __value;
|
||||
__value = encode(config.value);
|
||||
#ifdef HAVE_BERKELEY
|
||||
int err;
|
||||
char *k;
|
||||
char *v;
|
||||
k = (char *)config.key.c_str();
|
||||
v = (char *)__value.c_str();
|
||||
Dbt key(k, strlen(k));
|
||||
Dbt value(v, strlen(v));
|
||||
#else
|
||||
int ret;
|
||||
datum key = {(char *)config.key.c_str(), config.key.length()};
|
||||
datum value = {(char *)__value.c_str(), __value.length()};
|
||||
#endif
|
||||
|
||||
if(config.force == 1) {
|
||||
#ifdef HAVE_BERKELEY
|
||||
if((err = db->put(0, &key, &value, 0)) != 0) {
|
||||
cerr << "Database error" << "(" << strerror(err) << ")" << endl;
|
||||
exit(1);
|
||||
}
|
||||
#else
|
||||
ret = gdbm_store(db, key, value, GDBM_REPLACE);
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
#ifdef HAVE_BERKELEY
|
||||
if((err = db->put(NULL, &key, &value, DB_NOOVERWRITE)) != 0) {
|
||||
if(err == DB_KEYEXIST) {
|
||||
cerr << "Key " + config.key + " already exists" << "(" << strerror(err) << ")" << endl;
|
||||
exit(1);
|
||||
else if(c == config.separator && mode != "value" && config.token == "") {
|
||||
// key ready, the following stuff is the data!
|
||||
mode = "value";
|
||||
continue;
|
||||
}
|
||||
if(config.token != "") {
|
||||
/* we have a split token */
|
||||
line += char(c);
|
||||
}
|
||||
else {
|
||||
cerr << "Database error" << "(" << strerror(err) << ")" << endl;
|
||||
exit(1);
|
||||
if(mode == "key")
|
||||
key += char(c);
|
||||
else
|
||||
value += char(c);
|
||||
}
|
||||
}
|
||||
|
||||
pcre2_match_data_free(match_data);
|
||||
pcre2_code_free(p_pcre);
|
||||
|
||||
#ifdef HAVE_BERKELEY
|
||||
db->close(0);
|
||||
#else
|
||||
ret = gdbm_store(db, key, value, GDBM_INSERT);
|
||||
gdbm_close(db);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Insert data into the db
|
||||
*/
|
||||
void Engine::insert() {
|
||||
init();
|
||||
string __value;
|
||||
__value = encode(config.value);
|
||||
#ifdef HAVE_BERKELEY
|
||||
db->close(0);
|
||||
int err;
|
||||
char *k;
|
||||
char *v;
|
||||
k = (char *)config.key.c_str();
|
||||
v = (char *)__value.c_str();
|
||||
Dbt key(k, strlen(k));
|
||||
Dbt value(v, strlen(v));
|
||||
#else
|
||||
if(ret != 0) {
|
||||
cerr << pkg << ": " << gdbm_strerror(gdbm_errno) << endl;
|
||||
exit(1);
|
||||
}
|
||||
gdbm_close(db);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* update a database
|
||||
*/
|
||||
void Engine::update() {
|
||||
init();
|
||||
string __value;
|
||||
__value = encode(config.value);
|
||||
#ifdef HAVE_BERKELEY
|
||||
int err;
|
||||
char *k;
|
||||
char *v;
|
||||
k = (char *)config.key.c_str();
|
||||
v = (char *)__value.c_str();
|
||||
Dbt key(k, strlen(k));
|
||||
Dbt value(v, strlen(v));
|
||||
#else
|
||||
int ret;
|
||||
datum key = {(char *)config.key.c_str(), config.key.length()};
|
||||
datum value = {(char *)__value.c_str(), __value.length()};
|
||||
int ret;
|
||||
datum key = {(char *)config.key.c_str(), config.key.length()};
|
||||
datum value = {(char *)__value.c_str(), __value.length()};
|
||||
#endif
|
||||
|
||||
if(config.force == 1) {
|
||||
if(config.force == 1) {
|
||||
#ifdef HAVE_BERKELEY
|
||||
if((err = db->put(0, &key, &value, 0)) != 0) {
|
||||
cerr << "Database error" << "(" << strerror(err) << ")" << endl;
|
||||
exit(1);
|
||||
}
|
||||
#else
|
||||
ret = gdbm_store(db, key, value, GDBM_REPLACE);
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
#ifdef HAVE_BERKELEY
|
||||
Dbt val;
|
||||
err = db->get(0, &key, &val, 0);
|
||||
if(err == 0) {
|
||||
/* key exists, do the update */
|
||||
if((err = db->put(0, &key, &value, 0)) != 0) {
|
||||
cerr << "Database error" << "(" << strerror(err) << ")" << endl;
|
||||
exit(1);
|
||||
cerr << "Database error" << "(" << strerror(err) << ")" << endl;
|
||||
exit(1);
|
||||
}
|
||||
#else
|
||||
ret = gdbm_store(db, key, value, GDBM_REPLACE);
|
||||
#endif
|
||||
}
|
||||
else if(err == DB_NOTFOUND) {
|
||||
cerr << "Key " << config.key << " does not exist\n";
|
||||
else {
|
||||
#ifdef HAVE_BERKELEY
|
||||
if((err = db->put(NULL, &key, &value, DB_NOOVERWRITE)) != 0) {
|
||||
if(err == DB_KEYEXIST) {
|
||||
cerr << "Key " + config.key + " already exists" << "(" << strerror(err) << ")" << endl;
|
||||
exit(1);
|
||||
}
|
||||
else {
|
||||
cerr << "Database error" << "(" << strerror(err) << ")" << endl;
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
#else
|
||||
ret = gdbm_store(db, key, value, GDBM_INSERT);
|
||||
#endif
|
||||
}
|
||||
#ifdef HAVE_BERKELEY
|
||||
db->close(0);
|
||||
#else
|
||||
if(ret != 0) {
|
||||
cerr << pkg << ": " << gdbm_strerror(gdbm_errno) << endl;
|
||||
exit(1);
|
||||
}
|
||||
gdbm_close(db);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* update a database
|
||||
*/
|
||||
void Engine::update() {
|
||||
init();
|
||||
string __value;
|
||||
__value = encode(config.value);
|
||||
#ifdef HAVE_BERKELEY
|
||||
int err;
|
||||
char *k;
|
||||
char *v;
|
||||
k = (char *)config.key.c_str();
|
||||
v = (char *)__value.c_str();
|
||||
Dbt key(k, strlen(k));
|
||||
Dbt value(v, strlen(v));
|
||||
#else
|
||||
int ret;
|
||||
datum key = {(char *)config.key.c_str(), config.key.length()};
|
||||
datum value = {(char *)__value.c_str(), __value.length()};
|
||||
#endif
|
||||
|
||||
if(config.force == 1) {
|
||||
#ifdef HAVE_BERKELEY
|
||||
if((err = db->put(0, &key, &value, 0)) != 0) {
|
||||
cerr << "Database error" << "(" << strerror(err) << ")" << endl;
|
||||
exit(1);
|
||||
}
|
||||
#else
|
||||
ret = gdbm_store(db, key, value, GDBM_REPLACE);
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
#ifdef HAVE_BERKELEY
|
||||
Dbt val;
|
||||
err = db->get(0, &key, &val, 0);
|
||||
if(err == 0) {
|
||||
/* key exists, do the update */
|
||||
if((err = db->put(0, &key, &value, 0)) != 0) {
|
||||
cerr << "Database error" << "(" << strerror(err) << ")" << endl;
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
else if(err == DB_NOTFOUND) {
|
||||
cerr << "Key " << config.key << " does not exist\n";
|
||||
exit(1);
|
||||
}
|
||||
else {
|
||||
cerr << "Database error" << "(" << strerror(err) << ")" << endl;
|
||||
exit(1);
|
||||
}
|
||||
#else
|
||||
ret = gdbm_exists(db, key);
|
||||
if(ret) {
|
||||
ret = gdbm_store(db, key, value, GDBM_REPLACE);
|
||||
}
|
||||
else {
|
||||
cerr << "Key " << config.key << " does not exist\n";
|
||||
exit(1);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#ifdef HAVE_BERKELEY
|
||||
db->close(0);
|
||||
#else
|
||||
if(ret != 0) {
|
||||
cerr << pkg << ": " << gdbm_strerror(gdbm_errno) << endl;
|
||||
exit(1);
|
||||
}
|
||||
gdbm_close(db);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* remove an entry
|
||||
*/
|
||||
void Engine::remove() {
|
||||
init();
|
||||
#ifdef HAVE_BERKELEY
|
||||
Dbt key((char *)config.key.c_str(), config.key.length() + 1);
|
||||
int ret;
|
||||
if((ret = db->del(NULL, &key, 0)) != 0) {
|
||||
cerr << "Database error" << "(" << strerror(ret) << ")" << endl;
|
||||
exit(1);
|
||||
}
|
||||
db->close(0);
|
||||
#else
|
||||
datum key = {(char *)config.key.c_str(), config.key.length()};
|
||||
int ret;
|
||||
if((ret = gdbm_delete(db, key)) != 0) {
|
||||
cerr << "Database error" << "(" << strerror(ret) << ")" << endl;
|
||||
exit(1);
|
||||
}
|
||||
gdbm_close(db);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* search for specific data
|
||||
*/
|
||||
void Engine::select() {
|
||||
init();
|
||||
#ifdef HAVE_BERKELEY
|
||||
int err;
|
||||
char *k;
|
||||
k = (char *)config.key.c_str();
|
||||
Dbt key(k, strlen(k));
|
||||
Dbt value;
|
||||
err = db->get(0, &key, &value, 0);
|
||||
if(err == 0) {
|
||||
string gotvalue((char *)value.get_data(), value.get_size());
|
||||
if(config.reverse == 1) {
|
||||
cout << decode(gotvalue);
|
||||
if(config.with == 1) {
|
||||
cout << config.separator << config.key;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
else {
|
||||
if(config.with == 1) {
|
||||
cout << config.key << config.separator;
|
||||
}
|
||||
cout << decode(gotvalue) << endl;
|
||||
}
|
||||
}
|
||||
else {
|
||||
cerr << "Database error" << "(" << strerror(err) << ")" << endl;
|
||||
exit(1);
|
||||
}
|
||||
db->close(0);
|
||||
#else
|
||||
ret = gdbm_exists(db, key);
|
||||
if(ret) {
|
||||
ret = gdbm_store(db, key, value, GDBM_REPLACE);
|
||||
}
|
||||
else {
|
||||
cerr << "Key " << config.key << " does not exist\n";
|
||||
exit(1);
|
||||
}
|
||||
datum content;
|
||||
datum key = {(char *)config.key.c_str(), config.key.length()};
|
||||
content = gdbm_fetch(db, key);
|
||||
string V(content.dptr, content.dsize);
|
||||
if(config.with == 1)
|
||||
cout << config.key << config.separator;
|
||||
cout << decode(V) << endl;
|
||||
gdbm_close(db);
|
||||
#endif
|
||||
}
|
||||
#ifdef HAVE_BERKELEY
|
||||
db->close(0);
|
||||
#else
|
||||
if(ret != 0) {
|
||||
cerr << pkg << ": " << gdbm_strerror(gdbm_errno) << endl;
|
||||
exit(1);
|
||||
}
|
||||
gdbm_close(db);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* remove an entry
|
||||
*/
|
||||
void Engine::remove() {
|
||||
init();
|
||||
int ret;
|
||||
#ifdef HAVE_BERKELEY
|
||||
Dbt key((char *)config.key.c_str(), config.key.length() + 1);
|
||||
if((ret = db->del(NULL, &key, 0)) != 0) {
|
||||
cerr << "Database error" << "(" << strerror(ret) << ")" << endl;
|
||||
exit(1);
|
||||
}
|
||||
db->close(0);
|
||||
#else
|
||||
datum key = {(char *)config.key.c_str(), config.key.length()};
|
||||
ret = gdbm_delete(db, key);
|
||||
gdbm_close(db);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* search for specific data
|
||||
*/
|
||||
void Engine::select() {
|
||||
init();
|
||||
#ifdef HAVE_BERKELEY
|
||||
int err;
|
||||
char *k;
|
||||
k = (char *)config.key.c_str();
|
||||
Dbt key(k, strlen(k));
|
||||
Dbt value;
|
||||
err = db->get(0, &key, &value, 0);
|
||||
if(err == 0) {
|
||||
string gotvalue((char *)value.get_data(), value.get_size());
|
||||
if(config.reverse == 1) {
|
||||
cout << decode(gotvalue);
|
||||
if(config.with == 1) {
|
||||
cout << config.separator << config.key;
|
||||
}
|
||||
cout << endl;
|
||||
string Engine::encode(const string& data) {
|
||||
if(config.encrypted) {
|
||||
return rijn.encrypt(data);
|
||||
}
|
||||
else {
|
||||
if(config.with == 1) {
|
||||
cout << config.key << config.separator;
|
||||
}
|
||||
cout << decode(gotvalue) << endl;
|
||||
else
|
||||
return data;
|
||||
}
|
||||
|
||||
string Engine::decode(const string& data) {
|
||||
if(config.encrypted) {
|
||||
return rijn.decrypt(data);
|
||||
}
|
||||
else
|
||||
return data;
|
||||
}
|
||||
else {
|
||||
cerr << "Database error" << "(" << strerror(err) << ")" << endl;
|
||||
exit(1);
|
||||
}
|
||||
db->close(0);
|
||||
#else
|
||||
datum content;
|
||||
datum key = {(char *)config.key.c_str(), config.key.length()};
|
||||
content = gdbm_fetch(db, key);
|
||||
string V(content.dptr, content.dsize);
|
||||
if(config.with == 1)
|
||||
cout << config.key << config.separator;
|
||||
cout << decode(V) << endl;
|
||||
gdbm_close(db);
|
||||
#endif
|
||||
}
|
||||
|
||||
string Engine::encode(const string& data) {
|
||||
if(config.encrypted) {
|
||||
return rijn.encrypt(data);
|
||||
}
|
||||
else
|
||||
return data;
|
||||
}
|
||||
|
||||
string Engine::decode(const string& data) {
|
||||
if(config.encrypted) {
|
||||
return rijn.decrypt(data);
|
||||
}
|
||||
else
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user