/* =================== AnyDBM ===============================
Copyright (C) 1998, WebThing Ltd
Author: Nick Kew
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
-------------
Author/Copyright Holder Information: please see the README file and
the AnyDBM webpages at
============================================================ */
#include
#include
#include IMPL_H
#include "Tie.h"
#include "DBServer.h"
#include
#include
#include
/* Put in your own type here to template it */
class dbobject : public TiedObjectType {
char buf[rdbm_max_data] ;
public:
dbobject() { buf[0] = 0 ; }
dbobject(const char* x) { strcpy(buf, x?x:"") ; }
bool operator!() { return !strlen(buf) ; }
const bool operator!() const { return !strlen(buf) ; }
dbobject& operator=(const char* x) { strcpy(buf,x?x:"") ; return *this ; }
operator const char*() const { return buf ; }
operator char*() { return buf ; }
} ;
//typedef const char* dbobject; // the simplest case
typedef IMPL obj_dbm_ ; // bare-bones
typedef DBUser obj_dbm ; // with safe locking
typedef TiedArray obj_array ;
typedef TiedObject obj_iterator ;
int main() {
String val ;
String key ;
obj_dbm dbm ;
try {
obj_array data(dbm) ;
while ( ! cin.eof() ) {
String opt ;
cerr << "Please select an option (ctrl-D to exit):\n"
" W - Enter new data\n"
" R - Lookup data\n"
" D - Delete Entry\n"
" L - List entire DBM\n"
" => " ;
cin >> opt ;
if ( cin.eof() )
break ;
opt.capitalize() ;
int n = 0 ;
switch(opt[0]) {
case 'L':
for (obj_iterator p = *data ; !data.eof() ; ++p)
cout << ++n << ": " << p.key() << " => " << *p << endl ;
break ;
case 'R':
while ( cerr << "Lookup => " , cin >> key , !cin.eof() )
try {
cout << key << ": " << *data[key] << endl ;
} catch (dbmexception& e) {
cerr << "DBM error at " << key << " - "
<< dbm.errmsg1(e.errnum) << endl ;
}
break ;
case 'W':
while ( cerr << "Key/Val => " , cin >> key, readline(cin, val) )
try {
const char* cval = val ;
dbobject obj(cval) ;
data[key] = obj ;
cout << "Entered " << key << " => " << obj << endl ;
} catch (dbmexception& e) {
cerr << "DBM error writing " << key << " => " << val << " - "
<< dbm.errmsg1(e.errnum) << endl ;
}
break ;
case 'D':
while ( cerr << "Delete => " , cin >> key , !cin.eof() )
try {
/* data.del(key) ; */ data[key] = NullTiedObject() ;
cout << "Deleted " << key << " OK." << endl ;
} catch (dbmexception e) {
cerr << "DBM error deleting " << key << " - "
<< dbm.errmsg1(e.errnum) << endl ;
}
break ;
}
cin.clear() ;
}
} catch (dbmexception e) {
cerr << "DBM Error: " << dbm.errmsg1(e.errnum) << " - Exiting.\n" ;
} catch (...) {
cerr << "Yargh!!!!\n" ;
}
return 0 ;
}
// here dbm goes out of scope: performs sync()
// if necessary and closes connection