/*
dbuf.c - Dynamic buffer implementation
- version 1.1.0, March 28th, 2003
+ version 1.1.3, December 17th, 2006
- Copyright (c) 2003 Borut Razem
+ Copyright (c) 2002-2006 Borut Razem
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
if (dbuf->len + size > dbuf->alloc) {
/* new_allocated_size = current_allocated_size * 2^n */
/* can this be optimized? */
- while (dbuf->len + size >= dbuf->alloc)
+ do {
dbuf->alloc += dbuf->alloc;
+ }
+ while (dbuf->len + size > dbuf->alloc);
if ((dbuf->buf = realloc(dbuf->buf, dbuf->alloc)) == NULL)
return 0;
{
assert(dbuf != NULL);
assert(dbuf->alloc != 0);
- assert(size < dbuf->len);
+ assert(size <= dbuf->len);
if (size <= dbuf->len) {
dbuf->len = size;
assert(dbuf->alloc != 0);
assert(dbuf->buf != NULL);
- /* only if not already null terminated */
- if (((char *)dbuf->buf)[dbuf->len] != '\0') {
- dbuf_expand(dbuf, 1);
- ((char *)dbuf->buf)[dbuf->len] = '\0';
- }
+ dbuf_expand(dbuf, 1);
+ ((char *)dbuf->buf)[dbuf->len] = '\0';
return dbuf->buf;
}