/* * Copyright (c) 1994, 1995, 1996 Gunther Schadow. All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include #include #include #include #include #include #include class Datum { public: char *dptr; int dsize; Datum(const char *code) { dsize = strlen(code) + 1; dptr = (char *)malloc(dsize); memcpy(dptr, code, dsize - 1); } Datum(datum &d) { dsize = d.dsize; dptr = d.dptr; } ~Datum() { free(dptr); } operator datum () { return *(datum *)this; } operator char *() { return dptr; } }; int main(int argc, char *argv[]) { if(argc != 2) { cerr << "usage: " << argv[0] << " database" << endl; exit(1); } char dbf_name[512]; strcpy(dbf_name, argv[1]); strcat(dbf_name, ".db"); #ifdef GDBM_FAST GDBM_FILE dbf = gdbm_open(dbf_name, 0, // block_size = file system block GDBM_WRITER | GDBM_NEWDB | GDBM_FAST, 0755, NULL); #else GDBM_FILE dbf = gdbm_open(dbf_name, 0, // block_size = file system block GDBM_WRITER | GDBM_NEWDB, 0755, NULL); #endif if(dbf == NULL) { cerr << "failed to create gdbm file `" << dbf_name << "': " << gdbm_strerror(gdbm_errno); exit(1); } for(int index = 0; cin.ipfx(0); index++) { char *code = gets(cin, "\t\n"); char *text; if(match(cin, "\t\n")) text = gets(cin, "\n"); else text = ""; Datum cde(clws(trws(code))); Datum txt(clws(trws(text))); switch(gdbm_store(dbf, cde, txt, GDBM_INSERT)) { case -1: goto gdbm_fail; case 1: goto gdbm_fail; } delete [] code; delete [] text; } gdbm_close(dbf); return 0; gdbm_fail: cerr << "gdbm error: " << gdbm_strerror(gdbm_errno) << endl; return 1; }