1 b2cfc4e2 2003-09-30 devnull #include "lib9.h"
2 b2cfc4e2 2003-09-30 devnull #include <bio.h>
5 b2cfc4e2 2003-09-30 devnull Brdline(Biobuf *bp, int delim)
7 b2cfc4e2 2003-09-30 devnull char *ip, *ep;
10 b2cfc4e2 2003-09-30 devnull i = -bp->icount;
11 b2cfc4e2 2003-09-30 devnull if(i == 0) {
13 b2cfc4e2 2003-09-30 devnull * eof or other error
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;
25 b2cfc4e2 2003-09-30 devnull * first try in remainder of buffer (gbuf doesn't change)
27 b2cfc4e2 2003-09-30 devnull ip = (char*)bp->ebuf - i;
28 b2cfc4e2 2003-09-30 devnull ep = memchr(ip, delim, i);
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;
37 b2cfc4e2 2003-09-30 devnull * copy data to beginning of buffer
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;
44 b2cfc4e2 2003-09-30 devnull * append to buffer looking for the delim
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) {
51 b2cfc4e2 2003-09-30 devnull * end of file with no delim
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;
59 b2cfc4e2 2003-09-30 devnull bp->offset += j;
61 b2cfc4e2 2003-09-30 devnull ep = memchr(ip, delim, j);
64 b2cfc4e2 2003-09-30 devnull * found in new piece
65 b2cfc4e2 2003-09-30 devnull * copy back up and reset everything
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;
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;
81 b2cfc4e2 2003-09-30 devnull * full buffer without finding
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;
90 b2cfc4e2 2003-09-30 devnull Blinelen(Biobuf *bp)
93 b2cfc4e2 2003-09-30 devnull return bp->rdline;