Adiciona tinyblocks-environ e tela de Configuracoes (salvar/exportar CSV)

tinyblocks-environ (categoria "Ambiente"):
- blocos Environ.Get/Set/Has/Unset (store chave->valor; Get cai no getenv).
- API C enumeravel (count/key/value_at/get/has/set/unset) e
  tb_environ_export_csv(path) com escape de CSV; tb_environ_reset.
- library.toml com metadados; Makefile proprio (.a/.so).

Editor:
- biblioteca de settings no store da environ, com valores iniciais
  (autor, projeto, tema, escala, autosave).
- tela "Configurações" (botao Config no menu): lista as chaves, edita o
  valor da linha (Enter confirma, Esc cancela), "+ Nova" cria chave,
  Delete remove, campo de caminho + botoes "Salvar" e "Exportar CSV".
- Ctrl/scroll e foco de texto integrados ao mesmo fluxo do libman.

Build/testes:
- environ entra em CAT_LIBS (compila no editor, no harness e no libman).
- make test-environ (store, fallback getenv, escape CSV, blocos).
- harness: passo 18 cobre abrir Configuracoes, editar valor, salvar CSV
  e verificar o conteudo; Esc fecha.
This commit is contained in:
mguinhos committed 2026-10-20 20:36:14 -03:00
1 parent 3120d2d797
commit b29908e65b
9 files changed
+714 -7

No files matched your search

