3 * The olsr.org Optimized Link-State Routing daemon(olsrd)
4 * Copyright (c) 2004-2009, the olsr.org team - see HISTORY file
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of olsr.org, olsrd nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 * Visit http://www.olsr.org for more information.
36 * If you find this software useful feel free to make a donation
37 * to the project. For more information see the website or contact
38 * the copyright holders.
42 #include "common/autobuf.h"
51 static int autobuf_enlarge(struct autobuf *autobuf, int new_size);
55 abuf_init(struct autobuf *autobuf, int initial_size)
58 if (initial_size <= 0) {
63 autobuf->size = ROUND_UP_TO_POWER_OF_2(initial_size, AUTOBUFCHUNK);
64 autobuf->buf = calloc(autobuf->size, 1);
65 if (autobuf->buf == NULL) {
74 abuf_free(struct autobuf *autobuf)
83 autobuf_enlarge(struct autobuf *autobuf, int new_size)
86 if (new_size > autobuf->size) {
88 int roundUpSize = ROUND_UP_TO_POWER_OF_2(new_size, AUTOBUFCHUNK);
89 p = realloc(autobuf->buf, roundUpSize);
92 WSASetLastError(ENOMEM);
100 memset(&autobuf->buf[autobuf->size], 0, roundUpSize - autobuf->size);
101 autobuf->size = roundUpSize;
107 abuf_vappendf(struct autobuf *autobuf, const char *format, va_list ap)
113 rc = vsnprintf(autobuf->buf + autobuf->len, autobuf->size - autobuf->len, format, ap);
115 min_size = autobuf->len + rc;
116 if (min_size >= autobuf->size) {
117 if (autobuf_enlarge(autobuf, min_size) < 0) {
118 autobuf->buf[autobuf->len] = '\0';
121 vsnprintf(autobuf->buf + autobuf->len, autobuf->size - autobuf->len, format, ap2);
124 autobuf->len = min_size;
129 abuf_appendf(struct autobuf *autobuf, const char *fmt, ...)
134 rv = abuf_vappendf(autobuf, fmt, ap);
140 abuf_puts(struct autobuf *autobuf, const char *s)
143 if (autobuf_enlarge(autobuf, autobuf->len + len + 1) < 0) {
146 strcpy(autobuf->buf + autobuf->len, s);
152 abuf_strftime(struct autobuf *autobuf, const char *format, const struct tm *tm)
154 int rc = strftime(autobuf->buf + autobuf->len, autobuf->size - autobuf->len, format, tm);
156 /* we had an error! Probably the buffer too small. So we add some bytes. */
157 if (autobuf_enlarge(autobuf, autobuf->size + AUTOBUFCHUNK) < 0) {
158 autobuf->buf[autobuf->len] = '\0';
161 rc = strftime(autobuf->buf + autobuf->len, autobuf->size - autobuf->len, format, tm);
168 abuf_memcpy(struct autobuf *autobuf, const void *p, const unsigned int len)
170 if (autobuf_enlarge(autobuf, autobuf->len + len) < 0) {
173 memcpy(autobuf->buf + autobuf->len, p, len);
179 abuf_memcpy_prefix(struct autobuf *autobuf, const void *p, const unsigned int len)
181 if (autobuf_enlarge(autobuf, autobuf->len + len) < 0) {
184 memmove(&autobuf->buf[len], autobuf->buf, autobuf->len);
185 memcpy(autobuf->buf, p, len);
191 abuf_pull(struct autobuf * autobuf, int len) {
195 if (len != autobuf->len) {
196 memmove(autobuf->buf, &autobuf->buf[len], autobuf->len - len);
200 newsize = ROUND_UP_TO_POWER_OF_2(autobuf->len + 1, AUTOBUFCHUNK);
201 p = realloc(autobuf->buf, newsize);
204 WSASetLastError(ENOMEM);
211 autobuf->size = newsize;
219 * indent-tabs-mode: nil