Skip to content

Commit

Permalink
Merge pull request #10 from marcomorain/append-printf
Browse files Browse the repository at this point in the history
Functions to append/prepend with printf formatting
  • Loading branch information
stephenmathieson committed Jan 5, 2015
2 parents d3012fa + 2374425 commit 882d2ee
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
30 changes: 30 additions & 0 deletions buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
Expand Down Expand Up @@ -139,6 +140,35 @@ buffer_resize(buffer_t *self, size_t n) {
return 0;
}

/*
* Append a printf-style formatted string to the buffer.
*/
int buffer_appendf(buffer_t *self, const char *format, ...) {
va_list ap;
va_start(ap, format);
const int initial_len = buffer_length(self);

// First, we compute how many bytes are needed for the formatted string
// and allocate that much more space in the buffer.
va_list tmpa;
va_copy(tmpa, ap);
const int space_required = vsnprintf(NULL, 0, format, tmpa);
va_end(tmpa);
const int resized = buffer_resize(self, initial_len + space_required);
if (resized == -1) {
va_end(ap);
return -1;
}

// Next format the string into the space that we have made room for.
char* dst = self->data + initial_len;
const int bytes_formatted = vsnprintf(dst, 1+space_required, format, ap);

printf("Result '%s'\n", self->data);
va_end(ap);
return (bytes_formatted < 0) ? -1 : 0;
}

/*
* Append `str` to `self` and return 0 on success, -1 on failure.
*/
Expand Down
3 changes: 3 additions & 0 deletions buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ buffer_prepend(buffer_t *self, char *str);
int
buffer_append(buffer_t *self, const char *str);

int
buffer_appendf(buffer_t *self, const char *format, ...);

int
buffer_append_n(buffer_t *self, const char *str, size_t len);

Expand Down
12 changes: 12 additions & 0 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,17 @@ test_buffer_equals() {
buffer_free(b);
}

void test_buffer_formatting() {
buffer_t *buf = buffer_new();
int result = buffer_appendf(buf, "%d %s", 3, "cow");
assert(0 == result);
equal("3 cow", buffer_string(buf));
result = buffer_appendf(buf, " - 0x%08X", 0xdeadbeef);
assert(0 == result);
equal("3 cow - 0xDEADBEEF", buffer_string(buf));
buffer_free(buf);
}

void
test_buffer_indexof() {
buffer_t *buf = buffer_new_with_copy("Tobi is a ferret");
Expand Down Expand Up @@ -231,6 +242,7 @@ main(){
test_buffer_slice__end();
test_buffer_slice__end_overflow();
test_buffer_equals();
test_buffer_formatting();
test_buffer_indexof();
test_buffer_fill();
test_buffer_clear();
Expand Down

0 comments on commit 882d2ee

Please sign in to comment.