+2
View File
@@ -11,6 +11,7 @@ CFLAGS := -std=c11 -Wall -Wextra -pedantic -fvisibility=hidden \
-I../tinyblocks-graphics/include \
-I../tinyblocks-variables/include \
-I../tinyblocks-blueprint/include \
-I../tinyblocks-environ/include \
-I../tinyblocks-api/include \
-I../tinyblocks-libman/include
@@ -27,6 +28,7 @@ $(BUILD)/tb_editor.o: src/tb_editor.c include/tb_editor.h src/tb_stage_widget.h
../tinyblocks-graphics/include/tb_graphics.h \
../tinyblocks-variables/include/tb_variables.h \
../tinyblocks-blueprint/include/tb_blueprint.h \
../tinyblocks-environ/include/tb_environ.h \
../tinyblocks-api/include/tb_api.h \
../tinyblocks-libman/include/tb_libman.h
@mkdir -p $(BUILD)
+301 -2
View File
@@ -41,6 +41,7 @@
#include <tb_blueprint.h>
#include <tb_api.h>
#include <tb_libman.h>
#include <tb_environ.h>
#include "tb_stage_widget.h"
#include "../include/tb_editor.h"
@@ -98,6 +99,10 @@ TB_API void tb_editor_preload(const char* path) {
#define LIB_BTN_X 500
#define LIB_BTN_W 200
/* Botao "Config" (settings) ancorado a direita do menu */
#define CFG_BTN_W 78
#define CFG_BTN_X (WIN_W - CFG_BTN_W - 10)
/* Botao de buscar/adicionar library no fim da coluna de categorias */
#define LIBMAN_BTN_H 56
@@ -429,6 +434,18 @@ typedef struct {
int libman_scroll;
int libman_hover;
/* Configuracoes (settings) - store da tinyblocks-environ */
bool settings_open;
int settings_sel; /* linha selecionada (-1 = nenhuma) */
bool settings_path_focus; /* campo de caminho do CSV em foco */
bool settings_value_edit; /* editando o valor da linha sel. */
bool settings_new_key; /* digitando o nome de uma nova chave */
char settings_path[256];
char settings_edit[256]; /* buffer do valor em edicao */
char settings_key[64]; /* buffer da nova chave */
int settings_scroll;
int settings_hover;
int win_w, win_h;
} Ed;
@@ -456,6 +473,14 @@ static void libman_scroll_by(Ed* ed, float dy);
static void libman_render(Ed* ed, TinyBlocks_RenderableBlock* rb);
static bool hit_libman_foot(Ed* ed, float mx, float my);
/* Configuracoes (settings) - UI desenhada pelo editor */
static void settings_show(Ed* ed);
static void settings_click(Ed* ed, float mx, float my);
static void settings_key(Ed* ed, int sc, bool ctrl);
static void settings_text(Ed* ed, const char* typed);
static void settings_scroll_by(Ed* ed, float dy);
static void settings_render(Ed* ed, TinyBlocks_RenderableBlock* rb);
/* ===
* Helpers de desenho - emitem TinyBlocks_DrawCmd em um RenderableBlock
* === */
@@ -2512,6 +2537,7 @@ static bool hit_open_btn(float mx, float my);
static bool hit_save_btn(float mx, float my);
static bool hit_export_btn(float mx, float my);
static bool hit_libman_btn(float mx, float my);
static bool hit_settings_btn(Ed* ed, float mx, float my);
static void menu_button(TinyBlocks_RenderableBlock* rb, float x, float w, const char* label, bool active) {
rb_fill_rrect(rb, x, 7, w, MENU_H - 14, 6, active ? COL_MENU_DARK : COLA(255, 255, 255, 34));
@@ -2528,6 +2554,7 @@ static void render_menu(Ed* ed, TinyBlocks_RenderableBlock* rb) {
menu_button(rb, SAVE_BTN_X, SAVE_BTN_W, "Salvar", (active && ed->path_mode == PM_SAVE) || hit_save_btn(mx, my));
menu_button(rb, EXP_BTN_X, EXP_BTN_W, "Exportar", (ed->ctx_menu_open && ed->ctx_export) || hit_export_btn(mx, my));
menu_button(rb, LIBM_BTN_X, LIBM_BTN_W, "Bibliotecas", ed->libman_open || hit_libman_btn(mx, my));
menu_button(rb, CFG_BTN_X, CFG_BTN_W, "Config", ed->settings_open || hit_settings_btn(ed, mx, my));
if (ed->lib_hover || (active && ed->path_mode == PM_LIB))
rb_fill_rect(rb, LIB_BTN_X, 0, LIB_BTN_W, MENU_H, COL_MENU_DARK);
@@ -2549,7 +2576,7 @@ static void render_menu(Ed* ed, TinyBlocks_RenderableBlock* rb) {
rb_fill_rect(rb, bx + 11 + cw, MENU_H*0.5f - 8, 1.5f, 16, COL_TEXT);
} else if (ed->hot_status[0]) {
float w = tw(ed->hot_status, FS_SM);
rb_text_c(rb, WIN_W - 16 - w, MENU_H*0.5f, ed->hot_status,
rb_text_c(rb, CFG_BTN_X - 12 - w, MENU_H*0.5f, ed->hot_status,
ed->hot_status_ok ? COL_WHITE : COL_ERR_MENU, FS_SM);
}
}
@@ -3079,6 +3106,259 @@ static void libman_scroll_by(Ed* ed, float dy) {
if (ed->libman_scroll < 0) ed->libman_scroll = 0;
}
/* ===
* Configuracoes (settings) - store da tinyblocks-environ, salvo/exportado em CSV
* === */
#define ST_HEAD_H 64
#define ST_TOOL_Y ST_HEAD_H
#define ST_TOOL_H 64
#define ST_MARGIN 40
#define ST_ROW_H 40
#define ST_LIST_Y (ST_TOOL_Y + ST_TOOL_H + 6)
#define ST_BTN_H 40
static void settings_show(Ed* ed) {
ed->settings_open = true;
ed->settings_sel = -1;
ed->settings_path_focus = false;
ed->settings_value_edit = false;
ed->settings_new_key = false;
ed->settings_scroll = 0;
ed->settings_hover = -1;
if (!ed->settings_path[0]) strcpy(ed->settings_path, "tinyblocks-settings.csv");
ed->libman_open = false;
ed->path_input_active = false;
ed->ctx_menu_open = false;
}
static void settings_close(Ed* ed) {
ed->settings_open = false;
ed->settings_value_edit = false;
ed->settings_new_key = false;
ed->settings_path_focus = false;
}
static float settings_btn_w(int i) { return i == 1 ? 150.f : 96.f; }
static void settings_btn_rect(Ed* ed, int i, float* x, float* y, float* w, float* h) {
float gap = 12.f, cur = WIN_W - ST_MARGIN;
float pos[3];
for (int k = 2; k >= 0; k--) { cur -= settings_btn_w(k); pos[k] = cur; cur -= gap; }
*x = pos[i]; *y = ST_TOOL_Y + 12.f; *w = settings_btn_w(i); *h = ST_BTN_H;
}
static void settings_path_rect(Ed* ed, float* x, float* y, float* w, float* h) {
float bx; settings_btn_rect(ed, 0, &bx, y, w, h);
*x = ST_MARGIN; *w = bx - 20.f - ST_MARGIN;
}
static void st_button(TinyBlocks_RenderableBlock* rb, float x, float y, float w, float h,
const char* label, bool active) {
rb_fill_rrect(rb, x, y, w, h, 6, active ? COL_MENU_DARK : COL_MENU);
rb_text_c(rb, x + (w - tw(label, FS_UI)) * 0.5f, y + h * 0.5f, label, COL_WHITE, FS_UI);
}
static void settings_export(Ed* ed, const char* path) {
bool ok = tb_environ_export_csv(path);
if (ok) snprintf(ed->hot_status, sizeof(ed->hot_status),
"Configuracoes salvas (CSV): %.200s", path);
else snprintf(ed->hot_status, sizeof(ed->hot_status),
"Falha ao salvar: %.200s", tb_environ_last_error());
ed->hot_status_ok = ok;
}
/* Rect da linha i (com o deslocamento da linha "nova chave", se ativa). */
static void settings_row_rect(Ed* ed, int i, float* x, float* y, float* w, float* h) {
int off = ed->settings_new_key ? 1 : 0;
*x = ST_MARGIN; *y = ST_LIST_Y + (i + off) * ST_ROW_H - ed->settings_scroll;
*w = WIN_W - 2.f * ST_MARGIN; *h = ST_ROW_H;
}
static void settings_render(Ed* ed, TinyBlocks_RenderableBlock* rb) {
if (!ed->settings_open) return;
int n = tb_environ_count();
rb_fill_rect(rb, 0, 0, WIN_W, WIN_H, COL_UI_BG);
/* Cabecalho */
rb_fill_rect(rb, 0, 0, WIN_W, ST_HEAD_H, COL_MENU);
float bx = ST_MARGIN, by = ST_HEAD_H * 0.5f;
rb_fill_circle(rb, bx, by, 16, COLA(255,255,255,40));
rb_line(rb, bx+4, by-7, bx-5, by, 2.f, COL_WHITE);
rb_line(rb, bx-5, by, bx+4, by+7, 2.f, COL_WHITE);
rb_text_c(rb, bx + 28, by, "Voltar", COL_WHITE, FS_UI);
{ const char* t = "Configurações";
rb_text_c(rb, (WIN_W - tw(t, 20)) * 0.5f, by, t, COL_WHITE, 20); }
/* Barra de ferramentas */
rb_fill_rect(rb, 0, ST_TOOL_Y, WIN_W, ST_TOOL_H, COL_PANEL);
{
float px, py, pw, ph; settings_path_rect(ed, &px, &py, &pw, &ph);
rb_fill_rrect(rb, px, py, pw, ph, 6, COL_WHITE);
if (ed->settings_path_focus) rb_stroke_rect(rb, px-1, py-1, pw+2, ph+2, 2.f, COL_MENU);
rb_text_c(rb, px + 12, py + ph*0.5f, ed->settings_path, COL_TEXT, FS_UI);
if (ed->settings_path_focus) {
float cw = tw(ed->settings_path, FS_UI);
rb_fill_rect(rb, px + 13 + cw, py + ph*0.5f - 9, 1.5f, 18, COL_TEXT);
}
}
for (int i = 0; i < 3; i++) {
float x, y, w, h; settings_btn_rect(ed, i, &x, &y, &w, &h);
bool hov = in_rect(ed->mouse_x, ed->mouse_y, x, y, w, h);
st_button(rb, x, y, w, h,
i == 0 ? "Salvar" : i == 1 ? "Exportar CSV" : "+ Nova", hov);
}
/* Lista */
rb_fill_rect(rb, 0, ST_LIST_Y, WIN_W, WIN_H - ST_LIST_Y, COL_UI_BG);
clip_set(ed, rb, 0, ST_LIST_Y, WIN_W, WIN_H - ST_LIST_Y);
if (ed->settings_new_key) {
float y = ST_LIST_Y - ed->settings_scroll;
rb_fill_rrect(rb, ST_MARGIN, y + 2, WIN_W - 2*ST_MARGIN, ST_ROW_H - 4, 6, COL_PANEL);
rb_text_c(rb, ST_MARGIN + 16, y + ST_ROW_H*0.5f, "+", COL_MENU, FS_UI);
const char* t = ed->settings_key[0] ? ed->settings_key : "nome da chave (Enter confirma, Esc cancela)";
rb_text_c(rb, ST_MARGIN + 40, y + ST_ROW_H*0.5f, t,
ed->settings_key[0] ? COL_TEXT : COL_TEXT_DIM, FS_UI);
if (ed->settings_key[0]) {
float cw = tw(ed->settings_key, FS_UI);
rb_fill_rect(rb, ST_MARGIN + 41 + cw, y + ST_ROW_H*0.5f - 9, 1.5f, 18, COL_TEXT);
}
}
for (int i = 0; i < n; i++) {
float x, y, w, h; settings_row_rect(ed, i, &x, &y, &w, &h);
if (y + h < ST_LIST_Y || y > WIN_H) continue;
bool sel = (i == ed->settings_sel);
rb_fill_rrect(rb, x, y + 2, w, h - 4, 6, sel ? COL_PANEL : COL_FLY);
if (sel && !ed->settings_value_edit) rb_stroke_rect(rb, x, y+1, w, h-2, 2.f, COL_MENU);
rb_text_c(rb, x + 16, y + h*0.5f, tb_environ_key(i), COL_TEXT, FS_UI);
float vx = x + 16 + tw("", FS_UI) + 260.f;
if (vx > x + w - 260.f) vx = x + w - 260.f;
if (sel && ed->settings_value_edit) {
rb_fill_rrect(rb, vx - 6, y + 6, w - (vx - x) - 4, h - 12, 4, COL_WHITE);
rb_text_c(rb, vx, y + h*0.5f, ed->settings_edit, COL_TEXT, FS_UI);
float cw = tw(ed->settings_edit, FS_UI);
rb_fill_rect(rb, vx + 1 + cw, y + h*0.5f - 9, 1.5f, 18, COL_TEXT);
} else {
rb_text_c(rb, vx, y + h*0.5f, tb_environ_value_at(i), COL_TEXT_DIM, FS_UI);
}
}
clip_clear(ed, rb);
if (n == 0 && !ed->settings_new_key)
rb_text_c(rb, ST_MARGIN, ST_LIST_Y + 24,
"Nenhuma configuracao. Use \"+ Nova\".", COL_TEXT_DIM, FS_UI);
if (ed->hot_status[0])
rb_text_c(rb, ST_MARGIN, WIN_H - 22, ed->hot_status,
ed->hot_status_ok ? COL_TEXT_DIM : COL_ERR_MENU, FS_SM);
}
static void settings_click(Ed* ed, float mx, float my) {
if (my < ST_HEAD_H) { if (mx < 170) settings_close(ed); return; }
if (my < ST_TOOL_Y + ST_TOOL_H) {
float px, py, pw, ph; settings_path_rect(ed, &px, &py, &pw, &ph);
if (in_rect(mx, my, px, py, pw, ph)) {
ed->settings_path_focus = true; ed->settings_value_edit = false; ed->settings_new_key = false;
return;
}
for (int i = 0; i < 3; i++) {
float x, y, w, h; settings_btn_rect(ed, i, &x, &y, &w, &h);
if (!in_rect(mx, my, x, y, w, h)) continue;
ed->settings_path_focus = false;
if (i == 0) settings_export(ed, "tinyblocks-settings.csv");
else if (i == 1) settings_export(ed, ed->settings_path);
else { ed->settings_new_key = true; ed->settings_key[0] = 0;
ed->settings_value_edit = false; ed->settings_sel = -1;
ed->settings_scroll = 0; }
return;
}
ed->settings_path_focus = false;
return;
}
if (ed->settings_new_key && in_rect(mx, my, ST_MARGIN, ST_LIST_Y - ed->settings_scroll,
WIN_W - 2*ST_MARGIN, ST_ROW_H)) return;
int n = tb_environ_count();
for (int i = 0; i < n; i++) {
float x, y, w, h; settings_row_rect(ed, i, &x, &y, &w, &h);
if (!in_rect(mx, my, x, y, w, h)) continue;
ed->settings_sel = i;
ed->settings_new_key = false;
ed->settings_path_focus = false;
ed->settings_value_edit = true;
const char* v = tb_environ_value_at(i);
strncpy(ed->settings_edit, v ? v : "", sizeof(ed->settings_edit) - 1);
ed->settings_edit[sizeof(ed->settings_edit) - 1] = 0;
return;
}
ed->settings_sel = -1;
ed->settings_value_edit = false;
}
static void settings_key(Ed* ed, int sc, bool ctrl) {
(void)ctrl;
bool enter = sc == TB_KEY_RETURN;
if (sc == TB_KEY_ESCAPE) {
if (ed->settings_new_key) { ed->settings_new_key = false; ed->settings_key[0] = 0; }
else if (ed->settings_value_edit) ed->settings_value_edit = false;
else settings_close(ed);
return;
}
if (ed->settings_new_key) {
if (enter && ed->settings_key[0]) {
tb_environ_set(ed->settings_key, "");
ed->settings_new_key = false;
ed->settings_sel = tb_environ_count() - 1;
ed->settings_value_edit = true;
ed->settings_edit[0] = 0;
ed->settings_scroll = 0;
} else if (sc == TB_KEY_BACKSPACE) {
utf8_pop(ed->settings_key);
}
return;
}
if (ed->settings_value_edit && ed->settings_sel >= 0) {
if (enter) {
const char* k = tb_environ_key(ed->settings_sel);
if (k) tb_environ_set(k, ed->settings_edit);
ed->settings_value_edit = false;
} else if (sc == TB_KEY_BACKSPACE) {
utf8_pop(ed->settings_edit);
}
return;
}
if (ed->settings_path_focus && sc == TB_KEY_BACKSPACE) {
utf8_pop(ed->settings_path);
return;
}
if (sc == TB_KEY_DELETE && ed->settings_sel >= 0) {
const char* k = tb_environ_key(ed->settings_sel);
if (k) tb_environ_unset(k);
ed->settings_sel = -1;
}
}
static void settings_text(Ed* ed, const char* typed) {
if (ed->settings_new_key) append_text(ed->settings_key, sizeof(ed->settings_key), typed);
else if (ed->settings_value_edit) append_text(ed->settings_edit, sizeof(ed->settings_edit), typed);
else if (ed->settings_path_focus) append_text(ed->settings_path, sizeof(ed->settings_path), typed);
}
static void settings_scroll_by(Ed* ed, float dy) {
ed->settings_scroll -= (int)(dy * 48.f);
int off = ed->settings_new_key ? 1 : 0;
int content = (tb_environ_count() + off) * ST_ROW_H + 24;
int visible = (int)(WIN_H - ST_LIST_Y);
int maxs = content - visible;
if (maxs < 0) maxs = 0;
if (ed->settings_scroll > maxs) ed->settings_scroll = maxs;
if (ed->settings_scroll < 0) ed->settings_scroll = 0;
}
/* ===
* Arrastar e encaixar
* === */
@@ -3227,6 +3507,7 @@ static bool hit_open_btn(float mx, float my) { return in_rect(mx, my, OPEN_B
static bool hit_save_btn(float mx, float my) { return in_rect(mx, my, SAVE_BTN_X, 0, SAVE_BTN_W, MENU_H); }
static bool hit_export_btn(float mx, float my) { return in_rect(mx, my, EXP_BTN_X, 0, EXP_BTN_W, MENU_H); }
static bool hit_libman_btn(float mx, float my) { return in_rect(mx, my, LIBM_BTN_X, 0, LIBM_BTN_W, MENU_H); }
static bool hit_settings_btn(Ed* ed, float mx, float my) { return in_rect(mx, my, CFG_BTN_X, 0, CFG_BTN_W, MENU_H); }
static void start_path(Ed* ed, int mode, const char* def) {
ed->path_input_active = true;
@@ -3320,6 +3601,7 @@ static void ctx_exec(Ed* ed, int id) {
}
static void on_lmb_down(Ed* ed, float mx, float my) {
if (ed->settings_open) { settings_click(ed, mx, my); return; }
if (ed->libman_open) { libman_click(ed, mx, my); return; }
if (ed->comp_dialog_open) { dlg_click(ed, mx, my); return; }
if (ed->ctx_menu_open) {
@@ -3338,6 +3620,7 @@ static void on_lmb_down(Ed* ed, float mx, float my) {
else if (hit_open_btn(mx, my)) start_path(ed, PM_LOAD, "projeto.json");
else if (hit_save_btn(mx, my)) start_path(ed, PM_SAVE, "projeto.json");
else if (hit_libman_btn(mx, my)) libman_show(ed);
else if (hit_settings_btn(ed, mx, my)) settings_show(ed);
else if (hit_export_btn(mx, my)) {
ed->ctx_n = 0;
ed->ctx_items[ed->ctx_n++] = CM_EXP_JSON;
@@ -3473,6 +3756,7 @@ static void on_rmb_down(Ed* ed, float mx, float my) {
static void on_key(Ed* ed, int sc, bool ctrl) {
bool enter = sc == TB_KEY_RETURN;
if (ed->settings_open) { settings_key(ed, sc, ctrl); return; }
if (ed->libman_open) { libman_key(ed, sc); return; }
if (ed->comp_dialog_open) { dlg_key(ed, sc); return; }
if (ed->ctx_menu_open) { if (sc == TB_KEY_ESCAPE) ed->ctx_menu_open = false; return; }
@@ -3527,6 +3811,7 @@ static void on_key(Ed* ed, int sc, bool ctrl) {
}
static void on_text(Ed* ed, const char* typed) {
if (ed->settings_open) { settings_text(ed, typed); return; }
if (ed->libman_open) { libman_text(ed, typed); return; }
if (ed->comp_dialog_open) { dlg_text(ed, typed); return; }
if (ed->path_input_active) {
@@ -3589,7 +3874,9 @@ static void editor_eval(TinyBlocks_Block* self, TinyBlocks* rt) {
double sdx = ev_real(evb, GFX_IN_SCROLL_DX);
double sdy = ev_real(evb, GFX_IN_SCROLL_DY);
if (sdx != 0.0 || sdy != 0.0) {
if (ed->libman_open) {
if (ed->settings_open) {
settings_scroll_by(ed, (float)sdy);
} else if (ed->libman_open) {
libman_scroll_by(ed, (float)sdy);
} else if (mx >= WS_X && mx < WIN_W - STAGE_W && my >= WS_Y) {
CT.ws_sx -= (float)sdx * 40.f;
@@ -3655,6 +3942,7 @@ static bool editor_on_draw(TinyBlocks_Block* self, TinyBlocks* rt,
render_ctx_menu(ed, rb);
dlg_render(ed, rb);
libman_render(ed, rb); /* tela cheia: por cima de tudo */
settings_render(ed, rb);
*dst = rb;
return true;
@@ -3709,6 +3997,8 @@ static bool editor_instance_init(TinyBlocks_Block* self, TinyBlocks* rt) {
ed->bubble_bi = -1;
ed->ctx_menu_bi = -1;
ed->path_mode = PM_NONE;
ed->settings_sel = -1;
strcpy(ed->settings_path, "tinyblocks-settings.csv");
ed->tab_n = 1;
ed->cur_tab = 0;
@@ -3732,6 +4022,15 @@ static bool editor_instance_init(TinyBlocks_Block* self, TinyBlocks* rt) {
for (int i = 0; i < s_preload_n; i++) hotlib_load(ed, s_preload[i]);
s_preload_n = 0;
seed_example(ed);
/* Configuracoes iniciais (store da tinyblocks-environ) */
if (tb_environ_count() == 0) {
tb_environ_set("autor", "");
tb_environ_set("projeto", "sem_titulo");
tb_environ_set("tema", "claro");
tb_environ_set("escala", "1.0");
tb_environ_set("autosave", "0");
}
return true;
}
+29
View File
@@ -0,0 +1,29 @@
# Makefile da library environ (categoria "Ambiente")
# Constroi build/libtinyblocks-environ.a e build/libtinyblocks-environ.so
CC := gcc
BUILD := build
CFLAGS := -std=c11 -Wall -Wextra -pedantic -fvisibility=hidden \
-D_POSIX_C_SOURCE=200809L -I../../include -Iinclude
OBJ := $(BUILD)/tb_environ.o
LIB_A := $(BUILD)/libtinyblocks-environ.a
LIB_SO := $(BUILD)/libtinyblocks-environ.so
.PHONY: all static shared clean
all: static shared
static: $(LIB_A)
shared: $(LIB_SO)
$(OBJ): src/tb_environ.c include/tb_environ.h
@mkdir -p $(BUILD)
$(CC) $(CFLAGS) -fPIC -c $< -o $@
$(LIB_A): $(OBJ)
ar rcs $@ $^
$(LIB_SO): $(OBJ)
$(CC) -shared -o $@ $^
clean:
$(RM) -r $(BUILD)
@@ -0,0 +1,32 @@
/*
* TinyBlocks - tb_environ.h
* Library tinyblocks-environ: variaveis de ambiente / configuracao
* (chave -> valor). Independente do editor. Registra a categoria "Ambiente".
*
* O store pode ser usado tambem como "settings" pelo editor: enumeravel e
* exportavel para CSV.
*/
#ifndef TINYBLOCKS_ENVIRON_H
#define TINYBLOCKS_ENVIRON_H
#include <tinyblocks/tinyblocks.h>
/* -- Descriptor (para registrar os blocos no runtime) --- */
TB_API const TinyBlocks_LibraryDescriptor* tb_environ_get_library(void);
/* -- API C: store chave/valor (ordenado por insercao) --- */
TB_API void tb_environ_reset(void); /* limpa tudo */
TB_API int tb_environ_count(void);
TB_API const char* tb_environ_key(int i); /* NULL se fora do range */
TB_API const char* tb_environ_value_at(int i); /* NULL se fora do range */
TB_API const char* tb_environ_get(const char* key); /* "" se ausente */
TB_API bool tb_environ_has(const char* key);
TB_API void tb_environ_set(const char* key, const char* value);
TB_API void tb_environ_unset(const char* key);
/* Exporta/enumera como CSV "chave,valor". Retorna false em erro de IO. */
TB_API bool tb_environ_export_csv(const char* path);
TB_API const char* tb_environ_last_error(void);
#endif /* TINYBLOCKS_ENVIRON_H */
+15
View File
@@ -0,0 +1,15 @@
# Metadados da library environ. Lido pela UI/build sem recompilar o .so.
[library]
name = "tinyblocks-environ"
version = "1.0.0"
category = "Ambiente"
description = "Variaveis de ambiente e configuracao (chave, valor)"
color = [92, 107, 192, 255]
dark = [57, 73, 171, 255]
[build]
sources = ["src/tb_environ.c"]
includes = ["include"]
shared = "build/libtinyblocks-environ.so"
static = "build/libtinyblocks-environ.a"
depends = []
@@ -0,0 +1,197 @@
/*
* TinyBlocks - tb_environ.c
* Categoria "Ambiente": le/define variaveis de ambiente (chave -> valor).
*
* Mantem um store em memoria, ordenado por insercao, que serve de base para a
* tela de Configuracoes do editor (enumerar e salvar/exportar como CSV).
* `Environ.Get` consulta primeiro o store e, se nao houver, o ambiente real
* do processo (getenv).
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <tinyblocks/tinyblocks.h>
#include "tb_environ.h"
#define ENV_MAX 256
typedef struct { char* key; char* value; } EnvPair;
static EnvPair s_env[ENV_MAX];
static int s_env_n = 0;
static char s_err[256] = "";
/* -- Store --------------------------------------------------------------- */
static char* dup_str(const char* s) {
if (!s) s = "";
size_t n = strlen(s) + 1;
char* p = (char*)malloc(n);
if (p) memcpy(p, s, n);
return p;
}
static int env_find(const char* key) {
if (!key) return -1;
for (int i = 0; i < s_env_n; i++)
if (strcmp(s_env[i].key, key) == 0) return i;
return -1;
}
void tb_environ_reset(void) {
for (int i = 0; i < s_env_n; i++) { free(s_env[i].key); free(s_env[i].value); }
s_env_n = 0;
}
int tb_environ_count(void) { return s_env_n; }
const char* tb_environ_key(int i) {
return (i >= 0 && i < s_env_n) ? s_env[i].key : NULL;
}
const char* tb_environ_value_at(int i) {
return (i >= 0 && i < s_env_n) ? s_env[i].value : NULL;
}
bool tb_environ_has(const char* key) {
return env_find(key) >= 0;
}
const char* tb_environ_get(const char* key) {
int i = env_find(key);
if (i >= 0) return s_env[i].value;
const char* v = key ? getenv(key) : NULL;
return v ? v : "";
}
void tb_environ_set(const char* key, const char* value) {
if (!key || !key[0]) return;
if (!value) value = "";
int i = env_find(key);
if (i >= 0) {
free(s_env[i].value);
s_env[i].value = dup_str(value);
} else if (s_env_n < ENV_MAX) {
s_env[s_env_n].key = dup_str(key);
s_env[s_env_n].value = dup_str(value);
s_env_n++;
}
setenv(key, value, 1); /* mantem o ambiente do processo coerente */
}
void tb_environ_unset(const char* key) {
int i = env_find(key);
if (i < 0) return;
free(s_env[i].key);
free(s_env[i].value);
for (int j = i; j < s_env_n - 1; j++) s_env[j] = s_env[j + 1];
s_env_n--;
if (key) unsetenv(key);
}
const char* tb_environ_last_error(void) { return s_err; }
/* Escreve um campo CSV: aspas quando contem virgula, aspas ou quebra. */
static void csv_field(FILE* f, const char* s) {
if (!s) s = "";
bool quote = strpbrk(s, ",\"\n\r") != NULL;
if (!quote) { fputs(s, f); return; }
fputc('"', f);
for (const char* p = s; *p; p++) {
if (*p == '"') fputc('"', f);
fputc(*p, f);
}
fputc('"', f);
}
bool tb_environ_export_csv(const char* path) {
s_err[0] = 0;
if (!path || !path[0]) { snprintf(s_err, sizeof(s_err), "caminho vazio"); return false; }
FILE* f = fopen(path, "wb");
if (!f) { snprintf(s_err, sizeof(s_err), "nao foi possivel abrir %s", path); return false; }
fputs("chave,valor\n", f);
for (int i = 0; i < s_env_n; i++) {
csv_field(f, s_env[i].key);
fputc(',', f);
csv_field(f, s_env[i].value);
fputc('\n', f);
}
fclose(f);
return true;
}
/* -- Blocos -------------------------------------------------------------- */
static const char* in_text(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
return (v.kind == TB_VALUE_TEXT && v.as.text) ? v.as.text : "";
}
static void eval_get(TinyBlocks_Block* self, TinyBlocks* rt) {
tb_block_set_output(self, 0, tb_value_text(tb_environ_get(in_text(self, rt, 0))));
self->dirty = false;
}
static void eval_set(TinyBlocks_Block* self, TinyBlocks* rt) {
tb_environ_set(in_text(self, rt, 0), in_text(self, rt, 1));
self->dirty = false;
}
static void eval_has(TinyBlocks_Block* self, TinyBlocks* rt) {
tb_block_set_output(self, 0, tb_value_integer(tb_environ_has(in_text(self, rt, 0)) ? 1 : 0));
self->dirty = false;
}
static void eval_unset(TinyBlocks_Block* self, TinyBlocks* rt) {
tb_environ_unset(in_text(self, rt, 0));
self->dirty = false;
}
/* -- Registro ------------------------------------------------------------ */
static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
const TinyBlocks_SlotDesc* in, uint32_t ni,
const TinyBlocks_SlotDesc* out, uint32_t no,
void (*fn)(TinyBlocks_Block*, TinyBlocks*)) {
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, name, in, ni, out, no, fn);
return t && tb_library_add(lib, &t->base);
}
static bool environ_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
static const TinyBlocks_Category cat = {
"Ambiente",
{ 92/255.f, 107/255.f, 192/255.f, 1.f},
{ 57/255.f, 73/255.f, 171/255.f, 1.f},
};
tb_library_set_category(lib, &cat);
static const TinyBlocks_SlotDesc get_in[] = {{"nome", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc set_in[] = {{"nome", TB_VALUE_TEXT}, {"valor", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc has_in[] = {{"nome", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc unset_in[] = {{"nome", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc val_out[] = {{"valor", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc bool_out[] = {{"existe",TB_VALUE_INTEGER}};
return reg(rt, lib, "Environ.Get", get_in, 1, val_out, 1, eval_get)
&& reg(rt, lib, "Environ.Set", set_in, 2, NULL, 0, eval_set)
&& reg(rt, lib, "Environ.Has", has_in, 1, bool_out, 1, eval_has)
&& reg(rt, lib, "Environ.Unset", unset_in, 1, NULL, 0, eval_unset);
}
static void environ_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
(void)rt; (void)lib;
/* Mantem o store: o editor pode continuar lendo as settings. */
}
TINYBLOCKS_DEFINE_LIBRARY(environ,
"tinyblocks-environ",
"1.0.0",
environ_init,
environ_deinit)
const TinyBlocks_LibraryDescriptor* tb_environ_get_library(void) {
return &_tb_lib_desc_environ;
}