1 b2cfc4e2 2003-09-30 devnull #include "lib9.h"
2 b2cfc4e2 2003-09-30 devnull #include <bio.h>
4 b2cfc4e2 2003-09-30 devnull static char*
5 b2cfc4e2 2003-09-30 devnull badd(char *p, int *np, char *data, int ndata, int delim, int nulldelim)
10 b2cfc4e2 2003-09-30 devnull p = realloc(p, n+ndata+1);
12 b2cfc4e2 2003-09-30 devnull memmove(p+n, data, ndata);
13 b2cfc4e2 2003-09-30 devnull n += ndata;
14 b2cfc4e2 2003-09-30 devnull if(n>0 && nulldelim && p[n-1]==delim)
15 b2cfc4e2 2003-09-30 devnull p[--n] = '\0';
17 b2cfc4e2 2003-09-30 devnull p[n] = '\0';
20 b2cfc4e2 2003-09-30 devnull return p;
24 b2cfc4e2 2003-09-30 devnull Brdstr(Biobuf *bp, int delim, int nulldelim)
26 b2cfc4e2 2003-09-30 devnull char *ip, *ep, *p;
27 b2cfc4e2 2003-09-30 devnull int i, j;
29 b2cfc4e2 2003-09-30 devnull i = -bp->icount;
30 b2cfc4e2 2003-09-30 devnull bp->rdline = 0;
31 b2cfc4e2 2003-09-30 devnull if(i == 0) {
33 b2cfc4e2 2003-09-30 devnull * eof or other error
35 b2cfc4e2 2003-09-30 devnull if(bp->state != Bractive) {
36 b2cfc4e2 2003-09-30 devnull if(bp->state == Bracteof)
37 b2cfc4e2 2003-09-30 devnull bp->state = Bractive;
38 b2cfc4e2 2003-09-30 devnull bp->gbuf = bp->ebuf;
39 b2cfc4e2 2003-09-30 devnull return nil;
44 b2cfc4e2 2003-09-30 devnull * first try in remainder of buffer (gbuf doesn't change)
46 b2cfc4e2 2003-09-30 devnull ip = (char*)bp->ebuf - i;
47 b2cfc4e2 2003-09-30 devnull ep = memchr(ip, delim, i);
49 b2cfc4e2 2003-09-30 devnull j = (ep - ip) + 1;
50 b2cfc4e2 2003-09-30 devnull bp->icount += j;
51 b2cfc4e2 2003-09-30 devnull return badd(nil, &bp->rdline, ip, j, delim, nulldelim);
55 b2cfc4e2 2003-09-30 devnull * copy data to beginning of buffer
57 b2cfc4e2 2003-09-30 devnull if(i < bp->bsize)
58 b2cfc4e2 2003-09-30 devnull memmove(bp->bbuf, ip, i);
59 b2cfc4e2 2003-09-30 devnull bp->gbuf = bp->bbuf;
62 b2cfc4e2 2003-09-30 devnull * append to buffer looking for the delim
66 b2cfc4e2 2003-09-30 devnull ip = (char*)bp->bbuf + i;
67 b2cfc4e2 2003-09-30 devnull while(i < bp->bsize) {
68 b2cfc4e2 2003-09-30 devnull j = read(bp->fid, ip, bp->bsize-i);
69 b2cfc4e2 2003-09-30 devnull if(j <= 0 && i == 0)
70 b2cfc4e2 2003-09-30 devnull return p;
71 b2cfc4e2 2003-09-30 devnull if(j <= 0 && i > 0){
73 b2cfc4e2 2003-09-30 devnull * end of file but no delim. pretend we got a delim
74 b2cfc4e2 2003-09-30 devnull * by making the delim \0 and smashing it with nulldelim.
78 b2cfc4e2 2003-09-30 devnull delim = '\0';
79 b2cfc4e2 2003-09-30 devnull nulldelim = 1;
80 b2cfc4e2 2003-09-30 devnull *ep = delim; /* there will be room for this */
82 b2cfc4e2 2003-09-30 devnull bp->offset += j;
83 b2cfc4e2 2003-09-30 devnull ep = memchr(ip, delim, j);
88 b2cfc4e2 2003-09-30 devnull * found in new piece
89 b2cfc4e2 2003-09-30 devnull * copy back up and reset everything
91 b2cfc4e2 2003-09-30 devnull ip = (char*)bp->ebuf - i;
92 b2cfc4e2 2003-09-30 devnull if(i < bp->bsize){
93 b2cfc4e2 2003-09-30 devnull memmove(ip, bp->bbuf, i);
94 b2cfc4e2 2003-09-30 devnull bp->gbuf = (unsigned char*)ip;
96 b2cfc4e2 2003-09-30 devnull j = (ep - (char*)bp->bbuf) + 1;
97 b2cfc4e2 2003-09-30 devnull bp->icount = j - i;
98 b2cfc4e2 2003-09-30 devnull return badd(p, &bp->rdline, ip, j, delim, nulldelim);
100 b2cfc4e2 2003-09-30 devnull ip += j;
104 b2cfc4e2 2003-09-30 devnull * full buffer without finding; add to user string and continue
106 b2cfc4e2 2003-09-30 devnull p = badd(p, &bp->rdline, (char*)bp->bbuf, bp->bsize, 0, 0);
108 b2cfc4e2 2003-09-30 devnull bp->icount = 0;
109 b2cfc4e2 2003-09-30 devnull bp->gbuf = bp->ebuf;