Accept topic changes from servers that do not send topic-set timestamps (fixes SF...
[ircu2.10.12-pk.git] / libs / dbprim / ht_find.c
1 /*
2 ** Copyright (C) 2002 by Kevin L. Mitchell <klmitch@mit.edu>
3 **
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Library General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2 of the License, or (at your option) any later version.
8 **
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 ** Library General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Library General Public
15 ** License along with this library; if not, write to the Free
16 ** Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
17 ** MA 02111-1307, USA
18 **
19 ** @(#)$Id$
20 */
21 #include "dbprim.h"
22 #include "dbprim_int.h"
23
24 RCSTAG("@(#)$Id$");
25
26 /** \ingroup dbprim_hash
27  * \brief Find an entry in a hash table.
28  *
29  * This function looks up an entry matching the given \p key.
30  *
31  * \param table A pointer to a #hash_table_t.
32  * \param entry_p
33  *              A pointer to a pointer to a #hash_entry_t.  This is a
34  *              result parameter.  If \c NULL is passed, the lookup
35  *              will be performed and an appropriate error code
36  *              returned. 
37  * \param key   A pointer to a #db_key_t describing the item to find.
38  *
39  * \retval DB_ERR_BADARGS       An argument was invalid.
40  * \retval DB_ERR_NOENTRY       No matching entry was found.
41  */
42 unsigned long
43 ht_find(hash_table_t *table, hash_entry_t **entry_p, db_key_t *key)
44 {
45   unsigned long hash;
46   link_elem_t *elem;
47
48   initialize_dbpr_error_table(); /* initialize error table */
49
50   if (!ht_verify(table) || !key) /* verify arguments */
51     return DB_ERR_BADARGS;
52
53   if (!table->ht_count) /* no entries in table... */
54     return DB_ERR_NOENTRY;
55
56   hash = (*table->ht_func)(table, key) % table->ht_modulus; /* get hash */
57
58   /* walk through each element in that section */
59   for (elem = ll_first(&table->ht_table[hash]); elem; elem = le_next(elem))
60     /* compare keys... */
61     if (!(*table->ht_comp)(table, key,
62                            he_key((hash_entry_t *)le_object(elem)))) {
63       /* found one, return it */
64       if (entry_p)
65         *entry_p = le_object(elem);
66       return 0;
67     }
68
69   return DB_ERR_NOENTRY; /* couldn't find a matching entry */
70 }