Skip to content

Commit

Permalink
Functions to append/prepend with printf formatting
Browse files Browse the repository at this point in the history
Add buffer_appendf and bugger_prependf to allow the
user to append or prepend a string formatted with
printf-style syntax.

Fixes #3
  • Loading branch information
marcomorain committed Dec 29, 2014
1 parent d3012fa commit cf700d0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
45 changes: 45 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,50 @@ buffer_resize(buffer_t *self, size_t n) {
return 0;
}

/*
* Append or prepend a formatted string to the buffer.
*/
static int buffer_format_helper(buffer_t *self, const char* format, va_list ap,
int front) {
char *formatted;
int result, bytes_formatted;
bytes_formatted = vasprintf(&formatted, format, ap);
if (bytes_formatted < 0) {
return -1;
}

if (front) {
result = buffer_prepend(self, formatted);
} else {
result = buffer_append(self, formatted);
}
free(formatted);
return result;
}

/*
* 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 ret = buffer_format_helper(self, format, ap, 0);
va_end(ap);
return ret;
}

/*
* Prepend a printf-style formatted string to the buffer.
*/
int buffer_prependf(buffer_t *self, const char *format, ...) {
va_list ap;
va_start(ap, format);
const int ret = buffer_format_helper(self, format, ap, 1);
va_end(ap);
return ret;
}


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

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

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
11 changes: 11 additions & 0 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ 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);
result = buffer_prependf(buf, "0x%08X - ", 0xdeadbeef);
assert(0 == result);
equal("0xDEADBEEF - 3 cow", 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 +241,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 cf700d0

Please sign in to comment.