summaryrefslogtreecommitdiff
path: root/src/bin/sb.c
diff options
context:
space:
mode:
authorBoris Faure <billiob@gmail.com>2016-11-06 11:42:17 +0100
committerBoris Faure <billiob@gmail.com>2016-11-06 11:42:17 +0100
commitd47b350e8cbcb4766f9278cf7532439f5631cfca (patch)
tree8019cd6d39bb04af8be939dc6c7c7bcb0b8282f3 /src/bin/sb.c
parentfbe747fbfdda338fd176e62d9e876512605b4de3 (diff)
ty: add sb.{c,h} (was in termio.c)
Diffstat (limited to 'src/bin/sb.c')
-rw-r--r--src/bin/sb.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/bin/sb.c b/src/bin/sb.c
new file mode 100644
index 0000000..e780b36
--- /dev/null
+++ b/src/bin/sb.c
@@ -0,0 +1,45 @@
1#include "private.h"
2#include "sb.h"
3
4#include <stdlib.h>
5#include <stddef.h>
6#include <string.h>
7
8int
9ty_sb_add(struct ty_sb *sb, const char *s, size_t len)
10{
11 size_t new_len = sb->len + len;
12
13 if ((new_len >= sb->alloc) || !sb->buf)
14 {
15 size_t new_alloc = ((new_len + 15) / 16) * 24;
16 char *new_buf;
17
18 new_buf = realloc(sb->buf, new_alloc);
19 if (new_buf == NULL)
20 return -1;
21 sb->buf = new_buf;
22 sb->alloc = new_alloc;
23 }
24 memcpy(sb->buf + sb->len, s, len);
25 sb->len += len;
26 sb->buf[sb->len] = '\0';
27 return 0;
28}
29
30/* unlike eina_strbuf_rtrim, only trims \t, \f, ' ' */
31void
32ty_sb_spaces_rtrim(struct ty_sb *sb)
33{
34 if (!sb->buf)
35 return;
36
37 while (sb->len > 0)
38 {
39 char c = sb->buf[sb->len - 1];
40 if ((c != ' ') && (c != '\t') && (c != '\f'))
41 break;
42 sb->len--;
43 }
44 sb->buf[sb->len] = '\0';
45}