int databuf_reset(DataBuf *db) { if (debug) databuf_print(db, 1, "databuf_reset() entry"); if (!(db->flags & DATABUF_FLAG_PRESERVE_HEAD)) return -1; db->offset = 0; db->len = MIN(db->alloc_size, db->max_len); if (debug) databuf_print(db, 1, "databuf_reset() exit"); return 1; }
int main(int argc, char **argv) { size_t size = 0; DataBuf buf; char *data; assert(databuf_init(&buf, size, DATABUF_FLAG_STRING)); databuf_print(&buf, 1, "after init size=%d", size); #if 1 data = "a"; assert(databuf_strcat(&buf, data)); databuf_print(&buf, 1, "after strcat(%s)", data); data = "bb"; assert(databuf_strcat(&buf, data)); databuf_print(&buf, 1, "after strcat(%s)", data); data = "ccc"; assert(databuf_strcat(&buf, data)); databuf_print(&buf, 1, "after strcat(%s)", data); #endif databuf_free(&buf); #if 0 assert(databuf_init(&buf, size, 0)); databuf_print(&buf, 1, "after init size=%d", size); size = 8; data = make_data(size, "a"); assert(databuf_append(&buf, data, size)); databuf_print(&buf, 1, "after append size=%d", size); assert(databuf_append(&buf, data, size)); free(data); databuf_print(&buf, 1, "after append size=%d", size); assert(databuf_advance(&buf, 4)); databuf_print(&buf, 1, "after databuf_advance(%d", 4); size = 5; data = make_data(size, "b"); assert(databuf_append(&buf, data, size)); free(data); databuf_print(&buf, 1, "after append size=%d", size); size = 7; data = make_data(size, "c"); assert(databuf_append(&buf, data, size)); free(data); databuf_print(&buf, 1, "after append size=%d", size); databuf_free(&buf); #endif exit(0); }
int databuf_append(DataBuf *db, const char *src, size_t src_size) { size_t new_size; DATABUF_VALIDATE(db); if (src == NULL || src_size == 0) return 0; new_size = db->len+src_size; #ifdef DEBUG if (debug) databuf_print(db, 1, "databuf_append() size=%zd", src_size); #endif if ((new_size > db->alloc_size) || ((db->flags & DATABUF_FLAG_PRESERVE_HEAD) && !databuf_tail_available(db, src_size))) { /* not enough room, we must realloc */ void *new_alloc; databuf_shift_data_to_beginning(db); if ((new_alloc = realloc(db->alloc_ptr, new_size))) { db->alloc_ptr = new_alloc; db->alloc_size = new_size; } else { return -1; /* realloc failed */ } } else { /* we can fit within current allocation, but can we append? */ if (!databuf_tail_available(db, src_size)) { /* we can't append in place, must create room at tail by shifting data forward to the beginning of the allocation block */ databuf_shift_data_to_beginning(db); } } #ifdef DEBUG if (debug) databuf_print(db, 1, "databuf_append() about to memmove()"); #endif /* pointers all set up and room availble, move the data and update */ memmove(databuf_end(db), src, src_size); db->len = new_size; db->max_len = MAX(db->max_len, new_size); #ifdef DEBUG if (debug) databuf_print(db, 1, "databuf_append() conclusion"); #endif DATABUF_VALIDATE(db); return 1; }
int databuf_advance(DataBuf *db, size_t advance) { size_t actual_advance; DATABUF_VALIDATE(db); if (debug) databuf_print(db, 1, "databuf_advance() enter, advance=%d", advance); actual_advance = MIN(advance, db->len); db->offset += actual_advance; db->len -= actual_advance; if (debug) databuf_print(db, 1, "databuf_advance() leave, actual_advance=%d", actual_advance); DATABUF_VALIDATE(db); if (advance == actual_advance) { return 1; } else { errno = ESPIPE; // Illegal seek return -1; } }