Commit | Line | Data |
---|---|---|
b2e575b2 JB |
1 | /* Copyright (c) 1985 Regents of the University of California */ |
2 | ||
3 | /* | |
4 | * These routines are for faster tag lookup. They support the binary | |
5 | * search used in tagfind() instead of the linear search. The speedup | |
6 | * is quite noticable looking for a tag at the end of a long tags | |
7 | * file. Define FASTTAG in the Makefile to use these routines. | |
8 | */ | |
9 | ||
10 | #ifdef FASTTAG | |
11 | #ifndef lint | |
fa104d3b | 12 | static char *sccsid = "@(#)ex_tagio.c 7.3 (Berkeley) %G%"; |
b2e575b2 JB |
13 | #endif |
14 | ||
15 | #include <sys/file.h> | |
16 | #include "ex.h" | |
17 | ||
18 | static long offset = -1; | |
19 | static long block = -1; | |
20 | static int bcnt = 0; | |
21 | static int b_size = MAXBSIZE; | |
22 | static char *ibuf; | |
23 | ||
24 | topen(file, buf) | |
25 | char *file, *buf; | |
26 | { | |
27 | int fd; | |
28 | struct stat statb; | |
29 | ||
30 | offset = -1; | |
31 | block = -1; | |
32 | if ((fd = open(file, O_RDONLY, 0)) < 0) | |
33 | return(-1); | |
34 | if (fstat(fd, &statb) < 0) { | |
35 | (void)close(fd); | |
36 | return(-1); | |
37 | } | |
38 | ibuf = buf; | |
39 | b_size = statb.st_blksize; | |
40 | return(fd); | |
41 | } | |
42 | ||
43 | tseek(fd, off) | |
44 | int fd; | |
45 | long off; | |
46 | { | |
47 | int nblock; | |
48 | ||
49 | nblock = off / b_size * b_size; | |
50 | offset = off % b_size; | |
51 | if (nblock == block) | |
52 | return(0); | |
53 | block = nblock; | |
54 | if (lseek(fd, nblock, L_SET) < 0) | |
55 | return(-1); | |
56 | if ((bcnt = read(fd, ibuf, b_size)) < 0) | |
57 | return(-1); | |
58 | return(0); | |
59 | } | |
60 | ||
61 | tgets(buf, cnt, fd) | |
62 | register char *buf; | |
63 | int cnt; | |
64 | int fd; | |
65 | { | |
66 | register char *cp; | |
67 | register cc; | |
68 | ||
69 | cc = offset; | |
70 | if (cc == -1) { | |
71 | if ((bcnt = read(fd, ibuf, b_size)) <= 0) | |
72 | return (NULL); | |
73 | cc = 0; | |
74 | block = 0; | |
75 | } | |
76 | if (bcnt == 0) /* EOF */ | |
77 | return(NULL); | |
78 | cp = ibuf + cc; | |
79 | while (--cnt > 0) { | |
80 | if (++cc > bcnt) { | |
fa104d3b | 81 | block += b_size; |
b2e575b2 JB |
82 | if ((bcnt = read(fd, ibuf, b_size)) <= 0) { |
83 | offset = cc; | |
84 | return (NULL); | |
85 | } | |
b2e575b2 JB |
86 | cp = ibuf; |
87 | cc = 1; | |
88 | } | |
89 | if ((*buf++ = *cp++) == '\n') | |
90 | break; | |
91 | } | |
92 | *--buf = NULL; | |
93 | offset = cc; | |
94 | return(1); | |
95 | } | |
96 | ||
97 | tclose(fd) | |
98 | int fd; | |
99 | { | |
100 | (void)close(fd); | |
101 | offset = -1; | |
102 | block = -1; | |
103 | bcnt = 0; | |
104 | } | |
105 | #endif |