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