source: main/trunk/greenstone2/common-src/packages/gdbm/gdbm-1.8.3/testndbm.c@ 21356

Last change on this file since 21356 was 18019, checked in by mdewsnip, 15 years ago

Added gdbm-1.8.3 (downloaded as gdbm-1.8.3.tar.gz and unpacked), in preparation for adding code for reading both little and big endian databases.

File size: 4.9 KB
Line 
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, 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: [email protected]
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_FILE_H
39#include <sys/file.h>
40#endif
41#include <sys/stat.h>
42#if HAVE_FCNTL_H
43#include <fcntl.h>
44#endif
45#ifdef GNU
46#include "ndbm.h"
47#else
48#include <ndbm.h>
49#endif
50
51#define TRUE 1
52#define FALSE 0
53
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 DBM *dbm_file;
76
77 char done = FALSE;
78
79 char *file_name;
80
81 /* Argument checking. */
82 if (argc > 2)
83 {
84 printf ("Usage: %s [dbm-file] \n",argv[0]);
85 exit (2);
86 }
87
88 if (argc > 1)
89 {
90 file_name = argv[1];
91 }
92 else
93 {
94 file_name = "junkndbm";
95 }
96
97 /* Initialize. */
98 data_data.dptr = data_line;
99
100 dbm_file = dbm_open (file_name, O_RDWR|O_CREAT, 00664);
101 if (dbm_file == NULL)
102 {
103 printf ("dbm_open failed.\n");
104 exit (2);
105 }
106
107 /* Welcome message. */
108 printf ("\nWelcome to the gndbm test program. Type ? for help.\n\n");
109
110 while (!done)
111 {
112 printf ("com -> ");
113 cmd_ch = getchar ();
114 while (getchar () != '\n') /* Do nothing. */;
115 switch (cmd_ch)
116 {
117 case 'q':
118 done = TRUE;
119 break;
120
121 case 'f':
122 printf ("key -> ");
123 gets (key_line);
124 key_data.dptr = key_line;
125 key_data.dsize = strlen (key_line)+1;
126 return_data = dbm_fetch (dbm_file, key_data);
127 if (return_data.dptr != NULL)
128 printf ("data is ->%s\n\n", return_data.dptr);
129 else
130 printf ("No such item found.\n\n");
131 break;
132
133 case 's':
134 printf ("key -> ");
135 gets (key_line);
136 key_data.dptr = key_line;
137 key_data.dsize = strlen (key_line)+1;
138 printf ("data -> ");
139 gets (data_line);
140 data_data.dsize = strlen (data_line)+1;
141 if (dbm_store (dbm_file, key_data, data_data, DBM_REPLACE) != 0)
142 printf ("Item not inserted. \n");
143 printf ("\n");
144 break;
145
146 case 'd':
147 printf ("key -> ");
148 gets (key_line);
149 key_data.dptr = key_line;
150 key_data.dsize = strlen (key_line)+1;
151 if (dbm_delete (dbm_file, key_data) != 0)
152 printf ("Item not found or deleted\n");
153 printf ("\n");
154 break;
155
156 case '1':
157 key_data = dbm_firstkey (dbm_file);
158 if (key_data.dptr != NULL)
159 {
160 return_data = dbm_fetch (dbm_file, key_data);
161 printf ("key is ->%s\n", key_data.dptr);
162 printf ("data is ->%s\n\n", return_data.dptr);
163 }
164 else
165 printf ("No such item found.\n\n");
166 break;
167
168
169 case '2':
170 key_data = dbm_nextkey (dbm_file);
171 if (key_data.dptr != NULL)
172 {
173 return_data = dbm_fetch (dbm_file, key_data);
174 printf ("key is ->%s\n", key_data.dptr);
175 printf ("data is ->%s\n\n", return_data.dptr);
176 }
177 else
178 printf ("No such item found.\n\n");
179 break;
180
181 case 'c':
182 {
183 int temp;
184 temp = 0;
185 return_data = dbm_firstkey (dbm_file);
186 while (return_data.dptr != NULL)
187 {
188 temp++;
189 return_data = dbm_nextkey (dbm_file);
190 }
191 printf ("There are %d items in the database.\n\n", temp);
192 }
193 break;
194
195 case '?':
196 printf ("c - count elements\n");
197 printf ("d - delete\n");
198 printf ("f - fetch\n");
199 printf ("q - quit\n");
200 printf ("s - store\n");
201 printf ("1 - firstkey\n");
202 printf ("2 - nextkey on last return value\n\n");
203 break;
204
205 default:
206 printf ("What? \n\n");
207 break;
208
209 }
210 }
211
212 /* Quit normally. */
213 dbm_close (dbm_file);
214 exit (0);
215
216}
Note: See TracBrowser for help on using the repository browser.