00001 /* 00002 ** Copyright (C) 2002 by Kevin L. Mitchell <klmitch@mit.edu> 00003 ** 00004 ** This library is free software; you can redistribute it and/or 00005 ** modify it under the terms of the GNU Library General Public 00006 ** License as published by the Free Software Foundation; either 00007 ** version 2 of the License, or (at your option) any later version. 00008 ** 00009 ** This library is distributed in the hope that it will be useful, 00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 ** Library General Public License for more details. 00013 ** 00014 ** You should have received a copy of the GNU Library General Public 00015 ** License along with this library; if not, write to the Free 00016 ** Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, 00017 ** MA 02111-1307, USA 00018 ** 00019 ** @(#)$Id: ll_move.c,v 1.4 2006/07/13 05:36:16 klmitch Exp $ 00020 */ 00029 #include "dbprim.h" 00030 #include "dbprim_int.h" 00031 00032 RCSTAG("@(#)$Id: ll_move.c,v 1.4 2006/07/13 05:36:16 klmitch Exp $"); 00033 00034 unsigned long 00035 ll_move(link_head_t *list, link_elem_t *new, link_loc_t loc, 00036 link_elem_t *elem) 00037 { 00038 initialize_dbpr_error_table(); /* initialize error table */ 00039 00040 /* Verify arguments--if elem is set, must be a valid element; if 00041 * location is before or after, elem must be set 00042 */ 00043 if (!ll_verify(list) || !le_verify(new) || (elem && !le_verify(elem)) || 00044 ((loc == LINK_LOC_BEFORE || loc == LINK_LOC_AFTER) && !elem)) 00045 return DB_ERR_BADARGS; 00046 00047 /* elements can't be the same element */ 00048 if (elem && new == elem) 00049 return DB_ERR_BUSY; 00050 00051 /* elements must be in the same list */ 00052 if (list != new->le_head) 00053 return new->le_head ? DB_ERR_WRONGTABLE : DB_ERR_UNUSED; 00054 if (elem && list != elem->le_head) 00055 return elem->le_head ? DB_ERR_WRONGTABLE : DB_ERR_UNUSED; 00056 00057 if (new->le_next) /* Clip element out of it previous location */ 00058 new->le_next->le_prev = new->le_prev; 00059 if (new->le_prev) 00060 new->le_prev->le_next = new->le_next; 00061 else 00062 list->lh_first = new->le_next; 00063 00064 if (list->lh_last == new) /* Make sure we know where the last element is */ 00065 list->lh_last = new->le_prev; 00066 00067 switch (loc) { /* put it in the right place in the list */ 00068 case LINK_LOC_HEAD: 00069 if (!(elem = list->lh_first)) { /* insert before first element in list */ 00070 list->lh_first = new; /* list was empty, add element to list */ 00071 list->lh_last = new; 00072 return 0; /* and return, since the list was empty before. */ 00073 } 00074 /*FALLTHROUGH*/ 00075 case LINK_LOC_BEFORE: 00076 new->le_next = elem; /* prepare new element for its location */ 00077 new->le_prev = elem->le_prev; 00078 00079 elem->le_prev = new; /* insert element into list */ 00080 if (new->le_prev) 00081 new->le_prev->le_next = new; /* update previous element */ 00082 else /* update head of list */ 00083 list->lh_first = new; 00084 break; 00085 00086 case LINK_LOC_TAIL: 00087 if (!(elem = list->lh_last)) { /* insert after last element in list */ 00088 list->lh_first = new; /* list was empty, add element to list */ 00089 list->lh_last = new; 00090 return 0; /* and return, since the list was empty before. */ 00091 } 00092 /*FALLTHROUGH*/ 00093 case LINK_LOC_AFTER: 00094 new->le_next = elem->le_next; /* prepare new element for its location */ 00095 new->le_prev = elem; 00096 00097 elem->le_next = new; /* insert element into list */ 00098 if (new->le_next) 00099 new->le_next->le_prev = new; /* update next element */ 00100 else /* update tail of list */ 00101 list->lh_last = new; 00102 break; 00103 } 00104 00105 return 0; 00106 }