summaryrefslogtreecommitdiff
path: root/xc/doc/specs/PEX5/SI/build_ind.c
blob: e704343685b4d377498695aee53d38e408927277 (plain)
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/* $XConsortium: build_ind.c,v 5.2 94/04/17 20:05:30 rws Exp $ */

/***********************************************************

Copyright (c) 1990, 1991  X Consortium

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of the X Consortium shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from the X Consortium.

Copyright (c) 1990, 1991 by Sun Microsystems, Inc.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its 
documentation for any purpose and without fee is hereby granted, 
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in 
supporting documentation, and that the names of Sun Microsystems,
and the X Consortium, not be used in advertising or publicity 
pertaining to distribution of the software without specific, written 
prior permission.  

SUN MICROSYSTEMS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT 
SHALL SUN MICROSYSTEMS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL 
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

******************************************************************/

#include <ctype.h>
#include <stdio.h>
#include "indexer.h"

struct index_entry	*root;

/*  insert_page_entry places a new page number structure into the
    specified index node.     */

insert_page_entry(node, page)
	struct index_entry	*node;
	struct page_entry	*page;
{


	struct page_entry	*temp;

	page->next_page = NULL;

		/*  If there are no pages in the index node at this time we
		    just insert the new entry and return  */
	if (node->last_page == NULL) {
		node->last_page = page;
		node->page_entry = page;
		return;
	} 
		/*  This is a new page entry to be added to a list of existing
		    page entries.  We have to do some consistency checks  */
		/*  Eliminate duplicate page numbers */
	if (page->page_type == PAGE_NORMAL) {
		for (temp = node->page_entry; temp != NULL; temp = temp->next_page) {
			if (temp->page_type == PAGE_NORMAL ||
					temp->page_type == PAGE_MAJOR)
				if (strcmp(temp->page_number, page->page_number) == 0) {
					free (page);
					return;
				}
		}
	}
		/*  There should be only one major entry per reference  */
	if (page->page_type == PAGE_MAJOR) {
		for (temp = node->page_entry; temp != NULL; temp = temp->next_page) {
			if (temp->page_type == PAGE_MAJOR)
				fprintf(stderr,
		"%s: Page number on line %d -- duplicate major reference\n",
		command_name, line_number);
		}
		/*  Place major entries in front  */
		page->next_page = node->page_entry;
		node->page_entry = page;
		return;
	}
		/*  Start and end of range should follow one after the other  */
	if ((node->last_page->page_type == PAGE_START && page->page_type != PAGE_END) ||
	(node->last_page->page_type != PAGE_START && page->page_type == PAGE_END)) {
		fprintf(stderr, "%s: Page range on line %d must be contiguous\n",
				command_name, line_number);
		node->last_page->page_type = PAGE_NORMAL;
		page->page_type = PAGE_NORMAL;
		node->last_page->next_page = page;
		node->last_page = page;
		return;
	}
		/*  Just an ordinary page entry -- insert it  */
	node->last_page->next_page = page;
	node->last_page = page;
}


/*  insert_index_entry inserts a new index entry at its correct place in
    the tree of index entries and returns the address of the node
    corresponding to the entry inserted.  */

struct index_entry *
insert_index_entry(node, entry)
	struct	index_entry	*node;
	struct	index_entry	*entry;
{

	int	result;
	int	level;

	result = compare_entry(node, entry);

	if (result == 0) {	/*  exact match  */
				/*  Place the page entry for the duplicate */
				/*  into the list of pages for this node */
		insert_page_entry(node, entry->page_entry);
		free(entry);
		return(node);
	}

	if (result > 0)		/*  node greater than new entry -- */
		 		/*  move to lesser nodes */
		if (node->lesser != NULL)
			insert_index_entry(node->lesser, entry);
		else {
			node->lesser = entry;
			return (node->lesser);
		}
	else			/*  node less than new entry -- */
				/*  move to greater nodes */
		if (node->greater != NULL)
			insert_index_entry(node->greater, entry);
		else {
			node->greater = entry;
			return (node->greater);
		}
}


/*  compare_entry compares two index entries.  compare_entry returns
    0 for an exact match, < 0 for entry_1 < entry_2, and > 0 for
    entry_1 > entry_2.  Null entries are considered less than non-null
    entries. */

int
compare_entry(entry_1, entry_2)
	struct	index_entry	*entry_1;
	struct	index_entry	*entry_2;
{
	int	level;
	int	result;

	for (level = 0;  level < LEVELS;  level++) {
		result = compare_strings(entry_1->terms[level],
		entry_2->terms[level]);
		if (result != 0)
			return(result);
	}
	return (0);	/*  exact match  */
}


