Core: registro de metadados de blocos (tb_meta) + testes; metadados declarados nas libraries

This commit is contained in:
mguinhos committed 2026-10-20 22:07:40 -03:00
1 parent 22ae18f508
commit 036162280d
14 files changed
+397 -2

No files matched your search

+154
View File
@@ -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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <ctype.h>
#include <tinyblocks/tinyblocks.h>
#include <tinyblocks/tb_meta.h>
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/<dir>/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;
}