add changelog
[platform/upstream/gdbm.git] / src / gdbmseq.c
1 /* gdbmseq.c - Routines to visit all keys.  Not in sorted order. */
2
3 /* This file is part of GDBM, the GNU data base manager.
4    Copyright (C) 1990, 1991, 1993, 2007, 2011, 2013 Free Software Foundation,
5    Inc.
6
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)
10    any later version.
11
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.
16
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/>.   */
19
20 /* Include system configuration before all else. */
21 #include "autoconf.h"
22
23 #include "gdbmdefs.h"
24
25 /* Special extern for this file. */
26 extern char *_gdbm_read_entry (GDBM_FILE , int);
27
28
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. */
32
33 static void
34 get_next_key (GDBM_FILE dbf, int elem_loc, datum *return_val)
35 {
36   int   found;                  /* Have we found the next key. */
37   char  *find_data;             /* Data pointer returned by find_key. */
38
39   /* Find the next key. */
40   found = FALSE;
41   while (!found)
42     {
43       /* Advance to the next location in the bucket. */
44       elem_loc++;
45       if (elem_loc == dbf->header->bucket_elems)
46         {
47           /* We have finished the current bucket, get the next bucket.  */
48           elem_loc = 0;
49
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])
54             dbf->bucket_dir++;
55
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);          
59           else
60             /* No next key, just return. */
61             return ;
62         }
63       found = dbf->bucket->h_table[elem_loc].hash_value != -1;
64     }
65   
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);
71   else
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);
75 }
76
77
78 /* Start the visit of all keys in the database.  This produces something in
79    hash order, not in any sorted order.  */
80
81 datum
82 gdbm_firstkey (GDBM_FILE dbf)
83 {
84   datum return_val;             /* To return the first key. */
85
86   /* Set the default return value for not finding a first entry. */
87   return_val.dptr = NULL;
88
89   /* Initialize the gdbm_errno variable. */
90   gdbm_errno = GDBM_NO_ERROR;
91
92   /* Get the first bucket.  */
93   _gdbm_get_bucket (dbf, 0);
94
95   /* Look for first entry. */
96   get_next_key (dbf, -1, &return_val);
97
98   return return_val;
99 }
100
101
102 /* Continue visiting all keys.  The next key following KEY is returned. */
103
104 datum
105 gdbm_nextkey (GDBM_FILE dbf, datum key)
106 {
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. */
111
112   /* Initialize the gdbm_errno variable. */
113   gdbm_errno = GDBM_NO_ERROR;
114
115   /* Set the default return value for no next entry. */
116   return_val.dptr = NULL;
117
118   /* Do we have a valid key? */
119   if (key.dptr == NULL) return return_val;
120
121   /* Find the key.  */
122   elem_loc = _gdbm_findkey (dbf, key, &find_data, &hash_val);
123   if (elem_loc == -1) return return_val;
124
125   /* Find the next key. */  
126   get_next_key (dbf, elem_loc, &return_val);
127
128   return return_val;
129 }