diff --git a/include/igloo/buffer.h b/include/igloo/buffer.h index 107b7a4..f9749e2 100644 --- a/include/igloo/buffer.h +++ b/include/igloo/buffer.h @@ -81,6 +81,8 @@ void igloo_buffer_preallocate(igloo_buffer_t *buffer, size_t request); int igloo_buffer_get_data(igloo_buffer_t *buffer, const void **data, size_t *length); /* Gets data as a string. The string is '\0'-terminated. + * igloo_ro_stringify(buffer) will also return this buffer. + * * Parameters: * buffery * The buffer to operate on. diff --git a/src/buffer.c b/src/buffer.c index 49e21c2..55679b4 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -31,10 +31,12 @@ struct igloo_buffer_tag { }; static void __free(igloo_ro_t self); +static char * __stringify(igloo_ro_t self); igloo_RO_PUBLIC_TYPE(igloo_buffer_t, igloo_RO_TYPEDECL_FREE(__free), - igloo_RO_TYPEDECL_NEW_NOOP() + igloo_RO_TYPEDECL_NEW_NOOP(), + igloo_RO_TYPEDECL_STRINGIFY(__stringify) ); static void __free(igloo_ro_t self) @@ -139,6 +141,20 @@ int igloo_buffer_get_string(igloo_buffer_t *buffer, const char **string) return 0; } +static char * __stringify(igloo_ro_t self) +{ + igloo_buffer_t *buffer = igloo_RO_TO_TYPE(self, igloo_buffer_t); + const char *ret; + + if (!buffer) + return NULL; + + if (igloo_buffer_get_string(buffer, &ret) != 0) + return NULL; + + return strdup(ret); +} + int igloo_buffer_set_length(igloo_buffer_t *buffer, size_t length) { if (!buffer)