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 Add an element to a linked list.
29 * This function adds a given element to a specified linked list in
30 * the specified location.
32 * \param list A pointer to a #link_head_t.
33 * \param new A pointer to the #link_elem_t to be added to the
35 * \param loc A #link_loc_t indicating where the entry should be
37 * \param elem A pointer to a #link_elem_t describing another element
38 * in the list if \p loc is #LINK_LOC_BEFORE or
41 * \retval DB_ERR_BADARGS An argument was invalid.
42 * \retval DB_ERR_BUSY The element is already in a list.
43 * \retval DB_ERR_WRONGTABLE \p elem is in a different list.
44 * \retval DB_ERR_UNUSED \p elem is not in any list.
47 ll_add(link_head_t *list, link_elem_t *new, link_loc_t loc,
50 initialize_dbpr_error_table(); /* initialize error table */
52 /* Verify arguments--if elem is set, must be a valid element; if
53 * location is before or after, elem must be set
55 if (!ll_verify(list) || !le_verify(new) || (elem && !le_verify(elem)) ||
56 ((loc == LINK_LOC_BEFORE || loc == LINK_LOC_AFTER) && !elem))
57 return DB_ERR_BADARGS;
59 /* new element must not be in list already */
62 if (elem && list != elem->le_head) /* element must be in the list */
63 return elem->le_head ? DB_ERR_WRONGTABLE : DB_ERR_UNUSED;
65 list->lh_count++; /* increment the count of elements in the list */
67 new->le_head = list; /* point to head of list */
69 switch (loc) { /* put it in the right place in the list */
71 if (!(elem = list->lh_first)) { /* insert before first element in list */
72 list->lh_first = new; /* list was empty, add element to list */
74 return 0; /* and return, since the list was empty before. */
78 new->le_next = elem; /* prepare new element for its location */
79 new->le_prev = elem->le_prev;
81 elem->le_prev = new; /* insert element into list */
83 new->le_prev->le_next = new; /* update previous element */
84 else /* update head of list */
89 if (!(elem = list->lh_last)) { /* insert after last element in list */
90 list->lh_first = new; /* list was empty, add element to list */
92 return 0; /* and return, since the list was empty before. */
96 new->le_next = elem->le_next; /* prepare new element for its location */
99 elem->le_next = new; /* insert element into list */
101 new->le_next->le_prev = new; /* update next element */
102 else /* update tail of list */