Subversion Repositories tendra.SVN

Rev

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

Rev Author Line No. Line
2 7u83 1
/*
6 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
6 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:-
6 7u83 42
 
2 7u83 43
        (1) Its Recipients shall ensure that this Notice is
44
        reproduced upon any copies or amended versions of it;
6 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;
6 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;
6 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 "types.h"
63
#include "high.h"
64
#include "table.h"
65
#include "tdf.h"
66
#include "utility.h"
67
 
68
 
69
/*
70
    ARRAY OF HIGH LEVEL SORTS
71
 
72
    All high level sorts are held in the table high_sorts.
73
*/
74
 
6 7u83 75
high_sort *high_sorts = null;
76
int crt_high_sort = 0;
77
static int total_high_sort = 0;
2 7u83 78
 
79
 
80
/*
81
    ALLOCATE A NEW HIGH LEVEL SORT
82
 
83
    This routine allocates a new high level sort in the table high_sorts
84
    which is a copy of the local variable q.
85
*/
86
 
6 7u83 87
high_sort *
88
new_high_sort(high_sort *q)
2 7u83 89
{
6 7u83 90
    int c;
91
    high_sort *p;
92
    if (find_high_sort(q->name)!= SORT_unknown) {
93
	is_fatal = 0;
94
	input_error("Sort %s already defined", q->name);
2 7u83 95
    }
6 7u83 96
    c = crt_high_sort++;
97
    if (c >= total_high_sort) {
98
	total_high_sort += 100;
99
	high_sorts = realloc_nof(high_sorts, high_sort, total_high_sort);
2 7u83 100
    }
6 7u83 101
    p = high_sorts + c;
102
    p->name = q->name;
103
    p->id = c + high_start;
104
    p->res = q->res;
105
    p->no_args = q->no_args;
106
    p->args = q->args;
107
    return(p);
2 7u83 108
}
109
 
110
 
111
/*
112
    DEFINE A HIGH-LEVEL SORT FROM TOKEN INFORMATION
113
 
114
    This routine allocates a new high level sort in the table high_sorts
115
    which has name nm and sort given by the token information tok_info.
116
*/
117
 
6 7u83 118
void
119
set_high_sort(char *nm, tok_info *info)
2 7u83 120
{
6 7u83 121
    high_sort h;
122
    char *q = info->args;
123
    h.name = nm;
124
    h.res = info->res;
125
    if (q == null) {
126
	h.no_args = 0;
127
	h.args = null;
2 7u83 128
    } else {
6 7u83 129
	int i = 0;
130
	h.args = alloc_nof(sortname, strlen(q));
131
	while (*q) {
132
	    sortname s;
133
	    q = find_sortname(q, &s);
134
	    q++;
135
	    h.args[i++] = s;
2 7u83 136
	}
6 7u83 137
	h.no_args = i;
2 7u83 138
    }
6 7u83 139
    IGNORE new_high_sort(&h);
140
    return;
2 7u83 141
}
142
 
143
 
144
/*
145
    ENSURE THAT HIGH LEVEL SORTS ARE UNIQUELY NUMBERED
146
 
147
    This routine checks that two high level sorts with the same result
148
    and argument sorts are assigned the same sort number.  Given a
149
    high level sort h, it returns any equivalent sort.
150
*/
151
 
6 7u83 152
high_sort *
153
unique_high_sort(high_sort *h)
2 7u83 154
{
6 7u83 155
    int i, j;
156
    for (i = 0; i < crt_high_sort; i++) {
157
	high_sort *p = high_sorts + i;
158
	if (p->res == h->res && p->no_args == h->no_args) {
159
	    boolean ok = 1;
160
	    if (p == h) return(h);
161
	    for (j = 0; j < p->no_args && ok; j++) {
162
		if (p->args[j]!= h->args[j])ok = 0;
2 7u83 163
	    }
6 7u83 164
	    if (ok) {
165
		h->id = SORT_unknown;
166
		return(p);
2 7u83 167
	    }
168
	}
169
    }
6 7u83 170
    return(h);
2 7u83 171
}
172
 
173
 
174
/*
175
    FIND A HIGH-LEVEL SORT FROM ITS NAME
176
 
177
    This routine searches for a sort named nm, firstly in the built-in
178
    sorts, then in the high level sort table.  The corresponding sort
179
    number is returned.
180
*/
181
 
6 7u83 182
sortname
183
find_high_sort(char *nm)
2 7u83 184
{
6 7u83 185
    int i;
186
    construct *q = search_cons_hash(nm, SORT_sortname);
187
    if (q) {
188
	if (get_char_info(q)) {
189
	    is_fatal = 0;
190
	    input_error("Illegal sort name, %s", nm);
2 7u83 191
	}
6 7u83 192
	return((sortname)q->encoding);
2 7u83 193
    }
6 7u83 194
    for (i = 0; i < crt_high_sort; i++) {
195
	high_sort *p = high_sorts + i;
196
	if (streq(nm, p->name)) return(p->id);
2 7u83 197
    }
6 7u83 198
    return(SORT_unknown);
2 7u83 199
}
200
 
201
 
202
/*
203
    FORM A DECODE STRING FOR A HIGH-LEVEL SORT
204
 
205
    This routine forms the decode string corresponding to the arguments
206
    of the high level sort p.
207
*/
208
 
6 7u83 209
char *
210
find_decode_string(high_sort *p)
2 7u83 211
{
6 7u83 212
    int i, n = p->no_args;
213
    char abuff[100], *a = abuff;
214
    if (n == 0) return(null);
215
    for (i = 0; i < n; i++) {
216
	sortname s = p->args[i];
217
	if (is_high(s)) {
218
	    sprint_high_sort(a, s);
219
	    while (*a)a++;
2 7u83 220
	} else {
6 7u83 221
	    *(a++) = sort_letters[s];
2 7u83 222
	}
223
    }
6 7u83 224
    *a = 0;
225
    return(string_copy_aux(abuff));
2 7u83 226
}
227
 
228
 
229
/*
230
    FIND THE SORTNAME CORRESPONDING TO A DECODE STRING
231
 
232
    This routine finds the sort corresponding to the decode string
233
    pointed to by p.  This is returned via the pointer q, the procedure
234
    returning a pointer to the character at the end of the sort encoding.
235
*/
236
 
6 7u83 237
char *
238
find_sortname(char *p, sortname *q)
2 7u83 239
{
6 7u83 240
    int n = 0;
241
    sortname s;
242
    if (*p == 'T') {
243
	while (*(++p)!= '#')n = 10 * n + (*p - '0');
244
	s = (sortname)(high_start + n);
2 7u83 245
    } else {
6 7u83 246
	while (*p != sort_letters[n])n++;
247
	s = (sortname)n;
2 7u83 248
    }
6 7u83 249
    if (q)*q = s;
250
    return(p);
2 7u83 251
}