list1.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  $RCSfile$
00003                              -------------------
00004     cvs         : $Id$
00005     begin       : Sat Nov 15 2003
00006     copyright   : (C) 2003 by Martin Preuss
00007     email       : martin@libchipcard.de
00008 
00009  ***************************************************************************
00010  *                                                                         *
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU Lesser General Public            *
00013  *   License as published by the Free Software Foundation; either          *
00014  *   version 2.1 of the License, or (at your option) any later version.    *
00015  *                                                                         *
00016  *   This library is distributed in the hope that it will be useful,       *
00017  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00018  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00019  *   Lesser General Public License for more details.                       *
00020  *                                                                         *
00021  *   You should have received a copy of the GNU Lesser General Public      *
00022  *   License along with this library; if not, write to the Free Software   *
00023  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston,                 *
00024  *   MA  02111-1307  USA                                                   *
00025  *                                                                         *
00026  ***************************************************************************/
00027 
00028 
00029 #ifdef HAVE_CONFIG_H
00030 # include <config.h>
00031 #endif
00032 
00033 #include "list1_p.h"
00034 #include <gwenhywfar/misc.h>
00035 #include <gwenhywfar/debug.h>
00036 
00037 
00038 
00039 
00040 GWEN_LIST1 *GWEN_List1_new() {
00041   GWEN_LIST1 *l;
00042 
00043   GWEN_NEW_OBJECT(GWEN_LIST1, l);
00044   return l;
00045 }
00046 
00047 
00048 void GWEN_List1_free(GWEN_LIST1 *l) {
00049   if (l) {
00050     GWEN_FREE_OBJECT(l);
00051   }
00052 }
00053 
00054 
00055 
00056 int GWEN_List1_GetCount(const GWEN_LIST1 *l) {
00057   assert(l);
00058   return l->count;
00059 }
00060 
00061 
00062 
00063 int GWEN_List1_Add(GWEN_LIST1 *l, GWEN_LIST1_ELEMENT *el) {
00064   assert(l);
00065   assert(el);
00066   if (el->listPtr) {
00067     /* element is already part of another list */
00068     DBG_ERROR(GWEN_LOGDOMAIN, "Element is already part of a list");
00069     assert(0);
00070     return -1;
00071   }
00072 
00073   if (l->firstElement==0)
00074     l->firstElement=el;
00075 
00076   el->prevElement=l->lastElement;
00077   if (l->lastElement)
00078     l->lastElement->nextElement=el;
00079   l->lastElement=el;
00080 
00081   el->listPtr=l;
00082   l->count++;
00083 
00084   return 0;
00085 }
00086 
00087 
00088 
00089 int GWEN_List1_AddList(GWEN_LIST1 *dest, GWEN_LIST1 *l) {
00090   GWEN_LIST1_ELEMENT *el;
00091 
00092   assert(dest);
00093   assert(l);
00094 
00095   while((el=l->firstElement)) {
00096     GWEN_List1_Del(el);
00097     GWEN_List1_Add(dest, el);
00098   }
00099 
00100   return 0;
00101 }
00102 
00103 
00104 
00105 int GWEN_List1_Insert(GWEN_LIST1 *l, GWEN_LIST1_ELEMENT *el) {
00106   assert(l);
00107   assert(el);
00108   if (el->listPtr) {
00109     /* element is already part of another list */
00110     DBG_ERROR(GWEN_LOGDOMAIN, "Element is already part of a list");
00111     return -1;
00112   }
00113 
00114   el->nextElement=l->firstElement;
00115   l->firstElement=el;
00116 
00117   if (l->lastElement==0)
00118     l->lastElement=el;
00119 
00120   el->listPtr=l;
00121   l->count++;
00122 
00123   return 0;
00124 }
00125 
00126 
00127 
00128 int GWEN_List1_Del(GWEN_LIST1_ELEMENT *el) {
00129   GWEN_LIST1 *l;
00130 
00131   assert(el);
00132   l=el->listPtr;
00133 
00134   if (l==0) {
00135     /* not part of any list */
00136     DBG_ERROR(GWEN_LOGDOMAIN, "Element is not part of any list");
00137     return -1;
00138   }
00139 
00140   /* unlink from previous */
00141   if (el->prevElement)
00142     el->prevElement->nextElement=el->nextElement;
00143 
00144   /* unlink from next */
00145   if (el->nextElement)
00146     el->nextElement->prevElement=el->prevElement;
00147 
00148   /* unlink from list */
00149   if (l->firstElement==el)
00150     l->firstElement=el->nextElement;
00151   if (l->lastElement==el)
00152     l->lastElement=el->prevElement;
00153   l->count--;
00154 
00155   el->nextElement=0;
00156   el->prevElement=0;
00157   el->listPtr=0;
00158 
00159   return 0;
00160 }
00161 
00162 
00163 
00164 void *GWEN_List1_GetFirst(const GWEN_LIST1 *l) {
00165   if (l->firstElement)
00166     return l->firstElement->data;
00167   return 0;
00168 }
00169 
00170 
00171 
00172 void *GWEN_List1_GetLast(const GWEN_LIST1 *l) {
00173   if (l->lastElement)
00174     return l->lastElement->data;
00175   return 0;
00176 }
00177 
00178 
00179 
00180 
00181 
00182 
00183 GWEN_LIST1_ELEMENT *GWEN_List1Element_new(void *d) {
00184   GWEN_LIST1_ELEMENT *el;
00185 
00186   GWEN_NEW_OBJECT(GWEN_LIST1_ELEMENT, el);
00187   el->data=d;
00188 
00189   return el;
00190 }
00191 
00192 
00193 
00194 void GWEN_List1Element_free(GWEN_LIST1_ELEMENT *el) {
00195   if (el) {
00196     if (el->listPtr)
00197       GWEN_List1_Del(el);
00198     GWEN_FREE_OBJECT(el);
00199   }
00200 }
00201 
00202 
00203 
00204 void *GWEN_List1Element_GetData(const GWEN_LIST1_ELEMENT *el) {
00205   return el->data;
00206 }
00207 
00208 
00209 
00210 void *GWEN_List1Element_GetPrevious(const GWEN_LIST1_ELEMENT *el){
00211   if (el->prevElement)
00212     return el->prevElement->data;
00213   return 0;
00214 }
00215 
00216 
00217 
00218 void *GWEN_List1Element_GetNext(const GWEN_LIST1_ELEMENT *el){
00219   if (el->nextElement)
00220     return el->nextElement->data;
00221   return 0;
00222 }
00223 
00224 
00225 
00226 
00227 
00228 
00229 
00230 
00231 
00232 
Generated on Mon Jul 5 22:52:48 2010 for gwenhywfar by  doxygen 1.6.3