Skip to content

Commit

Permalink
added fbuf_detach() to the API
Browse files Browse the repository at this point in the history
to allow detaching an internal buffer from the fbuf
  • Loading branch information
xant committed May 22, 2014
1 parent d357c79 commit 19b524f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/fbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,26 @@ fbuf_clear(fbuf_t *fbuf)
fbuf->data[0] = '\0';
}

unsigned int fbuf_detach(fbuf_t *fbuf, char **buf)
{
if (!fbuf->used)
return 0;

if (fbuf->skip)
fbuf_shrink(fbuf);

unsigned int len = fbuf->used;

*buf = fbuf->data;

fbuf->used = 0;
fbuf->skip = 0;
fbuf->len = 0;
fbuf->data = NULL;

return len;
}

void
fbuf_destroy(fbuf_t *fbuf)
{
Expand Down
8 changes: 8 additions & 0 deletions src/fbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ unsigned int fbuf_shrink(fbuf_t *fbuf);
*/
void fbuf_clear(fbuf_t *fbuf);

/**
* @brief Detach the underlying buffer and reset the fbuf
* @param fbuf fbuf
* @param buf A reference to the pointer where to store the addres of the actual buffer
* @returns The size of the detached buffer (now stored in *buf)
*/
unsigned int fbuf_detach(fbuf_t *fbuf, char **buf);

/**
* @brief Destroys all information in the fbuf.
* @param fbuf fbuf
Expand Down

0 comments on commit 19b524f

Please sign in to comment.