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

Linux Cross Reference
Tina4/src/sys/lists/reclist.c

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

  1 /**@(#)Mutually recursive lists & strings handling
  2  */
  3 
  4 #include <tina/sys.h>
  5 #include <tina/sysfuncs.h>
  6 
  7 /* apply function to recursive lists */
  8 
  9 void    reclist_apply(void *ptr, int ptype, void (*func) ( /* ??? */ ), int type, void *data);
 10 
 11 void    reclist_list_apply(List * list, void (*func) ( /* ??? */ ), int type, void *data)
 12 {
 13     List   *ptr;
 14 
 15     for (ptr = list; ptr != NULL; ptr = ptr->next)
 16         reclist_apply(ptr->to, ptr->type, func, type, data);
 17 }
 18 
 19 void    reclist_string_apply(Tstring * str, void (*func) ( /* ??? */ ), int type, void *data)
 20 {
 21     List *ptr;
 22     List *end;
 23 
 24     if (str == NULL)
 25         return;
 26 
 27     end = str->end;
 28 
 29     for (ptr = str->start;; ptr = ptr->next)
 30     {
 31         reclist_apply(ptr->to, ptr->type, func, type, data);
 32 
 33         if (ptr == end)
 34             break;
 35     }
 36 }
 37 
 38 void    reclist_apply(void *ptr, int ptype, void (*func) ( /* ??? */ ), int type, void *data)
 39 
 40 /* ptr type */
 41 
 42 /* test type */
 43 
 44 {
 45     switch (ptype)
 46     {
 47         case LIST:
 48         reclist_list_apply((List *) ptr, func, type, data);
 49         break;
 50     case STRING:
 51         reclist_string_apply((Tstring *) ptr, func, type, data);
 52         break;
 53     default:
 54         if ((type == (int) NULL) || (type == ptype))
 55             func(ptr, ptype, data);
 56         break;
 57     }
 58 }
 59 
 60 /* reclist rm all occurences of to */
 61 
 62 void   *reclist_rm_entry(void *ptr, int type, void *to, void (*freefunc) ( /* ??? */ ));
 63 
 64 List   *reclist_list_rm_entry(List * list, void *to, void (*freefunc) ( /* ??? */ ))
 65 {
 66     List   *ptr;
 67     List   *next;
 68     List   *newlist = NULL;
 69 
 70     for (ptr = list; ptr != NULL; ptr = next)
 71     {
 72         void   *temp;
 73 
 74         next = ptr->next;
 75         temp = (void *) reclist_rm_entry(ptr->to, ptr->type, to, freefunc);
 76         if (temp != NULL)
 77             newlist = ref_addtostart(newlist, temp, ptr->type);
 78     }
 79     return (list_reverse(newlist));
 80 }
 81 
 82 Tstring *reclist_string_rm_entry(Tstring * str, void *to, void (*freefunc) ( /* ??? */ ))
 83 {
 84     List *ptr;
 85     List *end;
 86     List *next;
 87     List *newlist = NULL;
 88 
 89     if (str == NULL)
 90         return (NULL);
 91 
 92     end = str->end;
 93 
 94     for (ptr = str->start;; ptr = next)
 95     {
 96         void   *temp;
 97 
 98         next = ptr->next;
 99         temp = (void *) reclist_rm_entry(ptr->to, ptr->type, to, freefunc);
100 
101         if (temp != NULL)
102             newlist = dd_ref_addtostart(newlist, temp, ptr->type);
103         if (ptr == end)
104             break;
105     }
106     if (newlist == NULL)
107         return (NULL);
108     newlist = dd_list_reverse(newlist);
109     return (str_make(str->type, newlist, dd_get_end(newlist)));
110 }
111 
112 void   *reclist_rm_entry(void *ptr, int type, void *to, void (*freefunc) ( /* ??? */ ))
113 {
114     switch (type)
115     {
116         case LIST:
117         return ((void *) reclist_list_rm_entry((List *) ptr, to, freefunc));
118     case STRING:
119         return ((void *) reclist_string_rm_entry((Tstring *) ptr, to, freefunc));
120     default:
121         if (to != ptr)
122             return (ptr);
123         if (freefunc != NULL)
124             freefunc(ptr, type);
125         return (NULL);
126         break;
127     }
128     return (NULL);
129 }
130 
131 /* copy recursive lists possibly updating their types */
132 List   *reclist_list_update(List * list, void *(*func) ( /* ??? */ ), int type,
133                                     void *data)
134 {
135     List   *ptr;
136     List   *newlist = NULL;
137 
138     for (ptr = list; ptr != NULL; ptr = ptr->next)
139     {
140         void   *copy;
141         int     ptype = ptr->type;
142 
143         copy = reclist_update(ptr->to, &ptype, func, type, data);
144         if (copy != NULL)
145             newlist = ref_addtostart(newlist, copy, ptype);
146     }
147     return (list_reverse(newlist));
148 }
149 
150 Tstring *reclist_string_update(Tstring * str, void *(*func) ( /* ??? */ ), int type, void *data)
151 {
152     List *ptr;
153     List *end;
154     List *newlist = NULL;
155 
156     if (str == NULL)
157         return (NULL);
158 
159     end = str->end;
160 
161     for (ptr = str->start;; ptr = ptr->next)
162     {
163         void   *copy;
164         int     ptype = ptr->type;
165 
166         copy = reclist_update(ptr->to, &ptype, func, type, data);
167 
168         if (copy != NULL)
169             newlist = dd_ref_addtostart(newlist, copy, ptype);
170         if (ptr == end)
171             break;
172     }
173     if (newlist == NULL)
174         return (NULL);
175     newlist = dd_list_reverse(newlist);
176     str = str_make(str->type, newlist, dd_get_end(newlist));
177     if (str->type == LOOP)
178     {
179         str->end->next = str->start;
180         str->start->last = str->end;
181     }
182     return (str);
183 }
184 
185 void   *reclist_update(void *ptr, int *ptype, void *(*func) ( /* ??? */ ), int type, void *data)
186 
187 /* ptr type */
188 
189 /* test type */
190 
191 {
192     switch (*ptype)
193     {
194         case LIST:
195         return ((void *) reclist_list_update((List *) ptr, func, type, data));
196     case STRING:
197         return ((void *) reclist_string_update((Tstring *) ptr, func, type, data));
198     default:
199         if ((type == (int) NULL) || (type == *ptype))
200             return ((func == NULL) ? ptr : func(ptr, ptype, data));
201         break;
202     }
203     return (NULL);
204 }
205 
206 /* Update recursive lists to simple flat lists allow type of element to
207  * be updated */
208 List   *reclist_flat(void *ptr, int *ptype, void *(*func) ( /* ??? */ ), int type, void *data);
209 List   *reclist_list_flat(List * list, void *(*func) ( /* ??? */ ), int type, void *data)
210 {
211     List   *ptr;
212     List   *listend = NULL;
213     List   *flatlist = NULL;
214 
215     for (ptr = list; ptr != NULL; ptr = ptr->next)
216     {
217         List   *newlist;
218         int     ptype = ptr->type;
219 
220         newlist = reclist_flat(ptr->to, &ptype, func, type, data);
221         if (newlist == NULL)
222             continue;
223         if (flatlist == NULL)
224             flatlist = newlist;
225         else
226             listend->next = newlist;
227         listend = list_get_end(newlist);
228     }
229     return (flatlist);
230 }
231 
232 List   *reclist_string_flat(Tstring * str, void *(*func) ( /* ??? */ ), int type, void *data)
233 {
234     List *ptr;
235     List *end;
236     List   *listend = NULL;
237     List   *flatlist = NULL;
238 
239     if (str == NULL)
240         return (NULL);
241 
242     end = str->end;
243 
244     for (ptr = str->start;; ptr = ptr->next)
245     {
246         List   *newlist;
247         int     ptype = ptr->type;
248 
249         newlist = reclist_flat(ptr->to, &ptype, func, type, data);
250         if (newlist != NULL)
251         {
252             if (flatlist == NULL)
253                 flatlist = newlist;
254             else
255                 listend->next = newlist;
256             listend = list_get_end(newlist);
257         }
258         if (ptr == end)
259             break;
260     }
261     return (flatlist);
262 }
263 
264 List   *reclist_flat(void *ptr, int *ptype, void *(*func) ( /* ??? */ ), int type, void *data)
265 
266 /* ptr type */
267 
268 /* test type */
269 
270 {
271     switch (*ptype)
272     {
273         case LIST:
274         return (reclist_list_flat((List *) ptr, func, type, data));
275     case STRING:
276         return (reclist_string_flat((Tstring *) ptr, func, type, data));
277     default:
278         if ((type == (int) NULL) || (type == *ptype))
279         {
280             ptr = (func == NULL) ? ptr : func(ptr, ptype, data);
281             if (ptr == NULL)
282                 return (NULL);
283             return (link_alloc((void *) ptr, *ptype));
284         }
285         break;
286     }
287     return (NULL);
288 }
289 
290 /* copy recursive lists without updating their types */
291 
292 void   *reclist_copy(void *ptr, int ptype, void *(*func) ( /* ??? */ ), int type, void *data);
293 
294 List   *reclist_list_copy(List * list, void *(*func) ( /* ??? */ ), int type, void *data)
295 {
296     List   *ptr;
297     List   *newlist = NULL;
298 
299     for (ptr = list; ptr != NULL; ptr = ptr->next)
300     {
301         void   *copy;
302 
303         copy = reclist_copy(ptr->to, ptr->type, func, type, data);
304         if (copy != NULL)
305             newlist = ref_addtostart(newlist, copy, ptr->type);
306     }
307     return (list_reverse(newlist));
308 }
309 
310 Tstring *reclist_string_copy(Tstring * str, void *(*func) ( /* ??? */ ), int type, void *data)
311 {
312     List *ptr;
313     List *end;
314     List *newlist = NULL;
315 
316     if (str == NULL)
317         return (NULL);
318 
319     end = str->end;
320 
321     for (ptr = str->start;; ptr = ptr->next)
322     {
323         void   *copy;
324 
325         copy = reclist_copy(ptr->to, ptr->type, func, type, data);
326 
327         if (copy != NULL)
328             newlist = dd_ref_addtostart(newlist, copy, ptr->type);
329         if (ptr == end)
330             break;
331     }
332     if (newlist == NULL)
333         return (NULL);
334     newlist = dd_list_reverse(newlist);
335     return (str_make(str->type, newlist, dd_get_end(newlist)));
336 }
337 
338 void   *reclist_copy(void *ptr, int ptype, void *(*func) ( /* ??? */ ), int type, void *data)
339 
340 /* ptr type */
341 
342 /* test type */
343 
344 {
345     switch (ptype)
346     {
347         case LIST:
348         return ((void *) reclist_list_copy((List *) ptr, func, type, data));
349     case STRING:
350         return ((void *) reclist_string_copy((Tstring *) ptr, func, type, data));
351     default:
352         if ((type == (int) NULL) || (type == ptype))
353             return ((func == NULL) ? ptr : func(ptr, ptype, data));
354         break;
355     }
356     return (NULL);
357 }
358 
359 /* Recursive list free */
360 List   *reclist_list_free(List * list, void (*freefunc) ( /* ??? */ ), int type,
361                                   void *data)
362 {
363     List   *ptr;
364     List   *next;
365     List   *last = NULL;
366 
367     for (ptr = list; ptr != NULL; ptr = next)
368     {
369         next = ptr->next;
370         ptr->to = reclist_free(ptr->to, ptr->type, freefunc, type, data);
371         if (ptr->to == NULL)
372         {
373             if (last == NULL)
374                 list = link_rm_el(list);
375             else
376                 (void) link_rm_next_el(last);
377         } else
378             last = ptr;
379     }
380     return (list);
381 }
382 
383 Tstring *reclist_string_free(Tstring * str, void (*freefunc) ( /* ??? */ ), int type, void *data)
384 {
385     List *ptr;
386     List *start;
387     List *end;
388     List *next;
389     int     more = 1;
390 
391     if (str == NULL)
392         return (NULL);
393 
394     ptr = start = str->start;
395     end = str->end;
396 
397     do
398     {
399         next = ptr->next;
400         if (ptr == end)
401             more = 0;
402         ptr->to = reclist_free(ptr->to, ptr->type, freefunc, type, data);
403         if (ptr->to == NULL)
404         {
405             if (ptr == end)
406             {
407                 if (ptr == start)
408                     start = end = NULL;
409                 else
410                     end = end->last;
411             }
412             if (ptr == start)
413                 start = dd_link_rm_el(start);
414             else
415                 ptr = dd_link_rm_el(ptr);
416         }
417         ptr = next;
418     } while (more);
419 
420     if (start == NULL)
421     {
422         str_rm_only_str(str);
423         return (NULL);
424     }
425     str->start = start;
426     str->end = end;
427     str->count = ddstr_count(start, end);
428     return (str);
429 }
430 
431 void   *reclist_free(void *ptr, int ptype, void (*freefunc) ( /* ??? */ ), int type, void *data)
432 
433 /* ptr type */
434 
435 /* test type */
436 
437 {
438     switch (ptype)
439     {
440         case LIST:
441         return ((void *) reclist_list_free((List *) ptr, freefunc, type, data));
442     case STRING:
443         return ((void *) reclist_string_free((Tstring *) ptr, freefunc, type, data));
444     default:
445         if ((type != (int) NULL) && (type != ptype))
446             return (ptr);
447         if (freefunc != NULL)
448             freefunc(ptr, ptype, data);
449         return (NULL);
450     }
451 }
452 

~ [ 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.