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
/*
2
    		 Crown Copyright (c) 1997
3
 
4
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
12
 
13
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
15
 
16
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
19
 
20
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
22
        these conditions;
23
 
24
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
27
        it may be put.
28
*/
29
 
30
 
31
#include "config.h"
32
#include "list.h"
33
#include "utility.h"
34
 
35
 
36
/*
37
    SPARE LISTS
38
 
39
    This is a list of list structures which have been freed using
40
    free_list.  new_list trys to allocate new list structures from
41
    this list before using its internal array.
42
*/
43
 
44
static list *spare_lists = null ;
45
 
46
 
47
/*
48
    CREATE A NEW LIST
49
 
50
    This routine allocates a new list structure.
51
*/
52
 
53
static list *new_list
54
    PROTO_Z ()
55
{
56
    if ( spare_lists ) {
57
	list *p = spare_lists ;
58
	spare_lists = p->next ;
59
	return ( p ) ;
60
    } else {
61
	static int no_free = 0 ;
62
	static list *free_objs = null ;
63
	if ( no_free == 0 ) {
64
	    no_free = 1000 ;
65
	    free_objs = alloc_nof ( list, no_free ) ;
66
	}
67
	return ( free_objs + ( --no_free ) ) ;
68
    }
69
}
70
 
71
 
72
/*
73
    FREE A LIST
74
 
75
    This list returns p to free.
76
*/
77
 
78
void free_list
79
    PROTO_N ( ( p ) )
80
    PROTO_T ( list *p )
81
{
82
    spare_lists = add_list ( p, spare_lists ) ;
83
    return ;
84
}
85
 
86
 
87
/*
88
    JOIN TWO LISTS
89
 
90
    This routine joins two lists, p and q, and returns the result.
91
*/
92
 
93
list *add_list
94
    PROTO_N ( ( p, q ) )
95
    PROTO_T ( list *p X list *q )
96
{
97
    list *r ;
98
    if ( p == null ) return ( q ) ;
99
    if ( q == null ) return ( p ) ;
100
    for ( r = p ; r->next != null ; r = r->next ) /* empty */ ;
101
    r->next = q ;
102
    return ( p ) ;
103
}
104
 
105
 
106
/*
107
    ADD AN ITEM TO A LIST
108
 
109
    This routine adds a new item, s, to the end of the list p and returns
110
    the result.
111
*/
112
 
113
list *add_item
114
    PROTO_N ( ( p, s ) )
115
    PROTO_T ( list *p X char *s )
116
{
117
    list *q, *r ;
118
    q = new_list () ;
119
    q->item = s ;
120
    q->next = null ;
121
    if ( p == null ) return ( q ) ;
122
    for ( r = p ; r->next != null ; r = r->next ) /* empty */ ;
123
    r->next = q ;
124
    return ( p ) ;
125
}
126
 
127
 
128
/*
129
    INSERT AN ITEM INTO A LIST
130
 
131
    This routine adds a new item, s, to the start of the list p and
132
    returns the result.
133
*/
134
 
135
list *insert_item
136
    PROTO_N ( ( s, p ) )
137
    PROTO_T ( char *s X list *p )
138
{
139
    list *q = new_list () ;
140
    q->item = s ;
141
    q->next = p ;
142
    return ( q ) ;
143
}
144
 
145
 
146
/*
147
    CONVERT A STRING TO A LIST
148
 
149
    This routine converts a string to a list by breaking it at all white
150
    spaces (spaces and tabs).
151
*/
152
 
153
list *make_list
154
    PROTO_N ( ( s ) )
155
    PROTO_T ( char *s )
156
{
157
    list *r = null ;
158
    char *p = string_copy ( s ) ;
159
    while ( 1 ) {
160
	while ( *p == ' ' || *p == '\t' ) *( p++ ) = 0 ;
161
	if ( *p == 0 ) break ;
162
	r = add_item ( r, p ) ;
163
	while ( *p && *p != ' ' && *p != '\t' ) p++ ;
164
    }
165
    return ( r ) ;
166
}