b6b9d458 |
1 | .\" -*-nroff-*- |
2 | .de VS |
3 | .sp 1 |
d66d7727 |
4 | .RS |
b6b9d458 |
5 | .nf |
6 | .ft B |
7 | .. |
8 | .de VE |
9 | .ft R |
10 | .fi |
11 | .RE |
12 | .sp 1 |
13 | .. |
08da152e |
14 | .de hP |
b6b9d458 |
15 | .IP |
16 | .ft B |
17 | \h'-\w'\\$1\ 'u'\\$1\ \c |
18 | .ft P |
19 | .. |
20 | .ie t .ds o \(bu |
21 | .el .ds o o |
08da152e |
22 | .TH dstr 3 "8 May 1999" "mLib" |
b6b9d458 |
23 | dstr \- a simple dynamic string type |
08da152e |
24 | .\" @dstr_create |
25 | .\" @dstr_destroy |
26 | .\" @dstr_reset |
27 | .\" @dstr_ensure |
28 | .\" @dstr_tidy |
29 | .\" |
30 | .\" @dstr_putc |
31 | .\" @dstr_putz |
32 | .\" @dstr_puts |
33 | .\" @dstr_putf |
34 | .\" @dstr_putd |
35 | .\" @dstr_putm |
36 | .\" @dstr_putline |
37 | .\" @dstr_write |
38 | .\" |
39 | .\" @DCREATE |
40 | .\" @DDESTROY |
41 | .\" @DRESET |
42 | .\" @DENSURE |
43 | .\" @DPUTC |
44 | .\" @DPUTZ |
45 | .\" @DPUTS |
46 | .\" @DPUTD |
47 | .\" @DPUTM |
48 | .\" @DWRITE |
49 | .\" |
b6b9d458 |
50 | .SH SYNOPSIS |
51 | .nf |
52 | .B "#include <mLib/dstr.h>" |
53 | |
54 | .BI "void dstr_create(dstr *" d ); |
55 | .BI "void dstr_destroy(dstr *" d ); |
56 | .BI "void dstr_reset(dstr *" d ); |
57 | |
58 | .BI "void dstr_ensure(dstr *" d ", size_t " sz ); |
59 | .BI "void dstr_tidy(dstr *" d ); |
60 | |
61 | .BI "void dstr_putc(dstr *" d ", char " ch ); |
62 | .BI "void dstr_putz(dstr *" d ); |
63 | .BI "void dstr_puts(dstr *" d ", const char *" s ); |
64 | .BI "int dstr_vputf(dstr *" d ", va_list " ap ); |
d2a91066 |
65 | .BI "int dstr_putf(dstr *" d ", ...);" |
b6b9d458 |
66 | .BI "void dstr_putd(dstr *" d ", const dstr *" p ); |
67 | .BI "void dstr_putm(dstr *" d ", const void *" p ", size_t " sz ); |
68 | .BI "int dstr_putline(dstr *" d ", FILE *" fp ); |
69 | .BI "size_t dstr_write(const dstr *" d ", FILE *" fp ); |
70 | |
71 | .BI "void DCREATE(dstr *" d ); |
72 | .BI "void DDESTROY(dstr *" d ); |
73 | .BI "void DRESET(dstr *" d ); |
74 | .BI "void DENSURE(dstr *" d ", size_t " sz ); |
08da152e |
75 | .BI "void DPUTC(dstr *" c ", char " ch ); |
b6b9d458 |
76 | .BI "void DPUTZ(dstr *" d ); |
77 | .BI "void DPUTS(dstr *" d ", const char *" s ); |
78 | .BI "void DPUTD(dstr *" d ", const dstr *" p ); |
79 | .BI "void DPUTM(dstr *" d ", const void *" p ", size_t " sz ); |
80 | .BI "size_t DWRITE(const dstr *" d ", FILE *" fp ); |
81 | .fi |
82 | .SH SUMMARY |
83 | The header |
84 | .B dstr.h |
85 | declares a type for representing dynamically extending strings, and a |
86 | small collection of useful operations on them. None of the operations |
87 | returns a failure result on an out-of-memory condition; instead, the |
88 | exception |
89 | .B EXC_NOMEM |
90 | is raised. |
91 | .PP |
92 | Many of the functions which act on dynamic strings have macro |
93 | equivalents. These equivalent macros may evaluate their arguments |
94 | multiple times. |
95 | .SH "UNDERLYING TYPE" |
96 | A |
97 | .B dstr |
98 | object is a small structure with the following members: |
99 | .VS |
100 | typedef struct dstr { |
101 | char *buf; /* Pointer to string buffer */ |
102 | size_t sz; /* Size of the buffer */ |
103 | size_t len; /* Length of the string */ |
104 | } dstr; |
105 | .VE |
106 | The |
107 | .B buf |
108 | member points to the actual character data in the string. The data may |
109 | or may not be null terminated, depending on what operations have |
110 | recently been performed on it. None of the |
111 | .B dstr |
112 | functions depend on the string being null-terminated; indeed, all of |
113 | them work fine on strings containing arbitrary binary data. You can |
114 | force null-termination by calling the |
115 | .B dstr_putz |
116 | function, or the |
117 | .B DPUTZ |
118 | macro. |
119 | .PP |
120 | The |
121 | .B sz |
122 | member describes the current size of the buffer. This reflects the |
123 | maximum possible length of string that can be represented in |
124 | .B buf |
125 | without allocating a new buffer. |
126 | .PP |
127 | The |
128 | .B len |
129 | member describes the current length of the string. It is the number of |
130 | bytes in the string which are actually interesting. The length does |
131 | .I not |
132 | include a null-terminating byte, if there is one. |
133 | .PP |
134 | The following invariants are maintained by |
135 | .B dstr |
136 | and must hold when any function is called: |
08da152e |
137 | .hP \*o |
b6b9d458 |
138 | If |
139 | .B sz |
140 | is nonzero, then |
141 | .B buf |
142 | points to a block of memory of length |
143 | .BR sz . |
144 | If |
145 | .B sz |
146 | is zero, then |
147 | .B buf |
148 | is a null pointer. |
08da152e |
149 | .hP \*o |
b6b9d458 |
150 | At all times, |
151 | .BI sz " >= " len\fR. |
152 | .PP |
d2a91066 |
153 | Note that there is no equivalent of the standard C distinction between |
b6b9d458 |
154 | the empty string (a pointer to an array of characters whose first |
d2a91066 |
155 | element is zero) and the nonexistent string (a null pointer). Any |
b6b9d458 |
156 | .B dstr |
157 | whose |
158 | .B len |
159 | is zero is an empty string. |
160 | .SH "CREATION AND DESTRUCTION" |
161 | The caller is responsible for allocating the |
162 | .B dstr |
163 | structure. It can be initialized in any of the following ways: |
08da152e |
164 | .hP \*o |
b6b9d458 |
165 | Using the macro |
166 | .B DSTR_INIT |
167 | as an initializer in the declaration of the object. |
08da152e |
168 | .hP \*o |
b6b9d458 |
169 | Passing its address to the |
170 | .B dstr_create |
171 | function. |
08da152e |
172 | .hP \*o |
b6b9d458 |
173 | Passing its address to the (equivalent) |
174 | .B DCREATE |
175 | macro. |
176 | .PP |
177 | The initial value of a |
178 | .B dstr |
179 | is the empty string. |
180 | .PP |
181 | The additional storage space for a string's contents may be reclaimed by |
182 | passing it to the |
183 | .B dstr_destroy |
184 | function, or the |
185 | .B DDESTROY |
186 | macro. After destruction, a string's value is reset to the empty |
187 | string: |
188 | .I "it's still a valid" |
189 | .BR dstr . |
190 | However, once a string has been destroyed, it's safe to deallocate the |
191 | underlying |
192 | .B dstr |
193 | object. |
194 | .PP |
195 | The |
196 | .B dstr_reset |
197 | function empties a string |
198 | .I without |
199 | deallocating any memory. Therefore appending more characters is quick, |
d2a91066 |
200 | because the old buffer is still there and doesn't need to be allocated. |
b6b9d458 |
201 | Calling |
202 | .VS |
203 | dstr_reset(d); |
204 | .VE |
d2a91066 |
205 | is equivalent to directly assigning |
b6b9d458 |
206 | .VS |
207 | d->len = 0; |
208 | .VE |
209 | There's also a macro |
210 | .B DRESET |
211 | which does the same job as the |
212 | .B dstr_reset |
213 | function. |
214 | .SH "EXTENDING A STRING" |
215 | All memory allocation for strings is done by the function |
216 | .BR dstr_ensure . |
217 | Given a pointer |
218 | .I d |
219 | to a |
220 | .B dstr |
221 | and a size |
222 | .IR sz , |
223 | the function ensures that there are at least |
224 | .I sz |
225 | unused bytes in the string's buffer. The current algorithm for |
226 | extending the buffer is fairly unsophisticated, but seems to work |
227 | relatively well \- see the source if you really want to know what it's |
228 | doing. |
229 | .PP |
230 | Extending a string never returns a failure result. Instead, if there |
231 | isn't enough memory for a longer string, the exception |
232 | .B EXC_NOMEM |
233 | is raised. See |
08da152e |
234 | .BR exc (3) |
b6b9d458 |
235 | for more information about |
236 | .BR mLib 's |
237 | exception handling system. |
238 | .PP |
239 | Note that if an ensure operation needs to reallocate a string buffer, |
240 | any pointers you've taken into the string become invalid. |
241 | .PP |
242 | There's a macro |
243 | .B DENSURE |
244 | which does a quick inline check to see whether there's enough space in |
245 | a string's buffer. This saves a procedure call when no reallocation |
246 | needs to be done. The |
247 | .B DENSURE |
248 | macro is called in the same way as the |
249 | .B dstr_ensure |
250 | function. |
251 | .PP |
252 | The function |
253 | .B dstr_tidy |
254 | `trims' a string's buffer so that it's just large enough for the string |
255 | contents and a null terminating byte. This might raise an exception due |
256 | to lack of memory. (There are two possible ways this might happen. |
d2a91066 |
257 | Firstly, the underlying allocator might just be brain-damaged enough to |
b6b9d458 |
258 | fail on reducing a block's size. Secondly, tidying an empty string with no |
259 | buffer allocated for it causes allocation of a buffer large enough for |
260 | the terminating null byte.) |
261 | .SH "CONTRIBUTING DATA TO A STRING" |
262 | There are a collection of functions which add data to a string. All of |
263 | these functions add their new data to the |
264 | .I end |
265 | of the string. This is good, because programs usually build strings |
266 | left-to-right. If you want to do something more clever, that's up to |
267 | you. |
268 | .PP |
269 | Several of these functions have equivalent macros which do the main work |
270 | inline. (There still might need to be a function call if the buffer |
271 | needs to be extended.) |
272 | .PP |
273 | Any of these functions might extend the string, causing pointers into |
274 | the string buffer to be invalidated. If you don't want that to happen, |
275 | pre-ensure enough space before you start. |
276 | .PP |
277 | The simplest function is |
278 | .B dstr_putc |
279 | which appends a single character |
280 | .I ch |
281 | to the end of the string. It has a macro equivalent called |
282 | .BR DPUTC . |
283 | .PP |
284 | The function |
285 | .B dstr_putz |
286 | places a zero byte at the end of the string. It does |
287 | .I not |
288 | affect the string's length, so any other data added to the string will |
289 | overwrite the null terminator. This is useful if you want to pass your |
290 | string to one of the standard C library string-handling functions. The |
291 | macro |
292 | .B DPUTZ |
293 | does the same thing. |
294 | .PP |
295 | The function |
296 | .B dstr_puts |
297 | writes a C-style null-terminated string to the end of a dynamic string. |
298 | A terminating zero byte is also written, as if |
299 | .B dstr_putz |
300 | were called. The macro |
301 | .B DPUTS |
302 | does the same job. |
303 | .PP |
304 | The function |
305 | .B dstr_putf |
306 | works similarly to the standard |
307 | .BR sprintf (3) |
308 | function. It accepts a |
309 | .BR print (3)-style |
310 | format string and an arbitrary number of arguments to format and writes |
311 | the resulting text to the end of a dynamic string, returning the number |
312 | of characters so written. A terminating zero byte is also appended. |
313 | The formatting is intended to be convenient and safe rather than |
314 | efficient, so don't expect blistering performance. Similarly, there may |
315 | be differences between the formatting done by |
316 | .B dstr_putf |
317 | and |
318 | .BR sprintf (3) |
319 | because the former has to do most of its work itself. In particular, |
320 | .B dstr_putf |
321 | doesn't (and probably never will) understand the |
322 | .RB ` n$ ' |
d2a91066 |
323 | positional parameter notation accepted by many Unix C libraries. There |
b6b9d458 |
324 | is no macro equivalent of |
325 | .BR dstr_putf . |
326 | .PP |
327 | The function |
328 | .B dstr_vputf |
329 | provides access to the `guts' of |
330 | .BR dstr_putf : |
331 | given a format string and a |
332 | .B va_list |
333 | pointer, it will format the arguments according to the format string, |
334 | just as |
335 | .B dstr_putf |
336 | does. |
337 | .PP |
338 | The function |
339 | .B dstr_putd |
340 | appends the contents of one dynamic string to another. A null |
341 | terminator is also appended. The macro |
342 | .B DPUTD |
343 | does the same thing. |
344 | .PP |
345 | The function |
346 | .B dstr_putm |
347 | puts an arbitrary block of memory, addressed by |
348 | .IR p , |
349 | with length |
350 | .I sz |
351 | bytes, at the end of a dynamic string. No terminating null is appended: |
352 | it's assumed that if you're playing with arbitrary chunks of memory then |
353 | you're probably not going to be using the resulting data as a normal |
354 | text string. The macro |
355 | .B DPUTM |
356 | works the same way. |
357 | .PP |
358 | The function |
359 | .B dstr_putline |
360 | reads a line from an input stream |
361 | .I fp |
362 | and appends it to a string. If an error occurs, or end-of-file is |
363 | encountered, before any characters have been read, then |
364 | .B dstr_putline |
365 | returns the value |
366 | .BR EOF. |
367 | Otherwise, it reads until it encounters a newline character, an error, |
368 | or end-of-file, and returns the number of characters read. If reading |
369 | was terminated by a newline character, the newline character is |
370 | .I not |
371 | inserted in the buffer. A terminating null is appended, as by |
372 | .BR dstr_putz . |
373 | .SH "OTHER FUNCTIONS" |
374 | The |
375 | .B dstr_write |
376 | function writes a string to an output stream |
377 | .IR fp . |
378 | It returns the number of characters written, or |
379 | .B 0 |
380 | if an error occurred before the first write. No newline character is |
381 | written to the stream, unless the string actually contains one already. |
382 | The macro |
383 | .B DWRITE |
384 | is equivalent. |
385 | .SH "SECURITY CONSIDERATIONS" |
d2a91066 |
386 | The implementation of the |
b6b9d458 |
387 | .B dstr |
388 | functions is designed to do string handling in security-critical |
389 | programs. However, there may be bugs in the code somewhere. In |
390 | particular, the |
391 | .B dstr_putf |
392 | functions is quite complicated, and could do with some checking by |
393 | independent people who know what they're doing. |
08da152e |
394 | .SH "SEE ALSO" |
395 | .BR exc (3), |
396 | .BR mLib (3). |
b6b9d458 |
397 | .SH AUTHOR |
398 | Mark Wooding, <mdw@nsict.org> |