Accept topic changes from servers that do not send topic-set timestamps (fixes SF...
[ircu2.10.12-pk.git] / libs / dbprim / ll_add.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_link
27  * \brief Add an element to a linked list.
28  *
29  * This function adds a given element to a specified linked list in
30  * the specified location.
31  *
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
34  *              linked list.
35  * \param loc   A #link_loc_t indicating where the entry should be
36  *              added.
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
39  *              #LINK_LOC_AFTER.
40  *
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.
45  */
46 unsigned long
47 ll_add(link_head_t *list, link_elem_t *new, link_loc_t loc,
48        link_elem_t *elem)
49 {
50   initialize_dbpr_error_table(); /* initialize error table */
51
52   /* Verify arguments--if elem is set, must be a valid element; if
53    * location is before or after, elem must be set
54    */
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;
58
59   /* new element must not be in list already */
60   if (new->le_head)
61     return DB_ERR_BUSY;
62   if (elem && list != elem->le_head) /* element must be in the list */
63     return elem->le_head ? DB_ERR_WRONGTABLE : DB_ERR_UNUSED;
64
65   list->lh_count++; /* increment the count of elements in the list */
66
67   new->le_head = list; /* point to head of list */
68
69   switch (loc) { /* put it in the right place in the list */
70   case LINK_LOC_HEAD:
71     if (!(elem = list->lh_first)) { /* insert before first element in list */
72       list->lh_first = new; /* list was empty, add element to list */
73       list->lh_last = new;
74       return 0; /* and return, since the list was empty before. */
75     }
76     /*FALLTHROUGH*/
77   case LINK_LOC_BEFORE:
78     new->le_next = elem; /* prepare new element for its location */
79     new->le_prev = elem->le_prev;
80
81     elem->le_prev = new; /* insert element into list */
82     if (new->le_prev)
83       new->le_prev->le_next = new; /* update previous element */
84     else /* update head of list */
85       list->lh_first = new;
86     break;
87
88   case LINK_LOC_TAIL:
89     if (!(elem = list->lh_last)) { /* insert after last element in list */
90       list->lh_first = new; /* list was empty, add element to list */
91       list->lh_last = new;
92       return 0; /* and return, since the list was empty before. */
93     }
94     /*FALLTHROUGH*/
95   case LINK_LOC_AFTER:
96     new->le_next = elem->le_next; /* prepare new element for its location */
97     new->le_prev = elem;
98
99     elem->le_next = new; /* insert element into list */
100     if (new->le_next)
101       new->le_next->le_prev = new; /* update next element */
102     else /* update tail of list */
103       list->lh_last = new;
104     break;
105   }
106
107   return 0;
108 }