Home | History | Annotate | Line # | Download | only in dist
      1 /* $Xorg: list.c,v 1.4 2001/02/09 02:05:59 xorgcvs Exp $ */
      2 /******************************************************************************
      3 
      4 Copyright 1993, 1998  The Open Group
      5 
      6 Permission to use, copy, modify, distribute, and sell this software and its
      7 documentation for any purpose is hereby granted without fee, provided that
      8 the above copyright notice appear in all copies and that both that
      9 copyright notice and this permission notice appear in supporting
     10 documentation.
     11 
     12 The above copyright notice and this permission notice shall be included in
     13 all copies or substantial portions of the Software.
     14 
     15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
     18 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
     19 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
     20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     21 
     22 Except as contained in this notice, the name of The Open Group shall not be
     23 used in advertising or otherwise to promote the sale, use or other dealings
     24 in this Software without prior written authorization from The Open Group.
     25 ******************************************************************************/
     26 
     27 #include "xsm.h"
     28 
     29 List *
     30 ListInit(void)
     31 {
     32 	List *l;
     33 
     34 	l = (List *)XtMalloc(sizeof *l);
     35 	if(!l) return l;
     36 	l->next = l;
     37 	l->prev = l;
     38 	l->thing = NULL;
     39 	return l;
     40 }
     41 
     42 List *
     43 ListFirst(List *l)
     44 {
     45 	if(l->next->thing) return l->next;
     46 	else return NULL;
     47 }
     48 
     49 List *
     50 ListNext(List *l)
     51 {
     52 	if(l->next->thing) return l->next;
     53 	else return NULL;
     54 }
     55 
     56 void
     57 ListFreeAll(List *l)
     58 {
     59 	char *thing;
     60 	List *next;
     61 
     62 	next = l->next;
     63 	do {
     64 		l = next;
     65 		next = l->next;
     66 		thing = l->thing;
     67 		XtFree((char *)l);
     68 	} while(thing);
     69 }
     70 
     71 void
     72 ListFreeAllButHead(List *l)
     73 {
     74 	List *p, *next;
     75 
     76 	p = ListFirst(l);
     77 
     78 	while (p)
     79 	{
     80 	    next = ListNext (p);
     81 	    XtFree((char *) p);
     82 	    p = next;
     83 	}
     84 
     85 	l->next = l;
     86 	l->prev = l;
     87 }
     88 
     89 List *
     90 ListAddFirst(List *l, char *v)
     91 {
     92 	List *e;
     93 
     94 	e = (List *)XtMalloc(sizeof *e);
     95 	if(!e) return NULL;
     96 
     97 	e->thing = v;
     98 	e->prev = l;
     99 	e->next = e->prev->next;
    100 	e->prev->next = e;
    101 	e->next->prev = e;
    102 
    103 	return e;
    104 }
    105 
    106 List *
    107 ListAddLast(List *l, char *v)
    108 {
    109 	List *e;
    110 
    111 	e = (List *)XtMalloc(sizeof *e);
    112 	if(!e) return NULL;
    113 
    114 	e->thing = v;
    115 	e->next = l;
    116 	e->prev = e->next->prev;
    117 	e->prev->next = e;
    118 	e->next->prev = e;
    119 
    120 	return e;
    121 }
    122 
    123 void
    124 ListFreeOne(List *e)
    125 {
    126 	e->next->prev = e->prev;
    127 	e->prev->next = e->next;
    128 	XtFree((char *)e);
    129 }
    130 
    131 
    132 Status
    133 ListSearchAndFreeOne(List *l, char *thing)
    134 {
    135     	List *p;
    136 
    137 	for (p = ListFirst (l); p; p = ListNext (p))
    138 	    if (((char *) p->thing) == (char *) thing)
    139 	    {
    140 		ListFreeOne (p);
    141 		return (1);
    142 	    }
    143 
    144 	return (0);
    145 }
    146 
    147 
    148 int
    149 ListCount(List *l)
    150 {
    151 	int i;
    152 	List *e;
    153 
    154 	i = 0;
    155 	for(e = ListFirst(l); e; e = ListNext(e)) i++;
    156 
    157 	return i;
    158 }
    159