-
Notifications
You must be signed in to change notification settings - Fork 3
/
string_buf.c
95 lines (77 loc) · 1.86 KB
/
string_buf.c
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* Copyright (c) 2017, Yuxuan Shui <[email protected]> */
#include <string.h>
#include <stdlib.h>
#include "common.h"
#include <deai/helper.h>
#include "string_buf.h"
struct string_buf {
struct string_buf_node *head;
struct string_buf_node **tail;
};
struct string_buf_node {
char *str;
size_t len;
struct string_buf_node *next;
};
static inline char *strldup(const char *src, size_t *len) {
*len = strlen(src);
return strdup(src);
}
void string_buf_push(struct string_buf *buf, const char *str) {
auto n = tmalloc(struct string_buf_node, 1);
n->str = strldup(str, &n->len);
*buf->tail = n;
buf->tail = &n->next;
}
void string_buf_lpush(struct string_buf *buf, const char *str, size_t len) {
auto n = tmalloc(struct string_buf_node, 1);
n->str = strndup(str, len);
n->len = len;
*buf->tail = n;
buf->tail = &n->next;
}
bool string_buf_is_empty(struct string_buf *buf) {
return !buf->head;
}
void string_buf_clear(struct string_buf *buf) {
auto tmp = buf->head;
while(tmp) {
auto next = tmp->next;
free(tmp->str);
free(tmp);
tmp = next;
}
buf->head = NULL;
buf->tail = &buf->head;
}
char *string_buf_dump(struct string_buf *buf) {
size_t len = 0;
struct string_buf_node *tmp = buf->head;
while(tmp) {
len += strlen(tmp->str);
tmp = tmp->next;
}
char *ret = malloc(len+1);
char *pos = ret;
tmp = buf->head;
while(tmp) {
memcpy(pos, tmp->str, tmp->len);
pos += tmp->len;
auto next = tmp->next;
free(tmp->str);
free(tmp);
tmp = next;
}
buf->head = NULL;
buf->tail = &buf->head;
*pos = 0;
return ret;
}
struct string_buf *string_buf_new(void) {
auto ret = tmalloc(struct string_buf, 1);
ret->tail = &ret->head;
return ret;
}