2 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Chris Pressey <cpressey@catseye.mine.nu>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * $Id: buffer.c,v 1.2 2005/02/06 06:57:30 cpressey Exp $
38 * Routines to manipulate extensible buffers.
40 * Aura buffers are buffers that attempt to automatically expand
41 * when more data is written to them than they can initially hold.
42 * In addition, each extensible buffer contains a cursor from which
43 * its contents may be incrementally scanned.
56 * Create a new extensible buffer with the given initial size.
59 aura_buffer_new(size_t size
)
61 struct aura_buffer
*e
;
63 e
= malloc(sizeof(struct aura_buffer
));
69 e
->buf
= malloc(size
);
76 * Deallocate the memory used for an extensible buffer.
79 aura_buffer_free(struct aura_buffer
*e
)
89 * Return the underlying (static) buffer of an extensible buffer.
91 * NOTE that you should NEVER cache the returned pointer anywhere,
92 * as any further manipulation of the extensible buffer may cause
93 * it to be invalidated.
95 * ALSO NOTE that the buffer may contain embedded NULs, but will
96 * also be guaranteed to be NUL-terminated.
99 aura_buffer_buf(struct aura_buffer
*e
)
105 * Return the current length of the extensible buffer.
108 aura_buffer_len(struct aura_buffer
*e
)
114 * Return the current size of the extensible buffer. This is how
115 * big it's length may grow to before expanded.
118 aura_buffer_size(struct aura_buffer
*e
)
124 * Ensure that an extensible buffer's size is at least the given
125 * size. If it is not, it will be internally grown to that size.
126 * This does not affect the contents of the buffer in any way.
129 aura_buffer_ensure_size(struct aura_buffer
*e
, size_t size
)
131 if (e
->size
>= size
) return;
133 if ((e
->buf
= realloc(e
->buf
, e
->size
)) == NULL
) {
134 err(EX_UNAVAILABLE
, "realloc()");
139 * Set the contents of an extensible buffer from a regular (char *)
140 * buffer. The extensible buffer will grow if needed. Any existing
141 * contents of the extensible buffer are destroyed in this operation.
142 * Note that, because this requires that the length of the
143 * regular buffer be specified, it may safely contain NUL bytes.
146 aura_buffer_set(struct aura_buffer
*e
, const char *buf
, size_t length
)
148 while ((length
+ 1) > e
->size
) {
151 if ((e
->buf
= realloc(e
->buf
, e
->size
)) == NULL
) {
152 err(EX_UNAVAILABLE
, "realloc()");
154 memcpy(e
->buf
, buf
, length
);
156 e
->buf
[e
->len
] = '\0';
160 * Append the contents of a regular buffer to the end of the existing
161 * contents of an extensible buffer. The extensible buffer will grow
162 * if needed. Note that, because this requires that the length of the
163 * regular buffer be specified, it may safely contain NUL bytes.
166 aura_buffer_append(struct aura_buffer
*e
, const char *buf
, size_t length
)
168 while (e
->len
+ (length
+ 1) > e
->size
) {
171 if ((e
->buf
= realloc(e
->buf
, e
->size
)) == NULL
) {
172 err(EX_UNAVAILABLE
, "realloc()");
174 memcpy(e
->buf
+ e
->len
, buf
, length
);
176 e
->buf
[e
->len
] = '\0';
180 * Set the contents of an extensible buffer from an ASCIIZ string.
181 * This is identical to aura_buffer_set except that the length need not
182 * be specified, and the ASCIIZ string may not contain embedded NUL's.
185 aura_buffer_cpy(struct aura_buffer
*e
, const char *s
)
187 aura_buffer_set(e
, s
, strlen(s
));
191 * Append the contents of an ASCIIZ string to an extensible buffer.
192 * This is identical to aura_buffer_append except that the length need not
193 * be specified, and the ASCIIZ string may not contain embedded NUL's.
196 aura_buffer_cat(struct aura_buffer
*e
, const char *s
)
198 aura_buffer_append(e
, s
, strlen(s
));
202 * Append the entire contents of a text file to an extensible buffer.
205 aura_buffer_cat_file(struct aura_buffer
*e
, const char *fmt
, ...)
208 char *filename
, line
[1024];
212 vasprintf(&filename
, fmt
, args
);
215 if ((f
= fopen(filename
, "r")) == NULL
)
220 while (fgets(line
, 1023, f
) != NULL
) {
221 aura_buffer_cat(e
, line
);
230 * Append the entire output of a shell command to an extensible buffer.
233 aura_buffer_cat_pipe(struct aura_buffer
*e
, const char *fmt
, ...)
236 char *command
, line
[1024];
240 vasprintf(&command
, fmt
, args
);
243 if ((p
= popen(command
, "r")) == NULL
)
248 while (fgets(line
, 1023, p
) != NULL
) {
249 aura_buffer_cat(e
, line
);
257 /*** CURSORED FUNCTIONS ***/
260 * Note that the cursor can be anywhere from the first character to
261 * one position _beyond_ the last character in the buffer.
265 aura_buffer_seek(struct aura_buffer
*e
, size_t pos
)
267 if (pos
<= e
->size
) {
276 aura_buffer_tell(struct aura_buffer
*e
)
282 aura_buffer_eof(struct aura_buffer
*e
)
284 return(e
->pos
>= e
->size
);
288 aura_buffer_peek_char(struct aura_buffer
*e
)
290 return(e
->buf
[e
->pos
]);
294 aura_buffer_scan_char(struct aura_buffer
*e
)
296 return(e
->buf
[e
->pos
++]);
300 aura_buffer_compare(struct aura_buffer
*e
, const char *s
)
304 for (i
= 0, pos
= e
->pos
; s
[i
] != '\0' && pos
< e
->size
; i
++, pos
++) {
305 if (e
->buf
[pos
] != s
[i
])
309 if (pos
<= e
->size
) {
317 aura_buffer_expect(struct aura_buffer
*e
, const char *s
)
321 if ((pos
= aura_buffer_compare(e
, s
)) > 0) {
330 aura_buffer_push(struct aura_buffer
*e
, const void *src
, size_t len
)
332 aura_buffer_ensure_size(e
, e
->pos
+ len
);
333 memcpy(e
->buf
+ e
->pos
, src
, len
);
338 aura_buffer_pop(struct aura_buffer
*e
, void *dest
, size_t len
)
340 if (e
->pos
- len
> 0) {
342 memcpy(dest
, e
->buf
+ e
->pos
, len
);