diff --git a/docs/blocos.md b/docs/blocos.md index 67ff5a3..4baece2 100644 --- a/docs/blocos.md +++ b/docs/blocos.md @@ -23,7 +23,7 @@ ver [referencia-api.md](referencia-api.md)). Para regerar: [ferramentas/README.md](ferramentas/README.md). -Total: 436 tipos em 40 libraries. +Total: 437 tipos em 40 libraries. ## Índice @@ -38,7 +38,7 @@ Total: 436 tipos em 40 libraries. - [tinyblocks-vector](#tinyblocks-vector) (categoria "Listas") - 24 blocos - [tinyblocks-appearance](#tinyblocks-appearance) (categoria "Aparência") - 2 blocos - [tinyblocks-pen](#tinyblocks-pen) (categoria "Caneta") - 10 blocos -- [tinyblocks-fs](#tinyblocks-fs) (categoria "Arquivos") - 12 blocos +- [tinyblocks-fs](#tinyblocks-fs) (categoria "Arquivos") - 13 blocos - [tinyblocks-os](#tinyblocks-os) (categoria "Sistema") - 12 blocos - [tinyblocks-environ](#tinyblocks-environ) (categoria "Ambiente") - 4 blocos - [tinyblocks-socket](#tinyblocks-socket) (categoria "Socket") - 7 blocos @@ -298,6 +298,7 @@ Categoria na paleta: **Arquivos**. |---|---|---|---|---|---| | `FS.Open` | | repórter | `nome` (texto) | `handle` (inteiro) | | | `FS.Read` | | repórter | `arquivo` (inteiro), `bytes` (inteiro) | `texto` (texto) | | +| `FS.ReadText` | leia o arquivo (caminho)
padrões: `projeto.json` | repórter | `caminho` (texto) | `texto` (texto) | Le o arquivo inteiro de uma vez e devolve o texto (vazio se nao abrir) | | `FS.Write` | | comando | `arquivo` (inteiro), `texto` (texto) | - | | | `FS.Append` | | comando | `nome` (texto), `texto` (texto) | - | | | `FS.Tell` | | repórter | `arquivo` (inteiro) | `posicao` (inteiro) | | diff --git a/libraries/tinyblocks-editor/include/tb_editor_internal.h b/libraries/tinyblocks-editor/include/tb_editor_internal.h index 70eac3b..4af03af 100644 --- a/libraries/tinyblocks-editor/include/tb_editor_internal.h +++ b/libraries/tinyblocks-editor/include/tb_editor_internal.h @@ -389,6 +389,7 @@ static const BlockSpec s_specs[] = { {"Stage.ImageByNumber", "desenhe imagem número %1 em x %2 y %3", "1|0|0"}, {"Stage.Text", "escreva %1 no palco em x %2 y %3", "Olá!|-40|0"}, /* Arquivos: pastas e testes (tinyblocks-fs) */ + {"FS.ReadText", "leia o arquivo %1", "projeto.json"}, {"FS.ListDir", "itens da pasta %1", ".|"}, {"FS.ListCount", "quantidade de itens na pasta %1", ".|"}, {"FS.ListItem", "item %2 da pasta %1", ".|1"}, diff --git a/libraries/tinyblocks-fs/src/tb_fs.c b/libraries/tinyblocks-fs/src/tb_fs.c index e8481b5..274bb5c 100644 --- a/libraries/tinyblocks-fs/src/tb_fs.c +++ b/libraries/tinyblocks-fs/src/tb_fs.c @@ -78,6 +78,27 @@ static void eval_read(TinyBlocks_Block* self, TinyBlocks* rt) { self->dirty = false; } +static void eval_readtext(TinyBlocks_Block* self, TinyBlocks* rt) { + const char* path = tb_block_input_text(self, rt, 0); + char* buf = NULL; + if (path[0]) { + FILE* f = fopen(path, "rb"); + if (f) { + fseek(f, 0, SEEK_END); + long end = ftell(f); + fseek(f, 0, SEEK_SET); + size_t cap = (end > 0) ? (size_t)end : 0; + buf = (char*)malloc(cap + 1); + if (buf) { size_t n = fread(buf, 1, cap, f); buf[n] = 0; } + fclose(f); + } + } + 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 = tb_block_input_text(self, rt, 1); @@ -244,6 +265,8 @@ static bool fs_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { static const TinyBlocks_SlotDesc isdir_out[] = {{"é pasta?", TB_VALUE_INTEGER}}; static const TinyBlocks_SlotDesc exist_out[] = {{"existe?", TB_VALUE_INTEGER}}; + TB_BLOCK_META("FS.ReadText", "eval_readtext", + "Le o arquivo inteiro de uma vez e devolve o texto (vazio se nao abrir)"); TB_BLOCK_META("FS.ListDir", "eval_listdir", "Lista com os nomes dos itens da pasta (ordem alfabetica)"); TB_BLOCK_META("FS.ListCount", "eval_listcount", @@ -259,6 +282,7 @@ static bool fs_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { 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.ReadText", path_in, 1, text_out, 1, eval_readtext) && 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) diff --git a/tests/os_fs_test.c b/tests/os_fs_test.c index 3f95c98..3ff5ac1 100644 --- a/tests/os_fs_test.c +++ b/tests/os_fs_test.c @@ -1,7 +1,7 @@ /* * os_fs_test.c - testa tinyblocks-os (informacoes do sistema) e os blocos - * de pasta do tinyblocks-fs (FS.ListDir / ListCount / ListItem / IsFile / - * IsDir / Exists). + * de pasta do tinyblocks-fs (FS.ListDir / ListCount / ListItem / ReadText / + * IsFile / IsDir / Exists). */ #include #include @@ -124,6 +124,17 @@ int main(void) { 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);