diff --git a/Makefile b/Makefile index 4d9af8f..83df956 100644 --- a/Makefile +++ b/Makefile @@ -67,7 +67,8 @@ SRCS := src/tinyblocks.c \ src/tb_renderable_block.c \ src/tb_stage.c \ src/tb_json_block.c \ - src/tb_image.c + src/tb_image.c \ + src/tb_meta.c OBJS := $(patsubst src/%.c,$(BUILD)/%.o,$(SRCS)) OBJS_DBG := $(patsubst src/%.c,$(BUILD)/%-dbg.o,$(SRCS)) @@ -92,7 +93,7 @@ LIB_LDFLAGS := -L$(ED_DIR)/build -L$(GX_DIR)/build -L$(SD_DIR)/build \ .PHONY: all help debug release static shared example sdl2 clean run python-lib mcp-lib json \ editor run-editor run-sdl2 mylib vector customappearance graphics sdl2-backend test-editor \ appdir appimage run-appimage icon \ - test-toml test-plugin test-blueprint test-base64 test-curves test-ppm test-bmp test-png test-image test-vg test-svg test-jpg \ + test-toml test-plugin test-blueprint test-base64 test-curves test-meta test-ppm test-bmp test-png test-image test-vg test-svg test-jpg \ test-syntax test-regex test-markdown test-opentype \ category-libs category-sos libs \ lib-events lib-control lib-operators lib-variables lib-appearance lib-pen lib-fs \ @@ -494,6 +495,15 @@ $(BUILD)/base64_test: tests/base64_test.c $(B64_DIR)/src/tb_base64.c static test-base64: $(BUILD)/base64_test ## Roda o teste da base64 (ASan/UBSan) ./$(BUILD)/base64_test +# -- Teste dos metadados de blocos (core tb_meta) --- +$(BUILD)/meta_test: tests/meta_test.c $(OP_DIR)/src/tb_operators.c static + @mkdir -p $(BUILD) + $(CC) $(CFLAGS_DBG) -I$(OP_DIR)/include $(OP_DIR)/src/tb_operators.c \ + tests/meta_test.c -o $@ -Wl,--start-group $(LIB_A) -Wl,--end-group -lm + +test-meta: $(BUILD)/meta_test ## Roda o teste dos metadados de blocos (ASan/UBSan) + ./$(BUILD)/meta_test + # -- Teste da library curves --- $(BUILD)/curves_test: tests/curves_test.c $(CV_DIR)/src/tb_curves.c static @mkdir -p $(BUILD) diff --git a/include/tinyblocks/tb_meta.h b/include/tinyblocks/tb_meta.h new file mode 100644 index 0000000..4a0f960 --- /dev/null +++ b/include/tinyblocks/tb_meta.h @@ -0,0 +1,47 @@ +/* + * TinyBlocks - tb_meta.h + * Registro de metadados de blocos: opcionalmente uma library declara, para + * cada bloco, o nome da funcao C que o implementa, o arquivo-fonte e uma + * descricao. O editor usa isso no tooltip (Alt + hover) e para abrir o + * codigo-fonte na linha da funcao. + */ +#ifndef TINYBLOCKS_TB_META_H +#define TINYBLOCKS_TB_META_H + +#include +#include +#include + +typedef struct { + const char* type_name; /* nome do bloco, ex.: "Mover" */ + const char* func_name; /* funcao C que implementa o bloco */ + const char* file; /* arquivo-fonte (__FILE__) */ + const char* doc; /* descricao de uma linha (ou NULL) */ +} TinyBlocks_BlockMeta; + +/* Declara metadados (chame em _init()). `file` costuma ser __FILE__. */ +TB_API void tb_meta_declare(const char* type_name, const char* func_name, + const char* file, const char* doc); + +TB_API const TinyBlocks_BlockMeta* tb_meta_find(const char* type_name); +TB_API int tb_meta_count(void); +TB_API const TinyBlocks_BlockMeta* tb_meta_at(int i); +TB_API void tb_meta_clear(void); + +/* Resolve o caminho real do arquivo-fonte (procura em libraries/ etc.). + * Devolve true se conseguiu abrir/ler o arquivo em `out`. */ +TB_API bool tb_meta_resolve_file(const char* file, char* out, size_t cap); + +/* Linha (1-based) da definicao de `m->func_name` em `m->file`, ou -1. */ +TB_API int tb_meta_line(const TinyBlocks_BlockMeta* m); + +/* Le `max_lines` linhas a partir de `line` (1-based) do arquivo resolvido. + * Devolve o numero de linhas lidas. `buf` recebe as linhas unidas por '\n'. */ +TB_API int tb_meta_read_lines(const TinyBlocks_BlockMeta* m, int line, + int max_lines, char* buf, size_t cap); + +/* Declara os metadados de um bloco. Coloque em _init(). */ +#define TB_BLOCK_META(type_name, func_name, doc_str) \ + tb_meta_declare((type_name), (func_name), __FILE__, (doc_str)) + +#endif /* TINYBLOCKS_TB_META_H */ diff --git a/include/tinyblocks/tinyblocks.h b/include/tinyblocks/tinyblocks.h index 16812ae..bd9d283 100644 --- a/include/tinyblocks/tinyblocks.h +++ b/include/tinyblocks/tinyblocks.h @@ -36,6 +36,7 @@ #include "tb_vector_block.h" #include "tb_json_block.h" #include "tb_image.h" +#include "tb_meta.h" /* -- Runtime --- */ struct TinyBlocks { diff --git a/libraries/tinyblocks-appearance/src/tb_appearance.c b/libraries/tinyblocks-appearance/src/tb_appearance.c index 0f8af59..925844e 100644 --- a/libraries/tinyblocks-appearance/src/tb_appearance.c +++ b/libraries/tinyblocks-appearance/src/tb_appearance.c @@ -36,6 +36,12 @@ static bool appearance_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { }; tb_library_set_category(lib, &cat); + /* Metadados dos blocos (tooltip do editor: Alt + hover). */ + TB_BLOCK_META("IO.Print", "eval_print", + "Mostra um texto e um valor na saida"); + TB_BLOCK_META("IO.PrintInt", "eval_print_int", + "Mostra um numero inteiro na saida"); + static const TinyBlocks_SlotDesc print_in[] = {{"label", TB_VALUE_TEXT}, {"value", TB_VALUE_NONE}}; static const TinyBlocks_SlotDesc one_int[] = {{"n", TB_VALUE_INTEGER}}; diff --git a/libraries/tinyblocks-control/src/tb_control.c b/libraries/tinyblocks-control/src/tb_control.c index 58d4eef..4078067 100644 --- a/libraries/tinyblocks-control/src/tb_control.c +++ b/libraries/tinyblocks-control/src/tb_control.c @@ -26,6 +26,16 @@ static bool control_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { }; tb_library_set_category(lib, &cat); + /* Metadados dos blocos (tooltip do editor: Alt + hover). */ + TB_BLOCK_META("Control.Repeat", "eval_noop", + "Repete o corpo do bloco o numero de vezes indicado"); + TB_BLOCK_META("Control.RepeatUntil", "eval_noop", + "Repete o corpo enquanto a condicao for falsa"); + TB_BLOCK_META("Control.If", "eval_noop", + "Executa o corpo se a condicao for verdadeira"); + TB_BLOCK_META("Control.IfElse", "eval_noop", + "Executa o corpo 'entao' ou 'senao' conforme a condicao"); + static const TinyBlocks_SlotDesc times_in[] = {{"vezes", TB_VALUE_INTEGER}}; static const TinyBlocks_SlotDesc cond_in[] = {{"condição", TB_VALUE_INTEGER}}; return reg(rt, lib, "Control.Repeat", times_in, 1) diff --git a/libraries/tinyblocks-markdown/src/tb_markdown.c b/libraries/tinyblocks-markdown/src/tb_markdown.c index ff80a18..3bdc7c2 100644 --- a/libraries/tinyblocks-markdown/src/tb_markdown.c +++ b/libraries/tinyblocks-markdown/src/tb_markdown.c @@ -503,6 +503,14 @@ static bool markdown_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { { 55/255.f, 71/255.f, 79/255.f, 1.f}, }; tb_library_set_category(lib, &cat); + + /* Metadados dos blocos (tooltip do editor: Alt + hover). */ + TB_BLOCK_META("Markdown.ToHtml", "eval_html", + "Converte Markdown para HTML"); + TB_BLOCK_META("Markdown.ToXml", "eval_xml", + "Converte Markdown para XML"); + TB_BLOCK_META("Markdown.ToSvg", "eval_svg", + "Converte Markdown para uma imagem SVG"); static const TinyBlocks_SlotDesc in_md[] = {{"markdown", TB_VALUE_TEXT}}; static const TinyBlocks_SlotDesc out_text[] = {{"texto", TB_VALUE_TEXT}}; return reg(rt, lib, "Markdown.ToHtml", in_md, 1, out_text, 1, eval_html) diff --git a/libraries/tinyblocks-opentype/src/tb_opentype.c b/libraries/tinyblocks-opentype/src/tb_opentype.c index 852e33a..36b1f0f 100644 --- a/libraries/tinyblocks-opentype/src/tb_opentype.c +++ b/libraries/tinyblocks-opentype/src/tb_opentype.c @@ -363,6 +363,20 @@ static bool opentype_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { }; tb_library_set_category(lib, &cat); + /* Metadados dos blocos (tooltip do editor: Alt + hover). */ + TB_BLOCK_META("OpenType.Load", "eval_load", + "Carrega uma fonte OpenType/TrueType"); + TB_BLOCK_META("OpenType.UnitsPerEm", "eval_upem", + "Unidades por em da fonte"); + TB_BLOCK_META("OpenType.GlyphCount", "eval_glyphs", + "Numero de glifos da fonte"); + TB_BLOCK_META("OpenType.Advance", "eval_advance", + "Largura em pixels de um texto"); + TB_BLOCK_META("OpenType.Outline", "eval_outline", + "Contornos de um caractere (pontos JSON)"); + TB_BLOCK_META("OpenType.Render", "eval_render", + "Rasteriza um texto numa imagem"); + static const TinyBlocks_SlotDesc load_in[] = {{"caminho", TB_VALUE_TEXT}}; static const TinyBlocks_SlotDesc font_in[] = {{"fonte", TB_VALUE_REAL}}; static const TinyBlocks_SlotDesc adv_in[] = {{"fonte", TB_VALUE_REAL}, {"texto", TB_VALUE_TEXT}, {"tamanho", TB_VALUE_REAL}}; diff --git a/libraries/tinyblocks-operators/src/tb_operators.c b/libraries/tinyblocks-operators/src/tb_operators.c index b58451c..09d777f 100644 --- a/libraries/tinyblocks-operators/src/tb_operators.c +++ b/libraries/tinyblocks-operators/src/tb_operators.c @@ -179,6 +179,48 @@ static bool operators_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { }; tb_library_set_category(lib, &cat); + /* Metadados dos blocos (tooltip do editor: Alt + hover). */ + TB_BLOCK_META("Operators.Lt", "eval_op_lt", + "Verdadeiro se A for menor que B"); + TB_BLOCK_META("Operators.Eq", "eval_op_eq", + "Verdadeiro se A for igual a B"); + TB_BLOCK_META("Operators.Gt", "eval_op_gt", + "Verdadeiro se A for maior que B"); + TB_BLOCK_META("Operators.And", "eval_op_and", + "Verdadeiro se ambos os lados forem verdadeiros"); + TB_BLOCK_META("Operators.Or", "eval_op_or", + "Verdadeiro se ao menos um lado for verdadeiro"); + TB_BLOCK_META("Operators.Not", "eval_op_not", + "Inverte o valor logico"); + TB_BLOCK_META("Operators.Random", "eval_op_random", + "Numero inteiro aleatorio entre A e B"); + TB_BLOCK_META("Math.Add", "eval_add", + "Soma dois numeros"); + TB_BLOCK_META("Math.Sub", "eval_sub", + "Subtrai B de A"); + TB_BLOCK_META("Math.Mul", "eval_mul", + "Multiplica dois numeros"); + TB_BLOCK_META("Math.Div", "eval_div", + "Divide A por B"); + TB_BLOCK_META("Math.Mod", "eval_mod", + "Resto da divisao de A por B"); + TB_BLOCK_META("Math.Abs", "eval_abs", + "Valor absoluto de um numero"); + TB_BLOCK_META("Math.Max", "eval_max", + "Maior entre dois numeros"); + TB_BLOCK_META("Math.Min", "eval_min", + "Menor entre dois numeros"); + TB_BLOCK_META("Text.Concat", "eval_concat", + "Junta dois textos"); + TB_BLOCK_META("Text.Length", "eval_len", + "Numero de caracteres de um texto"); + TB_BLOCK_META("Literal.Integer", "eval_lit_int", + "Valor inteiro digitado"); + TB_BLOCK_META("Literal.Real", "eval_lit_real", + "Valor real (decimal) digitado"); + TB_BLOCK_META("Literal.Text", "eval_lit_text", + "Valor de texto digitado"); + static const TinyBlocks_SlotDesc any2[] = {{"a", TB_VALUE_NONE}, {"b", TB_VALUE_NONE}}; static const TinyBlocks_SlotDesc bool2[] = {{"a", TB_VALUE_INTEGER}, {"b", TB_VALUE_INTEGER}}; static const TinyBlocks_SlotDesc bool1[] = {{"a", TB_VALUE_INTEGER}}; diff --git a/libraries/tinyblocks-pen/src/tb_pen.c b/libraries/tinyblocks-pen/src/tb_pen.c index 2988912..ea9033e 100644 --- a/libraries/tinyblocks-pen/src/tb_pen.c +++ b/libraries/tinyblocks-pen/src/tb_pen.c @@ -70,6 +70,24 @@ static bool pen_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { }; tb_library_set_category(lib, &cat); + /* Metadados dos blocos (tooltip do editor: Alt + hover). */ + TB_BLOCK_META("Stage.Clear", "eval_clear", + "Limpa o palco"); + TB_BLOCK_META("Stage.Background", "eval_background", + "Define a cor de fundo do palco"); + TB_BLOCK_META("Stage.PenColor", "eval_pen_color", + "Define a cor da caneta"); + TB_BLOCK_META("Stage.PenSize", "eval_pen_size", + "Define a espessura da caneta"); + TB_BLOCK_META("Stage.Line", "eval_line", + "Desenha uma linha entre dois pontos"); + TB_BLOCK_META("Stage.Rect", "eval_rect", + "Desenha um retangulo"); + TB_BLOCK_META("Stage.Circle", "eval_circle", + "Desenha um circulo"); + TB_BLOCK_META("Stage.Text", "eval_text", + "Escreve um texto no palco"); + static const TinyBlocks_SlotDesc rgb[] = {{"r", TB_VALUE_REAL}, {"g", TB_VALUE_REAL}, {"b", TB_VALUE_REAL}}; static const TinyBlocks_SlotDesc size[] = {{"tamanho", TB_VALUE_REAL}}; static const TinyBlocks_SlotDesc line[] = {{"x1", TB_VALUE_REAL}, {"y1", TB_VALUE_REAL}, diff --git a/libraries/tinyblocks-regex/src/tb_regex.c b/libraries/tinyblocks-regex/src/tb_regex.c index 567f2eb..35d131b 100644 --- a/libraries/tinyblocks-regex/src/tb_regex.c +++ b/libraries/tinyblocks-regex/src/tb_regex.c @@ -292,6 +292,24 @@ static bool regex_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { }; tb_library_set_category(lib, &cat); + /* Metadados dos blocos (tooltip do editor: Alt + hover). */ + TB_BLOCK_META("Regex.Test", "eval_test", + "Verdadeiro se o padrao ocorre no texto"); + TB_BLOCK_META("Regex.Match", "eval_match", + "Verdadeiro se o padrao casa o texto inteiro"); + TB_BLOCK_META("Regex.Find", "eval_find", + "Primeiro trecho do texto que casa o padrao"); + TB_BLOCK_META("Regex.Group", "eval_group", + "Conteudo do grupo N do primeiro casamento"); + TB_BLOCK_META("Regex.Count", "eval_count", + "Quantas vezes o padrao ocorre no texto"); + TB_BLOCK_META("Regex.Groups", "eval_groups", + "Numero de grupos do padrao"); + TB_BLOCK_META("Regex.Replace", "eval_replace", + "Substitui as ocorrencias ($1..$9 como retorno)"); + TB_BLOCK_META("Regex.Split", "eval_split", + "Divide o texto pelo padrao (lista JSON)"); + static const TinyBlocks_SlotDesc two[] = {{"texto", TB_VALUE_TEXT}, {"padrao", TB_VALUE_TEXT}}; static const TinyBlocks_SlotDesc three[] = {{"texto", TB_VALUE_TEXT}, {"padrao", TB_VALUE_TEXT}, {"com", TB_VALUE_TEXT}}; static const TinyBlocks_SlotDesc grp[] = {{"texto", TB_VALUE_TEXT}, {"padrao", TB_VALUE_TEXT}, {"grupo", TB_VALUE_REAL}}; diff --git a/libraries/tinyblocks-syntax/src/tb_syntax.c b/libraries/tinyblocks-syntax/src/tb_syntax.c index 6a782ab..3bcc8a2 100644 --- a/libraries/tinyblocks-syntax/src/tb_syntax.c +++ b/libraries/tinyblocks-syntax/src/tb_syntax.c @@ -483,6 +483,14 @@ static bool syntax_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { }; tb_library_set_category(lib, &cat); + /* Metadados dos blocos (tooltip do editor: Alt + hover). */ + TB_BLOCK_META("Syntax.Highlight", "eval_highlight", + "Realca a sintaxe de um texto (XML)"); + TB_BLOCK_META("Syntax.Inline", "eval_inline", + "Realca uma unica linha de codigo (XML)"); + TB_BLOCK_META("Syntax.Languages", "eval_languages", + "Lista as linguagens suportadas"); + static const TinyBlocks_SlotDesc hl_in[] = {{"texto", TB_VALUE_TEXT}, {"linguagem", TB_VALUE_TEXT}}; static const TinyBlocks_SlotDesc out_text_s[] = {{"xml", TB_VALUE_TEXT}}; static const TinyBlocks_SlotDesc out_list_s[] = {{"linguagens", TB_VALUE_TEXT}}; diff --git a/libraries/tinyblocks-variables/src/tb_variables.c b/libraries/tinyblocks-variables/src/tb_variables.c index a307d2b..e956254 100644 --- a/libraries/tinyblocks-variables/src/tb_variables.c +++ b/libraries/tinyblocks-variables/src/tb_variables.c @@ -127,6 +127,14 @@ static bool variables_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { }; tb_library_set_category(lib, &cat); + /* Metadados dos blocos (tooltip do editor: Alt + hover). */ + TB_BLOCK_META("Variables.Set", "eval_var_set", + "Define o valor de uma variavel"); + TB_BLOCK_META("Variables.Change", "eval_var_change", + "Altera (incrementa) o valor de uma variavel"); + TB_BLOCK_META("Variables.Get", "eval_var_get", + "Le o valor de uma variavel"); + static const TinyBlocks_SlotDesc var_in[] = {{"variável", TB_VALUE_TEXT}}; static const TinyBlocks_SlotDesc var_set[] = {{"variável", TB_VALUE_TEXT}, {"valor", TB_VALUE_NONE}}; static const TinyBlocks_SlotDesc any_out[] = {{"valor", TB_VALUE_NONE}}; diff --git a/src/tb_meta.c b/src/tb_meta.c new file mode 100644 index 0000000..2a0ca95 --- /dev/null +++ b/src/tb_meta.c @@ -0,0 +1,154 @@ +/* + * TinyBlocks - tb_meta.c + * Registro global de metadados de blocos (arquivo/linha/funcao/doc) e + * utilitarios para localizar e ler o codigo-fonte correspondente. + */ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include +#include + +#include +#include + +static TinyBlocks_BlockMeta* s_meta = NULL; +static int s_n = 0, s_cap = 0; + +void tb_meta_declare(const char* type_name, const char* func_name, + const char* file, const char* doc) { + if (!type_name) return; + for (int i = 0; i < s_n; i++) { + if (strcmp(s_meta[i].type_name, type_name) == 0) { + /* atualiza mantendo os ponteiros mais completos */ + if (func_name) s_meta[i].func_name = func_name; + if (file) s_meta[i].file = file; + if (doc) s_meta[i].doc = doc; + return; + } + } + if (s_n == s_cap) { + int cap = s_cap ? s_cap * 2 : 64; + TinyBlocks_BlockMeta* p = (TinyBlocks_BlockMeta*)realloc(s_meta, (size_t)cap * sizeof(*p)); + if (!p) return; + s_meta = p; s_cap = cap; + } + TinyBlocks_BlockMeta* m = &s_meta[s_n++]; + m->type_name = type_name; + m->func_name = func_name; + m->file = file; + m->doc = doc; +} + +const TinyBlocks_BlockMeta* tb_meta_find(const char* type_name) { + if (!type_name) return NULL; + for (int i = 0; i < s_n; i++) + if (strcmp(s_meta[i].type_name, type_name) == 0) return &s_meta[i]; + return NULL; +} + +int tb_meta_count(void) { return s_n; } +const TinyBlocks_BlockMeta* tb_meta_at(int i) { + if (i < 0 || i >= s_n) return NULL; + return &s_meta[i]; +} +void tb_meta_clear(void) { s_n = 0; } + +static bool try_file(const char* path, char* out, size_t cap) { + FILE* f = fopen(path, "r"); + if (!f) return false; + fclose(f); + snprintf(out, cap, "%s", path); + return true; +} + +bool tb_meta_resolve_file(const char* file, char* out, size_t cap) { + if (!file || !*file) return false; + if (try_file(file, out, cap)) return true; + /* tenta a partir da raiz do repositorio (../../) */ + char cand[1200]; + snprintf(cand, sizeof(cand), "../../%s", file); + if (try_file(cand, out, cap)) return true; + /* procura em libraries//file */ + const char* bases[] = { "libraries", "../libraries", "../../libraries", NULL }; + for (int b = 0; bases[b]; b++) { + DIR* d = opendir(bases[b]); + if (!d) continue; + struct dirent* e; + while ((e = readdir(d))) { + if (e->d_name[0] == '.') continue; + snprintf(cand, sizeof(cand), "%s/%s/%s", bases[b], e->d_name, file); + if (try_file(cand, out, cap)) { closedir(d); return true; } + } + closedir(d); + } + return false; +} + +/* Verifica se `line` contem `name` como identificador seguido de '(' ou de + * espaco+'('. Retorna true e o indice do inicio do nome. */ +static bool line_has_func(const char* line, const char* name, const char** where) { + size_t nl = strlen(name); + const char* p = line; + while ((p = strstr(p, name))) { + bool ok_before = (p == line) || !(isalnum((unsigned char)p[-1]) || p[-1] == '_'); + bool ok_after = !(isalnum((unsigned char)p[nl]) || p[nl] == '_'); + if (ok_before && ok_after) { + const char* q = p + nl; + while (*q == ' ' || *q == '\t') q++; + if (*q == '(') { if (where) *where = p; return true; } + } + p += nl; + } + return false; +} + +int tb_meta_line(const TinyBlocks_BlockMeta* m) { + if (!m || !m->file || !m->func_name) return -1; + char path[1200]; + if (!tb_meta_resolve_file(m->file, path, sizeof(path))) return -1; + FILE* f = fopen(path, "r"); + if (!f) return -1; + char line[2048]; + int ln = 0, found = -1; + while (fgets(line, sizeof(line), f)) { + ln++; + const char* w = NULL; + if (line_has_func(line, m->func_name, &w)) { + found = ln; + /* definicao vs. declaracao: para na primeira ocorrencia */ + break; + } + } + fclose(f); + return found; +} + +int tb_meta_read_lines(const TinyBlocks_BlockMeta* m, int line, + int max_lines, char* buf, size_t cap) { + if (buf && cap) buf[0] = 0; + if (!m || !m->file || line < 1 || max_lines < 1) return 0; + char path[1200]; + if (!tb_meta_resolve_file(m->file, path, sizeof(path))) return 0; + FILE* f = fopen(path, "r"); + if (!f) return 0; + char tmp[2048]; + int ln = 0, got = 0; + size_t used = 0; + while (fgets(tmp, sizeof(tmp), f)) { + ln++; + if (ln < line) continue; + if (got >= max_lines) break; + size_t tl = strlen(tmp); + while (tl && (tmp[tl-1] == '\n' || tmp[tl-1] == '\r')) tmp[--tl] = 0; + size_t need = tl + (got ? 1 : 0); + if (used + need + 1 >= cap) break; + if (got) buf[used++] = '\n'; + memcpy(buf + used, tmp, tl); + used += tl; buf[used] = 0; + got++; + } + fclose(f); + return got; +} diff --git a/tests/meta_test.c b/tests/meta_test.c new file mode 100644 index 0000000..e62f865 --- /dev/null +++ b/tests/meta_test.c @@ -0,0 +1,51 @@ +/* + * meta_test.c - testa o registro de metadados de blocos (core tb_meta). + */ +#include +#include +#include +#include + +static int fails = 0; +#define CHECK(c, m) do { if (c) printf("ok %s\n", m); else { printf("FAIL %s\n", m); fails++; } } while (0) + +int main(void) { + TinyBlocks* rt; + tinyblocks_init(&rt, NULL); + tb_library_register(rt, tb_operators_get_library()); + + CHECK(tb_meta_count() > 10, "metadados declarados pela operators"); + + const TinyBlocks_BlockMeta* m = tb_meta_find("Math.Add"); + CHECK(m != NULL, "acha Math.Add"); + if (m) { + CHECK(m->func_name && !strcmp(m->func_name, "eval_add"), "funcao = eval_add"); + CHECK(m->doc && strstr(m->doc, "Soma"), "doc menciona 'Soma'"); + CHECK(m->file && strstr(m->file, "tb_operators.c"), "arquivo tb_operators.c"); + } + + const TinyBlocks_BlockMeta* lt = tb_meta_find("Operators.Lt"); + CHECK(lt && lt->func_name && !strcmp(lt->func_name, "eval_op_lt"), "funcao de Operators.Lt"); + + CHECK(tb_meta_find("Nao.Existe") == NULL, "bloco desconhecido -> NULL"); + CHECK(tb_meta_at(0) != NULL, "tb_meta_at(0)"); + CHECK(tb_meta_at(99999) == NULL, "tb_meta_at fora do range -> NULL"); + + if (m) { + char path[1200]; + CHECK(tb_meta_resolve_file(m->file, path, sizeof(path)), "resolve o arquivo-fonte"); + int line = tb_meta_line(m); + CHECK(line > 0, "localiza a linha de eval_add"); + if (line > 0) { + char buf[1024]; + int got = tb_meta_read_lines(m, line, 1, buf, sizeof(buf)); + CHECK(got == 1 && strstr(buf, "eval_add"), "le a linha da funcao"); + } + } + + tinyblocks_deinit(&rt); + + if (fails) printf("\n%d FALHA(S)\n", fails); + else printf("\nTUDO OK\n"); + return fails != 0; +}