]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: buffers: add a few functions to write chars, strings and blocks
authorWilly Tarreau <w@1wt.eu>
Fri, 28 Sep 2012 14:02:48 +0000 (16:02 +0200)
committerWilly Tarreau <w@1wt.eu>
Thu, 4 Oct 2012 20:26:09 +0000 (22:26 +0200)
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

index ae4e6804e6d2b9faff1cb9b0248c1dabca0db118..6ad16c617b29b433fc6994ced276e686227124c8 100644 (file)
@@ -26,6 +26,7 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include <common/chunk.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));
 }
 
+/* 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 */
 
 /*