gwenhywfar 4.0.3

list1.c

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