|
|
DataMuseum.dkPresents historical artifacts from the history of: DKUUG/EUUG Conference tapes |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about DKUUG/EUUG Conference tapes Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - metrics - downloadIndex: T f
Length: 3675 (0xe5b)
Types: TextFile
Names: »findkey.c«
└─⟦a05ed705a⟧ Bits:30007078 DKUUG GNU 2/12/89
└─⟦0befd2614⟧ »./gdbm-0.8.tar.Z«
└─⟦b993d2893⟧
└─⟦this⟧ »gdbm/findkey.c«
/* findkey.c - The routine that finds a key entry in the file. */
/* 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"
/* Find the KEY in the file and get ready to read the associated data. The
return value is the location in the current hash bucket of the KEY's
entry. If it is not found, the value -1 is returned. */
int
_dbm_findkey (dbf, key)
dbm_file_info *dbf;
datum key;
{
int new_hash_val; /* The new hash value. */
int bucket_hash_val; /* The hash value from the bucket. */
char *file_key; /* The complete key as stored in the file. */
int elem_loc; /* The location in the bucket. */
int home_loc; /* The home location in the bucket. */
int num_bytes; /* Number of bytes read. */
int key_size; /* Size of the key on the file. */
/* Compute hash value and load proper bucket. */
new_hash_val = _dbm_hash (key);
if (!_dbm_get_bucket (dbf, new_hash_val)) return -1;
/* Search for element in bucket. */
elem_loc = new_hash_val % dbf->header.bucket_elems;
home_loc = elem_loc;
bucket_hash_val = dbf->bucket->h_table[elem_loc].hash_value;
while (bucket_hash_val != -1)
{
key_size = dbf->bucket->h_table[elem_loc].key_size;
if (bucket_hash_val != new_hash_val
|| key_size != key.dsize
|| bcmp (dbf->bucket->h_table[elem_loc].key_start, key.dptr,
(SMALL < key_size ? SMALL : key_size)) != 0 )
{
/* Current elem_loc is not the item, go to next item. */
elem_loc = (elem_loc + 1) % dbf->header.bucket_elems;
if (elem_loc == home_loc) return -1;
bucket_hash_val = dbf->bucket->h_table[elem_loc].hash_value;
}
else
{
/* This may be the one we want. The only way to tell is to read it. */
file_key = (char *) alloca (key_size);
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, file_key, key_size);
if (num_bytes != key_size) _dbm_fatal (dbf, "read error");
if (bcmp (file_key, key.dptr, key_size) == 0 )
{
/* This is the item. */
return elem_loc;
}
else
{
/* Not the item, try the next one. Return if not found. */
elem_loc = (elem_loc + 1) % dbf->header.bucket_elems;
if (elem_loc == home_loc) return -1;
bucket_hash_val = dbf->bucket->h_table[elem_loc].hash_value;
}
}
}
/* If we get here, we never found the key. */
return -1;
}