DataMuseum.dk

Presents historical artifacts from the history of:

DKUUG/EUUG Conference tapes

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about DKUUG/EUUG Conference tapes

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - metrics - download
Index: T g

⟦272fb3c05⟧ TextFile

    Length: 4527 (0x11af)
    Types: TextFile
    Names: »gdbmseq.c«

Derivation

└─⟦a05ed705a⟧ Bits:30007078 DKUUG GNU 2/12/89
    └─⟦0befd2614⟧ »./gdbm-0.8.tar.Z« 
        └─⟦b993d2893⟧ 
            └─⟦this⟧ »gdbm/gdbmseq.c« 

TextFile

/* gdbmseq.c - Routines to visit all keys.  Not in sorted order. */

/*  GNU DBM  - DataBase Manager (database subroutines) by Philip A. Nelson
    Copyright (C) 1989  Free Software Foundation, Inc.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    You may contact the author by:
       e-mail:  phil@wwu.edu
      us-mail:  Philip A. Nelson
                Computer Science Department
                Western Washington University
                Bellingham, WA 98226
        phone:  (206) 676-3035
       
*************************************************************************/


#include <stdio.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "gdbm.h"

/* Start the visit of all keys in the database.  This produces something in
   hash order, not in any sorted order.  */

datum
gdbm_firstkey (dbf)
     dbm_file_info *dbf;
{
  datum return_val;		/* To return the first key. */
  int   index;			/* For looking through the bucket. */
  int   num_bytes;		/* For reading the key. */

  /* Set the default return value for not finding a first entry. */
  return_val.dptr = NULL;

  /* Get the first bucket.  */
  if (!_dbm_get_bucket (dbf, 0)) return return_val;

  /* Look for first entry. */
  if (dbf->bucket->count > 0)





    {
      index = 0;
      while (dbf->bucket->h_table[index].hash_value == -1) index++;
      num_bytes = lseek (dbf->desc, dbf->bucket->h_table[index].data_pointer,
			 L_SET);
      if (num_bytes != dbf->bucket->h_table[index].data_pointer)
	_dbm_fatal (dbf, "lseek error");
      return_val.dsize = dbf->bucket->h_table[index].key_size;
      return_val.dptr = (char *) malloc (return_val.dsize);
      if (return_val.dptr == NULL) _dbm_fatal (dbf, "malloc error");
      num_bytes = read (dbf->desc, return_val.dptr, return_val.dsize);
      if (num_bytes != return_val.dsize) _dbm_fatal (dbf, "read error");
    }

  return return_val;
}

\f



/* Continue visiting all keys.  The next key following KEY is returned. */

datum
gdbm_nextkey (dbf, key)
     dbm_file_info *dbf;
     datum key;
{
  datum  return_val;		/* The return value. */
  int    elem_loc;		/* The location in the bucket. */
  int    num_bytes;		/* Number of bytes read. */
  int    found;			/* Have we found the next key. */

  /* Set the default return value for no next entry. */
  return_val.dptr = NULL;

  /* Find the key.  */
  elem_loc = _dbm_findkey (dbf, key);
  if (elem_loc == -1) return return_val;
  
  /* Find the next key. */
  found = FALSE;
  while (!found)
    {
      elem_loc++;
      if (elem_loc == dbf->header.bucket_elems)
	{
	  /* It is in the next bucket.  */
	  elem_loc = 0;
	  while (dbf->bucket_dir < dbf->header.dir_size / 4
		 && dbf->bucket_adr == dbf->dir[dbf->bucket_dir])
	    dbf->bucket_dir++;
	  if (dbf->bucket_dir < dbf->header.dir_size / 4)
	    {
	      dbf->bucket_adr = dbf->dir[dbf->bucket_dir];
	      num_bytes = lseek (dbf->desc, dbf->bucket_adr, L_SET);
	      if (num_bytes != dbf->bucket_adr) _dbm_fatal (dbf, "lseek error");
	      num_bytes = read (dbf->desc, dbf->bucket,
				dbf->header.bucket_size);
	      if (num_bytes != dbf->header.bucket_size)
		_dbm_fatal (dbf,"read error");
	    }
	  else
	    {
	      /* No next key, return the NULL value. */
	      return return_val;
	    }
	}
      found = dbf->bucket->h_table[elem_loc].hash_value != -1;
    }
  
  /* Found the next key, read it and return it. */
  return_val.dsize = dbf->bucket->h_table[elem_loc].key_size;
  return_val.dptr = (char *) malloc (return_val.dsize);
  if (return_val.dptr == NULL) _dbm_fatal (dbf, "malloc error");
  num_bytes = lseek (dbf->desc,
		     dbf->bucket->h_table[elem_loc].data_pointer, L_SET);
  if (num_bytes != dbf->bucket->h_table[elem_loc].data_pointer)
    _dbm_fatal (dbf, "lseek error");
  num_bytes = read (dbf->desc, return_val.dptr, return_val.dsize);
  if (num_bytes != return_val.dsize) _dbm_fatal (dbf, "read error");

  return return_val;
}