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