home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / rcs / sources / testndbm.c < prev    next >
C/C++ Source or Header  |  1992-01-19  |  5KB  |  212 lines

  1. /* testndbm.c - Driver program to test the ndbm interface routines. */
  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 <sys/types.h>
  33. #ifndef OS2
  34. #include <sys/file.h>
  35. #endif /* not OS2 */
  36. #include <sys/stat.h>
  37. #ifdef SYSV
  38. #include <fcntl.h>
  39. #endif
  40. #ifdef GNU
  41. #include "ndbm.h"
  42. #else
  43. #include <ndbm.h>
  44. #endif
  45.  
  46. #define TRUE  1
  47. #define FALSE 0
  48.  
  49.  
  50. /* The test program allows one to call all the routines plus the hash function.
  51.    The commands are single letter commands.  The user is prompted for all other
  52.    information.  The commands are q (quit), f (fetch), s (store), d (delete),
  53.    1 (firstkey), n (nextkey) and h (hash function). */
  54.  
  55. void
  56. main (argc, argv)
  57.      int argc;
  58.      char *argv[];
  59. {
  60.  
  61.   int  cmd_ch;
  62.  
  63.   datum key_data;
  64.   datum data_data;
  65.   datum return_data;
  66.  
  67.   char key_line[500];
  68.   char data_line[1000];
  69.  
  70.   DBM *dbm_file;
  71.  
  72.   char done = FALSE;
  73.  
  74.   char *file_name;
  75.  
  76.   /* Argument checking. */
  77.   if (argc > 2)
  78.     {
  79.       printf ("Usage: %s [dbm-file] \n",argv[0]);
  80.       exit (2);
  81.     }
  82.  
  83.   if (argc > 1)
  84.     {
  85.       file_name = argv[1];
  86.     }
  87.   else
  88.     {
  89.       file_name = "junkndbm";
  90.     }
  91.  
  92.   /* Initialize. */
  93.   data_data.dptr = data_line;
  94.  
  95.   dbm_file = dbm_open (file_name, O_RDWR|O_CREAT, 00664);
  96.   if (dbm_file == NULL)
  97.     {
  98.       printf ("dbm_open failed.\n");
  99.       exit (2);
  100.     }
  101.  
  102.   /* Welcome message. */
  103.   printf ("\nWelcome to the gndbm test program.  Type ? for help.\n\n");
  104.  
  105.   while (!done)
  106.     {
  107.       printf ("com -> ");
  108.       cmd_ch = getchar ();
  109.       while (getchar () != '\n') /* Do nothing. */;
  110.       switch (cmd_ch)
  111.     {
  112.     case 'q':
  113.       done = TRUE;
  114.       break;
  115.  
  116.     case 'f':
  117.       printf ("key -> ");
  118.       gets (key_line);
  119.       key_data.dptr = key_line;
  120.       key_data.dsize = strlen (key_line)+1;
  121.       return_data = dbm_fetch (dbm_file, key_data);
  122.       if (return_data.dptr != NULL)
  123.           printf ("data is ->%s\n\n", return_data.dptr);
  124.       else
  125.         printf ("No such item found.\n\n");
  126.       break;
  127.  
  128.     case 's':
  129.       printf ("key -> ");
  130.       gets (key_line);
  131.       key_data.dptr = key_line;
  132.       key_data.dsize = strlen (key_line)+1;
  133.       printf ("data -> ");
  134.       gets (data_line);
  135.       data_data.dsize = strlen (data_line)+1;
  136.       if (dbm_store (dbm_file, key_data, data_data, DBM_REPLACE) != 0)
  137.         printf ("Item not inserted. \n");
  138.       printf ("\n");
  139.       break;
  140.  
  141.     case 'd':
  142.       printf ("key -> ");
  143.       gets (key_line);
  144.       key_data.dptr = key_line;
  145.       key_data.dsize = strlen (key_line)+1;
  146.       if (dbm_delete (dbm_file, key_data) != 0)
  147.         printf ("Item not found or deleted\n");
  148.       printf ("\n");
  149.       break;
  150.  
  151.     case '1':
  152.       key_data = dbm_firstkey (dbm_file);
  153.       if (key_data.dptr != NULL)
  154.         {
  155.           return_data = dbm_fetch (dbm_file, key_data);
  156.           printf ("key is ->%s\n", key_data.dptr);
  157.           printf ("data is ->%s\n\n", return_data.dptr);
  158.         }
  159.       else
  160.         printf ("No such item found.\n\n");
  161.       break;
  162.  
  163.  
  164.     case '2':
  165.       key_data = dbm_nextkey (dbm_file);
  166.       if (key_data.dptr != NULL)
  167.         {
  168.           return_data = dbm_fetch (dbm_file, key_data);
  169.           printf ("key is ->%s\n", key_data.dptr);
  170.           printf ("data is ->%s\n\n", return_data.dptr);
  171.         }
  172.       else
  173.         printf ("No such item found.\n\n");
  174.       break;
  175.  
  176.     case 'c':
  177.       {
  178.         int temp;
  179.         temp = 0;
  180.         return_data = dbm_firstkey (dbm_file);
  181.         while (return_data.dptr != NULL)
  182.           {
  183.         temp++;
  184.         return_data = dbm_nextkey (dbm_file);
  185.           }
  186.         printf ("There are %d items in the database.\n\n", temp);
  187.       }
  188.       break;
  189.  
  190.     case '?':
  191.       printf ("c - count elements\n");
  192.       printf ("d - delete\n");
  193.       printf ("f - fetch\n");
  194.       printf ("q - quit\n");
  195.       printf ("s - store\n");
  196.       printf ("1 - firstkey\n");
  197.       printf ("2 - nextkey on last return value\n\n");
  198.       break;
  199.  
  200.     default:
  201.       printf ("What? \n\n");
  202.       break;
  203.  
  204.     }
  205.     }
  206.  
  207.   /* Quit normally. */
  208.   dbm_close (dbm_file);
  209.   exit (0);
  210.  
  211. }
  212.