Files
mguinhosandClaude Opus 5 bf298ccefa Arquivos: bloco "leia o arquivo %1" (FS.ReadText)
Le o arquivo inteiro de uma vez e devolve o texto (vazio se nao abrir).
Com isso da para ler .obj/.json direto nos blocos, sem abrir/ler/fechar
na mao; e o que faltava para um leitor de Wavefront .obj em project.json.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-10-29 00:48:48 -03:00

157 lines
6.2 KiB
C

/*
* os_fs_test.c - testa tinyblocks-os (informacoes do sistema) e os blocos
* de pasta do tinyblocks-fs (FS.ListDir / ListCount / ListItem / ReadText /
* IsFile / IsDir / Exists).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <tinyblocks/tinyblocks.h>
#include <tinyblocks/tb_json_block.h>
#include <tb_fs.h>
#include <tb_os.h>
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)
static TinyBlocks* rt;
/* Cria o bloco, preenche as entradas (texto/inteiro) e avalia */
static TinyBlocks_Block* run(const char* type, const char* a, long long b) {
TinyBlocks_TypeBlock* t = tb_rt_find_type(rt, type);
if (!t) return NULL;
TinyBlocks_Block* blk = tb_block_create(rt, t);
if (a) {
TinyBlocks_Value v = tb_value_text(a);
tb_block_set_default(blk, 0, v);
tb_value_free(&v);
}
if (b >= 0) {
TinyBlocks_Value v = tb_value_integer(b);
tb_block_set_default(blk, 1, v);
tb_value_free(&v);
}
tb_block_eval(blk, rt);
return blk;
}
static long long out_int(TinyBlocks_Block* b) {
return (b && b->outputs[0].kind == TB_VALUE_INTEGER) ? b->outputs[0].as.integer : -1;
}
static const char* out_text(TinyBlocks_Block* b) {
return (b && b->outputs[0].kind == TB_VALUE_TEXT && b->outputs[0].as.text) ? b->outputs[0].as.text : "";
}
static void touch(const char* p) {
FILE* f = fopen(p, "wb");
if (f) { fputs("x", f); fclose(f); }
}
int main(void) {
tinyblocks_init(&rt, NULL);
TinyBlocks_LibraryBlock* fs = tb_library_register(rt, tb_fs_get_library());
TinyBlocks_LibraryBlock* os = tb_library_register(rt, tb_os_get_library());
CHECK(fs && os, "libraries fs e os registradas");
const TinyBlocks_Category* cat = os ? tb_library_category(os) : NULL;
CHECK(cat && !strcmp(cat->name, "Sistema"), "categoria 'Sistema'");
/* -- tinyblocks-os -- */
char buf[512];
tb_os_name(buf, sizeof(buf));
CHECK(buf[0], "tb_os_name devolve um nome");
TinyBlocks_Block* b = run("OS.Name", NULL, -1);
CHECK(!strcmp(out_text(b), buf), "OS.Name igual a tb_os_name");
tb_block_destroy(b, rt);
b = run("OS.Distro", NULL, -1);
CHECK(out_text(b)[0], "OS.Distro devolve texto");
tb_block_destroy(b, rt);
b = run("OS.Version", NULL, -1);
CHECK(out_text(b)[0], "OS.Version devolve a versao do kernel");
tb_block_destroy(b, rt);
b = run("OS.Arch", NULL, -1);
CHECK(out_text(b)[0], "OS.Arch devolve a arquitetura");
tb_block_destroy(b, rt);
b = run("OS.User", NULL, -1);
CHECK(out_text(b)[0], "OS.User devolve o usuario");
tb_block_destroy(b, rt);
b = run("OS.Cpus", NULL, -1);
CHECK(out_int(b) >= 1, "OS.Cpus >= 1");
tb_block_destroy(b, rt);
b = run("OS.MemoryMB", NULL, -1);
CHECK(out_int(b) > 0, "OS.MemoryMB > 0");
tb_block_destroy(b, rt);
b = run("OS.Is", buf, -1);
CHECK(out_int(b) == 1, "OS.Is(nome do SO) verdadeiro");
tb_block_destroy(b, rt);
b = run("OS.Is", "SistemaQueNaoExiste", -1);
CHECK(out_int(b) == 0, "OS.Is(outro) falso");
tb_block_destroy(b, rt);
TinyBlocks_TypeBlock* tis = tb_rt_find_type(rt, "OS.Is");
CHECK(tis && tis->output_desc_count == 1 &&
tis->output_descs[0].name[strlen(tis->output_descs[0].name) - 1] == '?',
"OS.Is declara saida booleana ('?')");
/* -- tinyblocks-fs: pastas -- */
char dir[] = "/tmp/tb_osfs_XXXXXX";
CHECK(mkdtemp(dir) != NULL, "pasta temporaria");
char p[256];
snprintf(p, sizeof(p), "%s/b.txt", dir); touch(p);
snprintf(p, sizeof(p), "%s/A.json", dir); touch(p);
snprintf(p, sizeof(p), "%s/sub", dir); mkdir(p, 0755);
b = run("FS.ListCount", dir, -1);
CHECK(out_int(b) == 3, "FS.ListCount = 3");
tb_block_destroy(b, rt);
b = run("FS.ListDir", dir, -1);
bool list_ok = b && b->outputs[0].kind == TB_VALUE_JSON;
if (list_ok) {
TinyBlocks_JsonValue* arr = b->outputs[0].as.json;
list_ok = tb_json_array_length(arr) == 3;
TinyBlocks_JsonValue* first = list_ok ? tb_json_array_get(arr, 0) : NULL;
list_ok = list_ok && first && !strcmp(tb_json_as_string(first), "A.json");
if (first) tb_json_release(first);
}
CHECK(list_ok, "FS.ListDir devolve lista ordenada [A.json, b.txt, sub]");
tb_block_destroy(b, rt);
b = run("FS.ListItem", dir, 2);
CHECK(!strcmp(out_text(b), "b.txt"), "FS.ListItem(2) = b.txt");
tb_block_destroy(b, rt);
b = run("FS.ListItem", dir, 9);
CHECK(!strcmp(out_text(b), ""), "FS.ListItem fora da faixa = vazio");
tb_block_destroy(b, rt);
/* FS.ReadText: o arquivo inteiro de uma vez */
snprintf(p, sizeof(p), "%s/b.txt", dir);
FILE* wf = fopen(p, "wb");
if (wf) { fputs("v 1 2 3\nf 1 2 3\n", wf); fclose(wf); }
b = run("FS.ReadText", p, -1);
CHECK(b != NULL && !strcmp(out_text(b), "v 1 2 3\nf 1 2 3\n"), "FS.ReadText le o arquivo inteiro");
tb_block_destroy(b, rt);
b = run("FS.ReadText", "/tmp/tb_nao_existe_xyz.txt", -1);
CHECK(b != NULL && !strcmp(out_text(b), ""), "FS.ReadText de arquivo inexistente = vazio");
tb_block_destroy(b, rt);
snprintf(p, sizeof(p), "%s/b.txt", dir);
b = run("FS.IsFile", p, -1); CHECK(out_int(b) == 1, "FS.IsFile(arquivo) verdadeiro"); tb_block_destroy(b, rt);
b = run("FS.IsDir", p, -1); CHECK(out_int(b) == 0, "FS.IsDir(arquivo) falso"); tb_block_destroy(b, rt);
b = run("FS.Exists", p, -1); CHECK(out_int(b) == 1, "FS.Exists(arquivo) verdadeiro"); tb_block_destroy(b, rt);
remove(p);
snprintf(p, sizeof(p), "%s/sub", dir);
b = run("FS.IsDir", p, -1); CHECK(out_int(b) == 1, "FS.IsDir(pasta) verdadeiro"); tb_block_destroy(b, rt);
b = run("FS.IsFile", p, -1); CHECK(out_int(b) == 0, "FS.IsFile(pasta) falso"); tb_block_destroy(b, rt);
rmdir(p);
snprintf(p, sizeof(p), "%s/A.json", dir);
remove(p);
b = run("FS.Exists", p, -1); CHECK(out_int(b) == 0, "FS.Exists(apagado) falso"); tb_block_destroy(b, rt);
rmdir(dir);
tinyblocks_deinit(&rt);
if (fails) printf("\n%d FALHA(S)\n", fails);
else printf("\nTUDO OK\n");
return fails != 0;
}