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 ------------------------------------------------------*/
39 /*----- Main code ---------------------------------------------------------*/
41 /* --- @lbuf_flush@ --- *
43 * Arguments: @lbuf *b@ = pointer to buffer block
44 * @char *p@ = pointer to where to start searching
45 * @size_t len@ = length of new material added
49 * Use: Flushes any complete lines in a line buffer. New material
50 * is assumed to have been added starting at @p@. If @p@ is
51 * null, then the scan starts at the beginning of the buffer,
52 * and the size of data already in the buffer is used in place
55 * It is assumed that the buffer is initially enabled. You
56 * shouldn't be contributing data to a disabled buffer anyway.
57 * However, the buffer handler may at some point disable itself,
58 * and @lbuf_flush@ can cope with this eventuality. Any pending
59 * data is left at the start of the buffer and can be flushed
60 * out by calling @lbuf_flush(b, 0, 0)@ if the buffer is ever
64 void lbuf_flush(lbuf
*b
, char *p
, size_t len
)
66 char *l
; /* Limit of data in buffer */
67 char *q
; /* Roving pointer through string */
68 char *base
; /* Base address of current line */
69 int cr
; /* Carriage return state */
71 if (b
->f
& LBUF_CLOSE
) {
76 /* --- Initialize variables as necessary --- */
87 /* --- Clear @base@ if I'm discarding an overlong line --- */
94 /* --- Now I march through the string --- */
96 for (q
= p
; q
< l
; q
++) {
98 /* --- Quickly discard uninteresting characters --- */
102 case LBUF_STRICTCRLF
:
103 if (*q
!= '\r' && *q
!= '\n') {
111 if (!cr
&& b
->delim
== LBUF_STRICTCRLF
)
119 /* --- I have a positive ID on a delimiter --- *
121 * If I'm interested in this string, report it to my owner.
127 len
--; /* Exercise: why is this safe? */
129 b
->func(base
, len
, b
->p
);
130 if (!(b
->f
& LBUF_ENABLE
)) {
139 /* --- Sift through the aftermath --- */
145 b
->func(base
, len
- 1, b
->p
);
146 } else if (base
!= b
->buf
)
147 memmove(b
->buf
, base
, len
);
156 /* --- @lbuf_close@ --- *
158 * Arguments: @lbuf *b@ = pointer to buffer block
162 * Use: Empties the buffer of any data currently lurking in it, and
163 * informs the client that this has happened. It's assumed that
164 * the buffer is enabled: you shouldn't be reading close events
165 * on disabled buffers. The buffer, if allocated, is freed.
168 void lbuf_close(lbuf
*b
)
170 if (b
->len
&& b
->len
!= b
->sz
) {
172 b
->func(b
->buf
, b
->len
, b
->p
);
175 x_free(b
->a
, b
->buf
);
179 if (b
->f
& LBUF_ENABLE
)
183 /* --- @lbuf_free@ --- *
185 * Arguments: @lbuf *b@ = pointer to buffer block
186 * @char **p@ = output pointer to free space
188 * Returns: Free buffer size.
190 * Use: Returns the free portion of a line buffer. Data can then be
191 * written to this portion, and split out into lines by calling
192 * @lbuf_flush@. A buffer is allocated if none currently
196 size_t lbuf_free(lbuf
*b
, char **p
)
198 /* --- There's a special case to consider --- *
200 * If a line from the file wouldn't fit in the buffer, I truncate it and
201 * return what would fit. The rest of the line ought to be discarded.
202 * This condition is signalled by @len = b->sz@, and means that the entire
203 * buffer is OK to be trashed. In other cases, @len@ is the amount of
204 * space currently occupied in the buffer. This special case is the reason
205 * this routine exists.
208 if (b
->len
!= 0 && b
->len
!= b
->sz
) {
209 *p
= b
->buf
+ b
->len
;
210 return (b
->sz
- b
->len
);
213 b
->buf
= x_alloc(b
->a
, b
->sz
);
219 /* --- @lbuf_snarf@ --- *
221 * Arguments: @lbuf *b@ = pointer to buffer block
222 * @const void *p@ = pointer to input data buffer
223 * @size_t sz@ = size of data in input buffer
227 * Use: Snarfs the data from the input buffer and spits it out as
228 * lines. This interface ignores the complexities of dealing
229 * with disablement: you should be using @lbuf_free@ to
230 * contribute data if you want to cope with that.
233 void lbuf_snarf(lbuf
*b
, const void *p
, size_t sz
)
236 while (sz
&& (b
->f
& LBUF_ENABLE
)) {
240 bsz
= lbuf_free(b
, &bp
);
244 lbuf_flush(b
, bp
, bsz
);
250 /* --- @lbuf_setsize@ --- *
252 * Arguments: @lbuf *b@ = pointer to buffer block
253 * @size_t sz@ = requested maximum line size
257 * Use: Modifies the size of the buffer associated with the block.
258 * It is an error to resize a buffer while it contains data.
261 void lbuf_setsize(lbuf
*b
, size_t sz
)
264 assert(((void)"Buffer in use in lbuf_setsize",
265 b
->len
== 0 || b
->len
== b
->sz
));
267 x_free(b
->a
, b
->buf
);
272 /* --- @lbuf_init@ --- *
274 * Arguments: @lbuf *b@ = pointer to buffer block
275 * @lbuf_func *func@ = handler function
276 * @void *p@ = argument pointer for @func@
280 * Use: Initializes a line buffer block. Any recognized lines are
281 * passed to @func@ for processing. No buffer is initially
282 * allocated; this is done when the buffer is actually required
283 * for the first time.
286 void lbuf_init(lbuf
*b
, lbuf_func
*func
, void *p
)
292 b
->delim
= LBUF_CRLF
;
295 lbuf_setsize(b
, 256);
298 /* --- @lbuf_destroy@ --- *
300 * Arguments: @lbuf *b@ = pointer to buffer block
304 * Use: Deallocates a line buffer and frees any resources it owned.
307 void lbuf_destroy(lbuf
*b
)
310 x_free(b
->a
, b
->buf
);
315 /*----- That's all, folks -------------------------------------------------*/