Subversion Repositories tendra.SVN

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
7 7u83 2
 * Copyright (c) 2002-2005 The TenDRA Project <http://www.tendra.org/>.
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 *    this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
11
 *    this list of conditions and the following disclaimer in the documentation
12
 *    and/or other materials provided with the distribution.
13
 * 3. Neither the name of The TenDRA Project nor the names of its contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific, prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
18
 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
 * EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 *
29
 * $Id$
30
 */
31
/*
2 7u83 32
    		 Crown Copyright (c) 1997
7 7u83 33
 
2 7u83 34
    This TenDRA(r) Computer Program is subject to Copyright
35
    owned by the United Kingdom Secretary of State for Defence
36
    acting through the Defence Evaluation and Research Agency
37
    (DERA).  It is made available to Recipients with a
38
    royalty-free licence for its use, reproduction, transfer
39
    to other parties and amendment for any purpose not excluding
40
    product development provided that any such use et cetera
41
    shall be deemed to be acceptance of the following conditions:-
7 7u83 42
 
2 7u83 43
        (1) Its Recipients shall ensure that this Notice is
44
        reproduced upon any copies or amended versions of it;
7 7u83 45
 
2 7u83 46
        (2) Any amended version of it shall be clearly marked to
47
        show both the nature of and the organisation responsible
48
        for the relevant amendment or amendments;
7 7u83 49
 
2 7u83 50
        (3) Its onward transfer from a recipient to another
51
        party shall be deemed to be that party's acceptance of
52
        these conditions;
7 7u83 53
 
2 7u83 54
        (4) DERA gives no warranty or assurance as to its
55
        quality or suitability for any purpose and DERA accepts
56
        no liability whatsoever in relation to any use to which
57
        it may be put.
58
*/
59
 
60
 
61
#include "config.h"
62
#include "list.h"
63
#include "utility.h"
64
 
65
 
66
/*
7 7u83 67
 * SPARE LISTS
68
 *
69
 * This is a list of list structures which have been freed using free_list.
70
 * new_list tries to allocate new list structures from this list before using
71
 * its internal array.
72
 */
2 7u83 73
 
7 7u83 74
static list *spare_lists = null;
2 7u83 75
 
76
 
77
/*
7 7u83 78
 * CREATE A NEW LIST
79
 *
80
 * This routine allocates a new list structure.
81
 */
2 7u83 82
 
7 7u83 83
static list *
84
new_list(void)
2 7u83 85
{
7 7u83 86
	if (spare_lists) {
87
		list *p = spare_lists;
88
		spare_lists = p->next;
89
		return (p);
90
	} else {
91
		static int no_free = 0;
92
		static list *free_objs = null;
93
		if (no_free == 0) {
94
			no_free = 1000;
95
			free_objs = alloc_nof(list, no_free);
96
		}
97
		return (free_objs + (--no_free));
2 7u83 98
	}
99
}
100
 
101
 
102
/*
7 7u83 103
 * FREE A LIST
104
 *
105
 * This list returns p to free.
106
 */
2 7u83 107
 
7 7u83 108
void
109
free_list(list *p)
110
{
111
	spare_lists = add_list(p, spare_lists);
112
	return;
113
}
2 7u83 114
 
7 7u83 115
 
116
/*
117
 * JOIN TWO LISTS
118
 *
119
 * This routine joins two lists, p and q, and returns the result.
120
 */
121
 
122
list *
123
add_list(list *p, list *q)
2 7u83 124
{
7 7u83 125
	list *r;
126
	if (p == null) {
127
		return (q);
128
	}
129
	if (q == null) {
130
		return (p);
131
	}
132
	for (r = p ; r->next != null ; r = r->next) {
133
		;	/* empty */
134
	}
135
	r->next = q;
136
	return (p);
2 7u83 137
}
138
 
139
 
140
/*
7 7u83 141
 * ADD AN ITEM TO A LIST
142
 *
143
 * This routine adds a new item, s, to the end of the list p and returns the
144
 * result.
145
 */
2 7u83 146
 
7 7u83 147
list *
148
add_item(list *p, char *s)
2 7u83 149
{
7 7u83 150
	list *q, *r;
151
	q = new_list();
152
	q->item = s;
153
	q->next = null;
154
	if (p == null) {
155
		return (q);
156
	}
157
	for ( r = p ; r->next != null ; r = r->next ) {
158
		;	/* empty */
159
	}
160
	r->next = q;
161
	return (p);
2 7u83 162
}
163
 
164
 
165
/*
7 7u83 166
 * INSERT AN ITEM INTO A LIST
167
 *
168
 * This routine adds a new item, s, to the start of the list p and returns the
169
 * result.
170
 */
2 7u83 171
 
7 7u83 172
list *
173
insert_item(char *s, list *p)
2 7u83 174
{
7 7u83 175
	list *q = new_list();
176
	q->item = s;
177
	q->next = p;
178
	return (q);
2 7u83 179
}
180
 
181
 
182
/*
7 7u83 183
 * Insert a command item in ascending order, based on their rank. Items with a
184
 * lower rank value are executed first.
185
 */
2 7u83 186
 
7 7u83 187
list*
188
insert_inorder(ordered_node* indata, list *inlst)
189
{
190
	list *head = inlst;
191
	list *curr = inlst;
192
	list *newlst  = new_list();
193
	list *prev = newlst;
194
	list *tmp = inlst;
2 7u83 195
 
7 7u83 196
	newlst->item = indata;
197
	newlst->next = NULL;
198
 
199
	if (inlst == NULL){
200
	        return newlst;
201
	}
202
 
203
	if (indata->rank < ((ordered_node*)curr->item)->rank){
204
	        newlst->next = inlst;
205
	        return newlst;
206
	}
207
 
208
	while (curr != NULL &&
209
	           ((ordered_node*)curr->item)->rank <= indata->rank) {
210
	        prev = curr;
211
	        curr = curr->next;
212
	}
213
	prev->next = newlst;
214
	newlst->next = curr;
215
	return head;
2 7u83 216
}
217
 
218
 
219
/*
7 7u83 220
 * CONVERT A STRING TO A LIST
221
 *
222
 * This routine converts a string to a list by breaking it at all white spaces
223
 * (spaces and tabs).
224
 */
2 7u83 225
 
7 7u83 226
list *
227
make_list(char *s)
2 7u83 228
{
7 7u83 229
	list *r = null;
230
	char *p = string_copy(s);
231
	while (1) {
232
		while (*p == ' ' || *p == '\t') {
233
			*(p++) = 0;
234
		}
235
		if (*p == 0) {
236
			break;
237
		}
238
		r = add_item(r, p);
239
		while (*p && *p != ' ' && *p != '\t') {
240
			p++;
241
		}
242
	}
243
	return (r);
2 7u83 244
}