~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina6/tina-libs/tina/sys/sysLst_prop.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /**********
  2  * 
  3  * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
  4  * University of Manchester, UK.  All rights reserved.
  5  * 
  6  * Redistribution and use in source and binary forms, with or without modification, 
  7  * are permitted provided that the following conditions are met:
  8  * 
  9  *   . Redistributions of source code must retain the above copyright notice, 
 10  *     this list of conditions and the following disclaimer.
 11  *    
 12  *   . Redistributions in binary form must reproduce the above copyright notice,
 13  *     this list of conditions and the following disclaimer in the documentation 
 14  *     and/or other materials provided with the distribution.
 15  * 
 16  *   . Neither the name of the University of Manchester nor the names of its
 17  *     contributors may be used to endorse or promote products derived from this 
 18  *     software without specific prior written permission.
 19  * 
 20  * 
 21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  * POSSIBILITY OF SUCH DAMAGE.
 32  *
 33  **********
 34  * 
 35  * Program :    TINA
 36  * File    :  $Source: /home/tina/cvs/tina-libs/tina/sys/sysLst_prop.c,v $
 37  * Date    :  $Date: 2003/09/22 16:09:02 $
 38  * Version :  $Revision: 1.4 $
 39  * CVS Id  :  $Id: sysLst_prop.c,v 1.4 2003/09/22 16:09:02 tony Exp $
 40  */
 41 /**
 42  *  @file Property list handling.  (Using generic lists & Props).
 43  *  @brief Prop is { int type; int count; void (*freefunc)(); void *to; } and such
 44  *  structres are appended to any Tina data structure which may require future extension.
 45  *  Additional data may be placed in and recovered from these lists as required.
 46  *
 47  *********
 48 */
 49 
 50 #include "sysLst_prop.h"
 51 
 52 #if HAVE_CONFIG_H
 53   #include <config.h>
 54 #endif
 55 
 56 #include <stdio.h>
 57 #include <tina/sys/sys_GenDef.h>
 58 #include <tina/sys/sys_LstDef.h>
 59 #include <tina/sys/sys_MemDef.h>
 60 #include <tina/sys/sysLst_list.h>
 61 #include <tina/sys/sysMem_ralloc.h>
 62 
 63 
 64 /* Alloc and init prop structure */
 65 Prop *prop_alloc(void *ptr, int type, void (*freefunc) ( /* ??? */ ))
 66 {
 67         Prop *prop = ts_ralloc(Prop);
 68 
 69         prop->type = type;
 70         prop->freefunc = freefunc;
 71         prop->count = 1;
 72         prop->to = ptr;
 73         return (prop);
 74 }
 75 
 76 /* Get first ref of given type */
 77 static Prop *prop_get_by_type(List * proplist, int type)
 78 {
 79         List *lptr;
 80         Prop *prop;
 81 
 82         for (lptr = proplist; lptr != NULL; lptr = lptr->next)
 83         {
 84                 prop = (Prop *) lptr->to;
 85                 if (prop != NULL && prop->type == type)
 86                         return (prop);
 87         }
 88 
 89         return (NULL);
 90 }
 91 
 92 static Prop *prop_get_by_ref(List * proplist, void *ptr)
 93 {
 94         List *lptr;
 95         Prop *prop;
 96 
 97         for (lptr = proplist; lptr != NULL; lptr = lptr->next)
 98         {
 99                 prop = (Prop *) lptr->to;
100                 if (prop->to == ptr)
101                         return (prop);
102         }
103 
104         return (NULL);
105 }
106 
107 /* Add a new prop to front */
108 List *proplist_add(List * proplist, void *ptr, int type,
109                                                                          void (*freefunc) ( /* ??? */ ))
110 {
111         List *lptr;
112 
113         lptr = link_alloc((void *) prop_alloc(ptr, type, freefunc), PROP_TYPE);
114         return (link_addtostart(proplist, lptr));
115 }
116 
117 /* Add if not present */
118 List *proplist_addifnp(List * proplist, void *ptr, int type,
119                                                                                          void (*freefunc) ( /* ??? */ ), Bool dofree)
120 {
121         List *lptr;
122         Prop *prop = prop_get_by_type(proplist, type);
123 
124         if (prop != NULL)
125         {
126                 if (dofree == true && prop->freefunc != NULL)
127                         prop->freefunc(prop->to, prop->type);
128                 prop->to = ptr;
129                 return (proplist);
130         }
131         lptr = link_alloc((void *) prop_alloc(ptr, type, freefunc), PROP_TYPE);
132         return (link_addtostart(proplist, lptr));
133 }
134 
135 /* Get first prop of specified type */
136 void *prop_get(List * proplist, int type)
137 {
138         Prop *prop = prop_get_by_type(proplist, type);
139 
140         return ((prop == NULL) ? NULL : prop->to);
141 }
142 
143 int prop_present(List * proplist, int type)
144 {
145         Prop *prop = prop_get_by_type(proplist, type);
146 
147         return (prop != NULL);
148 }
149 
150 int prop_ref_present(List * proplist, void *ptr)
151 {
152         List *lptr;
153 
154         for (lptr = proplist; lptr != NULL; lptr = lptr->next)
155                 if (((Prop *) lptr->to)->to == ptr)
156                         return (1);
157         return (0);
158 }
159 
160 /* Get first element of * proplist */
161 void *prop_first(List * proplist, int *type)
162 {
163         if (proplist == NULL)
164         {
165                 *type = 0;
166                 return (NULL);
167         }
168         *type = ((Prop *) proplist->to)->type;
169         return (((Prop *) proplist->to)->to);
170 }
171 
172 /* Set/reset prop of specified type */
173 int prop_set(List * proplist, void *ptr, int type, Bool dofree)
174 {
175         /* if true try to free the referenced property value */
176         Prop *prop = prop_get_by_type(proplist, type);
177 
178         if (prop == NULL)
179                 return (0);
180 
181         if (dofree == true && prop->freefunc != NULL)
182                 prop->freefunc(prop->to, prop->type);
183 
184         prop->to = ptr;
185         return (1);
186 }
187 
188 List *proplist_copy(List * proplist)
189 {
190         List *list_copy();
191         List *copy;
192         List *lptr;
193 
194         copy = list_copy(proplist, (void *(*)()) NULL, NULL);   /* copy list only (not
195                                                                                                                                                                                                                                  * * prop) */
196 
197         for (lptr = copy; lptr != NULL; lptr = lptr->next)
198                 ++((Prop *) (lptr->to))->count;
199         return (copy);
200 }
201 
202 List   *proplist_propcopy(List * proplist, void *(*prop_copy)())
203 {
204     List   *list_copy();
205     List   *copy;
206     List   *lptr;
207 
208     copy = list_copy(proplist, prop_copy, NULL);
209 
210     for (lptr = copy; lptr != NULL; lptr = lptr->next)
211         ++((Prop *) (lptr->to))->count;
212     return (copy);
213 }
214 
215 /* Rm prop ref from list */
216 List *proplist_rm(List * proplist, int type)
217 {
218         Prop *prop = prop_get_by_type(proplist, type);
219         List *list_rm_ref();
220 
221         if (prop == NULL)
222                 return (proplist);
223 
224         if (--prop->count)
225                 return (list_rm_ref(proplist, (void *) prop, (void (*)()) NULL));
226         else
227                 return (list_rm_ref(proplist, (void *) prop, (void (*)()) rfree));
228 }
229 
230 /* Rm prop ref from list */
231 List *proplist_rm_by_ref(List * proplist, void *ptr)
232 {
233         Prop *prop = prop_get_by_ref(proplist, ptr);
234         List *list_rm_ref();
235 
236         if (prop == NULL)
237                 return (proplist);
238 
239         if (--prop->count)
240                 return (list_rm_ref(proplist, (void *) prop, (void (*)()) NULL));
241         else
242                 return (list_rm_ref(proplist, (void *) prop, (void (*)()) rfree));
243 }
244 
245 /* Rm prop ref and free prop */
246 List *proplist_free(List * proplist, int type)
247 {
248         Prop *prop = prop_get_by_type(proplist, type);
249         List *list_rm_ref();
250 
251         if (prop == NULL)
252                 return (proplist);
253 
254         if (--prop->count)
255                 return (list_rm_ref(proplist, (void *) prop, (void (*)()) NULL));
256         else
257         {                                                                                                                       /* last reference */
258                 if (prop->freefunc != NULL)
259                         prop->freefunc(prop->to, prop->type);
260 
261                 return (list_rm_ref(proplist, (void *) prop, rfree));
262         }
263 }
264 
265 List *proplist_free_by_ref(List * proplist, void *ptr)
266 {
267         Prop *prop = prop_get_by_ref(proplist, ptr);
268         List *list_rm_ref();
269 
270         if (prop == NULL)
271                 return (proplist);
272 
273         if (--prop->count)
274                 return (list_rm_ref(proplist, (void *) prop, (void (*)()) NULL));
275         else
276         {                                                                                                                       /* last reference */
277                 if (prop->freefunc != NULL)
278                         prop->freefunc(prop->to, prop->type);
279 
280                 return (list_rm_ref(proplist, (void *) prop, rfree));
281         }
282 }
283 
284 static void prop_rm(Prop * prop)
285 {
286         if (prop == NULL)
287                 return;
288 
289         if (!(--prop->count))
290                 rfree((void *) prop);
291 }
292 
293 static void prop_free(Prop * prop)
294 {
295         if (prop == NULL)
296                 return;
297 
298         if (!(--prop->count))
299         {
300                 if (prop->freefunc != NULL)
301                         prop->freefunc(prop->to, prop->type);
302                 rfree((void *) prop);
303         }
304 }
305 
306 /* Rm all references in proplist */
307 void proplist_rmlist(List * proplist)
308 {
309         void list_rm();
310 
311         list_rm(proplist, prop_rm);
312 }
313 
314 /* Rm the first property in the list */
315 List *proplist_rm_first(List * proplist)
316 {
317         List *link_rm();
318 
319         return (link_rm(proplist, prop_rm));
320 }
321 
322 /* Free whole list */
323 void proplist_freelist(List * proplist)
324 {
325         void list_rm();
326 
327         list_rm(proplist, prop_free);
328 }
329 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.