Skip to content

Commit

Permalink
buffer: add buffer_put0 to store a NUL terminated string
Browse files Browse the repository at this point in the history
  • Loading branch information
martanne committed Sep 15, 2015
1 parent f20ea43 commit e575957
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 5 additions & 1 deletion buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@ void buffer_release(Buffer *buf) {
bool buffer_put(Buffer *buf, const void *data, size_t len) {
if (!buffer_grow(buf, len))
return false;
memcpy(buf->data, data, len);
memmove(buf->data, data, len);
buf->len = len;
return true;
}

bool buffer_put0(Buffer *buf, const char *data) {
return buffer_put(buf, data, strlen(data)+1);
}

bool buffer_append(Buffer *buf, const void *data, size_t len) {
size_t rem = buf->size - buf->len;
if (len > rem && !buffer_grow(buf, buf->size + len - rem))
Expand Down
2 changes: 2 additions & 0 deletions buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ bool buffer_grow(Buffer*, size_t size);
void buffer_truncate(Buffer*);
/* replace buffer content with given data, growing the buffer if needed */
bool buffer_put(Buffer*, const void *data, size_t len);
/* same but with NUL-terminated data */
bool buffer_put0(Buffer*, const char *data);
/* append futher content to the end of the buffer data */
bool buffer_append(Buffer*, const void *data, size_t len);
/* append NUl-terminated data */
Expand Down

0 comments on commit e575957

Please sign in to comment.