2 ** Copyright (C) 2002 by Kevin L. Mitchell <klmitch@mit.edu>
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.
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.
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,
22 #include "dbprim_int.h"
26 /** \ingroup dbprim_link
27 * \brief Find an element in a linked list.
29 * This function iterates through a linked list looking for an element
30 * that matches the given \p key.
32 * \param list A pointer to a #link_head_t.
34 * A pointer to a pointer to a #link_elem_t. This is a
35 * result parameter. \c NULL is an invalid value.
37 * A pointer to a comparison function used to compare the
38 * key to a particular element. See the documentation
39 * for #link_comp_t for more information.
40 * \param start A pointer to a #link_elem_t describing where in the
41 * linked list to start. If \c NULL is passed, the
42 * beginning of the list will be assumed.
43 * \param key A key to search for.
45 * \retval DB_ERR_BADARGS An argument was invalid.
46 * \retval DB_ERR_WRONGTABLE \p start is not in this linked list.
47 * \retval DB_ERR_NOENTRY No matching entry was found.
50 ll_find(link_head_t *list, link_elem_t **elem_p, link_comp_t comp_func,
51 link_elem_t *start, db_key_t *key)
55 initialize_dbpr_error_table(); /* initialize error table */
57 /* Verify arguments */
58 if (!ll_verify(list) || !elem_p || !comp_func || !key ||
59 (start && !le_verify(start)))
60 return DB_ERR_BADARGS;
62 /* Verify that the start element is in this list */
63 if (start && list != start->le_head)
64 return DB_ERR_WRONGTABLE;
66 /* search the list... */
67 for (elem = start ? start : list->lh_first; elem; elem = elem->le_next)
68 if (!(*comp_func)(key, elem->le_object)) { /* Compare... */
69 *elem_p = elem; /* comparison function must return "0" on match */
73 return DB_ERR_NOENTRY; /* Couldn't find the element */