Skip to content

Commit

Permalink
utils: new write_str_buf() helper function
Browse files Browse the repository at this point in the history
Signed-off-by: Eduardo Silva <[email protected]>
  • Loading branch information
edsiper committed Oct 12, 2017
1 parent 59b4943 commit bc7060b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/fluent-bit/flb_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ void flb_utils_bytes_to_human_readable_size(size_t bytes,
int flb_utils_time_split(char *time, int *sec, long *nsec);
int flb_utils_write_str(char *buf, int *off, size_t size,
char *str, size_t str_len);
int flb_utils_write_str_buf(char *str, size_t str_len, char **out, size_t *out_size);

#endif
40 changes: 40 additions & 0 deletions src/flb_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -619,3 +619,43 @@ int flb_utils_write_str(char *buf, int *off, size_t size,
*off += written;
return FLB_TRUE;
}


int flb_utils_write_str_buf(char *str, size_t str_len, char **out, size_t *out_size)
{
int ret;
int off;
char *tmp;
char *buf;
size_t s;

s = str_len + 1;
buf = flb_malloc(s);
if (!buf) {
flb_errno();
return -1;
}

while (1) {
off = 0;
ret = flb_utils_write_str(buf, &off, s, str, str_len);
if (ret == FLB_FALSE) {
s += 256;
tmp = flb_realloc(buf, s);
if (!tmp) {
flb_errno();
flb_free(buf);
return -1;
}
buf = tmp;
}
else {
/* done */
break;
}
}

*out = buf;
*out_size = off;
return 0;
}

0 comments on commit bc7060b

Please sign in to comment.