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 static char*
5 b2cfc4e2 2003-09-30 devnull badd(char *p, int *np, char *data, int ndata, int delim, int nulldelim)
6 b2cfc4e2 2003-09-30 devnull {
7 b2cfc4e2 2003-09-30 devnull int n;
8 b2cfc4e2 2003-09-30 devnull
9 b2cfc4e2 2003-09-30 devnull n = *np;
10 b2cfc4e2 2003-09-30 devnull p = realloc(p, n+ndata+1);
11 b2cfc4e2 2003-09-30 devnull if(p){
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';
16 b2cfc4e2 2003-09-30 devnull else
17 b2cfc4e2 2003-09-30 devnull p[n] = '\0';
18 b2cfc4e2 2003-09-30 devnull *np = n;
19 b2cfc4e2 2003-09-30 devnull }
20 b2cfc4e2 2003-09-30 devnull return p;
21 b2cfc4e2 2003-09-30 devnull }
22 b2cfc4e2 2003-09-30 devnull
23 b2cfc4e2 2003-09-30 devnull char*
24 b2cfc4e2 2003-09-30 devnull Brdstr(Biobuf *bp, int delim, int nulldelim)
25 b2cfc4e2 2003-09-30 devnull {
26 b2cfc4e2 2003-09-30 devnull char *ip, *ep, *p;
27 b2cfc4e2 2003-09-30 devnull int i, j;
28 b2cfc4e2 2003-09-30 devnull
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) {
32 b2cfc4e2 2003-09-30 devnull /*
33 b2cfc4e2 2003-09-30 devnull * eof or other error
34 b2cfc4e2 2003-09-30 devnull */
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;
40 b2cfc4e2 2003-09-30 devnull }
41 b2cfc4e2 2003-09-30 devnull }
42 b2cfc4e2 2003-09-30 devnull
43 b2cfc4e2 2003-09-30 devnull /*
44 b2cfc4e2 2003-09-30 devnull * first try in remainder of buffer (gbuf doesn't change)
45 b2cfc4e2 2003-09-30 devnull */
46 b2cfc4e2 2003-09-30 devnull ip = (char*)bp->ebuf - i;
47 b2cfc4e2 2003-09-30 devnull ep = memchr(ip, delim, i);
48 b2cfc4e2 2003-09-30 devnull if(ep) {
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);
52 b2cfc4e2 2003-09-30 devnull }
53 b2cfc4e2 2003-09-30 devnull
54 b2cfc4e2 2003-09-30 devnull /*
55 b2cfc4e2 2003-09-30 devnull * copy data to beginning of buffer
56 b2cfc4e2 2003-09-30 devnull */
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;
60 b2cfc4e2 2003-09-30 devnull
61 b2cfc4e2 2003-09-30 devnull /*
62 b2cfc4e2 2003-09-30 devnull * append to buffer looking for the delim
63 b2cfc4e2 2003-09-30 devnull */
64 b2cfc4e2 2003-09-30 devnull p = nil;
65 b2cfc4e2 2003-09-30 devnull for(;;){
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){
72 b2cfc4e2 2003-09-30 devnull /*
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.
75 b2cfc4e2 2003-09-30 devnull */
76 b2cfc4e2 2003-09-30 devnull j = 1;
77 b2cfc4e2 2003-09-30 devnull ep = ip;
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 */
81 b2cfc4e2 2003-09-30 devnull }else{
82 b2cfc4e2 2003-09-30 devnull bp->offset += j;
83 b2cfc4e2 2003-09-30 devnull ep = memchr(ip, delim, j);
84 b2cfc4e2 2003-09-30 devnull }
85 b2cfc4e2 2003-09-30 devnull i += j;
86 b2cfc4e2 2003-09-30 devnull if(ep) {
87 b2cfc4e2 2003-09-30 devnull /*
88 b2cfc4e2 2003-09-30 devnull * found in new piece
89 b2cfc4e2 2003-09-30 devnull * copy back up and reset everything
90 b2cfc4e2 2003-09-30 devnull */
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;
95 b2cfc4e2 2003-09-30 devnull }
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);
99 b2cfc4e2 2003-09-30 devnull }
100 b2cfc4e2 2003-09-30 devnull ip += j;
101 b2cfc4e2 2003-09-30 devnull }
102 fa325e9b 2020-01-10 cross
103 b2cfc4e2 2003-09-30 devnull /*
104 b2cfc4e2 2003-09-30 devnull * full buffer without finding; add to user string and continue
105 b2cfc4e2 2003-09-30 devnull */
106 b2cfc4e2 2003-09-30 devnull p = badd(p, &bp->rdline, (char*)bp->bbuf, bp->bsize, 0, 0);
107 b2cfc4e2 2003-09-30 devnull i = 0;
108 b2cfc4e2 2003-09-30 devnull bp->icount = 0;
109 b2cfc4e2 2003-09-30 devnull bp->gbuf = bp->ebuf;
110 b2cfc4e2 2003-09-30 devnull }
111 b2cfc4e2 2003-09-30 devnull }