chiark / gitweb /
*** empty log message ***
[mLib] / dstr.h
1 /* -*-c-*-
2  *
3  * $Id: dstr.h,v 1.1 1998/06/17 23:44:42 mdw Exp $
4  *
5  * Handle dynamically growing strings
6  *
7  * (c) 1998 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------*
11  *
12  * This file is part of the mLib utilities library.
13  *
14  * mLib is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
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
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with mLib; if not, write to the Free Software Foundation,
26  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27  */
28
29 /*----- Revision history --------------------------------------------------*
30  *
31  * $Log: dstr.h,v $
32  * Revision 1.1  1998/06/17 23:44:42  mdw
33  * Initial revision
34  *
35  */
36
37 #ifndef DSTR_H
38 #define DSTR_H
39
40 #ifdef __cplusplus
41   extern "C" {
42 #endif
43
44 /*----- Rationale ---------------------------------------------------------*
45  *
46  * This file declares what is hopefully a fairly useful collection of
47  * primitive string handling functions.  The idea is that the strings
48  * allocate memory for themselves as required.  The @dstr@ routines don't
49  * assume any sort of terminator character, so arbitrary binary data can
50  * be stored in a dynamic string.  With luck, this should put a stop to
51  * any buffer overflow problems.
52  */
53
54 /*----- Header files ------------------------------------------------------*/
55
56 #include <stdio.h>
57
58 /*----- Data structures ---------------------------------------------------*/
59
60 typedef struct dstr {
61   char *buf;                            /* Pointer to string buffer */
62   size_t sz;                            /* Size of the buffer */
63   size_t len;                           /* Length of the string */
64 } dstr;
65
66 /*----- Functions provided ------------------------------------------------*/
67
68 /* --- @dstr_create@ --- *
69  *
70  * Arguments:   @dstr *d@ = pointer to a dynamic string block
71  *
72  * Returns:     ---
73  *
74  * Use:         Initialises a dynamic string.
75  */
76
77 extern void dstr_create(dstr */*d*/);
78
79 /* --- @dstr_destroy@ --- *
80  *
81  * Arguments:   @dstr *d@ = pointer to a dynamic string block
82  *
83  * Returns:     ---
84  *
85  * Use:         Reclaims the space used by a dynamic string.
86  */
87
88 extern void dstr_destroy(dstr */*d*/);
89
90 /* --- @dstr_reset@ --- *
91  *
92  * Arguments:   @dstr *d@ = pointer to a dynaimc string block
93  *
94  * Returns:     ---
95  *
96  * Use:         Resets a string so that new data gets put at the beginning.
97  */
98
99 extern void dstr_reset(dstr */*d*/);
100
101 /* --- @dstr_ensure@ --- *
102  *
103  * Arguments:   @dstr *d@ = pointer to a dynamic string block
104  *              @size_t sz@ = amount of free space to ensure
105  *
106  * Returns:     ---
107  *
108  * Use:         Ensures that at least @sz@ bytes are available in the
109  *              given string.
110  */
111
112 extern void dstr_ensure(dstr */*d*/, size_t /*sz*/);
113
114 #define DENSURE(d, rq) do {                                             \
115   if ((d)->len + (rq) > (d)->sz) dstr_ensure((d), (rq));                \
116 } while (0)
117
118 /* --- @dstr_putc@ --- *
119  *
120  * Arguments:   @dstr *d@ = pointer to a dynamic string block
121  *              @char ch@ = character to append
122  *
123  * Returns:     ---
124  *
125  * Use:         Appends a character to a string.
126  */
127
128 extern void dstr_putc(dstr */*d*/, char /*ch*/);
129
130 #define DPUTC(d, ch) do {                                               \
131   DENSURE((d), 1);                                                      \
132   (d)->buf[(d)->len++] = (ch);                                          \
133 } while (0)
134
135 /* --- @dstr_putz@ --- *
136  *
137  * Arguments:   @dstr *d@ = pointer to a dynamic string block
138  *
139  * Returns:     ---
140  *
141  * Use:         Appends a null byte to a string.  The null byte does not
142  *              contribute to the string's length, and will be overwritten
143  *              by subsequent `put' operations.
144  */
145
146 extern void dstr_putz(dstr */*d*/);
147
148 #define DPUTZ(d) do {                                                   \
149   DENSURE((d), 1);                                                      \
150   (d)->buf[(d)->len] = 0;                                               \
151 } while (0)
152
153 /* --- @dstr_puts@ --- *
154  *
155  * Arguments:   @dstr *d@ = pointer to a dynamic string block
156  *              @const char *s@ = pointer to string to append
157  *
158  * Returns:     ---
159  *
160  * Use:         Appends a character string to a string.  A trailing null
161  *              byte is added, as for @dstr_putz@.
162  */
163
164 extern void dstr_puts(dstr */*d*/, const char */*s*/);
165
166 #define DPUTS(d, s) do {                                                \
167   size_t sz = strlen(s);                                                \
168   DENSURE((d), sz + 1);                                                 \
169   memcpy((d)->buf + (d)->len, (s), sz + 1);                             \
170   (d)->len += sz;                                                       \
171 } while (0)
172
173 /* --- @dstr_putd@ --- *
174  *
175  * Arguments:   @dstr *d@ = pointer to a dynamic string block
176  *              @const dstr *s@ = pointer to a dynamic string to append
177  *
178  * Returns:     ---
179  *
180  * Use:         Appends a dynamic string to a string.  A trailing null
181  *              byte is added, as for @dstr_putz@.
182  */
183
184 extern void dstr_putd(dstr */*d*/, const dstr */*s*/);
185
186 #define DPUTD(d, s) do {                                                \
187   DENSURE((d), (s)->len + 1);                                           \
188   memcpy((d)->buf + (d)->len, (s)->buf, (s)->len);                      \
189   (d)->len += (s)->len;                                                 \
190   (d)->buf[(d)->len] = 0;                                               \
191 } while (0)
192
193 /* --- @dstr_putm@ --- *
194  *
195  * Arguments:   @dstr *d@ = pointer to a dynamic string block
196  *              @const void *p@ = pointer to a block to append
197  *              @size_t sz@ = size of the block
198  *
199  * Returns:     Appends an arbitrary data block to a string.  No trailing
200  *              null is appended.
201  */
202
203 extern void dstr_putm(dstr */*d*/, const void */*p*/, size_t /*sz*/);
204
205 #define DPUTM(d, p, sz) do {                                            \
206   DENSURE((d), (sz));                                                   \
207   memcpy((d)->buf + (d)->len, (p), (sz));                               \
208   (d)->len += (sz);                                                     \
209 } while (0)
210
211 /* --- @dstr_tidy@ --- *
212  *
213  * Arguments:   @dstr *d@ = pointer to a dynamic string block
214  *
215  * Returns:     ---
216  *
217  * Use:         Reduces the amount of memory used by a string.  A trailing
218  *              null byte is added, as for @dstr_putz@.
219  */
220
221 extern void dstr_tidy(dstr */*d*/);
222
223 /* --- @dstr_putline@ --- *
224  *
225  * Arguments:   @dstr *d@ = pointer to a dynamic string block
226  *              @FILE *fp@ = a stream to read from
227  *
228  * Returns:     The number of characters read into the buffer, or @EOF@ if
229  *              end-of-file was reached before any characters were read.
230  *
231  * Use:         Appends the next line from the given input stream to the
232  *              string.  A trailing newline is not added; a trailing null
233  *              byte is appended, as for @dstr_putz@.
234  */
235
236 extern int dstr_putline(dstr */*d*/, FILE */*fp*/);
237
238 /* --- @dstr_write@ --- *
239  *
240  * Arguments:   @dstr *d@ = pointer to a dynamic string block
241  *              @FILE *fp@ = a stream to write on
242  *
243  * Returns:     The number of bytes written (as for @fwrite@).
244  *
245  * Use:         Writes a dynamic string to a file.
246  */
247
248 extern size_t dstr_write(dstr */*d*/, FILE */*fp*/);
249
250 /*----- That's all, folks -------------------------------------------------*/
251
252 #ifdef __cplusplus
253   }
254 #endif
255
256 #endif