1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
#include <malloc.h>
#include "rb_str.h"
typedef uint8_t byte;
static char *__rb_gets2(rb_t *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);
rb_remove(rb, (size_t)b_ptr - (size_t)data + b_len);
}
return ret;
}
char *rb_gets2(rb_t *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(rb_t *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);
rb_remove(rb, (size_t)ptr - (size_t)data + strlen(delimit));
}
free(data);
return ret;
}
rb_t *rb_vprintf(rb_t *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)))
rb_t *rb_printf(rb_t *rb, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
rb_vprintf(rb, fmt, ap);
va_end(ap);
return rb;
}
|