3 * $Id: lbuf.c,v 1.6 2002/01/13 13:32:52 mdw Exp $
5 * Block-to-line buffering
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.6 2002/01/13 13:32:52 mdw
34 * Pass line length to line handler function. Provide a @typedef@ for
35 * handler functions. Allow run-time configuration of line delimiters.
37 * Revision 1.5 2001/02/03 16:23:33 mdw
38 * Bug fix: handle a disable during a close-induced flush without dumping
41 * Revision 1.4 2000/06/17 10:38:14 mdw
42 * Add support for variable buffer sizes.
44 * Revision 1.3 1999/05/22 13:38:50 mdw
45 * Fix bug which discarded initial portions of incomplete lines.
47 * Revision 1.2 1999/05/17 20:36:08 mdw
48 * Make the magical constants for the buffer flags uppercase.
50 * Revision 1.1 1999/05/14 21:01:14 mdw
51 * Integrated `select' handling bits from the background resolver project.
55 /*----- Header files ------------------------------------------------------*/
66 /*----- Main code ---------------------------------------------------------*/
68 /* --- @lbuf_flush@ --- *
70 * Arguments: @lbuf *b@ = pointer to buffer block
71 * @char *p@ = pointer to where to start searching
72 * @size_t len@ = length of new material added
76 * Use: Flushes any complete lines in a line buffer. New material
77 * is assumed to have been added starting at @p@. If @p@ is
78 * null, then the scan starts at the beginning of the buffer,
79 * and the size of data already in the buffer is used in place
82 * It is assumed that the buffer is initially enabled. You
83 * shouldn't be contributing data to a disabled buffer anyway.
84 * However, the buffer handler may at some point disable itself,
85 * and @lbuf_flush@ can cope with this eventuality. Any pending
86 * data is left at the start of the buffer and can be flushed
87 * out by calling @lbuf_flush(b, 0, 0)@ if the buffer is ever
91 void lbuf_flush(lbuf
*b
, char *p
, size_t len
)
93 char *l
; /* Limit of data in buffer */
94 char *q
; /* Roving pointer through string */
95 char *base
; /* Base address of current line */
96 int cr
; /* Carriage return state */
98 if (b
->f
& LBUF_CLOSE
) {
103 /* --- Initialize variables as necessary --- */
114 /* --- Clear @base@ if I'm discarding an overlong line --- */
121 /* --- Now I march through the string --- */
123 for (q
= p
; q
< l
; q
++) {
125 /* --- Quickly discard uninteresting characters --- */
129 case LBUF_STRICTCRLF
:
130 if (*q
!= '\r' && *q
!= '\n') {
138 if (!cr
&& b
->delim
== LBUF_STRICTCRLF
)
146 /* --- I have a positive ID on a delimiter --- *
148 * If I'm interested in this string, report it to my owner.
154 len
--; /* Exercise: why is this safe? */
156 b
->func(base
, len
, b
->p
);
157 if (!(b
->f
& LBUF_ENABLE
)) {
166 /* --- Sift through the aftermath --- */
172 b
->func(base
, len
- 1, b
->p
);
173 } else if (base
!= b
->buf
)
174 memmove(b
->buf
, base
, len
);
183 /* --- @lbuf_close@ --- *
185 * Arguments: @lbuf *b@ = pointer to buffer block
189 * Use: Empties the buffer of any data currently lurking in it, and
190 * informs the client that this has happened. It's assumed that
191 * the buffer is enabled: you shouldn't be reading close events
192 * on disabled buffers. The buffer, if allocated, is freed.
195 void lbuf_close(lbuf
*b
)
197 if (b
->len
&& b
->len
!= b
->sz
) {
199 b
->func(b
->buf
, b
->len
, b
->p
);
202 x_free(b
->a
, b
->buf
);
206 if (b
->f
& LBUF_ENABLE
)
210 /* --- @lbuf_free@ --- *
212 * Arguments: @lbuf *b@ = pointer to buffer block
213 * @char **p@ = output pointer to free space
215 * Returns: Free buffer size.
217 * Use: Returns the free portion of a line buffer. Data can then be
218 * written to this portion, and split out into lines by calling
219 * @lbuf_flush@. A buffer is allocated if none currently
223 size_t lbuf_free(lbuf
*b
, char **p
)
225 /* --- There's a special case to consider --- *
227 * If a line from the file wouldn't fit in the buffer, I truncate it and
228 * return what would fit. The rest of the line ought to be discarded.
229 * This condition is signalled by @len = b->sz@, and means that the entire
230 * buffer is OK to be trashed. In other cases, @len@ is the amount of
231 * space currently occupied in the buffer. This special case is the reason
232 * this routine exists.
235 if (b
->len
!= 0 && b
->len
!= b
->sz
) {
236 *p
= b
->buf
+ b
->len
;
237 return (b
->sz
- b
->len
);
240 b
->buf
= x_alloc(b
->a
, b
->sz
);
246 /* --- @lbuf_snarf@ --- *
248 * Arguments: @lbuf *b@ = pointer to buffer block
249 * @const void *p@ = pointer to input data buffer
250 * @size_t sz@ = size of data in input buffer
254 * Use: Snarfs the data from the input buffer and spits it out as
255 * lines. This interface ignores the complexities of dealing
256 * with disablement: you should be using @lbuf_free@ to
257 * contribute data if you want to cope with that.
260 void lbuf_snarf(lbuf
*b
, const void *p
, size_t sz
)
263 while (sz
&& (b
->f
& LBUF_ENABLE
)) {
267 bsz
= lbuf_free(b
, &bp
);
271 lbuf_flush(b
, bp
, bsz
);
277 /* --- @lbuf_setsize@ --- *
279 * Arguments: @lbuf *b@ = pointer to buffer block
280 * @size_t sz@ = requested maximum line size
284 * Use: Modifies the size of the buffer associated with the block.
285 * It is an error to resize a buffer while it contains data.
288 void lbuf_setsize(lbuf
*b
, size_t sz
)
291 assert(((void)"Buffer in use in lbuf_setsize",
292 b
->len
== 0 || b
->len
== b
->sz
));
294 x_free(b
->a
, b
->buf
);
299 /* --- @lbuf_init@ --- *
301 * Arguments: @lbuf *b@ = pointer to buffer block
302 * @lbuf_func *func@ = handler function
303 * @void *p@ = argument pointer for @func@
307 * Use: Initializes a line buffer block. Any recognized lines are
308 * passed to @func@ for processing. No buffer is initially
309 * allocated; this is done when the buffer is actually required
310 * for the first time.
313 void lbuf_init(lbuf
*b
, lbuf_func
*func
, void *p
)
319 b
->delim
= LBUF_CRLF
;
322 lbuf_setsize(b
, 256);
325 /* --- @lbuf_destroy@ --- *
327 * Arguments: @lbuf *b@ = pointer to buffer block
331 * Use: Deallocates a line buffer and frees any resources it owned.
334 void lbuf_destroy(lbuf
*b
)
337 x_free(b
->a
, b
->buf
);
342 /*----- That's all, folks -------------------------------------------------*/