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.
This commit is contained in:
Willy Tarreau 2012-09-28 16:02:48 +02:00
parent c39b0d17f2
commit 8c89c2059f

View File

@ -26,6 +26,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <common/chunk.h>
#include <common/config.h> #include <common/config.h>
@ -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)); return buffer_replace2(b, pos, end, str, strlen(str));
} }
/* Tries to write char <c> into output data at buffer <b>. 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 <blk> into output data at buffer <b>. 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 <str> into output data at buffer <b>. 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 <chk> into output data at buffer <b>. 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 */ #endif /* _COMMON_BUFFER_H */
/* /*