File: list.c

package info (click to toggle)
magic 7.5.220-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 17,860 kB
file content (182 lines) | stat: -rw-r--r-- 4,755 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* list.c -
 *
 *	Routines to create and manipulate lisp style lists.
 *	A separate structure is used to link list elements together.
 *	Thus list elements do not require pointers and can be anything.
 *
 *     ********************************************************************* 
 *     * Copyright (C) 1985, 1990 Regents of the University of California. * 
 *     * Permission to use, copy, modify, and distribute this              * 
 *     * software and its documentation for any purpose and without        * 
 *     * fee is hereby granted, provided that the above copyright          * 
 *     * notice appear in all copies.  The University of California        * 
 *     * makes no representations about the suitability of this            * 
 *     * software for any purpose.  It is provided "as is" without         * 
 *     * express or implied warranty.  Export of this software outside     * 
 *     * of the United States of America may require an export license.    * 
 *     *********************************************************************
 */

#ifndef lint
static char rcsid[] __attribute__ ((unused)) = "$Header: /usr/cvsroot/magic-7.5/utils/list.c,v 1.1.1.1 2006/04/10 22:03:14 tim Exp $";
#endif  /* not lint */

#include <stdio.h>

#include "utils/magic.h"
#include "utils/utils.h"
#include "utils/malloc.h"
#include "utils/list.h"


/* ----------------------------------------------------------------------------
 *	ListPop --
 *	Returns top data item on list, deallocates top list element, and
 *	points list pointer to tail of list.
 *
 * Results:
 * 	First data item on list.
 *
 * Side effects:
 *	Deallocates head of list, and changes pointer to tail of list.
 * ----------------------------------------------------------------------------
 */

ClientData
ListPop(listPP)
    List ** listPP;		/* Pointer to pointer to list to pop */
{
    List *head = *listPP;
    ClientData result = LIST_FIRST(head);

    (*listPP) = LIST_TAIL(head);
    freeMagic((char *) head);

    return result;
}


/* ----------------------------------------------------------------------------
 *	ListContainsP --
 * 	Check if list contains given element.
 *
 * Results:
 * 	TRUE if element in list.
 *
 * Side effects:
 *	None.
 * ----------------------------------------------------------------------------
 */

bool
ListContainsP(element,list)
    ClientData element;	/* element to check for */
    List *list;		/* List to search */
{
    for(;list!=NULL && LIST_FIRST(list)!=element; list=LIST_TAIL(list))
	;	/* Null body */

    return list!=NULL;
}

/* ----------------------------------------------------------------------------
 *	ListDealloc --
 * 	Deallocate list strucs in list.
 *
 * Results:
 * 	None.
 *
 * Side effects:
 *	Storage reclaimed.
 *
 * NOTE:  Remember to set pointers to lists to null after deallocating 
 *        the list (if pointers are to be used again)!
 * ----------------------------------------------------------------------------
 */

void
ListDealloc(list)
    List *list;		/* List to free */
{
    for(;list!=NULL; list=LIST_TAIL(list))
	freeMagic((char *) list);

    return;
}


/* ----------------------------------------------------------------------------
 *	ListDeallocC --
 * 	Deallocate list strucs in list AND CONTENTS of list.
 *
 * Results:
 * 	None.
 *
 * Side effects:
 *	Storage reclaimed.
 *
 * NOTE:  Remember to set pointers to lists to null after deallocating 
 *        the list (if pointers are to be used again)!
 * ----------------------------------------------------------------------------
 */

void
ListDeallocC(list)
    List *list;		/* List to free */
{
    for(;list!=NULL; list=LIST_TAIL(list))
    {
        freeMagic((char *) LIST_FIRST(list));
	freeMagic((char *) list);
    }

    return;
}

/* ----------------------------------------------------------------------------
 *	ListLength --
 * 	Count number of elements in list.
 *
 * Results:
 * 	Returns number of elements in list
 *
 * Side effects:
 *	None.
 * ----------------------------------------------------------------------------
 */

int
ListLength(list)
    List *list;		/* List to search */
{
    int count = 0;

    for(;list!=NULL; list=LIST_TAIL(list))
	count++;

    return count;
}

/* ----------------------------------------------------------------------------
 *	ListReverse --
 * 	Make reversed copy of list.
 *
 * Results:
 * 	Pointer to reverse copy of list.
 *
 * Side effects:
 *	Allocates and builds new list.
 * ----------------------------------------------------------------------------
 */

List *
ListReverse(list)
    List *list;		/* List to search */
{
    List *revList = NULL;

    for(; list!=NULL; list=LIST_TAIL(list))
	LIST_ADD(LIST_FIRST(list),revList);

    return revList;
}