1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
/* A library of functions that implements a key-value database using a
hash as the data storage/retrieval system. */
/* Copyright (C) 1999-2000 Jim Hall <jhall@freedos.org> */
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* The following functions are deprecated: db_init, db_free, db_compare */
/* Hash concept borrowed from _The C Programming Language_, Second Ed.,
BWK/DMR. */
#include <stdio.h>
#include <stdlib.h> /* malloc */
#include <string.h> /* strdup */
#include "db.h"
/* Function prototypes */
unsigned hash (char *s);
/* Global variables */
static db_t *hashtab[HASHSIZE];
/* Functions */
/* hash() - My hash function. This is a little simple, but it will
suffice. */
unsigned
hash (char *s)
{
unsigned hashval;
for (hashval = 0; *s != '\0'; s++)
{
/* assign a hash value (iterative) */
hashval = *s + 31 * hashval;
}
/* return the hash value */
return (hashval % HASHSIZE);
}
/* db_fetch() - Query the hash and return a struct that contains the
key and the pointer. The calling function should not look beyond
that. */
db_t *
db_fetch (char *s)
{
db_t *db_ptr;
for (db_ptr = hashtab[hash(s)]; db_ptr != NULL; db_ptr = db_ptr->next)
{
if (strcmp (s, db_ptr->key) == 0)
{
/* found it */
return (db_ptr);
}
}
/* else, not found */
return (NULL);
}
/* db_insert() - Inserts a key,value pair into the hash. If the key
already exists in the hash, the new value is NOT inserted. */
db_t *
db_insert (char *key, char *value)
{
db_t *db_ptr;
unsigned hashval;
if ((db_ptr = db_fetch (key)) == NULL)
{
/* not found */
db_ptr = (db_t *) malloc (sizeof (*db_ptr));
if (db_ptr == NULL || (db_ptr->key = strdup (key)) == NULL)
{
return (NULL);
}
/* insert the key,value into the hash. */
hashval = hash(key);
db_ptr->next = hashtab[hashval];
hashtab[hashval] = db_ptr;
}
else
{
/* already there */
free ((void *) db_ptr->value);
}
if ((db_ptr ->value = strdup (value)) == NULL)
{
return (NULL);
}
/* else */
return (db_ptr);
}
|