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