1 /* gdbmseq.c - Routines to visit all keys. Not in sorted order. */
3 /* This file is part of GDBM, the GNU data base manager.
4 Copyright (C) 1990, 1991, 1993, 2007, 2011, 2013 Free Software Foundation,
7 GDBM is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GDBM is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GDBM. If not, see <http://www.gnu.org/licenses/>. */
20 /* Include system configuration before all else. */
25 /* Special extern for this file. */
26 extern char *_gdbm_read_entry (GDBM_FILE , int);
29 /* Find and read the next entry in the hash structure for DBF starting
30 at ELEM_LOC of the current bucket and using RETURN_VAL as the place to
31 put the data that is found. */
34 get_next_key (GDBM_FILE dbf, int elem_loc, datum *return_val)
36 int found; /* Have we found the next key. */
37 char *find_data; /* Data pointer returned by find_key. */
39 /* Find the next key. */
43 /* Advance to the next location in the bucket. */
45 if (elem_loc == dbf->header->bucket_elems)
47 /* We have finished the current bucket, get the next bucket. */
50 /* Find the next bucket. It is possible several entries in
51 the bucket directory point to the same bucket. */
52 while (dbf->bucket_dir < GDBM_DIR_COUNT (dbf)
53 && dbf->cache_entry->ca_adr == dbf->dir[dbf->bucket_dir])
56 /* Check to see if there was a next bucket. */
57 if (dbf->bucket_dir < GDBM_DIR_COUNT (dbf))
58 _gdbm_get_bucket (dbf, dbf->bucket_dir);
60 /* No next key, just return. */
63 found = dbf->bucket->h_table[elem_loc].hash_value != -1;
66 /* Found the next key, read it into return_val. */
67 find_data = _gdbm_read_entry (dbf, elem_loc);
68 return_val->dsize = dbf->bucket->h_table[elem_loc].key_size;
69 if (return_val->dsize == 0)
70 return_val->dptr = (char *) malloc (1);
72 return_val->dptr = (char *) malloc (return_val->dsize);
73 if (return_val->dptr == NULL) _gdbm_fatal (dbf, _("malloc error"));
74 memcpy (return_val->dptr, find_data, return_val->dsize);
78 /* Start the visit of all keys in the database. This produces something in
79 hash order, not in any sorted order. */
82 gdbm_firstkey (GDBM_FILE dbf)
84 datum return_val; /* To return the first key. */
86 /* Set the default return value for not finding a first entry. */
87 return_val.dptr = NULL;
89 /* Initialize the gdbm_errno variable. */
90 gdbm_errno = GDBM_NO_ERROR;
92 /* Get the first bucket. */
93 _gdbm_get_bucket (dbf, 0);
95 /* Look for first entry. */
96 get_next_key (dbf, -1, &return_val);
102 /* Continue visiting all keys. The next key following KEY is returned. */
105 gdbm_nextkey (GDBM_FILE dbf, datum key)
107 datum return_val; /* The return value. */
108 int elem_loc; /* The location in the bucket. */
109 char *find_data; /* Data pointer returned by _gdbm_findkey. */
110 int hash_val; /* Returned by _gdbm_findkey. */
112 /* Initialize the gdbm_errno variable. */
113 gdbm_errno = GDBM_NO_ERROR;
115 /* Set the default return value for no next entry. */
116 return_val.dptr = NULL;
118 /* Do we have a valid key? */
119 if (key.dptr == NULL) return return_val;
122 elem_loc = _gdbm_findkey (dbf, key, &find_data, &hash_val);
123 if (elem_loc == -1) return return_val;
125 /* Find the next key. */
126 get_next_key (dbf, elem_loc, &return_val);