2 dbuf_string.c - Append formatted string to the dynamic buffer
3 version 1.2.2, March 20th, 2012
5 Copyright (c) 2002-2012 Borut Razem
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 Boston, MA 02110-1301, USA.
28 #include "dbuf_string.h"
32 * Append string to the end of the buffer.
33 * The buffer is null terminated.
37 dbuf_append_str (struct dbuf_s
*dbuf
, const char *str
)
43 if (dbuf_append (dbuf
, str
, len
+ 1))
53 * Prepend string to the beginning of the buffer.
54 * The buffer is null terminated.
58 dbuf_prepend_str (struct dbuf_s
*dbuf
, const char *str
)
64 return (dbuf_prepend (dbuf
, str
, len
));
68 * Append single character to the end of the buffer.
69 * The buffer is null terminated.
73 dbuf_append_char (struct dbuf_s
*dbuf
, char chr
)
78 if (dbuf_append (dbuf
, buf
, 2))
88 * Prepend single character to the end of the buffer.
89 * The buffer is null terminated.
93 dbuf_prepend_char (struct dbuf_s
*dbuf
, char chr
)
98 return (dbuf_prepend_str (dbuf
, buf
));
102 * Append the formatted string to the end of the buffer.
103 * The buffer is null terminated.
107 dbuf_vprintf (struct dbuf_s
*dbuf
, const char *format
, va_list args
)
110 va_copy (args2
, args
);
111 int size
= vsnprintf (0, 0, format
, args2
) + 1;
114 assert (dbuf
!= NULL
);
115 assert (dbuf
->alloc
!= 0);
116 assert (dbuf
->buf
!= NULL
);
118 if (0 != _dbuf_expand (dbuf
, size
))
120 int len
= vsprintf (&(((char *)dbuf
->buf
)[dbuf
->len
]), format
, args
);
124 /* if written length is greater then the calculated one,
125 we have a buffer overrun! */
126 assert (len
<= size
);
137 * Append the formatted string to the end of the buffer.
138 * The buffer is null terminated.
142 dbuf_printf (struct dbuf_s
*dbuf
, const char *format
, ...)
147 va_start (arg
, format
);
148 len
= dbuf_vprintf (dbuf
, format
, arg
);
156 * Append line from file to the dynamic buffer
157 * The buffer is null terminated.
161 dbuf_getline (struct dbuf_s
*dbuf
, FILE *infp
)
166 while ((c
= getc (infp
)) != '\n' && c
!= EOF
)
170 dbuf_append (dbuf
, &chr
, 1);
173 /* add trailing NL */
178 dbuf_append (dbuf
, &chr
, 1);
181 /* terminate the line without increasing the length */
182 if (0 != _dbuf_expand (dbuf
, 1))
183 ((char *)dbuf
->buf
)[dbuf
->len
] = '\0';
185 return dbuf_get_length (dbuf
);
190 * Remove trailing newline from the string.
191 * The buffer is null terminated.
192 * It returns the total number of characters removed.
196 dbuf_chomp (struct dbuf_s
*dbuf
)
198 size_t i
= dbuf
->len
;
201 if (i
!= 0 && '\n' == ((char *)dbuf
->buf
)[i
- 1])
204 if (i
!= 0 && '\r' == ((char *)dbuf
->buf
)[i
- 1])
213 /* terminate the line without increasing the length */
214 if (_dbuf_expand(dbuf
, 1) != 0)
215 ((char *)dbuf
->buf
)[dbuf
->len
] = '\0';
222 * Write dynamic buffer to the file.
226 dbuf_write (struct dbuf_s
*dbuf
, FILE *dest
)
228 fwrite (dbuf_get_buf (dbuf
), 1, dbuf_get_length (dbuf
), dest
);
233 * Write dynamic buffer to the file and destroy it.
237 dbuf_write_and_destroy (struct dbuf_s
*dbuf
, FILE *dest
)
239 dbuf_write (dbuf
, dest
);