1
/* gdbmseq.c - Routines to visit all keys. Not in sorted order. */
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.
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)
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.
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.
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
27
*************************************************************************/
30
/* include system configuration before all else. */
34
#include "gdbmerrno.h"
36
/* Special extern for this file. */
37
extern char *_gdbm_read_entry _ARGS((gdbm_file_info *, int));
40
/* Find and read the next entry in the hash structure for DBF starting
41
at ELEM_LOC of the current bucket and using RETURN_VAL as the place to
42
put the data that is found. */
45
get_next_key (dbf, elem_loc, return_val)
50
int found; /* Have we found the next key. */
51
char *find_data; /* Data pointer returned by find_key. */
53
/* Find the next key. */
57
/* Advance to the next location in the bucket. */
59
if (elem_loc == dbf->header->bucket_elems)
61
/* We have finished the current bucket, get the next bucket. */
64
/* Find the next bucket. It is possible several entries in
65
the bucket directory point to the same bucket. */
66
while (dbf->bucket_dir < dbf->header->dir_size / sizeof (off_t)
67
&& dbf->cache_entry->ca_adr == dbf->dir[dbf->bucket_dir])
70
/* Check to see if there was a next bucket. */
71
if (dbf->bucket_dir < dbf->header->dir_size / sizeof (off_t))
72
_gdbm_get_bucket (dbf, dbf->bucket_dir);
74
/* No next key, just return. */
77
found = dbf->bucket->h_table[elem_loc].hash_value != -1;
80
/* Found the next key, read it into return_val. */
81
find_data = _gdbm_read_entry (dbf, elem_loc);
82
return_val->dsize = dbf->bucket->h_table[elem_loc].key_size;
83
if (return_val->dsize == 0)
84
return_val->dptr = (char *) malloc (1);
86
return_val->dptr = (char *) malloc (return_val->dsize);
87
if (return_val->dptr == NULL) _gdbm_fatal (dbf, "malloc error");
88
bcopy (find_data, return_val->dptr, return_val->dsize);
92
/* Start the visit of all keys in the database. This produces something in
93
hash order, not in any sorted order. */
99
datum return_val; /* To return the first key. */
101
/* Set the default return value for not finding a first entry. */
102
return_val.dptr = NULL;
104
/* Initialize the gdbm_errno variable. */
105
gdbm_errno = GDBM_NO_ERROR;
107
/* Get the first bucket. */
108
_gdbm_get_bucket (dbf, 0);
110
/* Look for first entry. */
111
get_next_key (dbf, -1, &return_val);
117
/* Continue visiting all keys. The next key following KEY is returned. */
120
gdbm_nextkey (dbf, key)
124
datum return_val; /* The return value. */
125
int elem_loc; /* The location in the bucket. */
126
char *find_data; /* Data pointer returned by _gdbm_findkey. */
127
word_t hash_val; /* Returned by _gdbm_findkey. */
129
/* Initialize the gdbm_errno variable. */
130
gdbm_errno = GDBM_NO_ERROR;
132
/* Set the default return value for no next entry. */
133
return_val.dptr = NULL;
135
/* Do we have a valid key? */
136
if (key.dptr == NULL) return return_val;
139
elem_loc = _gdbm_findkey (dbf, key, &find_data, &hash_val);
140
if (elem_loc == -1) return return_val;
142
/* Find the next key. */
143
get_next_key (dbf, elem_loc, &return_val);