home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / rcs / sources / gdbmseq.c < prev    next >
C/C++ Source or Header  |  1991-05-23  |  5KB  |  144 lines

  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, by Philip A. Nelson.
  4.     Copyright (C) 1990, 1991  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 1, 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.         phone:  (206) 676-3035
  27.  
  28. *************************************************************************/
  29.  
  30.  
  31. #include "gdbmdefs.h"
  32.  
  33.  
  34. #ifdef __STDC__
  35. static void get_next_key (gdbm_file_info *dbf, int elem_loc, datum *return_val);
  36. #endif /* not __STDC__ */
  37.  
  38.  
  39. /* Special extern for this file. */
  40. extern char *_gdbm_read_entry ();
  41.  
  42.  
  43. /* Find and read the next entry in the hash structure for DBF starting
  44.    at ELEM_LOC of the current bucket and using RETURN_VAL as the place to
  45.    put the data that is found. */
  46.  
  47. static void
  48. get_next_key (dbf, elem_loc, return_val)
  49.      gdbm_file_info *dbf;
  50.      int elem_loc;
  51.      datum *return_val;
  52. {
  53.   int   found;            /* Have we found the next key. */
  54.   char  *find_data;        /* Data pointer returned by find_key. */
  55.  
  56.   /* Find the next key. */
  57.   found = FALSE;
  58.   while (!found)
  59.     {
  60.       /* Advance to the next location in the bucket. */
  61.       elem_loc++;
  62.       if (elem_loc == dbf->header->bucket_elems)
  63.     {
  64.       /* We have finished the current bucket, get the next bucket.  */
  65.       elem_loc = 0;
  66.  
  67.       /* Find the next bucket.  It is possible several entries in
  68.          the bucket directory point to the same bucket. */
  69.       while (dbf->bucket_dir < dbf->header->dir_size / sizeof (long)
  70.          && dbf->cache_entry->ca_adr == dbf->dir[dbf->bucket_dir])
  71.         dbf->bucket_dir++;
  72.  
  73.       /* Check to see if there was a next bucket. */
  74.       if (dbf->bucket_dir < dbf->header->dir_size / sizeof (long))
  75.         _gdbm_get_bucket (dbf, dbf->bucket_dir);
  76.       else
  77.         /* No next key, just return. */
  78.         return ;
  79.     }
  80.       found = dbf->bucket->h_table[elem_loc].hash_value != -1;
  81.     }
  82.  
  83.   /* Found the next key, read it into return_val. */
  84.   find_data = _gdbm_read_entry (dbf, elem_loc);
  85.   return_val->dsize = dbf->bucket->h_table[elem_loc].key_size;
  86.   if (return_val->dsize == 0)
  87.     return_val->dptr = (char *) malloc (1);
  88.   else
  89.     return_val->dptr = (char *) malloc (return_val->dsize);
  90.   if (return_val->dptr == NULL) _gdbm_fatal (dbf, "malloc error");
  91.   bcopy (find_data, return_val->dptr, return_val->dsize);
  92. }
  93.  
  94.  
  95. /* Start the visit of all keys in the database.  This produces something in
  96.    hash order, not in any sorted order.  */
  97.  
  98. datum
  99. gdbm_firstkey (dbf)
  100.      gdbm_file_info *dbf;
  101. {
  102.   datum return_val;        /* To return the first key. */
  103.  
  104.   /* Set the default return value for not finding a first entry. */
  105.   return_val.dptr = NULL;
  106.  
  107.   /* Get the first bucket.  */
  108.   _gdbm_get_bucket (dbf, 0);
  109.  
  110.   /* Look for first entry. */
  111.   get_next_key (dbf, -1, &return_val);
  112.  
  113.   return return_val;
  114. }
  115.  
  116.  
  117. /* Continue visiting all keys.  The next key following KEY is returned. */
  118.  
  119. datum
  120. gdbm_nextkey (dbf, key)
  121.      gdbm_file_info *dbf;
  122.      datum key;
  123. {
  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.   long     hash_val;        /* Returned by _gdbm_findkey. */
  128.  
  129.   /* Set the default return value for no next entry. */
  130.   return_val.dptr = NULL;
  131.  
  132.   /* Do we have a valid key? */
  133.   if (key.dptr == NULL) return return_val;
  134.  
  135.   /* Find the key.  */
  136.   elem_loc = _gdbm_findkey (dbf, key, &find_data, &hash_val);
  137.   if (elem_loc == -1) return return_val;
  138.  
  139.   /* Find the next key. */
  140.   get_next_key (dbf, elem_loc, &return_val);
  141.  
  142.   return return_val;
  143. }
  144.