aboutsummaryrefslogtreecommitdiff
path: root/textproc/wordnet/files/patch-binsrch
blob: f63e294a6cd8751e957cb23d3c41acf2f8112ce6 (plain) (blame)
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
Clean out the unused functions. Improve and const-ify, what remains.
--- lib/binsrch.c	Fri May  6 13:17:38 2005
+++ lib/binsrch.c	Sun Jan 15 11:43:36 2006
@@ -5,8 +5,9 @@
 */
 
+#include "wn.h"
 #include <stdio.h>
 #include <string.h>
 
-static char *Id = "$Id: binsrch.c,v 1.15 2005/02/01 16:46:43 wn Rel $";
+__FBSDID("$Id: binsrch.c,v 1.15 2005/02/01 16:46:43 wn Rel $");
 
 /* Binary search - looks for the key passed at the start of a line
@@ -14,5 +15,4 @@
    a buffer containing the line in the file. */
 
-#define KEY_LEN		(1024)
 #define LINE_LEN	(1024*25)
 
@@ -25,201 +25,61 @@
 #undef getc
 
-char *read_index(long offset, FILE *fp) {
-    char *linep;
+const char *
+read_index(long offset, FILE *fp) {
 
-    linep = line;
     line[0] = '0';
 
-    fseek( fp, offset, SEEK_SET );
-    fgets(linep, LINE_LEN, fp);
+    fseek(fp, offset, SEEK_SET);
+    fgets(line, LINE_LEN, fp);
     return(line);
 }
 
-char *bin_search(char *searchkey, FILE *fp)
+static int
+sign(int number)
 {
-    int c;
-    long top, mid, bot, diff;
-    char *linep, key[KEY_LEN];
-    int length;
-
-    diff=666;
-    linep = line;
-    line[0] = '\0';
-
-    fseek(fp, 0L, 2);
-    top = 0;
-    bot = ftell(fp);
-    mid = (bot - top) / 2;
-
-    do {
-	fseek(fp, mid - 1, 0);
-	if(mid != 1)
-	    while((c = getc(fp)) != '\n' && c != EOF);
-        last_bin_search_offset = ftell( fp );
-	fgets(linep, LINE_LEN, fp);
-	length = (int)(strchr(linep, ' ') - linep);
-	strncpy(key, linep, length);
-	key[length] = '\0';
-	if(strcmp(key, searchkey) < 0) {
-	    top = mid;
-	    diff = (bot - top) / 2;
-	    mid = top + diff;
-	}
-	if(strcmp(key, searchkey) > 0) {
-	    bot = mid;
-	    diff = (bot - top) / 2;
-	    mid = top + diff;
-	}
-    } while((strcmp(key, searchkey)) && (diff != 0));
-    
-    if(!strcmp(key, searchkey))
-	return(line);
-    else
-	return(NULL);
+	if (number > 0)
+		return 1;
+	if (number < 0)
+		return -1;
+	return 0;
 }
-
-static long offset;
 
-static int bin_search_key(char *searchkey, FILE *fp)
+const char *
+bin_search(const char *searchkey, FILE *fp)
 {
-    int c;
-    long top, mid, bot, diff;
-    char *linep, key[KEY_LEN];
-    int length, offset1, offset2;
-
-    /* do binary search to find correct place in file to insert line */
-
-    diff=666;
-    linep = line;
-    line[0] = '\0';
-
-    fseek(fp, 0L, 2);
-    top = 0;
-    bot = ftell(fp);
-    if (bot == 0) {
-	offset = 0;
-	return(0);		/* empty file */
-    }
-    mid = (bot - top) / 2;
-
-    /* If only one line in file, don't work through loop */
-
-    length = 0;
-    rewind(fp);
-    while((c = getc(fp)) != '\n' && c != EOF)
-	line[length++] =  c;
-    if (getc(fp) == EOF) {	/* only 1 line in file */
-	length = (int)(strchr(linep, ' ') - linep);
-	strncpy(key, linep, length);
-	key[length] = '\0';
-	if(strcmp(key, searchkey) > 0) {
-	    offset = 0;
-	    return(0);		/* line with key is not found */
-	} else if (strcmp(key, searchkey) < 0) {
-	    offset = ftell(fp);
-	    return(0);		/* line with key is not found */
-	} else {
-	    offset = 0;
-	    return(1);		/* line with key is found */
+	int	c;
+	long	top, mid, bot; /* should be off_t */
+	int	length, keylen;
+
+	fseek(fp, 0L, 2);
+	bot = ftell(fp);
+	mid = bot / 2;
+	keylen = strlen(searchkey);
+
+	for (top = 0; bot - top >= 2; mid = (top + bot) / 2) {
+		fseek(fp, mid - 1, 0);
+		if(mid != 1)
+			while((c = getc(fp)) != '\n' && c != EOF);
+		last_bin_search_offset = ftell(fp);
+		if (fgets(line, LINE_LEN, fp) == NULL)
+			return(NULL);
+		length = strchr(line, ' ') - line;
+		switch (sign(strncmp(line, searchkey, length))) {
+		case 0:
+			/* a match up to the length! */
+			if (length == keylen)
+				return(line);
+			if (length > keylen)
+				/* the word read is longer than ours */
+				goto up;
+			/* FALLTHROUGH */
+		case -1:
+			top = mid;
+			continue;
+		case 1:
+		up:
+			bot = mid;
+		}
 	}
-    }
-
-    do {
-	fseek(fp, mid - 1, 0);
-	if(mid != 1)
-	    while((c = getc(fp)) != '\n' && c != EOF);
-	offset1 = ftell(fp);	/* offset at start of line */
-	if (fgets(linep, LINE_LEN, fp) != NULL) {
-  	    offset2 = ftell(fp); /* offset at start of next line */
-	    length = (int)(strchr(linep, ' ') - linep);
-	    strncpy(key, linep, length);
-	    key[length] = '\0';
-	    if(strcmp(key, searchkey) < 0) {	/* further in file */
-		top = mid;
-		diff = (bot - top) / 2;
-		mid = top + diff;
-		offset = offset2;
-	    }
-	    if(strcmp(key, searchkey) > 0) {	/* earlier in file */
-		bot = mid;
-		diff = (bot - top) / 2;
-		mid = top + diff;
-		offset = offset1;
-	    }
-	} else {
-	    bot = mid;
-	    diff = (bot - top) / 2;
-	    mid = top + diff;
-	}
-    } while((strcmp(key, searchkey)) && (diff != 0));
-
-    if(!strcmp(key, searchkey)) {
-	offset = offset1;	/* get to start of current line */
-	return(1);		/* line with key is found */
-    } else
-	return(0);		/* line with key is not found */
-}
-
-/* Copy contents from one file to another. */
-
-void copyfile(FILE *fromfp, FILE *tofp)
-{
-    int c;
-
-    while ((c = getc(fromfp)) != EOF)
-	putc(c, tofp);
-}
-
-/* Function to replace a line in a file.  Returns the original line,
-   or NULL in case of error. */
-
-char *replace_line(char *new_line, char *searchkey, FILE *fp)
-{
-    FILE *tfp;			/* temporary file pointer */
-
-    if (!bin_search_key(searchkey, fp))
-	return(NULL);		/* line with key not found */
-
-    if ((tfp = tmpfile()) == NULL)
-	return(NULL);		/* could not create temp file */
-    fseek(fp, offset, 0);
-    fgets(line, LINE_LEN, fp);	/* read original */
-    copyfile(fp, tfp);
-    if (fseek(fp, offset, 0) == -1)
-	return(NULL);		/* could not seek to offset */
-    fprintf(fp, new_line);	/* write line */
-    rewind(tfp);
-    copyfile(tfp, fp);
-
-    fclose(tfp);
-    fflush(fp);
-
-    return(line);
-}
-
-/* Find location to insert line at in file.  If line with this
-   key is already in file, return NULL. */
-
-char *insert_line(char *new_line, char *searchkey, FILE *fp)
-{
-    FILE *tfp;
-
-    if (bin_search_key(searchkey, fp))
 	return(NULL);
-    
-    if ((tfp = tmpfile()) == NULL)
-	return(NULL);		/* could not create temp file */
-    if (fseek(fp, offset, 0) == -1)
-	return(NULL);		/* could not seek to offset */
-    copyfile(fp, tfp);
-    if (fseek(fp, offset, 0) == -1)
-	return(NULL);		/* could not seek to offset */
-    fprintf(fp, new_line);	/* write line */
-    rewind(tfp);
-    copyfile(tfp, fp);
-
-    fclose(tfp);
-    fflush(fp);
-
-    return(new_line);
 }