Subversion Repositories planix.SVN

Rev

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

Rev Author Line No. Line
105 7u83 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
#if	!defined(lint) && defined(DOSCCS)
12
static char *sccsid = "@(#)ex_tagio.c	7.3 (Berkeley) 1/31/86";
13
#endif
14
 
107 7u83 15
#include <fcntl.h>
16
#include <stdio.h>
17
 
105 7u83 18
#include "ex.h"
19
 
20
static long offset = -1;
21
static long block = -1;
22
static int bcnt = 0;
23
static int b_size = MAXBSIZE;
24
static char *ibuf;
25
 
26
topen(file, buf)
27
char *file, *buf;
28
{
29
	int fd;
30
	struct stat statb;
31
 
32
	offset = -1;
33
	block = -1;
34
	if ((fd = open(file, O_RDONLY, 0)) < 0)
35
		return(-1);
36
	if (fstat(fd, &statb) < 0) {
37
		(void)close(fd);
38
		return(-1);
39
	}
40
	ibuf = buf;
107 7u83 41
/*	b_size = statb.st_blksize;*/
42
	bsize = MAXBSIZE;
105 7u83 43
	return(fd);
44
}
45
 
46
tseek(fd, off)
47
int fd;
48
long off;
49
{
50
	long nblock;
51
 
52
	nblock = off / b_size * b_size;
53
	offset = off % b_size;
54
	if (nblock == block)
55
		return(0);
56
	block = nblock;
107 7u83 57
	if (lseek(fd, nblock, SEEK_SET) < 0)
105 7u83 58
		return(-1);
59
	if ((bcnt = read(fd, ibuf, b_size)) < 0)
60
		return(-1);
61
	return(0);
62
}
63
 
64
tgets(buf, cnt, fd)
65
register char *buf;
66
int cnt;
67
int fd;
68
{
69
	register char *cp;
70
	register cc;
71
 
72
	cc = offset;
73
	if (cc == -1) {
74
		if ((bcnt = read(fd, ibuf, b_size)) <= 0)
107 7u83 75
			return 0;
105 7u83 76
		cc = 0;
77
		block = 0;
78
	}
79
	if (bcnt == 0)		/* EOF */
107 7u83 80
		return 0;
105 7u83 81
	cp = ibuf + cc;
82
	while (--cnt > 0) {
83
		if (++cc > bcnt) {
84
			block += b_size;
85
			if ((bcnt = read(fd, ibuf, b_size)) <= 0) {
86
				offset = cc;
107 7u83 87
				return 0;
105 7u83 88
			}
89
			cp = ibuf;
90
			cc = 1;
91
		}
92
		if ((*buf++ = *cp++) == '\n')
93
			break;
94
	}
107 7u83 95
	*--buf = 0;
105 7u83 96
	offset = cc;
107 7u83 97
	return 1;
105 7u83 98
}
99
 
100
tclose(fd)
101
int fd;
102
{
103
	(void)close(fd);
104
	offset = -1;
105
	block = -1;
106
	bcnt = 0;
107
}
108
#endif