Subversion Repositories SvarDOS

Rev

Details | Last modification | View Log | RSS feed

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