Blame


1 b2cfc4e2 2003-09-30 devnull #include "lib9.h"
2 b2cfc4e2 2003-09-30 devnull #include <bio.h>
3 b2cfc4e2 2003-09-30 devnull
4 b2cfc4e2 2003-09-30 devnull void*
5 b2cfc4e2 2003-09-30 devnull Brdline(Biobuf *bp, int delim)
6 b2cfc4e2 2003-09-30 devnull {
7 b2cfc4e2 2003-09-30 devnull char *ip, *ep;
8 b2cfc4e2 2003-09-30 devnull int i, j;
9 b2cfc4e2 2003-09-30 devnull
10 b2cfc4e2 2003-09-30 devnull i = -bp->icount;
11 b2cfc4e2 2003-09-30 devnull if(i == 0) {
12 b2cfc4e2 2003-09-30 devnull /*
13 b2cfc4e2 2003-09-30 devnull * eof or other error
14 b2cfc4e2 2003-09-30 devnull */
15 b2cfc4e2 2003-09-30 devnull if(bp->state != Bractive) {
16 b2cfc4e2 2003-09-30 devnull if(bp->state == Bracteof)
17 b2cfc4e2 2003-09-30 devnull bp->state = Bractive;
18 b2cfc4e2 2003-09-30 devnull bp->rdline = 0;
19 b2cfc4e2 2003-09-30 devnull bp->gbuf = bp->ebuf;
20 b2cfc4e2 2003-09-30 devnull return 0;
21 b2cfc4e2 2003-09-30 devnull }
22 b2cfc4e2 2003-09-30 devnull }
23 b2cfc4e2 2003-09-30 devnull
24 b2cfc4e2 2003-09-30 devnull /*
25 b2cfc4e2 2003-09-30 devnull * first try in remainder of buffer (gbuf doesn't change)
26 b2cfc4e2 2003-09-30 devnull */
27 b2cfc4e2 2003-09-30 devnull ip = (char*)bp->ebuf - i;
28 b2cfc4e2 2003-09-30 devnull ep = memchr(ip, delim, i);
29 b2cfc4e2 2003-09-30 devnull if(ep) {
30 b2cfc4e2 2003-09-30 devnull j = (ep - ip) + 1;
31 b2cfc4e2 2003-09-30 devnull bp->rdline = j;
32 b2cfc4e2 2003-09-30 devnull bp->icount += j;
33 b2cfc4e2 2003-09-30 devnull return ip;
34 b2cfc4e2 2003-09-30 devnull }
35 b2cfc4e2 2003-09-30 devnull
36 b2cfc4e2 2003-09-30 devnull /*
37 b2cfc4e2 2003-09-30 devnull * copy data to beginning of buffer
38 b2cfc4e2 2003-09-30 devnull */
39 b2cfc4e2 2003-09-30 devnull if(i < bp->bsize)
40 b2cfc4e2 2003-09-30 devnull memmove(bp->bbuf, ip, i);
41 b2cfc4e2 2003-09-30 devnull bp->gbuf = bp->bbuf;
42 b2cfc4e2 2003-09-30 devnull
43 b2cfc4e2 2003-09-30 devnull /*
44 b2cfc4e2 2003-09-30 devnull * append to buffer looking for the delim
45 b2cfc4e2 2003-09-30 devnull */
46 b2cfc4e2 2003-09-30 devnull ip = (char*)bp->bbuf + i;
47 b2cfc4e2 2003-09-30 devnull while(i < bp->bsize) {
48 b2cfc4e2 2003-09-30 devnull j = read(bp->fid, ip, bp->bsize-i);
49 b2cfc4e2 2003-09-30 devnull if(j <= 0) {
50 b2cfc4e2 2003-09-30 devnull /*
51 b2cfc4e2 2003-09-30 devnull * end of file with no delim
52 b2cfc4e2 2003-09-30 devnull */
53 b2cfc4e2 2003-09-30 devnull memmove(bp->ebuf-i, bp->bbuf, i);
54 b2cfc4e2 2003-09-30 devnull bp->rdline = i;
55 b2cfc4e2 2003-09-30 devnull bp->icount = -i;
56 b2cfc4e2 2003-09-30 devnull bp->gbuf = bp->ebuf-i;
57 b2cfc4e2 2003-09-30 devnull return 0;
58 b2cfc4e2 2003-09-30 devnull }
59 b2cfc4e2 2003-09-30 devnull bp->offset += j;
60 b2cfc4e2 2003-09-30 devnull i += j;
61 b2cfc4e2 2003-09-30 devnull ep = memchr(ip, delim, j);
62 b2cfc4e2 2003-09-30 devnull if(ep) {
63 b2cfc4e2 2003-09-30 devnull /*
64 b2cfc4e2 2003-09-30 devnull * found in new piece
65 b2cfc4e2 2003-09-30 devnull * copy back up and reset everything
66 b2cfc4e2 2003-09-30 devnull */
67 b2cfc4e2 2003-09-30 devnull ip = (char*)bp->ebuf - i;
68 b2cfc4e2 2003-09-30 devnull if(i < bp->bsize){
69 b2cfc4e2 2003-09-30 devnull memmove(ip, bp->bbuf, i);
70 b2cfc4e2 2003-09-30 devnull bp->gbuf = (unsigned char*)ip;
71 b2cfc4e2 2003-09-30 devnull }
72 b2cfc4e2 2003-09-30 devnull j = (ep - (char*)bp->bbuf) + 1;
73 b2cfc4e2 2003-09-30 devnull bp->rdline = j;
74 b2cfc4e2 2003-09-30 devnull bp->icount = j - i;
75 b2cfc4e2 2003-09-30 devnull return ip;
76 b2cfc4e2 2003-09-30 devnull }
77 b2cfc4e2 2003-09-30 devnull ip += j;
78 b2cfc4e2 2003-09-30 devnull }
79 b2cfc4e2 2003-09-30 devnull
80 b2cfc4e2 2003-09-30 devnull /*
81 b2cfc4e2 2003-09-30 devnull * full buffer without finding
82 b2cfc4e2 2003-09-30 devnull */
83 b2cfc4e2 2003-09-30 devnull bp->rdline = bp->bsize;
84 b2cfc4e2 2003-09-30 devnull bp->icount = -bp->bsize;
85 b2cfc4e2 2003-09-30 devnull bp->gbuf = bp->bbuf;
86 b2cfc4e2 2003-09-30 devnull return 0;
87 b2cfc4e2 2003-09-30 devnull }
88 b2cfc4e2 2003-09-30 devnull
89 b2cfc4e2 2003-09-30 devnull int
90 b2cfc4e2 2003-09-30 devnull Blinelen(Biobuf *bp)
91 b2cfc4e2 2003-09-30 devnull {
92 b2cfc4e2 2003-09-30 devnull
93 b2cfc4e2 2003-09-30 devnull return bp->rdline;
94 b2cfc4e2 2003-09-30 devnull }