File: makebdb.c

package info (click to toggle)
perdition 2.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,676 kB
  • sloc: ansic: 14,560; sh: 5,274; makefile: 391
file content (216 lines) | stat: -rw-r--r-- 5,471 bytes parent folder | download | duplicates (8)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**********************************************************************
 * makebdb.c                                                   May 2002
 * ChrisS                                              chriss@pipex.net
 *
 * Create a bdb file from a : delimited flat file read from stdin
 * Based (very heavily) on makegdbm.c by Horms
 *
 * 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 <db.h>

#include "options.h"

#ifdef DMALLOC
#include <dmalloc.h>
#endif


#define MAX_LINE_LENGTH 4096
#define FIELD_DELIMITER ':'

void makebdb_new(const makebdb_options_t options);
void makebdb_undo(const makebdb_options_t options);

int main(int argc, char **argv){
  makebdb_options_t options;

  options=makebdb_options(argc, argv);


  if(options.undo){
    makebdb_undo(options);
  }
  else{
    makebdb_new(options);
  }

  return(0);
}
  

/**********************************************************************
 * makebdb_new
 * Create a new bdb file from a flat file read from stdin
 * pre: options: options structure specifying what to do
 * post: bdb file created and populated
 **********************************************************************/

void makebdb_new(const makebdb_options_t options){
  DB *dbp;
  DBT key;
  DBT content;
  int ret;
  int i;
  char line[MAX_LINE_LENGTH];
  int status;
  int blank;
  int lineno;

  if ((ret=db_create(&dbp, NULL, 0)) != 0) {
    fprintf(stderr,"makebdb_new: db_create: %s\n",db_strerror(ret));
    exit(-1);
  }
  if ((ret=dbp->open(
    dbp,
#ifdef HAVE_BDB_OPEN_7
    NULL,
#endif
    options.mapname,
    NULL,
    DB_HASH,
    DB_CREATE|DB_TRUNCATE,
    0644
  )) != 0) {
    fprintf(stderr,"makebdb_new: DB->open: %s\n",db_strerror(ret));
    exit(-1);
  }

  /*Warning: This loop contains some of the worst code ever written*/
  memset(&key, 0, sizeof(key));
  memset(&content, 0, sizeof(content));
  status=0;
  lineno=0;
  while(fgets(line, MAX_LINE_LENGTH, stdin)!=NULL){
    blank=1;
    key.data=line;
    key.size = MAX_LINE_LENGTH;
    content.size = MAX_LINE_LENGTH;
    for(i=0;i<MAX_LINE_LENGTH;i++){
      if(blank && *(line+i)!=' ' && *(line+i)!='\t'&& *(line+i)!='\n'){
        blank=0;
      }
      if(key.size == MAX_LINE_LENGTH && *(line+i)==FIELD_DELIMITER){
        key.size=i;
        content.data=line+i+1;
        continue;
      }
      if(content.size == MAX_LINE_LENGTH && *(line+i)=='\n'){
        content.size=i-key.size-1;
        lineno++;
        break;
      }
    }
    fflush(NULL);
    if(!blank && !status &&
       (key.size == MAX_LINE_LENGTH || content.size == MAX_LINE_LENGTH)){
      status=1;
    }
    if(status){
      if(*(line+i)=='\n'){
        fprintf(stderr, "makebdb: invalid input on line: %d \n", lineno);
        status=0;
      }
      continue;
    }

    if(blank){
      continue;
    }
    if ((ret = dbp->put(dbp, NULL, &key, &content, DB_NOOVERWRITE))!=0){
      *((char *)key.data+key.size)='\0';
      fprintf(stderr, "makebdb: %s (key: %s)\n",db_strerror(ret),
		      (char *)key.data);
    }

  }

  dbp->close(dbp, 0);
}


/**********************************************************************
 * makebdb_undo
 * print out a : delimited flat file to stdout of a bdb file
 * pre: options: options structure sepcifying what to do
 * post: glat file output to stdout
 **********************************************************************/

void makebdb_undo(makebdb_options_t options){
  DB *dbp;
  DBC *dbcp;
  DBT key;
  DBT content;
  int ret;
  
  if ((ret=db_create(&dbp, NULL, 0)) != 0) {
    fprintf(stderr,"makebdb_undo: db_create: %s\n",db_strerror(ret));
    exit(-1);
  }
  if ((ret=dbp->open(
    dbp,
#ifdef HAVE_BDB_OPEN_7
    NULL,
#endif
    options.mapname,
    NULL,
    DB_HASH,
    DB_RDONLY,
    0644
  )) != 0) {
    fprintf(stderr,"makebdb_undo: DB->open: %s\n",db_strerror(ret));
    exit(-1);
  }

  if ((ret = dbp->cursor(dbp, NULL, &dbcp, 0))!=0) {
    fprintf(stderr,"makebdb_undo: DB->cursor: %s\n",db_strerror(ret));
    exit(-1);
  };

  memset(&key, 0, sizeof(key));
  memset(&content, 0, sizeof(content));

  while((ret=dbcp->c_get(dbcp,&key,&content,DB_NEXT))==0)
  {
    char c;
    fwrite((char *)key.data,  key.size, 1, stdout);
    c = ':';
    fwrite(&c, 1, 1, stdout);
    fwrite((char *)content.data,  content.size, 1, stdout);
    c = '\n';
    fwrite(&c, 1, 1, stdout);
  }
  if(ret != DB_NOTFOUND){
    fprintf(stderr,"makebdb_undo: DBcursor->get: %s\n",db_strerror(ret));
  }

  dbcp->c_close(dbcp);
  dbp->close(dbp, 0);
}