File: stack.c

package info (click to toggle)
magic 7.5.220-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 17,860 kB
file content (289 lines) | stat: -rw-r--r-- 7,009 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * stack.c --
 *
 * General purpose stack manipulation routines.
 *
 *     ********************************************************************* 
 *     * 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/stack.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/stack.h"
#include "utils/malloc.h"

bool stackCopyStr;

/*
 * ----------------------------------------------------------------------------
 * StackNew --
 *
 * Allocate and initialize a new Stack.
 *
 * Results:
 *	Returns a pointer to a newly heap-allocated and initialized
 *	stack, with its growth increment set to the specified
 *	size (in entries).
 *
 * Side effects:
 *	None.
 * ----------------------------------------------------------------------------
 */

Stack *
StackNew(sincr)
    int sincr;		/* Number of entries by which to grow storage area */
{
    Stack *stack;

    stack = (Stack *) mallocMagic((unsigned) (sizeof (Stack)));
    stack->st_incr = sincr;
    stack->st_body = (struct stackBody *) mallocMagic((unsigned) (stackSize(sincr)));
    stack->st_ptr = stack->st_body->sb_data;
    stack->st_body->sb_next = NULL;

    return (stack);
}

/*
 * ----------------------------------------------------------------------------
 * StackFree --
 *
 * Deallocate a Stack.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Deallocates all memory currently assigned to the Stack.
 *
 * ----------------------------------------------------------------------------
 */

void
StackFree(stack)
    Stack *stack;
{
    struct stackBody *stackp, *stacknext;

    for (stackp = stack->st_body; stackp != NULL; stackp = stacknext)
    {
	stacknext = stackp->sb_next;
	freeMagic((char *) stackp);
    }

    freeMagic((char *) stack);
}

/*
 * ----------------------------------------------------------------------------
 * StackPush --
 *
 * Push a new element on to a stack.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	The argument stack is updated to reflect the new
 *	item placed upon it.
 *
 * ----------------------------------------------------------------------------
 */
void
StackPush(arg, stack)
    ClientData arg;
    Stack *stack;
{
    struct stackBody *bodyNew;

    if (stack->st_ptr >= &stack->st_body->sb_data[stack->st_incr])
    {
	bodyNew = (struct stackBody *) mallocMagic((unsigned) (stackSize(stack->st_incr)));
	bodyNew->sb_next = stack->st_body;
	stack->st_ptr = bodyNew->sb_data;
	stack->st_body = bodyNew;
    }

    *(stack->st_ptr++) = arg;
}

/*
 * ----------------------------------------------------------------------------
 * StackPop --
 *
 * Pop the top element from a Stack and return it.
 *
 * Results:
 *	Top element from stack.
 *	If the stack is already empty, returns NULL.
 *	Callers should probably avoid popping from an empty
 *	stack.
 *
 * Side effects:
 *	Updates the stack to reflect the result of popping its top.
 *
 * ----------------------------------------------------------------------------
 */

ClientData
StackPop(stack)
    Stack *stack;
{
    struct stackBody *bodyOld;

    if (stackBodyEmpty(stack))
    {
	bodyOld = stack->st_body;
	if (bodyOld->sb_next == NULL)
	    return ((ClientData) NULL);
	stack->st_body = bodyOld->sb_next;
	stack->st_ptr = &stack->st_body->sb_data[stack->st_incr];
	freeMagic((char *) bodyOld);
    }

    return (*--(stack->st_ptr));
}

/*
 * ----------------------------------------------------------------------------
 * StackLook --
 *
 * Return the top element from a Stack, but don't pop it off.
 *
 * Results:
 *	Top element from stack.
 *	If the stack is already empty, returns NULL.
 *
 * Side effects:
 *	None.
 *
 * ----------------------------------------------------------------------------
 */

ClientData
StackLook(stack)
    Stack *stack;
{
    struct stackBody *bodyNext;

    if (stackBodyEmpty(stack))
    {
	bodyNext = stack->st_body->sb_next;
	if (bodyNext == NULL)
	    return ((ClientData) NULL);
	return (bodyNext->sb_data[stack->st_incr - 1]);
    }

    return ( *(stack->st_ptr - 1) );
}

/*
 * ----------------------------------------------------------------------------
 *
 * StackEnum --
 *
 * 	Enumerate all elements on the stack.  Call the supplied function
 *	for each occurrence.
 *
 *	The supplied function is of the form:
 *		int func(stackItem, i, clientData)
 *		    ClientData stackItem;    Item put on the stack
 *		    int i;		     Index of the item on stack
 *		    ClientData cd;	     Points to whatever you want
 *	The function normally returns 0.  The enumeration terminates if it
 *	returns anything else.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	None.
 *
 * ----------------------------------------------------------------------------
 */
void
StackEnum(stack, func, cd)
    Stack    * stack;
    int     (* func)();
    ClientData cd;
{
    int i, j;
    struct stackBody * sb;

    i=1;
    for(sb=stack->st_body; sb!=(struct stackBody *) NULL; sb=sb->sb_next)
    {
	for(j=0; j<=stack->st_incr; j++)
	{
	    if( &(sb->sb_data[j]) == stack->st_ptr ) return;
	    if((*func)(sb->sb_data[j], i, cd)) return;
	    i++;
	}
    }
}

/*
 * ----------------------------------------------------------------------------
 *
 * StackCopy --
 *
 * 	Make a copy of a stack.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Memory may get allocated if the copystr parameter says to copy
 *	strings rather than pointers to them.
 *	dest gets a copy of src.  If dest is non-null, it gets freed.
 *
 * ----------------------------------------------------------------------------
 */
void
StackCopy(src, dest, copystr)
    Stack * src, ** dest;
    bool copystr;
{
    int stackCopyFn();

    stackCopyStr=copystr;
    if(*dest!=(Stack *) NULL)
	StackFree(*dest);
    if(src==(Stack *) NULL)
	*dest = src;
    else
    {
	*dest = StackNew(src->st_incr);
	StackEnum(src, stackCopyFn, (ClientData) * dest);
    }
}

/*ARGSUSED*/
int
stackCopyFn(stackItem, i, cd)
    ClientData stackItem;
    int i;
    ClientData cd;
{
    if(stackCopyStr)
	StackPush((ClientData) StrDup((char **) NULL, stackItem), (Stack *) cd);
    else
	StackPush(stackItem, (Stack *) cd);
    return(0);
}