struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / util / dbuf_string.c
blob972a722e3b7b0ada5034019205285dbfd06cfb32
1 /*
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)
10 any later version.
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.
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <assert.h>
28 #include "dbuf_string.h"
32 * Append string to the end of the buffer.
33 * The buffer is null terminated.
36 int
37 dbuf_append_str (struct dbuf_s *dbuf, const char *str)
39 size_t len;
40 assert (str != NULL);
42 len = strlen (str);
43 if (dbuf_append (dbuf, str, len + 1))
45 --dbuf->len;
46 return 1;
48 else
49 return 0;
53 * Prepend string to the beginning of the buffer.
54 * The buffer is null terminated.
57 int
58 dbuf_prepend_str (struct dbuf_s *dbuf, const char *str)
60 size_t len;
61 assert (str != NULL);
63 len = strlen (str);
64 return (dbuf_prepend (dbuf, str, len));
68 * Append single character to the end of the buffer.
69 * The buffer is null terminated.
72 int
73 dbuf_append_char (struct dbuf_s *dbuf, char chr)
75 char buf[2];
76 buf[0] = chr;
77 buf[1] = '\0';
78 if (dbuf_append (dbuf, buf, 2))
80 --dbuf->len;
81 return 1;
83 else
84 return 0;
88 * Prepend single character to the end of the buffer.
89 * The buffer is null terminated.
92 int
93 dbuf_prepend_char (struct dbuf_s *dbuf, char chr)
95 char buf[2];
96 buf[0] = chr;
97 buf[1] = '\0';
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)
109 va_list args2;
110 va_copy (args2, args);
111 int size = vsnprintf (0, 0, format, args2) + 1;
112 va_end (args2);
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);
122 if (len >= 0)
124 /* if written length is greater then the calculated one,
125 we have a buffer overrun! */
126 assert (len <= size);
127 dbuf->len += len;
129 return len;
132 return 0;
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, ...)
144 va_list arg;
145 int len;
147 va_start (arg, format);
148 len = dbuf_vprintf (dbuf, format, arg);
149 va_end (arg);
151 return len;
156 * Append line from file to the dynamic buffer
157 * The buffer is null terminated.
160 size_t
161 dbuf_getline (struct dbuf_s *dbuf, FILE *infp)
163 int c;
164 char chr;
166 while ((c = getc (infp)) != '\n' && c != EOF)
168 chr = c;
170 dbuf_append (dbuf, &chr, 1);
173 /* add trailing NL */
174 if (c == '\n')
176 chr = c;
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.
195 size_t
196 dbuf_chomp (struct dbuf_s *dbuf)
198 size_t i = dbuf->len;
199 size_t ret;
201 if (i != 0 && '\n' == ((char *)dbuf->buf)[i - 1])
203 --i;
204 if (i != 0 && '\r' == ((char *)dbuf->buf)[i - 1])
206 --i;
210 ret = dbuf->len - i;
211 dbuf->len = i;
213 /* terminate the line without increasing the length */
214 if (_dbuf_expand(dbuf, 1) != 0)
215 ((char *)dbuf->buf)[dbuf->len] = '\0';
217 return ret;
222 * Write dynamic buffer to the file.
225 void
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.
236 void
237 dbuf_write_and_destroy (struct dbuf_s *dbuf, FILE *dest)
239 dbuf_write (dbuf, dest);
241 dbuf_destroy (dbuf);