#include #define _POSIX_C_SOURCE 200809L #include #include #include #include "tinyblocks/tinyblocks.h" /* -- Vtable --- */ static void text_eval(TinyBlocks_Block* self, TinyBlocks* rt) { (void)rt; TinyBlocks_TextBlock* tb = (TinyBlocks_TextBlock*)self; tb_block_set_output(self, 0, tb_value_text(tb->value)); self->dirty = false; } static void text_destroy(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_TextBlock* tb = (TinyBlocks_TextBlock*)self; tb_free(rt, tb->value); } static size_t text_to_string(TinyBlocks_Block* self, TinyBlocks* rt, char* buf, size_t sz) { (void)rt; TinyBlocks_TextBlock* tb = (TinyBlocks_TextBlock*)self; return (size_t)snprintf(buf, sz, "\"%s\"", tb->value ? tb->value : ""); } static TinyBlocks_Block* text_clone(const TinyBlocks_Block* self, TinyBlocks* rt) { const TinyBlocks_TextBlock* tb = (const TinyBlocks_TextBlock*)self; TinyBlocks_TextBlock* clone = tb_text_block_create(rt, tb->value); if (clone && self->name) clone->base.name = tb_strdup(rt, self->name); return (TinyBlocks_Block*)clone; } static const TinyBlocks_BlockVTable s_text_vtable = { .eval = text_eval, .destroy = text_destroy, .to_string = text_to_string, .clone = text_clone, }; /* -- Ciclo de vida --- */ TinyBlocks_TextBlock* tb_text_block_create(TinyBlocks* rt, const char* value) { TinyBlocks_TextBlock* tb = (TinyBlocks_TextBlock*)tb_block_alloc_base( rt, TB_KIND_TEXT, sizeof(TinyBlocks_TextBlock), 0, 1, &s_text_vtable); if (!tb) return NULL; tb->value = tb_strdup(rt, value ? value : ""); return tb; } void tb_text_block_destroy(TinyBlocks_TextBlock* tb, TinyBlocks* rt) { tb_block_destroy(&tb->base, rt); } /* -- Acesso --- */ const char* tb_text_block_get(const TinyBlocks_TextBlock* tb) { return tb ? tb->value : NULL; } void tb_text_block_set(TinyBlocks_TextBlock* tb, const char* value) { if (!tb) return; /* Usa realloc para evitar precisar de rt aqui */ char* copy = value ? strdup(value) : strdup(""); free(tb->value); tb->value = copy; tb->base.dirty = true; } void tb_text_block_append(TinyBlocks_TextBlock* tb, const char* suffix) { if (!tb || !suffix) return; size_t old_len = tb->value ? strlen(tb->value) : 0; size_t suf_len = strlen(suffix); char* buf = realloc(tb->value, old_len + suf_len + 1); if (!buf) return; memcpy(buf + old_len, suffix, suf_len + 1); tb->value = buf; tb->base.dirty = true; }