File: conv2gdbm.c

package info (click to toggle)
gdbm 1.7.3-25
  • links: PTS
  • area: main
  • in suites: hamm, slink
  • size: 588 kB
  • ctags: 348
  • sloc: ansic: 3,527; makefile: 330; sh: 186; cpp: 25
file content (184 lines) | stat: -rw-r--r-- 4,278 bytes parent folder | download | duplicates (13)
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
/* conv2gdbm.c - This is a program to convert dbm files to gdbm files. */

/*  This file is part of GDBM, the GNU data base manager, by Philip A. Nelson.
    Copyright (C) 1990, 1991, 1993  Free Software Foundation, Inc.

    GDBM 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, or (at your option)
    any later version.

    GDBM 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 GDBM; see the file COPYING.  If not, write to
    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

    You may contact the author by:
       e-mail:  phil@cs.wwu.edu
      us-mail:  Philip A. Nelson
                Computer Science Department
                Western Washington University
                Bellingham, WA 98226
       
*************************************************************************/


/* include system configuration before all else. */
#include "autoconf.h"

#include <stdio.h>
#if HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include "gdbm.h"

#include "getopt.h"

extern int dbminit ();
extern datum fetch ();
extern datum firstkey ();
extern datum nextkey ();

static void usage();

/* Boolean Constants */
#define TRUE 1
#define FALSE 0

int
main (argc, argv)
     int argc;
     char *argv[];
{
  GDBM_FILE gfile;	/* The gdbm file. */
  datum key;		/* Key and data pairs retrieved. */
  datum data;
  int   errors;		/* error count. */
  int   num;		/* insert count. */
  int   block_size;     /* gdbm block size. */
  char  quiet;		/* Do work Quietly? */
  char  option_char;    /* The option character. */

  char *dbm_file, *gdbm_file;   /* pointers to the file names. */

  /* Initialize things. */
  quiet = FALSE;
  block_size = 0;

  /* Check for proper arguments. */

  if (argc < 2)
    usage (argv[0]);

  /* Check for the options.  */
  while  ( (option_char = getopt (argc, argv, "b:q")) != EOF)
    {
      switch (option_char)
	{
	case 'b':
	  block_size = atoi (optarg);
	  break;

	case 'q':
	  quiet = TRUE;
	  break;

	default:
	  usage (argv[0]);
	}
    }

  /* The required dbm file name. */
  if (argc <= optind)
    {
      usage (argv[0]);
    }
  else
    {
      dbm_file = argv[optind];
      gdbm_file = argv[optind];
      optind += 1;
    }

  /* The optional gdbm file name. */
  if (argc > optind)
    {
      gdbm_file = argv[optind];
      optind += 1;
    }

  /* No more arguments are legal. */
  if (argc > optind)  usage (argv[0]);
  
  /* Open the dbm file. */
  if (dbminit (dbm_file) != 0)
    {
      printf ("%s: dbm file not opened\n", argv[0]);
      exit (2);
    }

  /* Open the gdbm file.  Since the dbm files have .pag and .dir we
     will use the file name without any extension.  */
  gfile = gdbm_open (gdbm_file, block_size, GDBM_WRCREAT, 00664, NULL);
  if (gfile == NULL)
    {
      printf ("%s: gdbm file not opened\n", argv[0]);
      exit (2);
    }


  /* Do the conversion.  */
  errors = 0;
  num = 0;

  if (!quiet)
    printf ("%s: Converting %s.pag and %s.dir to %s.\n", argv[0], dbm_file,
	    dbm_file, gdbm_file);

  /* The convert loop - read a key/data pair from the dbm file and insert
     it into the gdbm file. */

  for (key = firstkey (); key.dptr != NULL; key = nextkey (key))
    {
      data = fetch (key);
      if (gdbm_store (gfile, key, data, GDBM_INSERT) != 0)
	{
	  errors++;
	}
      else
	{
	  num++;
	  if ( !quiet && ((num % 100) == 0))
	    {
	      printf (".");
	      if ( (num % 7000) == 0)
		printf ("\n");
	    }
	}
    }

  gdbm_close (gfile);

  if (!quiet)
    {
      /* Final reporting. */
      if (errors)
	printf ("%s: %d items not inserted into %s.\n", argv[0],
	        errors, gdbm_file);

      printf  ("%s: %d items inserted into %s.\n", argv[0], num, gdbm_file);
    }
  exit(0);
  /* NOTREACHED */
}

static void usage (name)
     char *name;
{
  printf ("usage: %s [-q] [-b block_size] dbmfile [gdbmfile]\n", name);
  exit (2);
}