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_add.c,v 1.3 2006/07/13 05:36:16 klmitch Exp $ 00020 */ 00028 #include "dbprim.h" 00029 #include "dbprim_int.h" 00030 00031 RCSTAG("@(#)$Id: ll_add.c,v 1.3 2006/07/13 05:36:16 klmitch Exp $"); 00032 00033 unsigned long 00034 ll_add(link_head_t *list, link_elem_t *new, link_loc_t loc, 00035 link_elem_t *elem) 00036 { 00037 initialize_dbpr_error_table(); /* initialize error table */ 00038 00039 /* Verify arguments--if elem is set, must be a valid element; if 00040 * location is before or after, elem must be set 00041 */ 00042 if (!ll_verify(list) || !le_verify(new) || (elem && !le_verify(elem)) || 00043 ((loc == LINK_LOC_BEFORE || loc == LINK_LOC_AFTER) && !elem)) 00044 return DB_ERR_BADARGS; 00045 00046 /* new element must not be in list already */ 00047 if (new->le_head) 00048 return DB_ERR_BUSY; 00049 if (elem && list != elem->le_head) /* element must be in the list */ 00050 return elem->le_head ? DB_ERR_WRONGTABLE : DB_ERR_UNUSED; 00051 00052 list->lh_count++; /* increment the count of elements in the list */ 00053 00054 new->le_head = list; /* point to head of list */ 00055 00056 switch (loc) { /* put it in the right place in the list */ 00057 case LINK_LOC_HEAD: 00058 if (!(elem = list->lh_first)) { /* insert before first element in list */ 00059 list->lh_first = new; /* list was empty, add element to list */ 00060 list->lh_last = new; 00061 return 0; /* and return, since the list was empty before. */ 00062 } 00063 /*FALLTHROUGH*/ 00064 case LINK_LOC_BEFORE: 00065 new->le_next = elem; /* prepare new element for its location */ 00066 new->le_prev = elem->le_prev; 00067 00068 elem->le_prev = new; /* insert element into list */ 00069 if (new->le_prev) 00070 new->le_prev->le_next = new; /* update previous element */ 00071 else /* update head of list */ 00072 list->lh_first = new; 00073 break; 00074 00075 case LINK_LOC_TAIL: 00076 if (!(elem = list->lh_last)) { /* insert after last element in list */ 00077 list->lh_first = new; /* list was empty, add element to list */ 00078 list->lh_last = new; 00079 return 0; /* and return, since the list was empty before. */ 00080 } 00081 /*FALLTHROUGH*/ 00082 case LINK_LOC_AFTER: 00083 new->le_next = elem->le_next; /* prepare new element for its location */ 00084 new->le_prev = elem; 00085 00086 elem->le_next = new; /* insert element into list */ 00087 if (new->le_next) 00088 new->le_next->le_prev = new; /* update next element */ 00089 else /* update tail of list */ 00090 list->lh_last = new; 00091 break; 00092 } 00093 00094 return 0; 00095 }