From 8c89c2059fa4dace2e0c74cbb009c5a94d054fc1 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Fri, 28 Sep 2012 16:02:48 +0200 Subject: [PATCH] MINOR: buffers: add a few functions to write chars, strings and blocks bo_put{chr,blk,str,chk} are used to write data on the output of a buffer. Output is truncated if the buffer is not large enough. --- include/common/buffer.h | 55 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/include/common/buffer.h b/include/common/buffer.h index ae4e6804e..6ad16c617 100644 --- a/include/common/buffer.h +++ b/include/common/buffer.h @@ -26,6 +26,7 @@ #include #include +#include #include @@ -352,6 +353,60 @@ static inline int buffer_replace(struct buffer *b, char *pos, char *end, const c return buffer_replace2(b, pos, end, str, strlen(str)); } +/* Tries to write char into output data at buffer . Supports wrapping. + * Data are truncated if buffer is full. + */ +static inline void bo_putchr(struct buffer *b, char c) +{ + if (buffer_len(b) == b->size) + return; + *b->p = c; + b->p = b_ptr(b, 1); + b->o++; +} + +/* Tries to copy block into output data at buffer . Supports wrapping. + * Data are truncated if buffer is too short. + */ +static inline void bo_putblk(struct buffer *b, const char *blk, int len) +{ + int cur_len = buffer_len(b); + int half; + + if (len > b->size - cur_len) + len = (b->size - cur_len); + if (!len) + return; + + half = buffer_contig_space(b); + if (half > len) + half = len; + + memcpy(b->p, blk, half); + b->p = b_ptr(b, half); + if (len > half) { + memcpy(b->p, blk, len - half); + b->p = b_ptr(b, half); + } + b->o += len; +} + +/* Tries to copy string into output data at buffer . Supports wrapping. + * Data are truncated if buffer is too short. + */ +static inline void bo_putstr(struct buffer *b, const char *str) +{ + return bo_putblk(b, str, strlen(str)); +} + +/* Tries to copy chunk into output data at buffer . Supports wrapping. + * Data are truncated if buffer is too short. + */ +static inline void bo_putchk(struct buffer *b, const struct chunk *chk) +{ + return bo_putblk(b, chk->str, chk->len); +} + #endif /* _COMMON_BUFFER_H */ /*