File: tixList.c

package info (click to toggle)
tix 8.4.3-10
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 9,080 kB
  • ctags: 7,129
  • sloc: ansic: 28,082; tcl: 22,774; python: 7,577; makefile: 331; cs: 253; sh: 210; perl: 128
file content (319 lines) | stat: -rw-r--r-- 7,149 bytes parent folder | download | duplicates (7)
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319

/*	$Id: tixList.c,v 1.1.1.1 2000/05/17 11:08:42 idiscovery Exp $	*/

/* 
 * tixList.c --
 *
 *	Implements easy-to-use link lists.
 *
 * Copyright (c) 1996, Expert Interface Technologies
 *
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 */
#include <tixPort.h>
#include <tixInt.h>

#define NEXT(info,ptr) (char*)(*(char**)((ptr+(info->nextOffset))))
#define PREV(info,ptr) (char*)(*(char**)((ptr+(info->prevOffset))))

static void 		SetNext _ANSI_ARGS_((Tix_ListInfo * info,
			    char * ptr, char * next));

static void SetNext(info, ptr, next)
    Tix_ListInfo * info;
    char * ptr;
    char * next;
{
    char ** next_ptr = (char**)((ptr+(info->nextOffset)));
    * next_ptr = next;
}

void Tix_LinkListInit(lPtr)
    Tix_LinkList * lPtr;
{
    lPtr->numItems = 0;
    lPtr->head   = (char*)NULL;
    lPtr->tail   = (char*)NULL;
}


void
Tix_LinkListAppend(infoPtr, lPtr, itemPtr, flags)
    Tix_ListInfo * infoPtr;
    Tix_LinkList * lPtr;
    char * itemPtr;
    int flags;
{
    char * ptr;

    if (flags | TIX_UNIQUE) {
	/* Check for uniqueness */
	for (ptr=lPtr->head;
	     ptr!=NULL;
	     ptr=NEXT(infoPtr,ptr)) {
	    if (ptr == itemPtr) {
		return;
	    }
	}
    }
    if (lPtr->head == NULL) {
	lPtr->head = lPtr->tail = itemPtr;
    } else {
	SetNext(infoPtr, lPtr->tail, itemPtr);
	lPtr->tail = itemPtr;
    }

    SetNext(infoPtr, itemPtr, NULL);
    ++ lPtr->numItems;
}

void Tix_LinkListIteratorInit(liPtr)
    Tix_ListIterator * liPtr;
{
    liPtr->started = 0;
}

void Tix_LinkListStart(infoPtr, lPtr, liPtr)
    Tix_ListInfo * infoPtr;
    Tix_LinkList * lPtr;
    Tix_ListIterator * liPtr;
{
    if (lPtr->head == NULL) {
	liPtr->last = NULL;
	liPtr->curr = NULL;
    } else {
	liPtr->last = liPtr->curr = lPtr->head;
    }
    liPtr->deleted = 0;
    liPtr->started = 1;
}

void Tix_LinkListNext(infoPtr, lPtr, liPtr)
    Tix_ListInfo * infoPtr;
    Tix_LinkList * lPtr;
    Tix_ListIterator * liPtr;
{
    if (liPtr->curr == NULL) {
	return;
    }
    if (liPtr->deleted == 1) {
	/* the curr pointer has already been adjusted */
	liPtr->deleted = 0;
	return;
    }

    liPtr->last = liPtr->curr;
    liPtr->curr = NEXT(infoPtr, liPtr->curr);
}

/*
 *----------------------------------------------------------------------
 * Tix_LinkListDelete --
 *
 *	Deletes an element from the linklist. The proper step of deleting
 *	an element is:
 *
 *	for (Tix_SimpleListStart(&list, &li); !Tix_SimpleListDone(&li);
 *	         Tix_SimpleListNext (&list, &li)) {
 *	    MyData * p = (MyData*)li.curr;
 *	    if (someCondition) {
 *		Tix_SimpleListDelete(&cPtr->subWDefs, &li);
 *		ckfree((char*)p);
 *          }
 *	}
 *
 *	i.e., The pointer can be freed only after Tix_SimpleListDelete().
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	The pointers in the list are adjusted  and the liPtr is advanced
 *	to the next element.
 *----------------------------------------------------------------------
 */
void
Tix_LinkListDelete(infoPtr, lPtr, liPtr)
    Tix_ListInfo * infoPtr;
    Tix_LinkList * lPtr;
    Tix_ListIterator * liPtr;
{
    if (liPtr->curr == NULL) {
	/* %% probably is a mistake */
	return;
    }
    if (liPtr->deleted == 1) {
	/* %% probably is a mistake */
	return;
    }
    if (lPtr->head == lPtr->tail) {
	lPtr->head  = lPtr->tail = NULL;
	liPtr->curr = NULL;
    }
    else if (lPtr->head == liPtr->curr) {
	lPtr->head  = NEXT(infoPtr, liPtr->curr);
	liPtr->curr = lPtr->head;
	liPtr->last = lPtr->head;
    }
    else if (lPtr->tail == liPtr->curr) {
	lPtr->tail = liPtr->last;
	SetNext(infoPtr, lPtr->tail, NULL);
	liPtr->curr = NULL;
    }
    else {
	SetNext(infoPtr, liPtr->last, NEXT(infoPtr, liPtr->curr));
	liPtr->curr = NEXT(infoPtr, liPtr->last);
    }
    -- lPtr->numItems;

    liPtr->deleted = 1;
}

