summaryrefslogtreecommitdiff
path: root/src/rb_str.c
diff options
context:
space:
mode:
authorOlivier Gayot <duskcoder@gmail.com>2013-12-21 23:09:09 +0100
committerOlivier Gayot <duskcoder@gmail.com>2014-01-18 13:43:52 +0100
commit3522c68dcd38e3eef393771d65aff6de2f815da3 (patch)
treeabe4ba88238482dbfb6fdebcded3f5c3626d1c7d /src/rb_str.c
parent9f25faad5fe732e498942818dc45de78ce7f3766 (diff)
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
Diffstat (limited to 'src/rb_str.c')
-rw-r--r--src/rb_str.c120
1 files changed, 120 insertions, 0 deletions
diff --git a/src/rb_str.c b/src/rb_str.c
new file mode 100644
index 0000000..b80628f
--- /dev/null
+++ b/src/rb_str.c
@@ -0,0 +1,120 @@
+#include <malloc.h>
+
+#include "rb_str.h"
+
+typedef uint8_t byte;
+
+static char *__rb_gets2(t_rb *rb, const char *const *delim,
+ const byte *data, size_t size)
+{
+ char *ret = NULL;
+ char *b_ptr = NULL;
+ char *ptr;
+ size_t b_len; /* length of the current best delimiter */
+ size_t len;
+
+ for (; *delim != NULL; ++delim) {
+ len = strlen(*delim);
+ ptr = memmem(data, size, *delim, len);
+
+ if (ptr == NULL)
+ continue;
+
+ if ((b_ptr == NULL) || (ptr < b_ptr)
+ || ((ptr == b_ptr) && len > b_len))
+ {
+ b_ptr = ptr;
+ b_len = len;
+ }
+ }
+
+ if (b_ptr != NULL) {
+ ret = strndup((const char *)data, (size_t)b_ptr - (size_t)data);
+
+ /* TODO replace with an appropriate function */
+ rb_get(rb, NULL, (size_t)b_ptr - (size_t)data + b_len);
+ }
+
+ return ret;
+}
+
+char *rb_gets2(t_rb *rb, const char *const *delim)
+{
+ size_t size;
+ char *ret;
+ byte *data;
+
+ data = malloc((rb->size_filled) * sizeof(byte));
+
+ if (data == NULL) {
+ return NULL;
+ }
+
+ size = rb_peek(rb, data, rb->size_filled);
+
+ ret = __rb_gets2(rb, delim, data, size);
+
+ free(data);
+
+ return ret;
+}
+
+char *rb_gets(t_rb *rb, const char *delimit)
+{
+ char *data = malloc((rb->size_filled) * sizeof(char));
+
+ if (data == NULL) {
+ return NULL;
+ }
+
+ size_t size = rb_peek(rb, data, rb->size_filled);
+ void *ptr = memmem(data, size, delimit, strlen(delimit));
+ char *ret = NULL;
+
+ if (ptr != NULL) {
+ ret = strndup(data, (size_t)ptr - (size_t)data);
+ /*
+ * TODO replace by a function which only removes data from the
+ * ring buffer
+ */
+ rb_get(rb, data, (size_t)ptr - (size_t)data + strlen(delimit));
+ }
+
+ free(data);
+
+ return ret;
+}
+
+t_rb *rb_vprintf(t_rb *rb, const char *fmt, va_list ap)
+{
+ char *buffer;
+
+ if (vasprintf(&buffer, fmt, ap) < 0) {
+ /*
+ * TODO the manual says that the content of buffer is undefined,
+ * we should reconsider the use of vasprintf()
+ */
+
+ return rb;
+ }
+
+ rb_puts(rb, buffer);
+
+ free(buffer);
+
+ return rb;
+}
+
+ __attribute__((format(printf, 2, 3)))
+t_rb *rb_printf(t_rb *rb, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+
+ rb_vprintf(rb, fmt, ap);
+
+ va_end(ap);
+
+ return rb;
+}