65 lines
1.0 KiB
C
65 lines
1.0 KiB
C
#include "strbuf.h"
|
|
|
|
StrBuf *
|
|
StrBuf_init(Arena *a)
|
|
{
|
|
StrBuf *l = (StrBuf*)aalloc(a, sizeof(StrBuf));
|
|
if (!l) return nil;
|
|
|
|
l->head = nil;
|
|
l->tail = nil;
|
|
l->count = 0;
|
|
|
|
return l;
|
|
}
|
|
|
|
void *
|
|
StrBuf_append(Arena *a, StrBuf *buf, char *str)
|
|
{
|
|
u32 length = slen(str);
|
|
|
|
void *dest;
|
|
void *ptr = aalloc(a, sizeof(Node) + length);
|
|
Node *node = (Node *)ptr;
|
|
|
|
if (!node) return nil;
|
|
|
|
node->next = nil;
|
|
node->size = length;
|
|
|
|
if (!buf->head) {
|
|
buf->head = buf->tail = node;
|
|
} else {
|
|
buf->tail->next = node;
|
|
buf->tail = node;
|
|
}
|
|
|
|
buf->count++;
|
|
|
|
dest = node_value(node);
|
|
if (str && length > 0) {
|
|
mcpy(dest, str, length);
|
|
}
|
|
|
|
return dest;
|
|
}
|
|
|
|
char *
|
|
StrBuf_toS(Arena *a, StrBuf *buf)
|
|
{
|
|
Node *curr;
|
|
char *tmp_str;
|
|
|
|
char *str = (char *)aend(a);
|
|
|
|
if (!buf || !str) return nil;
|
|
|
|
curr = buf->head;
|
|
while (curr) {
|
|
tmp_str = node_value(curr);
|
|
amcpy(a, tmp_str, curr->size);
|
|
curr = curr->next;
|
|
}
|
|
|
|
return str;
|
|
} |