Subversion Repositories tendra.SVN

Rev

Rev 5 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5 Rev 6
Line -... Line 1...
-
 
1
/*
-
 
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
 */
1
/*
31
/*
2
    		 Crown Copyright (c) 1997
32
    		 Crown Copyright (c) 1997
3
    
33
 
4
    This TenDRA(r) Computer Program is subject to Copyright
34
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
35
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
36
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
37
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
38
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
39
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
40
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
41
    shall be deemed to be acceptance of the following conditions:-
12
    
42
 
13
        (1) Its Recipients shall ensure that this Notice is
43
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
44
        reproduced upon any copies or amended versions of it;
15
    
45
 
16
        (2) Any amended version of it shall be clearly marked to
46
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
47
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
48
        for the relevant amendment or amendments;
19
    
49
 
20
        (3) Its onward transfer from a recipient to another
50
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
51
        party shall be deemed to be that party's acceptance of
22
        these conditions;
52
        these conditions;
23
    
53
 
24
        (4) DERA gives no warranty or assurance as to its
54
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
55
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
56
        no liability whatsoever in relation to any use to which
27
        it may be put.
57
        it may be put.
28
*/
58
*/
Line 38... Line 68...
38
 
68
 
39
    All the program's memory allocation is through the routines defined in
69
    All the program's memory allocation is through the routines defined in
40
    this file.  This routine allocates sz bytes of memory.
70
    this file.  This routine allocates sz bytes of memory.
41
*/
71
*/
42
 
72
 
43
gen_ptr xmalloc
73
gen_ptr
44
    PROTO_N ( ( sz ) )
-
 
45
    PROTO_T ( long sz )
74
xmalloc(long sz)
46
{
75
{
47
    gen_ptr p = malloc ( ( size_t ) sz ) ;
76
    gen_ptr p = malloc((size_t)sz);
48
    if ( p == NULL ) error ( ERROR_FATAL, "Memory allocation error" ) ;
77
    if (p == NULL)error(ERROR_FATAL, "Memory allocation error");
49
    return ( p ) ;
78
    return(p);
50
}
79
}
51
 
80
 
52
 
81
 
53
/*
82
/*
54
    CONTROLLED VERSION OF CALLOC
83
    CONTROLLED VERSION OF CALLOC
55
 
84
 
56
    This routine allocates and initializes n objects of size sz bytes.
85
    This routine allocates and initializes n objects of size sz bytes.
57
*/
86
*/
58
 
87
 
59
gen_ptr xcalloc
88
gen_ptr
60
    PROTO_N ( ( n, sz ) )
-
 
61
    PROTO_T ( long n X long sz )
89
xcalloc(long n, long sz)
62
{
90
{
63
    gen_ptr p = calloc ( ( size_t ) sz, ( size_t ) n ) ;
91
    gen_ptr p = calloc((size_t)sz,(size_t)n);
64
    if ( p == NULL ) error ( ERROR_FATAL, "Memory allocation error" ) ;
92
    if (p == NULL)error(ERROR_FATAL, "Memory allocation error");
65
    return ( p ) ;
93
    return(p);
66
}
94
}
67
 
95
 
68
 
96
 
69
/*
97
/*
70
    CONTROLLED VERSION OF REALLOC
98
    CONTROLLED VERSION OF REALLOC
71
 
99
 
72
    This routine reallocates the block of memory p to contain sz bytes.
100
    This routine reallocates the block of memory p to contain sz bytes.
73
    p can be the result of a previous memory allocation routine, or NULL.
101
    p can be the result of a previous memory allocation routine, or NULL.
74
*/
102
*/
75
 
103
 
76
gen_ptr xrealloc
104
gen_ptr
77
    PROTO_N ( ( p, sz ) )
-
 
78
    PROTO_T ( gen_ptr p X long sz )
105
xrealloc(gen_ptr p, long sz)
79
{
106
{
80
    gen_ptr q ;
107
    gen_ptr q;
81
    if ( p ) {
108
    if (p) {
82
	q = realloc ( p, ( size_t ) sz ) ;
109
	q = realloc(p,(size_t)sz);
83
    } else {
110
    } else {
84
	q = malloc ( ( size_t ) sz ) ;
111
	q = malloc((size_t)sz);
85
    }
112
    }
86
    if ( q == NULL ) error ( ERROR_FATAL, "Memory allocation error" ) ;
113
    if (q == NULL)error(ERROR_FATAL, "Memory allocation error");
87
    return ( q ) ;
114
    return(q);
88
}
115
}
89
 
116
 
90
 
117
 
