- tinyblocks-toml: lexer + parser TOML (include/lexeme.h, include/ast.h, src/lexer.c, src/parser.c) e bloco Toml.Get. - tinyblocks-fs: categoria "Arquivos" com FS.Open/Read/Write/Append/Tell/Close. - library.toml em cada library com metadados (categoria, cor, descricao) e receita de build, sem precisar embutir isso no .so. - Makefiles proprios por library com build/ local; Makefile raiz delega. - Palco (tb_stage) movido para o core; pen deixa de depender do editor, o que torna os .so arrastaveis. - Editor linka com -rdynamic para resolver simbolos do core em .so via dlopen. - Testes: test-toml (parser) e test-plugin (dlopen de .so standalone).
160 lines
5.6 KiB
C
160 lines
5.6 KiB
C
/*
|
|
* TinyBlocks - tb_fs.c
|
|
* Categoria "Arquivos": abra / leia / escreva / anexe / posicao.
|
|
* Mantem uma pequena tabela de arquivos abertos (handle inteiro >= 1).
|
|
*/
|
|
#define _POSIX_C_SOURCE 200809L
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdint.h>
|
|
|
|
#include <tinyblocks/tinyblocks.h>
|
|
#include "tb_fs.h"
|
|
|
|
#define FS_MAX_FILES 64
|
|
|
|
static FILE* s_files[FS_MAX_FILES];
|
|
|
|
static int fs_slot(void) {
|
|
for (int i = 0; i < FS_MAX_FILES; i++) if (!s_files[i]) return i;
|
|
return -1;
|
|
}
|
|
|
|
static FILE* fs_get(int64_t handle) {
|
|
int i = (int)handle - 1;
|
|
if (i < 0 || i >= FS_MAX_FILES) return NULL;
|
|
return s_files[i];
|
|
}
|
|
|
|
static void fs_close_slot(int i) {
|
|
if (i >= 0 && i < FS_MAX_FILES && s_files[i]) { fclose(s_files[i]); s_files[i] = NULL; }
|
|
}
|
|
|
|
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 int64_t in_int(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
|
|
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
|
|
if (v.kind == TB_VALUE_INTEGER) return v.as.integer;
|
|
if (v.kind == TB_VALUE_REAL) return (int64_t)v.as.real;
|
|
return 0;
|
|
}
|
|
|
|
static void eval_open(TinyBlocks_Block* self, TinyBlocks* rt) {
|
|
const char* path = in_text(self, rt, 0);
|
|
int slot = fs_slot();
|
|
int64_t handle = 0;
|
|
if (slot >= 0 && path[0]) {
|
|
FILE* f = fopen(path, "rb");
|
|
if (f) { s_files[slot] = f; handle = slot + 1; }
|
|
}
|
|
tb_block_set_output(self, 0, tb_value_integer(handle));
|
|
self->dirty = false;
|
|
}
|
|
|
|
static void eval_read(TinyBlocks_Block* self, TinyBlocks* rt) {
|
|
FILE* f = fs_get(in_int(self, rt, 0));
|
|
int64_t want = in_int(self, rt, 1);
|
|
char* buf = NULL; size_t n = 0;
|
|
if (f) {
|
|
long start = ftell(f);
|
|
fseek(f, 0, SEEK_END);
|
|
long end = ftell(f);
|
|
fseek(f, start, SEEK_SET);
|
|
size_t avail = (end > start) ? (size_t)(end - start) : 0;
|
|
if (want > 0 && (size_t)want < avail) avail = (size_t)want;
|
|
buf = (char*)malloc(avail + 1);
|
|
if (buf) {
|
|
n = fread(buf, 1, avail, f);
|
|
buf[n] = 0;
|
|
}
|
|
}
|
|
TinyBlocks_Value v = tb_value_text(buf ? buf : "");
|
|
free(buf);
|
|
tb_block_set_output(self, 0, v);
|
|
self->dirty = false;
|
|
}
|
|
|
|
static void eval_write(TinyBlocks_Block* self, TinyBlocks* rt) {
|
|
FILE* f = fs_get(in_int(self, rt, 0));
|
|
const char* text = in_text(self, rt, 1);
|
|
if (f) fwrite(text, 1, strlen(text), f);
|
|
self->dirty = false;
|
|
}
|
|
|
|
static void eval_append(TinyBlocks_Block* self, TinyBlocks* rt) {
|
|
const char* path = in_text(self, rt, 0);
|
|
const char* text = in_text(self, rt, 1);
|
|
if (path[0]) {
|
|
FILE* f = fopen(path, "ab");
|
|
if (f) { fwrite(text, 1, strlen(text), f); fclose(f); }
|
|
}
|
|
self->dirty = false;
|
|
}
|
|
|
|
static void eval_tell(TinyBlocks_Block* self, TinyBlocks* rt) {
|
|
FILE* f = fs_get(in_int(self, rt, 0));
|
|
long pos = f ? ftell(f) : -1;
|
|
tb_block_set_output(self, 0, tb_value_integer(pos));
|
|
self->dirty = false;
|
|
}
|
|
|
|
static void eval_close(TinyBlocks_Block* self, TinyBlocks* rt) {
|
|
int64_t h = in_int(self, rt, 0);
|
|
fs_close_slot((int)h - 1);
|
|
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 fs_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
|
static const TinyBlocks_Category cat = {
|
|
"Arquivos",
|
|
{ 0/255.f, 188/255.f, 212/255.f, 1.f},
|
|
{ 0/255.f, 151/255.f, 167/255.f, 1.f},
|
|
};
|
|
tb_library_set_category(lib, &cat);
|
|
|
|
static const TinyBlocks_SlotDesc open_in[] = {{"nome", TB_VALUE_TEXT}};
|
|
static const TinyBlocks_SlotDesc read_in[] = {{"arquivo", TB_VALUE_INTEGER}, {"bytes", TB_VALUE_INTEGER}};
|
|
static const TinyBlocks_SlotDesc write_in[] = {{"arquivo", TB_VALUE_INTEGER}, {"texto", TB_VALUE_TEXT}};
|
|
static const TinyBlocks_SlotDesc append_in[] = {{"nome", TB_VALUE_TEXT}, {"texto", TB_VALUE_TEXT}};
|
|
static const TinyBlocks_SlotDesc tell_in[] = {{"arquivo", TB_VALUE_INTEGER}};
|
|
static const TinyBlocks_SlotDesc close_in[] = {{"arquivo", TB_VALUE_INTEGER}};
|
|
static const TinyBlocks_SlotDesc handle_out[]= {{"handle", TB_VALUE_INTEGER}};
|
|
static const TinyBlocks_SlotDesc text_out[] = {{"texto", TB_VALUE_TEXT}};
|
|
static const TinyBlocks_SlotDesc pos_out[] = {{"posicao",TB_VALUE_INTEGER}};
|
|
|
|
return reg(rt, lib, "FS.Open", open_in, 1, handle_out, 1, eval_open)
|
|
&& reg(rt, lib, "FS.Read", read_in, 2, text_out, 1, eval_read)
|
|
&& reg(rt, lib, "FS.Write", write_in, 2, NULL, 0, eval_write)
|
|
&& reg(rt, lib, "FS.Append", append_in, 2, NULL, 0, eval_append)
|
|
&& reg(rt, lib, "FS.Tell", tell_in, 1, pos_out, 1, eval_tell)
|
|
&& reg(rt, lib, "FS.Close", close_in, 1, NULL, 0, eval_close);
|
|
}
|
|
|
|
static void fs_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
|
(void)rt; (void)lib;
|
|
for (int i = 0; i < FS_MAX_FILES; i++) fs_close_slot(i);
|
|
}
|
|
|
|
TINYBLOCKS_DEFINE_LIBRARY(fs,
|
|
"tinyblocks-fs",
|
|
"1.0.0",
|
|
fs_init,
|
|
fs_deinit)
|
|
|
|
const TinyBlocks_LibraryDescriptor* tb_fs_get_library(void) {
|
|
return &_tb_lib_desc_fs;
|
|
}
|