Accept topic changes from servers that do not send topic-set timestamps (fixes SF...
[ircu2.10.12-pk.git] / libs / dbprim / ll_move.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 Move an element within a linked list.
28  *
29  * This function moves a specified element within the linked list.
30  *
31  * \param list  A pointer to a #link_head_t.
32  * \param elem  A pointer to the #link_elem_t describing the element
33  *              to be moved.
34  * \param loc   A #link_loc_t indicating where the entry should be
35  *              moved to.
36  * \param elem2 A pointer to a #link_elem_t describing another element
37  *              in the list if \p loc is #LINK_LOC_BEFORE or
38  *              #LINK_LOC_AFTER.
39  *
40  * \retval DB_ERR_BADARGS       An argument was invalid.
41  * \retval DB_ERR_BUSY          \p elem and \p elem2 are the same
42  *                              element.
43  * \retval DB_ERR_WRONGTABLE    \p elem or \p elem2 are in a different
44  *                              list.
45  * \retval DB_ERR_UNUSED        \p elem or \p elem2 are not in any
46  *                              list.
47  */
48 unsigned long
49 ll_move(link_head_t *list, link_elem_t *new, link_loc_t loc,
50         link_elem_t *elem)
51 {
52   initialize_dbpr_error_table(); /* initialize error table */
53
54   /* Verify arguments--if elem is set, must be a valid element; if
55    * location is before or after, elem must be set
56    */
57   if (!ll_verify(list) || !le_verify(new) || (elem && !le_verify(elem)) ||
58       ((loc == LINK_LOC_BEFORE || loc == LINK_LOC_AFTER) && !elem))
59     return DB_ERR_BADARGS;
60
61   /* elements can't be the same element */
62   if (elem && new == elem)
63     return DB_ERR_BUSY;
64
65   /* elements must be in the same list */
66   if (list != new->le_head)
67     return new->le_head ? DB_ERR_WRONGTABLE : DB_ERR_UNUSED;
68   if (elem && list != elem->le_head)
69     return elem->le_head ? DB_ERR_WRONGTABLE : DB_ERR_UNUSED;
70
71   if (new->le_next) /* Clip element out of it previous location */
72     new->le_next->le_prev = new->le_prev;
73   if (new->le_prev)
74     new->le_prev->le_next = new->le_next;
75   else
76     list->lh_first = new->le_next;
77
78   if (list->lh_last == new) /* Make sure we know where the last element is */
79     list->lh_last = new->le_prev;
80
81   switch (loc) { /* put it in the right place in the list */
82   case LINK_LOC_HEAD:
83     if (!(elem = list->lh_first)) { /* insert before first element in list */
84       list->lh_first = new; /* list was empty, add element to list */
85       list->lh_last = new;
86       return 0; /* and return, since the list was empty before. */
87     }
88     /*FALLTHROUGH*/
89   case LINK_LOC_BEFORE:
90     new->le_next = elem; /* prepare new element for its location */
91     new->le_prev = elem->le_prev;
92
93     elem->le_prev = new; /* insert element into list */
94     if (new->le_prev)
95       new->le_prev->le_next = new; /* update previous element */
96     else /* update head of list */
97       list->lh_first = new;
98     break;
99
100   case LINK_LOC_TAIL:
101     if (!(elem = list->lh_last)) { /* insert after last element in list */
102       list->lh_first = new; /* list was empty, add element to list */
103       list->lh_last = new;
104       return 0; /* and return, since the list was empty before. */
105     }
106     /*FALLTHROUGH*/
107   case LINK_LOC_AFTER:
108     new->le_next = elem->le_next; /* prepare new element for its location */
109     new->le_prev = elem;
110
111     elem->le_next = new; /* insert element into list */
112     if (new->le_next)
113       new->le_next->le_prev = new; /* update next element */
114     else /* update tail of list */
115       list->lh_last = new;
116     break;
117   }
118
119   return 0;
120 }