91
/*
118
/*
92
    CONTROLLED VERSION OF FREE
119
    CONTROLLED VERSION OF FREE
93
 
120
 
94
    This routine frees the block of memory p.  p can be the result of a
121
    This routine frees the block of memory p.  p can be the result of a
95
    previous memory allocation routine, or NULL.
122
    previous memory allocation routine, or NULL.
96
*/
123
*/
97
 
124
 
98
void xfree
125
void
99
    PROTO_N ( ( p ) )
-
 
100
    PROTO_T ( gen_ptr p )
126
xfree(gen_ptr p)
101
{
127
{
102
    if ( p ) free ( p ) ;
128
    if (p)free(p);
103
    return ;
129
    return;
104
}
130
}
105
 
131
 
106
 
132
 
107
/*
133
/*
108
    ALLOCATE SPACE FOR A STRING
134
    ALLOCATE SPACE FOR A STRING
109
 
135
 
110
    This routine allocates space for n characters.  The memory allocation
136
    This routine allocates space for n characters.  The memory allocation
111
    is buffered except for very long strings.
137
    is buffered except for very long strings.
112
*/
138
*/
113
 
139
 
114
char *xstr
140
char *
115
    PROTO_N ( ( n ) )
-
 
116
    PROTO_T ( long n )
141
xstr(long n)
117
{
142
{
118
    char *r ;
143
    char *r;
119
    if ( n >= 1000 ) {
144
    if (n >= 1000) {
120
	r = xmalloc_nof ( char, n ) ;
145
	r = xmalloc_nof(char, n);
121
    } else {
146
    } else {
122
	static long chars_left = 0 ;
147
	static long chars_left = 0;
123
	static char *chars_free = 0 ;
148
	static char *chars_free = 0;
124
	if ( n >= chars_left ) {
149
	if (n >= chars_left) {
125
	    chars_left = 5000 ;
150
	    chars_left = 5000;
126
	    chars_free = xmalloc_nof ( char, chars_left ) ;
151
	    chars_free = xmalloc_nof(char, chars_left);
127
	}
152
	}
128
	r = chars_free ;
153
	r = chars_free;
129
	chars_free += n ;
154
	chars_free += n;
130
	chars_left -= n ;
155
	chars_left -= n;
131
    }
156
    }
132
    return ( r ) ;
157
    return(r);
133
}
158
}
134
 
159
 
135
 
160
 
136
/*
161
/*
137
    COPY A STRING
162
    COPY A STRING
138
 
163
 
139
    This routine allocates space for a persistent copy of the string s.
164
    This routine allocates space for a persistent copy of the string s.
140
*/
165
*/
141
 
166
 
142
char *xstrcpy
167
char *
143
    PROTO_N ( ( s ) )
-
 
144
    PROTO_T ( CONST char *s )
168
xstrcpy(CONST char *s)
145
{
169
{
146
    long n ;
170
    long n;
147
    char *r ;
171
    char *r;
148
    if ( s == NULL ) return ( NULL ) ;
172
    if (s == NULL) return(NULL);
149
    n = ( long ) strlen ( s ) + 1 ;
173
    n = (long)strlen(s) + 1;
150
    r = xstr ( n ) ;
174
    r = xstr(n);
151
    strcpy_v ( r, s ) ;
175
    strcpy_v(r, s);
152
    return ( r ) ;
176
    return(r);
153
}
177
}
154
 
178
 
155
 
179
 
156
/*
180
/*
157
    CONCATENATE TWO STRINGS
181
    CONCATENATE TWO STRINGS
158
 
182
 
159
    This routine allocates space for a persistent copy of the string s
183
    This routine allocates space for a persistent copy of the string s
160
    followed by the string t.
184
    followed by the string t.
161
*/
185
*/
162
 
186
 
163
char *xstrcat
187
char *
164
    PROTO_N ( ( s, t ) )
-
 
165
    PROTO_T ( CONST char *s X CONST char *t )
188
xstrcat(CONST char *s, CONST char *t)
166
{
189
{
167
    char *r ;
190
    char *r;
168
    long n, m ;
191
    long n, m;
169
    if ( s == NULL ) return ( xstrcpy ( t ) ) ;
192
    if (s == NULL) return(xstrcpy(t));
170
    if ( t == NULL ) return ( xstrcpy ( s ) ) ;
193
    if (t == NULL) return(xstrcpy(s));
171
    n = ( long ) strlen ( s ) ;
194
    n = (long)strlen(s);
172
    m = n + ( long ) strlen ( t ) + 1 ;
195
    m = n + (long)strlen(t) + 1;
173
    r = xstr ( m ) ;
196
    r = xstr(m);
174
    strcpy_v ( r, s ) ;
197
    strcpy_v(r, s);
175
    strcpy_v ( r + n, t ) ;
198
    strcpy_v(r + n, t);
176
    return ( r ) ;
199
    return(r);
177
}
200
}