/*----------------------------------------------------------------------
 *  Tix_LinkListInsert --
 *
 *	Insert the item at the position indicated by liPtr
 *----------------------------------------------------------------------
 */
void Tix_LinkListInsert(infoPtr, lPtr, itemPtr, liPtr)
    Tix_ListInfo * infoPtr;
    Tix_LinkList * lPtr;
    char * itemPtr;
    Tix_ListIterator * liPtr;
{
    if (lPtr->numItems == 0) {
	/* Just do an append
	 */
	Tix_LinkListAppend(infoPtr, lPtr, itemPtr, 0);

	/* Fix the iterator (%% I am not sure if this is necessary)
	 */
	liPtr->curr = liPtr->last = lPtr->head;
	return;
    }

    if (liPtr->curr == NULL) {
	/* %% probably is a mistake */
	return;
    }
    if (lPtr->head == lPtr->tail) {
	lPtr->head = itemPtr;
	SetNext(infoPtr, lPtr->head, lPtr->tail);
	liPtr->last = itemPtr;
	liPtr->curr = itemPtr;
    }
    else if (liPtr->curr == lPtr->head) {
	lPtr->head = itemPtr;
	SetNext(infoPtr, lPtr->head, liPtr->curr);
	liPtr->last = itemPtr;
	liPtr->curr = itemPtr;
    }
    else {
	SetNext(infoPtr, liPtr->last, itemPtr);
	SetNext(infoPtr, itemPtr,     liPtr->curr);
	liPtr->last = itemPtr;
    }
    ++ lPtr->numItems;
}

/*----------------------------------------------------------------------
 * Tix_LinkListFindAndDelete --
 *
 *	Find an element and delete it.
 *
 * liPtr:
 *	Can be NULL.
 *	If non-NULL, the search will start from the current entry indexed
 *	by liPtr;
 *
 * Return value:
 *	1 if element is found and deleted
 *	0 if element is not found
 *----------------------------------------------------------------------
 */
int Tix_LinkListFindAndDelete(infoPtr, lPtr, itemPtr, liPtr)
    Tix_ListInfo * infoPtr;
    Tix_LinkList * lPtr;
    char * itemPtr;
    Tix_ListIterator * liPtr;
{
    Tix_ListIterator defIterator;

    if (liPtr == NULL) {
	Tix_LinkListIteratorInit(&defIterator);
	liPtr = &defIterator;
    }

    if (!liPtr->started) {
	Tix_LinkListStart(infoPtr, lPtr, liPtr);
    }

    if (Tix_LinkListFind(infoPtr, lPtr, itemPtr, liPtr)) {
	Tix_LinkListDelete(infoPtr, lPtr, liPtr);
	return 1;
    } else {
	return 0;
    }
}

int Tix_LinkListDeleteRange(infoPtr, lPtr, fromPtr, toPtr, liPtr)
    Tix_ListInfo * infoPtr;
    Tix_LinkList * lPtr;
    char * fromPtr;
    char * toPtr;
    Tix_ListIterator * liPtr;
{
    Tix_ListIterator defIterator;
    int start = 0;
    int deleted = 0;

    if (liPtr == NULL) {
	Tix_LinkListIteratorInit(&defIterator);
	liPtr = &defIterator;
    }
    if (!liPtr->started) {
	Tix_LinkListStart(infoPtr, lPtr, liPtr);
    }

    for (;
	 !Tix_LinkListDone(liPtr);
	 Tix_LinkListNext (infoPtr, lPtr, liPtr)) {

	if (liPtr->curr == fromPtr) {
	    start = 1;
	}
	if (start) {
	    Tix_LinkListDelete(infoPtr, lPtr, liPtr);
	    ++ deleted;
	}
	if (liPtr->curr == toPtr) {
	    break;
	}
    }

    return deleted;
}

int Tix_LinkListFind(infoPtr, lPtr, itemPtr, liPtr)
    Tix_ListInfo * infoPtr;
    Tix_LinkList * lPtr;
    char * itemPtr;
    Tix_ListIterator * liPtr;
{
    if (!liPtr->started) {
	Tix_LinkListStart(infoPtr, lPtr, liPtr);
    }

    for (Tix_LinkListStart(infoPtr, lPtr, liPtr);
	 !Tix_LinkListDone(liPtr);
	 Tix_LinkListNext (infoPtr, lPtr, liPtr)) {

	if (liPtr->curr == itemPtr) {
	    return 1;				/* found! */
	}
    }

    return 0;					/* Can't find */
}