1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
/**********************************************************************
* makegdbm.c May 1999
* Horms horms@verge.net.au
*
* Create a gdbm file from a : delimited flat file read from stdin
*
* perdition
* Mail retrieval proxy server
* Copyright (C) 1999-2005 Horms
*
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
**********************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
#include "options.h"
#ifdef DMALLOC
#include <dmalloc.h>
#endif
#define MAX_LINE_LENGTH 4096
#define FIELD_DELIMITER ':'
void makegdbm_new(const makegdbm_options_t options);
void makegdbm_undo(const makegdbm_options_t options);
int main(int argc, char **argv){
makegdbm_options_t options;
options=makegdbm_options(argc, argv);
if(options.undo){
makegdbm_undo(options);
}
else{
makegdbm_new(options);
}
return(0);
}
/**********************************************************************
* makegdbm_new
* Create a nrew gdbm file from a flat file read from stdin
* pre: options: options structure specifying what to do
* post: gdbm file created and populated
**********************************************************************/
void makegdbm_new(const makegdbm_options_t options){
GDBM_FILE dbf;
datum key;
datum content;
int i;
char line[MAX_LINE_LENGTH];
int status;
int blank;
int lineno;
if((dbf=gdbm_open(options.mapname, 0, GDBM_NEWDB|GDBM_FAST, 0644, 0))==NULL){
fprintf(
stderr,
"makegdbm_new: gdbm_open: %s: %s\n",
gdbm_strerror(gdbm_errno),
strerror(errno)
);
exit(-1);
}
/*Warning: This loop contains some of the worst code ever written*/
status=0;
lineno=0;
while(fgets(line, MAX_LINE_LENGTH, stdin)!=NULL){
blank=1;
key.dptr=line;
key.dsize=-1;
content.dsize=-1;
for(i=0;i<MAX_LINE_LENGTH;i++){
if(blank && *(line+i)!=' ' && *(line+i)!='\t'&& *(line+i)!='\n'){
blank=0;
}
if(key.dsize==-1 && *(line+i)==FIELD_DELIMITER){
key.dsize=i;
content.dptr=line+i+1;
continue;
}
if(content.dsize==-1 && *(line+i)=='\n'){
content.dsize=i-key.dsize-1;
lineno++;
break;
}
}
fflush(NULL);
if(!blank && !status && (key.dsize<1 || content.dsize<1)){
status=1;
}
if(status){
if(*(line+i)=='\n'){
fprintf(stderr, "makegdbm: invalid input on line: %d \n", lineno);
status=0;
}
continue;
}
if(blank){
continue;
}
if(gdbm_store(dbf, key, content, GDBM_INSERT)>0){
*(key.dptr+key.dsize)='\0';
fprintf(stderr, "makegdbm: dupicate key: %s\n", key.dptr);
}
}
gdbm_close(dbf);
}
/**********************************************************************
* makegdbm_undo
* print out a : delimited flat file to stdout of a gdbm file
* pre: options: options structure sepcifying what to do
* post: glat file output to stdout
**********************************************************************/
void makegdbm_undo(makegdbm_options_t options){
GDBM_FILE dbf;
datum this_key;
datum next_key;
datum content;
if((dbf=gdbm_open(options.mapname, 0, GDBM_READER, 0644, 0))==NULL){
fprintf(
stderr,
"makegdbm_undo: gdbm_open: %s: %s\n",
gdbm_strerror(gdbm_errno),
strerror(errno)
);
exit(-1);
}
this_key=gdbm_firstkey(dbf);
if(this_key.dptr==NULL){
fprintf(stderr, "makegdbm: makegdbm_undo: no first key\n");
gdbm_close(dbf);
exit(0);
}
while(1){
fwrite(this_key.dptr, 1, this_key.dsize, stdout);
fwrite(":", 1, 2, stdout);
content=gdbm_fetch(dbf,this_key);
if(content.dptr!=NULL){
fwrite(content.dptr, 1, content.dsize, stdout);
}
fwrite("\n", 1, 2, stdout);
next_key=gdbm_nextkey(dbf,this_key);
if(next_key.dptr==NULL){
break;
}
free(this_key.dptr);
this_key=next_key;
}
gdbm_close(dbf);
}
|