3 * $Id: lbuf.c,v 1.7 2004/04/08 01:36:13 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 /*----- Header files ------------------------------------------------------*/
41 /*----- Main code ---------------------------------------------------------*/
43 /* --- @lbuf_flush@ --- *
45 * Arguments: @lbuf *b@ = pointer to buffer block
46 * @char *p@ = pointer to where to start searching
47 * @size_t len@ = length of new material added
51 * Use: Flushes any complete lines in a line buffer. New material
52 * is assumed to have been added starting at @p@. If @p@ is
53 * null, then the scan starts at the beginning of the buffer,
54 * and the size of data already in the buffer is used in place
57 * It is assumed that the buffer is initially enabled. You
58 * shouldn't be contributing data to a disabled buffer anyway.
59 * However, the buffer handler may at some point disable itself,
60 * and @lbuf_flush@ can cope with this eventuality. Any pending
61 * data is left at the start of the buffer and can be flushed
62 * out by calling @lbuf_flush(b, 0, 0)@ if the buffer is ever
66 void lbuf_flush(lbuf
*b
, char *p
, size_t len
)
68 char *l
; /* Limit of data in buffer */
69 char *q
; /* Roving pointer through string */
70 char *base
; /* Base address of current line */
71 int cr
; /* Carriage return state */
73 if (b
->f
& LBUF_CLOSE
) {
78 /* --- Initialize variables as necessary --- */
89 /* --- Clear @base@ if I'm discarding an overlong line --- */
96 /* --- Now I march through the string --- */
98 for (q
= p
; q
< l
; q
++) {
100 /* --- Quickly discard uninteresting characters --- */
104 case LBUF_STRICTCRLF
:
105 if (*q
!= '\r' && *q
!= '\n') {
113 if (!cr
&& b
->delim
== LBUF_STRICTCRLF
)
121 /* --- I have a positive ID on a delimiter --- *
123 * If I'm interested in this string, report it to my owner.
129 len
--; /* Exercise: why is this safe? */
131 b
->func(base
, len
, b
->p
);
132 if (!(b
->f
& LBUF_ENABLE
)) {
141 /* --- Sift through the aftermath --- */
147 b
->func(base
, len
- 1, b
->p
);
148 } else if (base
!= b
->buf
)
149 memmove(b
->buf
, base
, len
);
158 /* --- @lbuf_close@ --- *
160 * Arguments: @lbuf *b@ = pointer to buffer block
164 * Use: Empties the buffer of any data currently lurking in it, and
165 * informs the client that this has happened. It's assumed that
166 * the buffer is enabled: you shouldn't be reading close events
167 * on disabled buffers. The buffer, if allocated, is freed.
170 void lbuf_close(lbuf
*b
)
172 if (b
->len
&& b
->len
!= b
->sz
) {
174 b
->func(b
->buf
, b
->len
, b
->p
);
177 x_free(b
->a
, b
->buf
);
181 if (b
->f
& LBUF_ENABLE
)
185 /* --- @lbuf_free@ --- *
187 * Arguments: @lbuf *b@ = pointer to buffer block
188 * @char **p@ = output pointer to free space
190 * Returns: Free buffer size.
192 * Use: Returns the free portion of a line buffer. Data can then be
193 * written to this portion, and split out into lines by calling
194 * @lbuf_flush@. A buffer is allocated if none currently
198 size_t lbuf_free(lbuf
*b
, char **p
)
200 /* --- There's a special case to consider --- *
202 * If a line from the file wouldn't fit in the buffer, I truncate it and
203 * return what would fit. The rest of the line ought to be discarded.
204 * This condition is signalled by @len = b->sz@, and means that the entire
205 * buffer is OK to be trashed. In other cases, @len@ is the amount of
206 * space currently occupied in the buffer. This special case is the reason
207 * this routine exists.
210 if (b
->len
!= 0 && b
->len
!= b
->sz
) {
211 *p
= b
->buf
+ b
->len
;
212 return (b
->sz
- b
->len
);
215 b
->buf
= x_alloc(b
->a
, b
->sz
);
221 /* --- @lbuf_snarf@ --- *
223 * Arguments: @lbuf *b@ = pointer to buffer block
224 * @const void *p@ = pointer to input data buffer
225 * @size_t sz@ = size of data in input buffer
229 * Use: Snarfs the data from the input buffer and spits it out as
230 * lines. This interface ignores the complexities of dealing
231 * with disablement: you should be using @lbuf_free@ to
232 * contribute data if you want to cope with that.
235 void lbuf_snarf(lbuf
*b
, const void *p
, size_t sz
)
238 while (sz
&& (b
->f
& LBUF_ENABLE
)) {
242 bsz
= lbuf_free(b
, &bp
);
246 lbuf_flush(b
, bp
, bsz
);
252 /* --- @lbuf_setsize@ --- *
254 * Arguments: @lbuf *b@ = pointer to buffer block
255 * @size_t sz@ = requested maximum line size
259 * Use: Modifies the size of the buffer associated with the block.
260 * It is an error to resize a buffer while it contains data.
263 void lbuf_setsize(lbuf
*b
, size_t sz
)
266 assert(((void)"Buffer in use in lbuf_setsize",
267 b
->len
== 0 || b
->len
== b
->sz
));
269 x_free(b
->a
, b
->buf
);
274 /* --- @lbuf_init@ --- *
276 * Arguments: @lbuf *b@ = pointer to buffer block
277 * @lbuf_func *func@ = handler function
278 * @void *p@ = argument pointer for @func@
282 * Use: Initializes a line buffer block. Any recognized lines are
283 * passed to @func@ for processing. No buffer is initially
284 * allocated; this is done when the buffer is actually required
285 * for the first time.
288 void lbuf_init(lbuf
*b
, lbuf_func
*func
, void *p
)
294 b
->delim
= LBUF_CRLF
;
297 lbuf_setsize(b
, 256);
300 /* --- @lbuf_destroy@ --- *
302 * Arguments: @lbuf *b@ = pointer to buffer block
306 * Use: Deallocates a line buffer and frees any resources it owned.
309 void lbuf_destroy(lbuf
*b
)
312 x_free(b
->a
, b
->buf
);
317 /*----- That's all, folks -------------------------------------------------*/