/*
 *  compare_terms compares the collating terms of two index entries.
 *  compare_terms returns:
 *  0 for an exact match,
 *  < 0 for entry_1 < entry_2,
 *  > 0 for entry_1 > entry_2.
 *
 *  Null entries are considered less than non-null entries.
*/

int
compare_terms(entry_1, entry_2)
	struct	index_entry	*entry_1;
	struct	index_entry	*entry_2;
{
	int	level;
	int	result;

	for (level = 0;  level < LEVELS;  level++) {
		result = compare_strings(entry_1->terms[level],
		entry_2->terms[level]);
		if (result != 0)
			return(result);
	}
	return (0);	/*  exact match  */
}


/*  compare_prints compares the printing terms of two index entries.
 *  compare_prints returns:
 *  0 for an exact match,
 *  1 for different
*/

/* int
compare_prints(entry_1, entry_2)
	struct	print_entry	*entry_1;
	struct	print_entry	*entry_2;
{
	int	level;
	int	result;

	for (level = 0;  level < LEVELS;  level++) {
		if (entry_1->print_field[level] == NULL &&
				entry_2->print_field[level] == NULL)
			continue;
		if (entry_1->print_field[level] == NULL &&
				entry_2->print_field[level] != NULL)
			return(1);
		if (entry_1->print_field[level] != NULL &&
				entry_2->print_field[level] == NULL)
			return(1);
		if (strcmp(entry_1->print_field[level],
				entry_2->print_field[level]) 1= 0)
			return(1);
	}
	return (0);
}	 */





/*  compare_strings compares the two strings passed as arguments, with the
    following extra constraints:
    .  lower-case letters and upper-case letters are considered the same.
    .  Punctuation characters come to the front of the collating sequence.
    .  A shorter string is considered extended with spaces.

    compare_strings returns:  0 if string_1 is equal to string_2,
			     -1 if string_1 is less than string_2,
    			     +1 if string_1 is greater than string_2  */

int
compare_strings(string_1, string_2)
	char	*string_1;
	char	*string_2;
{
	int	length_1;	/*  length of first string  */
	int	length_2;	/*  length of second string  */
	int	pos_1;	 	/*  character position in string_1 */
	int	pos_2;		/*  character position in string_2 */
	char	char_1;	 	/*  character from string_1 */
	char	char_2;		/*  character from string_2 */

				/*  first check for null strings  */
	if (string_1 == NULL && string_2 == NULL)
		return(0);
	if (string_1 == NULL && string_2 != NULL)
		return(-1);
	if (string_1 != NULL && string_2 == NULL)
		return(1);
				/*  Exact match just returns straight away  */
	if (strcmp(string_1, string_2) == 0)
		return(0);

	length_1 = strlen(string_1);
	length_2 = strlen(string_2);
				/*  check for zero-length strings  */
	if (length_1 == 0 && length_2 == 0)
		return(0);
	if (length_1 == 0 && length_2 != 0)
		return(-1);
	if (length_1 != 0 && length_2 == 0)
		return(1);

	pos_1 = pos_2 = 0;
	while (TRUE) {
		if (pos_1 >= length_1)
			char_1 = ' ';
		else
			char_1 = string_1[pos_1];
		char_1 = isupper(char_1) ? tolower(char_1) : char_1;

		if (pos_2 >= length_2)
			char_2 = ' ';
		else
			char_2 = string_2[pos_2];
		char_2 = isupper(char_2) ? tolower(char_2) : char_2;

			/*  First test alphanumerics  */
		if (isalnum(char_1) && isalnum(char_2)) {
			if (char_1 > char_2)
				return(1);
			if (char_1 < char_2)
				return(-1);
		}

			/*  Alphanumerics collate higher than punctuation  */
		if (isalnum(char_1) && ispunct(char_2))
			return(1);
		if (ispunct(char_1) && isalnum(char_2))
			return(-1);

			/*  Punctuation collates among itself  */
		if (ispunct(char_1) && ispunct(char_2)) {
			if (char_1 > char_2)
				return(1);
			if (char_1 < char_2)
				return(-1);
		}

			/*  Spaces collate lower than anything else  */
		if (char_1 > char_2)
			return(1);
		if (char_1 < char_2)
			return(-1);

		if (pos_1 >= length_1 && pos_2 >= length_2)
			break;
		pos_1++;
		pos_2++;
	}
	if (char_1 > char_2)
		return(1);
	if (char_1 < char_2)
		return(-1);
	return(0);	/*  Should not happen  */
}