1:
  2:
  3:/*
  4: * $Id: list.c,v 1.1 2002/10/10 12:40:02 koschke Exp $
  5: *
  6: * CONCEPTS
  7: * Copyright (C) 1994 Technical University of Braunschweig, Germany
  8: * Written by Christian Lindig (lindig@ips.cs.tu-bs.de)
  9: * 
 10: * This program is free software; you can redistrbute it and/or modify
 11: * it under the terms of the GNU General Public License as published by
 12: * the Free Software Foundation; either version 2 of the License, or
 13: * (at your option) any later version.
 14: * 
 15: * This program is distributed in the hope that it will be useful,
 16: * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18: * GNU General Public License for more details.
 19: * 
 20: * You should have received a copy of the GNU General Public License
 21: * along with this program; if not, write to the Free Software
 22: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 23: *
 24: *
 25: * A generic list package - the content is totally managed by the caller
 26: */
 27:
 28:/* requires: panic, free, malloc */
 29:
 30:#include "config.h"
 31:#if defined(STDC_HEADERS) || defined(HAVE_STDLIB_H)
 32:#  include <stdlib.h>
 33:#endif
 34:
 35:#ifdef DBMALLOC
 36:#  include <malloc.h>
 37:#endif
 38:
 39:#include "defines.h"
 40:#include "panic.h"
 41:#include "list.h"
 42:
 43:/*
 44: * ListNew
 45: * 
 46: * creates a new List, initializes it and returns a pointer to
 47: * it. Later on LeistDelete can be used to delete the member of the
 48: * lists but this will not free the List structure itself. The user
 49: * must call free() explicitly */
 50:
 51:List *
 52:ListNew ()
 53:
 54:{
 55:	List *list;
 56:	list = (List*) malloc (sizeof(List));
 57:	if (!list) {
 58:		panic ("malloc failed in %s:%i",__FILE__,__LINE__) ;
 59:	}
 60:	ListInit (list);	/* initialize list */
 61:	return list;
 62:}
 63:
 64:/*
 65: * ListInit sets everything in an just created List to a defined state
 66: * - should be called whenever a List is created */
 67:
 68:VOID
 69:ListInit (list)
 70:
 71:	List *list;
 72:
 73:{
 74:	list->entries = 0;	/* no members */
 75:	list->first = LIST_NULL ;
 76:}
 77:
 78:/*
 79: * ListDelete remove memory associated to a List from memory but DOES
 80: * NOT remove memory associated with the client data - this should be
 81: * freed before ListDelete is called. ListDelete does not free the
 82: * List struct! */
 83:
 84:VOID 
 85:ListDelete (list) 
 86:	List *list;
 87:{
 88:	ListEntry *entry,*old ;
 89:
 90:	/* free all ListEntries one by one */
 91:	entry = list->first ;
 92:	while (entry) {
 93:		old = entry ;
 94:		entry = entry->next ;
 95:		free ((char*) old);
 96:	}
 97:}
 98:
 99:/*
100: * ListNewEntry creates a new ListElement - i.e. a new entry to hold
101: * client data and retuns a pointer to the new entry. */
102:
103:ListEntry *
104:ListNewEntry (list) 
105:	List *list;
106:{
107:
108:	ListEntry *entry ;
109:
110:	entry = (ListEntry*) malloc (sizeof(ListEntry));
111:	if (!entry) {
112:		panic ("malloc failed in %s:%i",__FILE__,__LINE__) ;
113:	}
114:
115:	entry->clientdata = (char*) NULL ; /* no client data yet */
116:	entry->list = list;
117:	entry->next = list->first ;
118:	list->first = entry ;
119:	list->entries++ ;
120:
121:	return entry ;
122:}
123:
124:/*
125: * ListDeleteEntry
126: *
127: * remove a ListEntry from a chain - does not free client data. From
128: * entry the list it is in can be deduced so the List structure does
129: * not to be passed seperatetly to ListDeleteEntry. */
130:
131:VOID
132:ListDeleteEntry (entry) 
133:	ListEntry *entry;
134:{
135:
136:	ListEntry *prev ;
137:
138:	if (entry == entry->list->first) {
139:		/* first entry in chain */
140:		entry->list->first = entry->next ;
141:	} else {
142:		for (prev = entry->list->first ;; prev = prev->next) {
143:			if (prev == LIST_NULL) {
144:				panic ("malformed List in %s:%i"
145:				       ,__FILE__,__LINE__) ;
146:			}
147:			if (prev->next == entry) {
148:				/* found - remove from chain */
149:				prev->next = entry->next ;
150:				break ;
151:			}
152:		}
153:	}
154:	entry->list->entries--;	/* decrease number of entries */
155:	free((char*)entry);	/* free ListEntry - assumes that data
156:				   associated with entry is allready
157:				   free()'ed */
158:
159:}
160:
161:/*
162: * ListGetValue
163: * returns the client data from a given element */
164: 
165:char *
166:ListGetValue (entry) 
167:	ListEntry *entry ;
168:{
169:	return (entry->clientdata) ;
170:}
171:		
172:/*
173: * ListFindEntry
174: * returns a pointer to the entry which client data matches data. The
175: * user supplied function eq() is called for matching */
176:
177:ListEntry *
178:ListFindEntry (list,data,eq)
179:	
180:	List *list;
181:	char *data;
182:	int (*eq)_ANSI_ARGS_((char*,char*));
183:
184:{
185:
186:	ListEntry *entry;
187:
188:	entry = list->first;
189:	while (entry && !(*eq)(data,entry->clientdata)) {
190:		entry = entry->next;
191:	}
192:	return entry ;
193:}
194:
195:
196:/*
197: * ListSetValue
198: * set the client data of a given entry */
199:
200:VOID
201:ListSetValue (entry,data) 
202:
203:	ListEntry *entry;
204:	char *data;
205:{
206:	entry->clientdata = data ;
207:}
208:		
209:	
210:
211:	
212:



back ...