chiark / gitweb /
buf: Fix two embarassing bugs found while writing Lisp bindings.
[mLib] / dstr.c
CommitLineData
0875b58f 1/* -*-c-*-
2 *
8656dc50 3 * $Id: dstr.c,v 1.16 2004/04/08 01:36:11 mdw Exp $
0875b58f 4 *
5 * Handle dynamically growing strings
6 *
7 * (c) 1998 Straylight/Edgeware
8 */
9
c846879c 10/*----- Licensing notice --------------------------------------------------*
0875b58f 11 *
12 * This file is part of the mLib utilities library.
13 *
14 * mLib is free software; you can redistribute it and/or modify
c846879c 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.
18 *
0875b58f 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
c846879c 22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
0bd98442 25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
0875b58f 28 */
29
0875b58f 30/*----- Header files ------------------------------------------------------*/
31
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35
36#include "alloc.h"
37#include "dstr.h"
38
39/*----- Tunable constants -------------------------------------------------*/
40
002eaee3 41/*
3ca4c9f8 42 * If the buffer is empty, it is set to @DSTR_INITSZ@ bytes in size.
43 * Otherwise, it's set to the next power of two that's large enough. This is
44 * memory-hungry, but efficient.
45 */
46
f8509853 47#define DSTR_INITSZ 64 /* Initial buffer size */
3ca4c9f8 48
0875b58f 49/*----- Main code ---------------------------------------------------------*/
50
51/* --- @dstr_create@ --- *
52 *
53 * Arguments: @dstr *d@ = pointer to a dynamic string block
54 *
55 * Returns: ---
56 *
96c5fe33 57 * Use: Initializes a dynamic string.
0875b58f 58 */
59
fb467548 60void dstr_create(dstr *d) { DCREATE(d); }
0875b58f 61
62/* --- @dstr_destroy@ --- *
63 *
64 * Arguments: @dstr *d@ = pointer to a dynamic string block
65 *
66 * Returns: ---
67 *
68 * Use: Reclaims the space used by a dynamic string.
69 */
70
fb467548 71void dstr_destroy(dstr *d) { DDESTROY(d); }
0875b58f 72
73/* --- @dstr_reset@ --- *
74 *
c6e0eaf0 75 * Arguments: @dstr *d@ = pointer to a dynamic string block
0875b58f 76 *
77 * Returns: ---
78 *
79 * Use: Resets a string so that new data gets put at the beginning.
80 */
81
fb467548 82void dstr_reset(dstr *d) { DRESET(d); }
0875b58f 83
84/* --- @dstr_ensure@ --- *
85 *
86 * Arguments: @dstr *d@ = pointer to a dynamic string block
87 * @size_t sz@ = amount of free space to ensure
88 *
89 * Returns: ---
90 *
91 * Use: Ensures that at least @sz@ bytes are available in the
92 * given string.
93 */
94
95void dstr_ensure(dstr *d, size_t sz)
96{
97 size_t rq = d->len + sz;
98 size_t nsz;
99
100 /* --- If we have enough space, just leave it --- */
101
102 if (rq <= d->sz)
103 return;
104
3ca4c9f8 105 /* --- Grow the buffer --- */
0875b58f 106
107 nsz = d->sz;
108
f8509853 109 if (nsz == 0)
110 nsz = (DSTR_INITSZ >> 1);
111 do nsz <<= 1; while (nsz < rq);
0875b58f 112
113 if (d->buf)
b5ea4de3 114 d->buf = x_realloc(d->a, d->buf, nsz, d->sz);
0875b58f 115 else
20eb516f 116 d->buf = x_alloc(d->a, nsz);
0875b58f 117 d->sz = nsz;
118}
119
120/* --- @dstr_putc@ --- *
121 *
122 * Arguments: @dstr *d@ = pointer to a dynamic string block
123 * @char ch@ = character to append
124 *
125 * Returns: ---
126 *
127 * Use: Appends a character to a string.
128 */
129
fb467548 130void dstr_putc(dstr *d, char ch) { DPUTC(d, ch); }
0875b58f 131
132/* --- @dstr_putz@ --- *
133 *
134 * Arguments: @dstr *d@ = pointer to a dynamic string block
135 *
136 * Returns: ---
137 *
138 * Use: Appends a null byte to a string. The null byte does not
139 * contribute to the string's length, and will be overwritten
140 * by subsequent `put' operations.
141 */
142
fb467548 143void dstr_putz(dstr *d) { DPUTZ(d); }
0875b58f 144
145/* --- @dstr_puts@ --- *
146 *
147 * Arguments: @dstr *d@ = pointer to a dynamic string block
148 * @const char *s@ = pointer to string to append
149 *
150 * Returns: ---
151 *
152 * Use: Appends a character string to a string. A trailing null
153 * byte is added, as for @dstr_putz@.
154 */
155
fb467548 156void dstr_puts(dstr *d, const char *s) { DPUTS(d, s); }
0875b58f 157
158/* --- @dstr_putd@ --- *
159 *
160 * Arguments: @dstr *d@ = pointer to a dynamic string block
161 * @const dstr *s@ = pointer to a dynamic string to append
162 *
163 * Returns: ---
164 *
165 * Use: Appends a dynamic string to a string. A trailing null
166 * byte is added, as for @dstr_putz@.
167 */
168
fb467548 169void dstr_putd(dstr *d, const dstr *s) { DPUTD(d, s); }
0875b58f 170
171/* --- @dstr_putm@ --- *
172 *
173 * Arguments: @dstr *d@ = pointer to a dynamic string block
174 * @const void *p@ = pointer to a block to append
175 * @size_t sz@ = size of the block
176 *
177 * Returns: Appends an arbitrary data block to a string. No trailing
178 * null is appended.
179 */
180
fb467548 181void dstr_putm(dstr *d, const void *p, size_t sz) { DPUTM(d, p, sz); }
0875b58f 182
183/* --- @dstr_tidy@ --- *
184 *
185 * Arguments: @dstr *d@ = pointer to a dynamic string block
186 *
187 * Returns: ---
188 *
189 * Use: Reduces the amount of memory used by a string. A trailing
190 * null byte is added, as for @dstr_putz@.
191 */
192
193void dstr_tidy(dstr *d)
194{
b5ea4de3 195 d->buf = x_realloc(d->a, d->buf, d->len + 1, d->sz);
f8509853 196 d->buf[d->len] = 0;
0875b58f 197 d->sz = d->len + 1;
198}
199
200/* --- @dstr_putline@ --- *
201 *
202 * Arguments: @dstr *d@ = pointer to a dynamic string block
203 * @FILE *fp@ = a stream to read from
204 *
205 * Returns: The number of characters read into the buffer, or @EOF@ if
206 * end-of-file was reached before any characters were read.
207 *
208 * Use: Appends the next line from the given input stream to the
209 * string. A trailing newline is not added; a trailing null
210 * byte is appended, as for @dstr_putz@.
211 */
212
213int dstr_putline(dstr *d, FILE *fp)
214{
215 size_t left = d->sz - d->len;
216 size_t off = d->len;
217 int rd = 0;
218 int ch;
219
220 for (;;) {
221
0875b58f 222 /* --- Read the next byte --- */
223
224 ch = getc(fp);
225
226 /* --- End-of-file when no characters read is special --- */
227
228 if (ch == EOF && !rd)
229 return (EOF);
230
f8509853 231 /* --- Make sure there's some buffer space --- */
232
233 if (!left) {
9994cbdc 234 d->len = off;
f8509853 235 dstr_ensure(d, 1);
236 left = d->sz - off;
237 }
238
0875b58f 239 /* --- End-of-file or newline ends the loop --- */
240
241 if (ch == EOF || ch == '\n') {
242 d->buf[off] = 0;
243 d->len = off;
244 return rd;
245 }
246
247 /* --- Append the character and continue --- */
248
249 d->buf[off++] = ch;
250 left--; rd++;
251 }
252}
253
254/* --- @dstr_write@ --- *
255 *
256 * Arguments: @dstr *d@ = pointer to a dynamic string block
257 * @FILE *fp@ = a stream to write on
258 *
259 * Returns: The number of bytes written (as for @fwrite@).
260 *
261 * Use: Writes a dynamic string to a file.
262 */
263
fb467548 264size_t dstr_write(const dstr *d, FILE *fp) { return (DWRITE(d, fp)); }
0875b58f 265
266/*----- That's all, folks -------------------------------------------------*/