From ee99d5d047080218bde02e2d06e0d14518fefa4f Mon Sep 17 00:00:00 2001 From: Dmitri Pal Date: Fri, 23 Jul 2010 19:33:04 -0400 Subject: Added a convenience function Added a function to add a string to the buffer. Allows adding const strings to the buffer. This solution eliminates all the hassle of type casting that was needed before. --- basicobjects/simplebuffer.c | 28 ++++++++++++++++++++++++++++ basicobjects/simplebuffer.h | 9 +++++++++ basicobjects/simplebuffer_ut.c | 18 ++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/basicobjects/simplebuffer.c b/basicobjects/simplebuffer.c index 8ddc628..b47d6b6 100644 --- a/basicobjects/simplebuffer.c +++ b/basicobjects/simplebuffer.c @@ -133,6 +133,34 @@ int simplebuffer_add_raw(struct simplebuffer *data, return error; } +/* Function to add string to the end of the buffer. */ +int simplebuffer_add_str(struct simplebuffer *data, + const char *str, + uint32_t len, + uint32_t block) +{ + + int error = EOK; + uint32_t size; + + TRACE_FLOW_ENTRY(); + + size = len + 1; + error = simplebuffer_grow(data, size, + ((block > size) ? block : size)); + if (error) { + TRACE_ERROR_NUMBER("Failed to grow buffer.", error); + return error; + } + + memcpy(data->buffer + data->length, str, len); + data->length += len; + data->buffer[data->length] = '\0'; + + TRACE_FLOW_EXIT(); + return error; +} + /* Finction to add CR to the buffer */ int simplebuffer_add_cr(struct simplebuffer *data) { diff --git a/basicobjects/simplebuffer.h b/basicobjects/simplebuffer.h index 002e8b6..4d02321 100644 --- a/basicobjects/simplebuffer.h +++ b/basicobjects/simplebuffer.h @@ -53,6 +53,15 @@ int simplebuffer_add_raw(struct simplebuffer *data, uint32_t len, uint32_t block); + +/* Function to add string to the buffer. + * Same as above just uses string as an argument. + */ +int simplebuffer_add_str(struct simplebuffer *data, + const char *str, + uint32_t len, + uint32_t block); + /* Finction to add CR to the buffer */ int simplebuffer_add_cr(struct simplebuffer *data); diff --git a/basicobjects/simplebuffer_ut.c b/basicobjects/simplebuffer_ut.c index f94a1c0..61272da 100644 --- a/basicobjects/simplebuffer_ut.c +++ b/basicobjects/simplebuffer_ut.c @@ -43,6 +43,7 @@ int simple_test(void) struct simplebuffer *data = NULL; char str1[] = "test string 1"; char str2[] = "test string 2"; + const char str3[] = "test string 3"; uint32_t left = 0; int i; const unsigned char *buf; @@ -82,6 +83,23 @@ int simple_test(void) return error; } + error = simplebuffer_add_cr(data); + if (error) { + printf("Failed to add CR to an object %d\n", error); + simplebuffer_free(data); + return error; + } + + error = simplebuffer_add_str(data, + str3, + strlen(str3), + 1); + if (error) { + printf("Failed to add string to an object %d\n", error); + simplebuffer_free(data); + return error; + } + left = simplebuffer_get_len(data); buf = simplebuffer_get_buf(data); -- cgit