3 * Block-to-line buffering
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
37 /*----- Main code ---------------------------------------------------------*/
39 /* --- @lbuf_flush@ --- *
41 * Arguments: @lbuf *b@ = pointer to buffer block
42 * @char *p@ = pointer to where to start searching
43 * @size_t len@ = length of new material added
47 * Use: Flushes any complete lines in a line buffer. New material
48 * is assumed to have been added starting at @p@. If @p@ is
49 * null, then the scan starts at the beginning of the buffer,
50 * and the size of data already in the buffer is used in place
53 * It is assumed that the buffer is initially enabled. You
54 * shouldn't be contributing data to a disabled buffer anyway.
55 * However, the buffer handler may at some point disable itself,
56 * and @lbuf_flush@ can cope with this eventuality. Any pending
57 * data is left at the start of the buffer and can be flushed
58 * out by calling @lbuf_flush(b, 0, 0)@ if the buffer is ever
62 void lbuf_flush(lbuf *b, char *p, size_t len)
64 char *l; /* Limit of data in buffer */
65 char *q; /* Roving pointer through string */
66 char *base; /* Base address of current line */
67 int cr; /* Carriage return state */
69 if (b->f & LBUF_CLOSE) {
74 /* --- Initialize variables as necessary --- */
85 /* --- Clear @base@ if I'm discarding an overlong line --- */
92 /* --- Now I march through the string --- */
94 for (q = p; q < l; q++) {
96 /* --- Quickly discard uninteresting characters --- */
100 case LBUF_STRICTCRLF:
101 if (*q != '\r' && *q != '\n') {
109 if (!cr && b->delim == LBUF_STRICTCRLF)
117 /* --- I have a positive ID on a delimiter --- *
119 * If I'm interested in this string, report it to my owner.
125 len--; /* Exercise: why is this safe? */
127 b->func(base, len, b->p);
128 if (!(b->f & LBUF_ENABLE)) {
137 /* --- Sift through the aftermath --- */
143 b->func(base, len - 1, b->p);
144 } else if (base != b->buf)
145 memmove(b->buf, base, len);
154 /* --- @lbuf_close@ --- *
156 * Arguments: @lbuf *b@ = pointer to buffer block
160 * Use: Empties the buffer of any data currently lurking in it, and
161 * informs the client that this has happened. It's assumed that
162 * the buffer is enabled: you shouldn't be reading close events
163 * on disabled buffers. The buffer, if allocated, is freed.
166 void lbuf_close(lbuf *b)
168 if (b->len && b->len != b->sz) {
170 b->func(b->buf, b->len, b->p);
173 x_free(b->a, b->buf);
177 if (b->f & LBUF_ENABLE)
181 /* --- @lbuf_free@ --- *
183 * Arguments: @lbuf *b@ = pointer to buffer block
184 * @char **p@ = output pointer to free space
186 * Returns: Free buffer size.
188 * Use: Returns the free portion of a line buffer. Data can then be
189 * written to this portion, and split out into lines by calling
190 * @lbuf_flush@. A buffer is allocated if none currently
194 size_t lbuf_free(lbuf *b, char **p)
196 /* --- There's a special case to consider --- *
198 * If a line from the file wouldn't fit in the buffer, I truncate it and
199 * return what would fit. The rest of the line ought to be discarded.
200 * This condition is signalled by @len = b->sz@, and means that the entire
201 * buffer is OK to be trashed. In other cases, @len@ is the amount of
202 * space currently occupied in the buffer. This special case is the reason
203 * this routine exists.
206 if (b->len != 0 && b->len != b->sz) {
207 *p = b->buf + b->len;
208 return (b->sz - b->len);
211 b->buf = x_alloc(b->a, b->sz);
217 /* --- @lbuf_snarf@ --- *
219 * Arguments: @lbuf *b@ = pointer to buffer block
220 * @const void *p@ = pointer to input data buffer
221 * @size_t sz@ = size of data in input buffer
225 * Use: Snarfs the data from the input buffer and spits it out as
226 * lines. This interface ignores the complexities of dealing
227 * with disablement: you should be using @lbuf_free@ to
228 * contribute data if you want to cope with that.
231 void lbuf_snarf(lbuf *b, const void *p, size_t sz)
234 while (sz && (b->f & LBUF_ENABLE)) {
238 bsz = lbuf_free(b, &bp);
242 lbuf_flush(b, bp, bsz);
248 /* --- @lbuf_setsize@ --- *
250 * Arguments: @lbuf *b@ = pointer to buffer block
251 * @size_t sz@ = requested maximum line size
255 * Use: Modifies the size of the buffer associated with the block.
256 * It is an error to resize a buffer while it contains data.
259 void lbuf_setsize(lbuf *b, size_t sz)
262 assert(((void)"Buffer in use in lbuf_setsize",
263 b->len == 0 || b->len == b->sz));
265 x_free(b->a, b->buf);
270 /* --- @lbuf_init@ --- *
272 * Arguments: @lbuf *b@ = pointer to buffer block
273 * @lbuf_func *func@ = handler function
274 * @void *p@ = argument pointer for @func@
278 * Use: Initializes a line buffer block. Any recognized lines are
279 * passed to @func@ for processing. No buffer is initially
280 * allocated; this is done when the buffer is actually required
281 * for the first time.
284 void lbuf_init(lbuf *b, lbuf_func *func, void *p)
290 b->delim = LBUF_CRLF;
293 lbuf_setsize(b, 256);
296 /* --- @lbuf_destroy@ --- *
298 * Arguments: @lbuf *b@ = pointer to buffer block
302 * Use: Deallocates a line buffer and frees any resources it owned.
305 void lbuf_destroy(lbuf *b)
308 x_free(b->a, b->buf);
313 /*----- That's all, folks -------------------------------------------------*/