70 lines
1.2 KiB
C
70 lines
1.2 KiB
C
#include "strbuf.h"
|
|
|
|
List *
|
|
StrBuf_init(Arena *a)
|
|
{
|
|
List *l = (List*)aalloc(a, sizeof(List));
|
|
if (!l) return nil;
|
|
|
|
l->head = nil;
|
|
l->tail = nil;
|
|
l->count = 0;
|
|
|
|
return l;
|
|
}
|
|
|
|
void *
|
|
StrBuf_append_exactly(Arena *a, List *buf, char *str, u32 length)
|
|
{
|
|
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;
|
|
}
|
|
|
|
void *
|
|
StrBuf_append(Arena *a, List *buf, char *str)
|
|
{
|
|
return StrBuf_append_exactly(a, buf, str, slen(str));
|
|
}
|
|
|
|
char *
|
|
StrBuf_toS(Arena *a, List *buf, u32 *out_length)
|
|
{
|
|
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);
|
|
if (out_length) *out_length += curr->size;
|
|
curr = curr->next;
|
|
}
|
|
|
|
return str;
|
|
} |