home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / rcs / sources / conv2gdb.c < prev    next >
C/C++ Source or Header  |  1991-03-22  |  4KB  |  177 lines

  1. /* conv2gdbm.c - This is a program to convert dbm files to gdbm files. */
  2.  
  3. /*  This file is part of GDBM, the GNU data base manager, by Philip A. Nelson.
  4.     Copyright (C) 1990, 1991  Free Software Foundation, Inc.
  5.  
  6.     GDBM is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 1, or (at your option)
  9.     any later version.
  10.  
  11.     GDBM is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with GDBM; see the file COPYING.  If not, write to
  18.     the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     You may contact the author by:
  21.        e-mail:  phil@cs.wwu.edu
  22.       us-mail:  Philip A. Nelson
  23.                 Computer Science Department
  24.                 Western Washington University
  25.                 Bellingham, WA 98226
  26.         phone:  (206) 676-3035
  27.  
  28. *************************************************************************/
  29.  
  30.  
  31. #include <stdio.h>
  32. #include "gdbm.h"
  33.  
  34. extern int dbminit ();
  35. extern datum fetch ();
  36. extern datum firstkey ();
  37. extern datum nextkey ();
  38.  
  39. extern char *optarg;
  40. extern int optind;
  41.  
  42. /* Boolean Constants */
  43. #define TRUE 1
  44. #define FALSE 0
  45.  
  46. main (argc, argv)
  47.      int argc;
  48.      char *argv[];
  49. {
  50.   GDBM_FILE gfile;    /* The gdbm file. */
  51.   datum key;        /* Key and data pairs retrieved. */
  52.   datum data;
  53.   int   errors;        /* error count. */
  54.   int   num;        /* insert count. */
  55.   int   block_size;     /* gdbm block size. */
  56.   char  quiet;        /* Do work Quietly? */
  57.   char  option_char;    /* The option character. */
  58.  
  59.   char *dbm_file, *gdbm_file;   /* pointers to the file names. */
  60.  
  61.   /* Initialize things. */
  62.   quiet = FALSE;
  63.   block_size = 0;
  64.  
  65.   /* Check for proper arguments. */
  66.  
  67.   if (argc < 2)
  68.     usage (argv[0]);
  69.  
  70.   /* Check for the options.  */
  71.   while  ( (option_char = getopt (argc, argv, "b:q")) != EOF)
  72.     {
  73.       switch (option_char)
  74.     {
  75.     case 'b':
  76.       block_size = atoi (optarg);
  77.       break;
  78.  
  79.     case 'q':
  80.       quiet = TRUE;
  81.       break;
  82.  
  83.     default:
  84.       usage (argv[0]);
  85.     }
  86.     }
  87.  
  88.   /* The required dbm file name. */
  89.   if (argc <= optind)
  90.     {
  91.       usage (argv[0]);
  92.     }
  93.   else
  94.     {
  95.       dbm_file = argv[optind];
  96.       gdbm_file = argv[optind];
  97.       optind += 1;
  98.     }
  99.  
  100.   /* The optional gdbm file name. */
  101.   if (argc > optind)
  102.     {
  103.       gdbm_file = argv[optind];
  104.       optind += 1;
  105.     }
  106.  
  107.   /* No more arguments are legal. */
  108.   if (argc > optind)  usage (argv[0]);
  109.  
  110.   /* Open the dbm file. */
  111.   if (dbminit (dbm_file) != 0)
  112.     {
  113.       printf ("%s: dbm file not opened\n", argv[0]);
  114.       exit (2);
  115.     }
  116.  
  117.   /* Open the gdbm file.  Since the dbm files have .pag and .dir we
  118.      will use the file name without any extension.  */
  119.   gfile = gdbm_open (gdbm_file, block_size, GDBM_WRCREAT, 00664, NULL);
  120.   if (gfile == NULL)
  121.     {
  122.       printf ("%s: gdbm file not opened\n", argv[0]);
  123.       exit (2);
  124.     }
  125.  
  126.  
  127.   /* Do the conversion.  */
  128.   errors = 0;
  129.   num = 0;
  130.  
  131.   if (!quiet)
  132.     printf ("%s: Converting %s.pag and %s.dir to %s.\n", argv[0], dbm_file,
  133.         dbm_file, gdbm_file);
  134.  
  135.   /* The convert loop - read a key/data pair from the dbm file and insert
  136.      it into the gdbm file. */
  137.  
  138.   for (key = firstkey (); key.dptr != NULL; key = nextkey (key))
  139.     {
  140.       data = fetch (key);
  141.       if (gdbm_store (gfile, key, data, GDBM_INSERT) != 0)
  142.     {
  143.       errors++;
  144.     }
  145.       else
  146.     {
  147.       num++;
  148.       if ( !quiet && ((num % 100) == 0))
  149.         {
  150.           printf (".");
  151.           if ( (num % 7000) == 0)
  152.         printf ("\n");
  153.         }
  154.     }
  155.     }
  156.  
  157.   gdbm_close (gfile);
  158.  
  159.   if (!quiet)
  160.     {
  161.       /* Final reporting. */
  162.       if (errors)
  163.     printf ("%s: %d items not inserted into %s.\n", argv[0],
  164.             errors, gdbm_file);
  165.  
  166.       printf  ("%s: %d items inserted into %s.\n", argv[0], num, gdbm_file);
  167.     }
  168.  
  169. }
  170.  
  171. usage (name)
  172.      char *name;
  173. {
  174.   printf ("usage: %s [-q] [-b block_size] dbmfile [gdbmfile]\n", name);
  175.   exit (2);
  176. }
  177.