Libraries os, string, number e vector (listas); pastas e booleanos no fs
- tinyblocks-fs: itens da pasta (lista), quantidade/item N da pasta e os booleanos "é arquivo?", "é pasta?" e "existe?". - tinyblocks-os (categoria Sistema): sistema operacional, distribuição, versão do kernel, arquitetura, computador, usuário, pastas, CPUs, memória, tempo ligado e "o sistema é [ ]?". - tinyblocks-string (Texto): maiúsculas/minúsculas, trecho, letra N, posição, troca, apara, repete, inverte, divide em lista e testes; conta letras UTF-8. - tinyblocks-number (Números): reais, potência, raiz, arredondamentos, trigonometria em graus, logaritmos, pi, aleatório, limite, formatação, par/primo/inteiro. - tinyblocks-vector (Listas, blocos List.*): listas JSON imutáveis com adicionar, inserir, remover, trocar, item N, posição, fatia, ordenar, juntar, soma; campos aceitam "[1,2]" ou "a, b". O exemplo de desenho vetorial passa a se chamar tinyblocks-vector-draw. - Editor: saída única cujo nome termina em '?' vira bloco booleano (hexágono); listas JSON aparecem como texto no "escreva"; rótulos PT. - Testes: make test-os e make test-stdlib. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f0abc7593a
commit
33705b2903
25 files changed
+2262
-9
No files matched your search
@@ -3,7 +3,7 @@
|
||||
name = "tinyblocks-fs"
|
||||
version = "1.0.0"
|
||||
category = "Arquivos"
|
||||
description = "Abra, leia, escreva, anexe e posicao de arquivo"
|
||||
description = "Abra, leia, escreva e anexe arquivos; liste pastas; teste arquivo/pasta"
|
||||
color = [0, 188, 212, 255]
|
||||
dark = [0, 151, 167, 255]
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/*
|
||||
* TinyBlocks - tb_fs.c
|
||||
* Categoria "Arquivos": abra / leia / escreva / anexe / posicao.
|
||||
* Categoria "Arquivos": abra / leia / escreva / anexe / posicao, listar
|
||||
* pasta (FS.ListDir/ListCount/ListItem) e testes FS.IsFile/IsDir/Exists.
|
||||
* Mantem uma pequena tabela de arquivos abertos (handle inteiro >= 1).
|
||||
*/
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
@@ -8,8 +9,12 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <strings.h>
|
||||
#include <dirent.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include <tinyblocks/tb_json_block.h>
|
||||
#include "tb_fs.h"
|
||||
|
||||
#define FS_MAX_FILES 64
|
||||
@@ -107,6 +112,104 @@ static void eval_close(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
/* -- Pastas --------------------------------------------------------------- */
|
||||
|
||||
static int cmp_name(const void* a, const void* b) {
|
||||
return strcasecmp(*(char* const*)a, *(char* const*)b);
|
||||
}
|
||||
|
||||
/* Nomes da pasta (sem "." e ".."), em ordem alfabetica. Vazio = pasta
|
||||
atual. Devolve um vetor alocado (libere com free_names) e o total. */
|
||||
static char** list_names(const char* path, int* count) {
|
||||
*count = 0;
|
||||
DIR* d = opendir(path[0] ? path : ".");
|
||||
if (!d) return NULL;
|
||||
char** v = NULL;
|
||||
int n = 0, cap = 0;
|
||||
struct dirent* de;
|
||||
while ((de = readdir(d)) != NULL) {
|
||||
if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) continue;
|
||||
if (n == cap) {
|
||||
int nc = cap ? cap * 2 : 32;
|
||||
char** nv = realloc(v, (size_t)nc * sizeof(char*));
|
||||
if (!nv) break;
|
||||
v = nv;
|
||||
cap = nc;
|
||||
}
|
||||
size_t len = strlen(de->d_name);
|
||||
char* copy = malloc(len + 1);
|
||||
if (!copy) break;
|
||||
memcpy(copy, de->d_name, len + 1);
|
||||
v[n++] = copy;
|
||||
}
|
||||
closedir(d);
|
||||
if (n > 1) qsort(v, (size_t)n, sizeof(char*), cmp_name);
|
||||
*count = n;
|
||||
return v;
|
||||
}
|
||||
|
||||
static void free_names(char** v, int n) {
|
||||
for (int i = 0; i < n; i++) free(v[i]);
|
||||
free(v);
|
||||
}
|
||||
|
||||
static void eval_listdir(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
int n = 0;
|
||||
char** names = list_names(in_text(self, rt, 0), &n);
|
||||
TinyBlocks_JsonValue* arr = tb_json_array();
|
||||
for (int i = 0; arr && i < n; i++) {
|
||||
TinyBlocks_JsonValue* s = tb_json_string(names[i]);
|
||||
if (s) { tb_json_array_push(arr, s); tb_json_release(s); }
|
||||
}
|
||||
free_names(names, n);
|
||||
tb_block_set_output(self, 0, tb_value_json(arr));
|
||||
tb_json_release(arr);
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void eval_listcount(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
int n = 0;
|
||||
char** names = list_names(in_text(self, rt, 0), &n);
|
||||
free_names(names, n);
|
||||
tb_block_set_output(self, 0, tb_value_integer(n));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
/* item N (1..total) da pasta, em ordem alfabetica; fora da faixa = "" */
|
||||
static void eval_listitem(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
int n = 0;
|
||||
char** names = list_names(in_text(self, rt, 0), &n);
|
||||
int64_t k = in_int(self, rt, 1);
|
||||
const char* item = (k >= 1 && k <= n) ? names[k - 1] : "";
|
||||
tb_block_set_output(self, 0, tb_value_text(item));
|
||||
free_names(names, n);
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void eval_isfile(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
struct stat st;
|
||||
const char* p = in_text(self, rt, 0);
|
||||
bool ok = p[0] && stat(p, &st) == 0 && S_ISREG(st.st_mode);
|
||||
tb_block_set_output(self, 0, tb_value_integer(ok ? 1 : 0));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void eval_isdir(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
struct stat st;
|
||||
const char* p = in_text(self, rt, 0);
|
||||
bool ok = p[0] && stat(p, &st) == 0 && S_ISDIR(st.st_mode);
|
||||
tb_block_set_output(self, 0, tb_value_integer(ok ? 1 : 0));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void eval_exists(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
struct stat st;
|
||||
const char* p = in_text(self, rt, 0);
|
||||
bool ok = p[0] && stat(p, &st) == 0;
|
||||
tb_block_set_output(self, 0, tb_value_integer(ok ? 1 : 0));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
/* -- Registro ------------------------------------------------------------ */
|
||||
|
||||
static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
|
||||
@@ -134,13 +237,42 @@ static bool fs_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
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}};
|
||||
static const TinyBlocks_SlotDesc dir_in[] = {{"pasta", TB_VALUE_TEXT}};
|
||||
static const TinyBlocks_SlotDesc item_in[] = {{"pasta", TB_VALUE_TEXT}, {"numero", TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc path_in[] = {{"caminho", TB_VALUE_TEXT}};
|
||||
static const TinyBlocks_SlotDesc list_out[] = {{"lista", TB_VALUE_JSON}};
|
||||
static const TinyBlocks_SlotDesc count_out[] = {{"quantidade", TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc name_out[] = {{"nome", TB_VALUE_TEXT}};
|
||||
/* saida terminada em '?' = bloco booleano (hexagonal no editor) */
|
||||
static const TinyBlocks_SlotDesc file_out[] = {{"é arquivo?", TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc isdir_out[] = {{"é pasta?", TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc exist_out[] = {{"existe?", TB_VALUE_INTEGER}};
|
||||
|
||||
TB_BLOCK_META("FS.ListDir", "eval_listdir",
|
||||
"Lista com os nomes dos itens da pasta (ordem alfabetica)");
|
||||
TB_BLOCK_META("FS.ListCount", "eval_listcount",
|
||||
"Quantos itens (arquivos e pastas) a pasta tem");
|
||||
TB_BLOCK_META("FS.ListItem", "eval_listitem",
|
||||
"Nome do item N da pasta (1 = primeiro, ordem alfabetica)");
|
||||
TB_BLOCK_META("FS.IsFile", "eval_isfile",
|
||||
"Verdadeiro se o caminho for um arquivo");
|
||||
TB_BLOCK_META("FS.IsDir", "eval_isdir",
|
||||
"Verdadeiro se o caminho for uma pasta");
|
||||
TB_BLOCK_META("FS.Exists", "eval_exists",
|
||||
"Verdadeiro se o caminho existir (arquivo ou pasta)");
|
||||
|
||||
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);
|
||||
&& reg(rt, lib, "FS.Close", close_in, 1, NULL, 0, eval_close)
|
||||
&& reg(rt, lib, "FS.ListDir", dir_in, 1, list_out, 1, eval_listdir)
|
||||
&& reg(rt, lib, "FS.ListCount", dir_in, 1, count_out, 1, eval_listcount)
|
||||
&& reg(rt, lib, "FS.ListItem", item_in, 2, name_out, 1, eval_listitem)
|
||||
&& reg(rt, lib, "FS.IsFile", path_in, 1, file_out, 1, eval_isfile)
|
||||
&& reg(rt, lib, "FS.IsDir", path_in, 1, isdir_out, 1, eval_isdir)
|
||||
&& reg(rt, lib, "FS.Exists", path_in, 1, exist_out, 1, eval_exists);
|
||||
}
|
||||
|
||||
static void fs_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
|
||||
Reference in new issue
Block a user