lambda-calc-1/stringtree/append.c
2025-01-20 13:40:51 -06:00

155 lines
2 KiB
C

#include <assert.h>
#include <debug.h>
#include <memory/smalloc.h>
#include <string/new.h>
#include <string/inc.h>
#include <string/free.h>
#include "inc.h"
#include "struct.h"
#include "append.h"
void stringtree_append_cstr(
struct stringtree* this,
const wchar_t* cstr)
{
ENTER;
struct child* new = smalloc(sizeof(*new));
new->kind = ck_cstr;
new->cstr = cstr;
new->prev = NULL;
new->next = NULL;
if (this->tail)
{
this->tail->next = new;
new->prev = this->tail;
this->tail = new;
}
else
{
this->head = new;
this->tail = new;
}
EXIT;
}
void stringtree_append_formatstr(
struct stringtree* this,
const char* fmt, ...)
{
ENTER;
va_list va;
va_start(va, fmt);
struct string* string = new_string_from_ascii_format_vargs(fmt, va);
stringtree_append_string(this, string);
free_string(string);
va_end(va);
EXIT;
}
void stringtree_append_string(
struct stringtree* this,
struct string* string)
{
ENTER;
struct child* new = smalloc(sizeof(*new));
new->kind = ck_string;
new->string = inc_string(string);
new->prev = NULL;
new->next = NULL;
if (this->tail)
{
this->tail->next = new;
new->prev = this->tail;
this->tail = new;
}
else
{
this->head = new;
this->tail = new;
}
EXIT;
}
void stringtree_append_stringtree(
struct stringtree* this,
struct stringtree* stringtree)
{
ENTER;
struct child* new = smalloc(sizeof(*new));
new->kind = ck_stringtree;
new->stringtree = inc_stringtree(stringtree);
new->prev = NULL;
new->next = NULL;
if (this->tail)
{
this->tail->next = new;
new->prev = this->tail;
this->tail = new;
}
else
{
this->head = new;
this->tail = new;
}
EXIT;
}