2 * UCW Library -- Fast Buffered I/O: Strings
4 * (c) 1997--2006 Martin Mares <mj@ucw.cz>
5 * (c) 2006--2018 Pavel Charvat <pchar@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
12 #include <ucw/fastbuf.h>
13 #include <ucw/mempool.h>
16 char * /* Non-standard */
17 bgets(struct fastbuf *f, char *b, uint l)
21 uint src_len = bdirect_read_prepare(f, &src);
26 uint cnt = MIN(l, src_len);
27 for (uint i = cnt; i--;)
32 bdirect_read_commit(f, src);
37 if (unlikely(cnt == l))
38 bthrow(f, "toolong", "%s: Line too long", f->name);
40 bdirect_read_commit(f, src);
41 src_len = bdirect_read_prepare(f, &src);
50 bgets_nodie(struct fastbuf *f, char *b, uint l)
53 byte *src, *start = b;
54 uint src_len = bdirect_read_prepare(f, &src);
59 uint cnt = MIN(l, src_len);
60 for (uint i = cnt; i--;)
65 bdirect_read_commit(f, src);
70 bdirect_read_commit(f, src);
74 src_len = bdirect_read_prepare(f, &src);
79 return b - (char *)start;
83 bgets_bb(struct fastbuf *f, struct bb_t *bb, uint limit)
87 uint src_len = bdirect_read_prepare(f, &src);
92 uint len = 0, buf_len = MIN(bb->len, limit);
95 uint cnt = MIN(src_len, buf_len);
96 for (uint i = cnt; i--;)
101 bdirect_read_commit(f, src);
109 bdirect_read_commit(f, src);
110 src_len = bdirect_read_prepare(f, &src);
116 if (unlikely(len == limit))
117 bthrow(f, "toolong", "%s: Line too long", f->name);
118 bb_do_grow(bb, len + 1);
120 buf_len = MIN(bb->len, limit) - len;
128 return buf - bb->ptr;
132 bgets_mp(struct fastbuf *f, struct mempool *mp)
135 uint src_len = bdirect_read_prepare(f, &src);
138 byte *buf = mp_start_noalign(mp, 1);
139 size_t buf_len = mp_avail(mp);
142 uint cnt = MIN(src_len, buf_len);
143 for (uint i = cnt; i--;)
148 bdirect_read_commit(f, src);
155 bdirect_read_commit(f, src);
156 src_len = bdirect_read_prepare(f, &src);
162 buf = mp_spread(mp, buf, 1);
163 buf_len = mp_avail(mp) - (buf - (byte *)mp_ptr(mp));
170 buf = mp_spread(mp, buf, 1);
172 return mp_end(mp, buf);
176 bgets0(struct fastbuf *f, char *b, uint l)
180 uint src_len = bdirect_read_prepare(f, &src);
185 uint cnt = MIN(l, src_len);
186 for (uint i = cnt; i--;)
191 bdirect_read_commit(f, src);
196 if (unlikely(cnt == l))
197 bthrow(f, "toolong", "%s: Line too long", f->name);
199 bdirect_read_commit(f, src);
200 src_len = bdirect_read_prepare(f, &src);