From 3522c68dcd38e3eef393771d65aff6de2f815da3 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Sat, 21 Dec 2013 23:09:09 +0100 Subject: rb: add ascii support in order to be able to deal with strings (i.e null terminated), we provide a new set of functions rb_puts rb_printf / rb_vprintf rb_gets / rb_gets2 --- rb_str.h | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 rb_str.h (limited to 'rb_str.h') diff --git a/rb_str.h b/rb_str.h new file mode 100644 index 0000000..f48d685 --- /dev/null +++ b/rb_str.h @@ -0,0 +1,54 @@ +#ifndef RB_STR_H +#define RB_STR_H + +#include +#include + +#include "rb.h" + +/* + * locate in the ring buffer pointed to by rb the first string ending by the + * string pointed to by string and return it. the string returned is + * allocated using malloc and should be freed by the developer. + * if no such delimiter can be found, NULL is returned and the ring buffer + * is left unmodified. Otherwise, the string is removed from it. + * nb: the delimiter is discarded in the returned string + * + * common use case: + * request = rb_gets(rb, "\r\n") + */ +char *rb_gets(t_rb *rb, const char *delim); + +/* + * same as the above function but takes a null terminated array of delimiters + * the function will return the shortest string matching the criteria. + * if two or more delimiters are found at the same place in the ring buffer, + * the longest will be discarded + * + * common use case: + * request = rb_gets(rb, (const char *[]){"\n", "\r", "\r\n"}) + * which will return a request disregarding whether netcat or telnet is used + */ +char *rb_gets2(t_rb *rb, const char *const *delim); + +/* + * this functions writes str to the ring buffer pointed by rb. If the ring + * buffer is not large enough to contain all the data, the beginning will be + * overriden. However, a buffer overflow is not susceptible to happen. + */ +static inline t_rb *rb_puts(t_rb *rb, const char *str) +{ + return rb_put(rb, str, strlen(str)); +} + +t_rb *rb_vprintf(t_rb *rb, const char *fmt, va_list list); + +/* + * function which behaves like sprintf but which writes into a ring buffer + * if the ring buffer is not large enough to contain all the data, + * the beginning will be overriden. However, a buffer overflow will not occur + */ + __attribute__((format(printf, 2, 3))) +t_rb *rb_printf(t_rb *rb, const char *fmt, ...); + +#endif /* RB_STR_H */ -- cgit v1.2.3