Adiciona tinyblocks-blueprint e libraries json/xml/csv; abrir/salvar/exportar projeto
- tinyblocks-blueprint: dona do estado de edicao (abas, pool de blocos, vinculos parent/prel/next/sub/args, blocos compostos e variaveis) e da serializacao. JSON nativo (to/from) usando a AST JSON do core e export JSON/XML/CSV. Testado com round-trip (test-blueprint). - tinyblocks-json: lexer + parser + emissor JSON independentes. - tinyblocks-xml: lexer + parser de XML -> AST JSON do core + emissor. - tinyblocks-csv: divisor de linhas/campos + parser/emissor sobre a AST JSON. - Editor: botoes Abrir / Salvar / Exportar JSON XML CSV no menu; converte Ed <-> TbBlueprint; barra de caminho com modo; .json pode ser arrastado. - Variaveis: tb_variables_clear e tb_variables_set_text. - Harness: teste de salvar/abrir projeto; tests/blueprint_test.c.
This commit is contained in:
1 parent
dc23b92d15
commit
1199d2d7c7
39 files changed
+3462
-24
No files matched your search
@@ -0,0 +1,32 @@
|
||||
# Makefile da library blueprint (modelo de edicao + serializacao)
|
||||
CC := gcc
|
||||
BUILD := build
|
||||
CFLAGS := -std=c11 -Wall -Wextra -pedantic -fvisibility=hidden \
|
||||
-D_POSIX_C_SOURCE=200809L -I../../include -Iinclude
|
||||
|
||||
OBJS := $(BUILD)/blueprint.o $(BUILD)/serialize.o $(BUILD)/tb_blueprint.o
|
||||
LIB_A := $(BUILD)/libtinyblocks-blueprint.a
|
||||
LIB_SO := $(BUILD)/libtinyblocks-blueprint.so
|
||||
|
||||
.PHONY: all static shared clean
|
||||
all: static shared
|
||||
static: $(LIB_A)
|
||||
shared: $(LIB_SO)
|
||||
|
||||
$(BUILD)/blueprint.o: src/blueprint.c include/blueprint.h
|
||||
@mkdir -p $(BUILD)
|
||||
$(CC) $(CFLAGS) -fPIC -c $< -o $@
|
||||
$(BUILD)/serialize.o: src/serialize.c include/blueprint.h
|
||||
@mkdir -p $(BUILD)
|
||||
$(CC) $(CFLAGS) -fPIC -c $< -o $@
|
||||
$(BUILD)/tb_blueprint.o: src/tb_blueprint.c include/tb_blueprint.h include/blueprint.h
|
||||
@mkdir -p $(BUILD)
|
||||
$(CC) $(CFLAGS) -fPIC -c $< -o $@
|
||||
|
||||
$(LIB_A): $(OBJS)
|
||||
ar rcs $@ $^
|
||||
$(LIB_SO): $(OBJS)
|
||||
$(CC) -shared -o $@ $^
|
||||
|
||||
clean:
|
||||
$(RM) -r $(BUILD)
|
||||
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
* TinyBlocks - tinyblocks-blueprint / include/blueprint.h
|
||||
*
|
||||
* TbBlueprint e o modelo de um "projeto" do editor (estilo Scratch):
|
||||
* - abas (TbBpTab) com um pool estavel de blocos (TbBpBlock);
|
||||
* - vinculos entre blocos por indice: parent/prel, next, sub[2], args[];
|
||||
* - definicoes de blocos compostos (TbBpUserBlock);
|
||||
* - variaveis (TbBpVar).
|
||||
*
|
||||
* A library e dona do estado de edicao e da logica de serializacao:
|
||||
* converter para/de JSON (formato nativo) e exportar JSON/XML/CSV.
|
||||
*
|
||||
* A AST usada na serializacao e a do core (TinyBlocks_JsonValue).
|
||||
*/
|
||||
#ifndef TB_BLUEPRINT_H
|
||||
#define TB_BLUEPRINT_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include <tinyblocks/tb_json_block.h>
|
||||
|
||||
#define TB_BP_MAX_WS 256
|
||||
#define TB_BP_MAX_INPUTS 16
|
||||
#define TB_BP_MAX_TABS 8
|
||||
#define TB_BP_MAX_UBLOCKS 16
|
||||
#define TB_BP_MAX_UPARTS 16
|
||||
#define TB_BP_MAX_USLOTS 8
|
||||
#define TB_BP_MAX_VARS 64
|
||||
#define TB_BP_LIT_CAP 512
|
||||
|
||||
/* Formatos de serializacao */
|
||||
enum { TB_BP_FMT_JSON, TB_BP_FMT_XML, TB_BP_FMT_CSV };
|
||||
|
||||
/* Relacao de um bloco com o pai (identica ao editor) */
|
||||
#define TB_BP_REL_NONE (-1)
|
||||
#define TB_BP_REL_NEXT 0
|
||||
#define TB_BP_REL_SUB0 1
|
||||
#define TB_BP_REL_ARG0 3
|
||||
|
||||
/* -- Blocos e abas --- */
|
||||
typedef struct {
|
||||
bool used;
|
||||
char type[64]; /* "Events.WhenRun", "User.X", ... */
|
||||
float x, y;
|
||||
int parent, prel;
|
||||
int next;
|
||||
int sub[2];
|
||||
int args[TB_BP_MAX_INPUTS];
|
||||
char in_text[TB_BP_MAX_INPUTS][64];
|
||||
char lit[TB_BP_LIT_CAP];
|
||||
} TbBpBlock;
|
||||
|
||||
typedef struct {
|
||||
TbBpBlock blocks[TB_BP_MAX_WS];
|
||||
int top[TB_BP_MAX_WS];
|
||||
int top_n;
|
||||
float ws_sx, ws_sy;
|
||||
char name[48];
|
||||
bool composite;
|
||||
int comp_id; /* indice em user_blocks, ou -1 */
|
||||
int color_idx;
|
||||
} TbBpTab;
|
||||
|
||||
/* -- Blocos compostos (protótipo) --- */
|
||||
typedef enum { TB_BP_PARAM_NUM, TB_BP_PARAM_BOOL, TB_BP_PARAM_LABEL } TbBpParamKind;
|
||||
typedef struct { TbBpParamKind kind; char name[32]; } TbBpParam;
|
||||
|
||||
typedef struct {
|
||||
char name[48];
|
||||
int input_count, output_count;
|
||||
char input_names[TB_BP_MAX_USLOTS][32];
|
||||
char output_names[TB_BP_MAX_USLOTS][32];
|
||||
TbBpParam parts[TB_BP_MAX_UPARTS];
|
||||
int part_n;
|
||||
int color_idx;
|
||||
int tab_idx;
|
||||
} TbBpUserBlock;
|
||||
|
||||
/* -- Variaveis --- */
|
||||
typedef enum { TB_BP_VAL_INT, TB_BP_VAL_REAL, TB_BP_VAL_TEXT } TbBpValueKind;
|
||||
typedef struct {
|
||||
char name[32];
|
||||
TbBpValueKind kind;
|
||||
double num;
|
||||
char text[128];
|
||||
} TbBpVar;
|
||||
|
||||
/* -- Documento --- */
|
||||
typedef struct {
|
||||
TbBpTab tabs[TB_BP_MAX_TABS];
|
||||
int tab_count;
|
||||
TbBpUserBlock user_blocks[TB_BP_MAX_UBLOCKS];
|
||||
int user_block_n;
|
||||
TbBpVar vars[TB_BP_MAX_VARS];
|
||||
int var_n;
|
||||
int cur_tab;
|
||||
} TbBlueprint;
|
||||
|
||||
/* -- Ciclo de vida / edicao --- */
|
||||
TbBlueprint* tb_blueprint_new(void);
|
||||
void tb_blueprint_free(TbBlueprint* bp);
|
||||
void tb_blueprint_clear(TbBlueprint* bp);
|
||||
|
||||
/* Cria uma aba; devolve o indice ou -1. */
|
||||
int tb_blueprint_add_tab(TbBlueprint* bp, const char* name, bool composite);
|
||||
TbBpTab* tb_blueprint_tab(TbBlueprint* bp, int i);
|
||||
|
||||
/* Aloca um bloco no pool da aba; devolve o indice ou -1. */
|
||||
int tb_blueprint_alloc_block(TbBpTab* t);
|
||||
/* Liga child ao pai na relacao rel (child < 0 desliga). */
|
||||
void tb_blueprint_link(TbBpTab* t, int parent, int rel, int child);
|
||||
void tb_blueprint_top_add(TbBpTab* t, int bi);
|
||||
|
||||
/* Variaveis */
|
||||
void tb_blueprint_var_set_text(TbBlueprint* bp, const char* name, const char* value);
|
||||
|
||||
/* -- Serializacao --- */
|
||||
/* Formato nativo (JSON). to_json devolve ref nova; from_json nao consome 'root'. */
|
||||
TinyBlocks_JsonValue* tb_blueprint_to_json(const TbBlueprint* bp);
|
||||
TbBlueprint* tb_blueprint_from_json(const TinyBlocks_JsonValue* root);
|
||||
|
||||
/* Serializa para texto no formato pedido (malloc; libere com free). */
|
||||
char* tb_blueprint_serialize(const TbBlueprint* bp, int fmt, bool pretty);
|
||||
/* Le do texto. Suporta o formato nativo (JSON). */
|
||||
TbBlueprint* tb_blueprint_parse(const char* text, int fmt, char* err, size_t errcap);
|
||||
|
||||
/* Arquivos */
|
||||
bool tb_blueprint_save_file(const TbBlueprint* bp, const char* path, int fmt, bool pretty);
|
||||
TbBlueprint* tb_blueprint_load_file(const char* path, char* err, size_t errcap);
|
||||
const char* tb_blueprint_format_name(int fmt); /* "json"/"xml"/"csv" */
|
||||
|
||||
#endif /* TB_BLUEPRINT_H */
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* TinyBlocks - tb_blueprint.h
|
||||
* Library tinyblocks-blueprint: modelo de edicao (blueprint) e serializacao.
|
||||
*/
|
||||
#ifndef TINYBLOCKS_BLUEPRINT_H
|
||||
#define TINYBLOCKS_BLUEPRINT_H
|
||||
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include "blueprint.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
TB_API const TinyBlocks_LibraryDescriptor* tb_blueprint_get_library(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* TINYBLOCKS_BLUEPRINT_H */
|
||||
@@ -0,0 +1,15 @@
|
||||
# Metadados da library blueprint.
|
||||
[library]
|
||||
name = "tinyblocks-blueprint"
|
||||
version = "1.0.0"
|
||||
category = "Blueprint"
|
||||
description = "Modelo de edicao (abas/blocos/variaveis) e serializacao JSON/XML/CSV"
|
||||
color = [110, 140, 255, 255]
|
||||
dark = [70, 95, 210, 255]
|
||||
|
||||
[build]
|
||||
sources = ["src/blueprint.c", "src/serialize.c", "src/tb_blueprint.c"]
|
||||
includes = ["include"]
|
||||
shared = "build/libtinyblocks-blueprint.so"
|
||||
static = "build/libtinyblocks-blueprint.a"
|
||||
depends = []
|
||||
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
* TinyBlocks - tinyblocks-blueprint / src/blueprint.c
|
||||
* Gestao do modelo de edicao (abas, blocos, vinculos, variaveis).
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "blueprint.h"
|
||||
|
||||
TbBlueprint* tb_blueprint_new(void) {
|
||||
TbBlueprint* bp = (TbBlueprint*)calloc(1, sizeof(TbBlueprint));
|
||||
if (!bp) return NULL;
|
||||
tb_blueprint_clear(bp);
|
||||
return bp;
|
||||
}
|
||||
|
||||
void tb_blueprint_free(TbBlueprint* bp) {
|
||||
free(bp);
|
||||
}
|
||||
|
||||
void tb_blueprint_clear(TbBlueprint* bp) {
|
||||
if (!bp) return;
|
||||
memset(bp, 0, sizeof(*bp));
|
||||
for (int i = 0; i < TB_BP_MAX_TABS; i++) {
|
||||
TbBpTab* t = &bp->tabs[i];
|
||||
t->comp_id = -1;
|
||||
for (int b = 0; b < TB_BP_MAX_WS; b++) {
|
||||
t->blocks[b].parent = -1;
|
||||
t->blocks[b].prel = TB_BP_REL_NONE;
|
||||
t->blocks[b].next = -1;
|
||||
t->blocks[b].sub[0] = t->blocks[b].sub[1] = -1;
|
||||
for (int s = 0; s < TB_BP_MAX_INPUTS; s++) t->blocks[b].args[s] = -1;
|
||||
}
|
||||
}
|
||||
for (int u = 0; u < TB_BP_MAX_UBLOCKS; u++) bp->user_blocks[u].tab_idx = -1;
|
||||
}
|
||||
|
||||
int tb_blueprint_add_tab(TbBlueprint* bp, const char* name, bool composite) {
|
||||
if (!bp || bp->tab_count >= TB_BP_MAX_TABS) return -1;
|
||||
int idx = bp->tab_count++;
|
||||
TbBpTab* t = &bp->tabs[idx];
|
||||
memset(t, 0, sizeof(*t));
|
||||
t->comp_id = -1;
|
||||
t->composite = composite;
|
||||
if (name && name[0]) snprintf(t->name, sizeof(t->name), "%s", name);
|
||||
else snprintf(t->name, sizeof(t->name), "Codigo %d", idx + 1);
|
||||
for (int b = 0; b < TB_BP_MAX_WS; b++) {
|
||||
t->blocks[b].parent = -1;
|
||||
t->blocks[b].prel = TB_BP_REL_NONE;
|
||||
t->blocks[b].next = -1;
|
||||
t->blocks[b].sub[0] = t->blocks[b].sub[1] = -1;
|
||||
for (int s = 0; s < TB_BP_MAX_INPUTS; s++) t->blocks[b].args[s] = -1;
|
||||
}
|
||||
return idx;
|
||||
}
|
||||
|
||||
TbBpTab* tb_blueprint_tab(TbBlueprint* bp, int i) {
|
||||
if (!bp || i < 0 || i >= bp->tab_count) return NULL;
|
||||
return &bp->tabs[i];
|
||||
}
|
||||
|
||||
int tb_blueprint_alloc_block(TbBpTab* t) {
|
||||
if (!t) return -1;
|
||||
for (int i = 0; i < TB_BP_MAX_WS; i++) {
|
||||
if (t->blocks[i].used) continue;
|
||||
TbBpBlock* b = &t->blocks[i];
|
||||
memset(b, 0, sizeof(*b));
|
||||
b->used = true;
|
||||
b->parent = -1;
|
||||
b->prel = TB_BP_REL_NONE;
|
||||
b->next = -1;
|
||||
b->sub[0] = b->sub[1] = -1;
|
||||
for (int s = 0; s < TB_BP_MAX_INPUTS; s++) b->args[s] = -1;
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void tb_blueprint_link(TbBpTab* t, int parent, int rel, int child) {
|
||||
if (!t || parent < 0 || parent >= TB_BP_MAX_WS) return;
|
||||
TbBpBlock* p = &t->blocks[parent];
|
||||
if (rel == TB_BP_REL_NEXT) p->next = child;
|
||||
else if (rel < TB_BP_REL_ARG0) p->sub[rel - TB_BP_REL_SUB0] = child;
|
||||
else p->args[rel - TB_BP_REL_ARG0] = child;
|
||||
if (child >= 0 && child < TB_BP_MAX_WS) {
|
||||
t->blocks[child].parent = parent;
|
||||
t->blocks[child].prel = rel;
|
||||
}
|
||||
}
|
||||
|
||||
void tb_blueprint_top_add(TbBpTab* t, int bi) {
|
||||
if (!t) return;
|
||||
for (int i = 0; i < t->top_n; i++) {
|
||||
if (t->top[i] != bi) continue;
|
||||
memmove(&t->top[i], &t->top[i + 1], (size_t)(t->top_n - i - 1) * sizeof(int));
|
||||
t->top_n--;
|
||||
break;
|
||||
}
|
||||
if (t->top_n < TB_BP_MAX_WS) t->top[t->top_n++] = bi;
|
||||
}
|
||||
|
||||
void tb_blueprint_var_set_text(TbBlueprint* bp, const char* name, const char* value) {
|
||||
if (!bp || !name || !name[0]) return;
|
||||
int idx = -1;
|
||||
for (int i = 0; i < bp->var_n; i++)
|
||||
if (strcmp(bp->vars[i].name, name) == 0) { idx = i; break; }
|
||||
if (idx < 0) {
|
||||
if (bp->var_n >= TB_BP_MAX_VARS) return;
|
||||
idx = bp->var_n++;
|
||||
memset(&bp->vars[idx], 0, sizeof(bp->vars[idx]));
|
||||
snprintf(bp->vars[idx].name, sizeof(bp->vars[idx].name), "%s", name);
|
||||
}
|
||||
TbBpVar* v = &bp->vars[idx];
|
||||
const char* s = value ? value : "";
|
||||
char* end = NULL;
|
||||
long long iv = strtoll(s, &end, 10);
|
||||
if (end && *end == 0 && end != s) { v->kind = TB_BP_VAL_INT; v->num = (double)iv; v->text[0] = 0; return; }
|
||||
double dv = strtod(s, &end);
|
||||
if (end && *end == 0 && end != s) { v->kind = TB_BP_VAL_REAL; v->num = dv; v->text[0] = 0; return; }
|
||||
v->kind = TB_BP_VAL_TEXT;
|
||||
snprintf(v->text, sizeof(v->text), "%s", s);
|
||||
}
|
||||
|
||||
const char* tb_blueprint_format_name(int fmt) {
|
||||
switch (fmt) {
|
||||
case TB_BP_FMT_XML: return "xml";
|
||||
case TB_BP_FMT_CSV: return "csv";
|
||||
default: return "json";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,572 @@
|
||||
/*
|
||||
* TinyBlocks - tinyblocks-blueprint / src/serialize.c
|
||||
* JSON nativo (to/from), exportacao JSON/XML/CSV e I/O de arquivo.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "blueprint.h"
|
||||
|
||||
/* ============================================================
|
||||
* Blueprint -> arvore JSON (formato nativo)
|
||||
* ============================================================ */
|
||||
#define PUT(o, k, val) do { TinyBlocks_JsonValue* _v = (val); tb_json_object_set((o), (k), _v); tb_json_release(_v); } while (0)
|
||||
|
||||
static TinyBlocks_JsonValue* jstr_arr_of(const char names[][32], int n) {
|
||||
TinyBlocks_JsonValue* a = tb_json_array();
|
||||
for (int i = 0; i < n; i++) {
|
||||
TinyBlocks_JsonValue* v = tb_json_string(names[i]);
|
||||
tb_json_array_push(a, v);
|
||||
tb_json_release(v);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
TinyBlocks_JsonValue* tb_blueprint_to_json(const TbBlueprint* bp) {
|
||||
if (!bp) return NULL;
|
||||
TinyBlocks_JsonValue* root = tb_json_object();
|
||||
if (!root) return NULL;
|
||||
PUT(root, "format", tb_json_string("tinyblocks-project"));
|
||||
PUT(root, "version", tb_json_number(1));
|
||||
PUT(root, "cur_tab", tb_json_number(bp->cur_tab));
|
||||
|
||||
TinyBlocks_JsonValue* tabs = tb_json_array();
|
||||
PUT(root, "tabs", tabs);
|
||||
for (int ti = 0; ti < bp->tab_count; ti++) {
|
||||
const TbBpTab* T = &bp->tabs[ti];
|
||||
TinyBlocks_JsonValue* jt = tb_json_object();
|
||||
PUT(jt, "name", tb_json_string(T->name));
|
||||
PUT(jt, "composite", tb_json_bool(T->composite));
|
||||
PUT(jt, "comp_id", tb_json_number(T->comp_id));
|
||||
PUT(jt, "color", tb_json_number(T->color_idx));
|
||||
PUT(jt, "ws_sx", tb_json_number(T->ws_sx));
|
||||
PUT(jt, "ws_sy", tb_json_number(T->ws_sy));
|
||||
|
||||
TinyBlocks_JsonValue* jb = tb_json_array();
|
||||
PUT(jt, "blocks", jb);
|
||||
for (int i = 0; i < TB_BP_MAX_WS; i++) {
|
||||
const TbBpBlock* b = &T->blocks[i];
|
||||
if (!b->used) continue;
|
||||
TinyBlocks_JsonValue* o = tb_json_object();
|
||||
PUT(o, "i", tb_json_number(i));
|
||||
PUT(o, "type", tb_json_string(b->type));
|
||||
PUT(o, "x", tb_json_number(b->x));
|
||||
PUT(o, "y", tb_json_number(b->y));
|
||||
PUT(o, "parent", tb_json_number(b->parent));
|
||||
PUT(o, "prel", tb_json_number(b->prel));
|
||||
PUT(o, "next", tb_json_number(b->next));
|
||||
TinyBlocks_JsonValue* sub = tb_json_array();
|
||||
for (int k = 0; k < 2; k++) { TinyBlocks_JsonValue* e = tb_json_number(b->sub[k]); tb_json_array_push(sub, e); tb_json_release(e); }
|
||||
PUT(o, "sub", sub);
|
||||
TinyBlocks_JsonValue* args = tb_json_array();
|
||||
for (int s = 0; s < TB_BP_MAX_INPUTS; s++) { TinyBlocks_JsonValue* e = tb_json_number(b->args[s]); tb_json_array_push(args, e); tb_json_release(e); }
|
||||
PUT(o, "args", args);
|
||||
TinyBlocks_JsonValue* txt = tb_json_array();
|
||||
for (int s = 0; s < TB_BP_MAX_INPUTS; s++) { TinyBlocks_JsonValue* e = tb_json_string(b->in_text[s]); tb_json_array_push(txt, e); tb_json_release(e); }
|
||||
PUT(o, "in_text", txt);
|
||||
PUT(o, "lit", tb_json_string(b->lit));
|
||||
tb_json_array_push(jb, o);
|
||||
tb_json_release(o);
|
||||
}
|
||||
TinyBlocks_JsonValue* top = tb_json_array();
|
||||
PUT(jt, "top", top);
|
||||
for (int i = 0; i < T->top_n; i++) { TinyBlocks_JsonValue* e = tb_json_number(T->top[i]); tb_json_array_push(top, e); tb_json_release(e); }
|
||||
tb_json_array_push(tabs, jt);
|
||||
tb_json_release(jt);
|
||||
}
|
||||
|
||||
TinyBlocks_JsonValue* ubs = tb_json_array();
|
||||
PUT(root, "user_blocks", ubs);
|
||||
for (int u = 0; u < bp->user_block_n; u++) {
|
||||
const TbBpUserBlock* d = &bp->user_blocks[u];
|
||||
TinyBlocks_JsonValue* jo = tb_json_object();
|
||||
PUT(jo, "name", tb_json_string(d->name));
|
||||
PUT(jo, "color", tb_json_number(d->color_idx));
|
||||
PUT(jo, "tab", tb_json_number(d->tab_idx));
|
||||
PUT(jo, "inputs", jstr_arr_of(d->input_names, d->input_count));
|
||||
PUT(jo, "outputs", jstr_arr_of(d->output_names, d->output_count));
|
||||
TinyBlocks_JsonValue* parts = tb_json_array();
|
||||
for (int i = 0; i < d->part_n; i++) {
|
||||
TinyBlocks_JsonValue* po = tb_json_object();
|
||||
PUT(po, "kind", tb_json_number(d->parts[i].kind));
|
||||
PUT(po, "name", tb_json_string(d->parts[i].name));
|
||||
tb_json_array_push(parts, po);
|
||||
tb_json_release(po);
|
||||
}
|
||||
PUT(jo, "parts", parts);
|
||||
tb_json_array_push(ubs, jo);
|
||||
tb_json_release(jo);
|
||||
}
|
||||
|
||||
TinyBlocks_JsonValue* vars = tb_json_array();
|
||||
PUT(root, "variables", vars);
|
||||
for (int i = 0; i < bp->var_n; i++) {
|
||||
const TbBpVar* v = &bp->vars[i];
|
||||
TinyBlocks_JsonValue* jo = tb_json_object();
|
||||
PUT(jo, "name", tb_json_string(v->name));
|
||||
PUT(jo, "kind", tb_json_number(v->kind));
|
||||
PUT(jo, "num", tb_json_number(v->num));
|
||||
PUT(jo, "text", tb_json_string(v->text));
|
||||
tb_json_array_push(vars, jo);
|
||||
tb_json_release(jo);
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
/* ============================================================
|
||||
* Arvore JSON -> Blueprint
|
||||
* ============================================================ */
|
||||
static const char* jget_str(const TinyBlocks_JsonValue* o, const char* k, const char* def) {
|
||||
TinyBlocks_JsonValue* v = tb_json_object_get(o, k);
|
||||
const char* s = v ? tb_json_as_string(v) : NULL;
|
||||
tb_json_release(v);
|
||||
return s ? s : def;
|
||||
}
|
||||
static double jget_num(const TinyBlocks_JsonValue* o, const char* k, double def) {
|
||||
TinyBlocks_JsonValue* v = tb_json_object_get(o, k);
|
||||
double d = v ? tb_json_as_number(v, def) : def;
|
||||
tb_json_release(v);
|
||||
return d;
|
||||
}
|
||||
static bool jget_bool(const TinyBlocks_JsonValue* o, const char* k, bool def) {
|
||||
TinyBlocks_JsonValue* v = tb_json_object_get(o, k);
|
||||
bool b = v ? tb_json_as_bool(v, def) : def;
|
||||
tb_json_release(v);
|
||||
return b;
|
||||
}
|
||||
static void jcopy_names(const TinyBlocks_JsonValue* o, const char* k, char names[][32], int max, int* n) {
|
||||
*n = 0;
|
||||
TinyBlocks_JsonValue* a = tb_json_object_get(o, k);
|
||||
for (uint32_t i = 0; a && i < tb_json_array_length(a) && *n < max; i++) {
|
||||
TinyBlocks_JsonValue* e = tb_json_array_get(a, i);
|
||||
const char* s = e ? tb_json_as_string(e) : NULL;
|
||||
snprintf(names[*n], 32, "%.31s", s ? s : "");
|
||||
(*n)++;
|
||||
tb_json_release(e);
|
||||
}
|
||||
tb_json_release(a);
|
||||
}
|
||||
|
||||
TbBlueprint* tb_blueprint_from_json(const TinyBlocks_JsonValue* root) {
|
||||
if (!root) return NULL;
|
||||
TbBlueprint* bp = tb_blueprint_new();
|
||||
if (!bp) return NULL;
|
||||
|
||||
TinyBlocks_JsonValue* ubs = tb_json_object_get(root, "user_blocks");
|
||||
for (uint32_t u = 0; ubs && u < tb_json_array_length(ubs) && bp->user_block_n < TB_BP_MAX_UBLOCKS; u++) {
|
||||
TinyBlocks_JsonValue* jo = tb_json_array_get(ubs, u);
|
||||
TbBpUserBlock* d = &bp->user_blocks[bp->user_block_n];
|
||||
memset(d, 0, sizeof(*d));
|
||||
snprintf(d->name, sizeof(d->name), "%s", jget_str(jo, "name", ""));
|
||||
d->color_idx = (int)jget_num(jo, "color", 0);
|
||||
d->tab_idx = (int)jget_num(jo, "tab", -1);
|
||||
jcopy_names(jo, "inputs", d->input_names, TB_BP_MAX_USLOTS, &d->input_count);
|
||||
jcopy_names(jo, "outputs", d->output_names, TB_BP_MAX_USLOTS, &d->output_count);
|
||||
TinyBlocks_JsonValue* parts = tb_json_object_get(jo, "parts");
|
||||
d->part_n = 0;
|
||||
for (uint32_t i = 0; parts && i < tb_json_array_length(parts) && d->part_n < TB_BP_MAX_UPARTS; i++) {
|
||||
TinyBlocks_JsonValue* po = tb_json_array_get(parts, i);
|
||||
d->parts[d->part_n].kind = (TbBpParamKind)(int)jget_num(po, "kind", 0);
|
||||
snprintf(d->parts[d->part_n].name, 32, "%.31s", jget_str(po, "name", ""));
|
||||
d->part_n++;
|
||||
tb_json_release(po);
|
||||
}
|
||||
tb_json_release(parts);
|
||||
tb_json_release(jo);
|
||||
bp->user_block_n++;
|
||||
}
|
||||
tb_json_release(ubs);
|
||||
|
||||
TinyBlocks_JsonValue* tabs = tb_json_object_get(root, "tabs");
|
||||
for (uint32_t ti = 0; tabs && ti < tb_json_array_length(tabs) && bp->tab_count < TB_BP_MAX_TABS; ti++) {
|
||||
TinyBlocks_JsonValue* jt = tb_json_array_get(tabs, ti);
|
||||
int idx = tb_blueprint_add_tab(bp, jget_str(jt, "name", "Codigo"), jget_bool(jt, "composite", false));
|
||||
TbBpTab* T = &bp->tabs[idx];
|
||||
T->comp_id = (int)jget_num(jt, "comp_id", -1);
|
||||
T->color_idx = (int)jget_num(jt, "color", 0);
|
||||
T->ws_sx = (float)jget_num(jt, "ws_sx", 0);
|
||||
T->ws_sy = (float)jget_num(jt, "ws_sy", 0);
|
||||
|
||||
TinyBlocks_JsonValue* jb = tb_json_object_get(jt, "blocks");
|
||||
for (uint32_t bi = 0; jb && bi < tb_json_array_length(jb); bi++) {
|
||||
TinyBlocks_JsonValue* o = tb_json_array_get(jb, bi);
|
||||
int i = (int)jget_num(o, "i", -1);
|
||||
if (i >= 0 && i < TB_BP_MAX_WS) {
|
||||
TbBpBlock* b = &T->blocks[i];
|
||||
b->used = true;
|
||||
snprintf(b->type, sizeof(b->type), "%s", jget_str(o, "type", ""));
|
||||
b->x = (float)jget_num(o, "x", 0);
|
||||
b->y = (float)jget_num(o, "y", 0);
|
||||
b->parent = (int)jget_num(o, "parent", -1);
|
||||
b->prel = (int)jget_num(o, "prel", TB_BP_REL_NONE);
|
||||
b->next = (int)jget_num(o, "next", -1);
|
||||
TinyBlocks_JsonValue* sub = tb_json_object_get(o, "sub");
|
||||
for (int k = 0; k < 2; k++) { TinyBlocks_JsonValue* e = tb_json_array_get(sub, (uint32_t)k); b->sub[k] = e ? (int)tb_json_as_number(e, -1) : -1; tb_json_release(e); }
|
||||
tb_json_release(sub);
|
||||
TinyBlocks_JsonValue* args = tb_json_object_get(o, "args");
|
||||
for (int s = 0; s < TB_BP_MAX_INPUTS; s++) { TinyBlocks_JsonValue* e = tb_json_array_get(args, (uint32_t)s); b->args[s] = e ? (int)tb_json_as_number(e, -1) : -1; tb_json_release(e); }
|
||||
tb_json_release(args);
|
||||
TinyBlocks_JsonValue* txt = tb_json_object_get(o, "in_text");
|
||||
for (int s = 0; s < TB_BP_MAX_INPUTS; s++) {
|
||||
TinyBlocks_JsonValue* e = tb_json_array_get(txt, (uint32_t)s);
|
||||
if (e) { const char* sv = tb_json_as_string(e); if (sv) snprintf(b->in_text[s], sizeof(b->in_text[s]), "%s", sv); }
|
||||
tb_json_release(e);
|
||||
}
|
||||
tb_json_release(txt);
|
||||
snprintf(b->lit, sizeof(b->lit), "%s", jget_str(o, "lit", ""));
|
||||
}
|
||||
tb_json_release(o);
|
||||
}
|
||||
tb_json_release(jb);
|
||||
|
||||
TinyBlocks_JsonValue* top = tb_json_object_get(jt, "top");
|
||||
T->top_n = 0;
|
||||
for (uint32_t i = 0; top && i < tb_json_array_length(top) && T->top_n < TB_BP_MAX_WS; i++) {
|
||||
TinyBlocks_JsonValue* e = tb_json_array_get(top, i);
|
||||
T->top[T->top_n++] = e ? (int)tb_json_as_number(e, 0) : 0;
|
||||
tb_json_release(e);
|
||||
}
|
||||
tb_json_release(top);
|
||||
tb_json_release(jt);
|
||||
}
|
||||
tb_json_release(tabs);
|
||||
|
||||
TinyBlocks_JsonValue* vars = tb_json_object_get(root, "variables");
|
||||
for (uint32_t i = 0; vars && i < tb_json_array_length(vars) && bp->var_n < TB_BP_MAX_VARS; i++) {
|
||||
TinyBlocks_JsonValue* o = tb_json_array_get(vars, i);
|
||||
TbBpVar* v = &bp->vars[bp->var_n];
|
||||
memset(v, 0, sizeof(*v));
|
||||
snprintf(v->name, sizeof(v->name), "%s", jget_str(o, "name", ""));
|
||||
v->kind = (TbBpValueKind)(int)jget_num(o, "kind", TB_BP_VAL_TEXT);
|
||||
v->num = jget_num(o, "num", 0);
|
||||
snprintf(v->text, sizeof(v->text), "%s", jget_str(o, "text", ""));
|
||||
bp->var_n++;
|
||||
tb_json_release(o);
|
||||
}
|
||||
tb_json_release(vars);
|
||||
|
||||
bp->cur_tab = (int)jget_num(root, "cur_tab", 0);
|
||||
return bp;
|
||||
}
|
||||
|
||||
/* ============================================================
|
||||
* Buffer de saida + escritores JSON/XML/CSV
|
||||
* ============================================================ */
|
||||
typedef struct { char* p; size_t len, cap; } Buf;
|
||||
|
||||
static void bput(Buf* b, const char* s, size_t n) {
|
||||
if (b->len + n + 1 > b->cap) {
|
||||
size_t nc = b->cap ? b->cap : 128;
|
||||
while (nc < b->len + n + 1) nc *= 2;
|
||||
char* p = (char*)realloc(b->p, nc);
|
||||
if (!p) return;
|
||||
b->p = p; b->cap = nc;
|
||||
}
|
||||
memcpy(b->p + b->len, s, n); b->len += n; b->p[b->len] = 0;
|
||||
}
|
||||
static void bs(Buf* b, const char* s) { bput(b, s, strlen(s)); }
|
||||
static void bc(Buf* b, char c) { bput(b, &c, 1); }
|
||||
static void bindent(Buf* b, int depth) { for (int i = 0; i < depth; i++) bs(b, " "); }
|
||||
|
||||
static void quote(Buf* b, const char* s) {
|
||||
bc(b, '"');
|
||||
for (const unsigned char* p = (const unsigned char*)s; *p; p++) {
|
||||
switch (*p) {
|
||||
case '"': bs(b, "\\\""); break;
|
||||
case '\\': bs(b, "\\\\"); break;
|
||||
case '\n': bs(b, "\\n"); break;
|
||||
case '\r': bs(b, "\\r"); break;
|
||||
case '\t': bs(b, "\\t"); break;
|
||||
default: bc(b, (char)*p);
|
||||
}
|
||||
}
|
||||
bc(b, '"');
|
||||
}
|
||||
|
||||
static void json_rec(Buf* b, const TinyBlocks_JsonValue* j, bool pretty, int depth) {
|
||||
if (!j) { bs(b, "null"); return; }
|
||||
char tmp[64];
|
||||
switch (tb_json_kind(j)) {
|
||||
case TB_JSON_NULL: bs(b, "null"); break;
|
||||
case TB_JSON_BOOL: bs(b, tb_json_as_bool(j, false) ? "true" : "false"); break;
|
||||
case TB_JSON_NUMBER: {
|
||||
double d = tb_json_as_number(j, 0);
|
||||
if (d == (double)(long long)d) snprintf(tmp, sizeof(tmp), "%lld", (long long)d);
|
||||
else snprintf(tmp, sizeof(tmp), "%g", d);
|
||||
bs(b, tmp);
|
||||
break;
|
||||
}
|
||||
case TB_JSON_STRING: quote(b, tb_json_as_string(j) ? tb_json_as_string(j) : ""); break;
|
||||
case TB_JSON_ARRAY:
|
||||
bc(b, '[');
|
||||
for (uint32_t i = 0; i < tb_json_array_length(j); i++) {
|
||||
TinyBlocks_JsonValue* e = tb_json_array_get(j, i);
|
||||
if (i) bc(b, ',');
|
||||
if (pretty) { bc(b, '\n'); bindent(b, depth + 1); }
|
||||
json_rec(b, e, pretty, depth + 1);
|
||||
tb_json_release(e);
|
||||
}
|
||||
if (pretty && tb_json_array_length(j)) { bc(b, '\n'); bindent(b, depth); }
|
||||
bc(b, ']');
|
||||
break;
|
||||
case TB_JSON_OBJECT:
|
||||
bc(b, '{');
|
||||
for (uint32_t i = 0; i < tb_json_object_length(j); i++) {
|
||||
const char* k = tb_json_object_key_at(j, i);
|
||||
TinyBlocks_JsonValue* e = tb_json_object_get(j, k);
|
||||
if (i) bc(b, ',');
|
||||
if (pretty) { bc(b, '\n'); bindent(b, depth + 1); }
|
||||
quote(b, k);
|
||||
bc(b, ':');
|
||||
if (pretty) bc(b, ' ');
|
||||
json_rec(b, e, pretty, depth + 1);
|
||||
tb_json_release(e);
|
||||
}
|
||||
if (pretty && tb_json_object_length(j)) { bc(b, '\n'); bindent(b, depth); }
|
||||
bc(b, '}');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void esc_xml(Buf* b, const char* s) {
|
||||
for (const char* q = s; *q; q++) {
|
||||
switch (*q) {
|
||||
case '<': bs(b, "<"); break;
|
||||
case '>': bs(b, ">"); break;
|
||||
case '&': bs(b, "&"); break;
|
||||
case '"': bs(b, """); break;
|
||||
default: bc(b, *q);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void xattr(Buf* b, const char* k, const char* v) {
|
||||
bs(b, " "); bs(b, k); bs(b, "=\""); esc_xml(b, v ? v : ""); bs(b, "\"");
|
||||
}
|
||||
static void xattr_num(Buf* b, const char* k, long long v) {
|
||||
char t[32]; snprintf(t, sizeof(t), "%lld", v); xattr(b, k, t);
|
||||
}
|
||||
static void xattr_real(Buf* b, const char* k, double v) {
|
||||
char t[32];
|
||||
if (v == (double)(long long)v) snprintf(t, sizeof(t), "%lld", (long long)v);
|
||||
else snprintf(t, sizeof(t), "%g", v);
|
||||
xattr(b, k, t);
|
||||
}
|
||||
static void xattr_bool(Buf* b, const char* k, bool v) { xattr(b, k, v ? "true" : "false"); }
|
||||
|
||||
/* XML completo do projeto (escrito direto do modelo). */
|
||||
static void blueprint_xml(Buf* b, const TbBlueprint* bp) {
|
||||
bs(b, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
|
||||
bs(b, "<tinyblocks");
|
||||
xattr(b, "format", "tinyblocks-project");
|
||||
xattr_num(b, "version", 1);
|
||||
bs(b, ">\n");
|
||||
|
||||
bindent(b, 1); bs(b, "<tabs>\n");
|
||||
for (int ti = 0; ti < bp->tab_count; ti++) {
|
||||
const TbBpTab* T = &bp->tabs[ti];
|
||||
bindent(b, 2); bs(b, "<tab");
|
||||
xattr(b, "name", T->name);
|
||||
xattr_bool(b, "composite", T->composite);
|
||||
xattr_num(b, "comp_id", T->comp_id);
|
||||
xattr_num(b, "color", T->color_idx);
|
||||
xattr_real(b, "ws_sx", T->ws_sx);
|
||||
xattr_real(b, "ws_sy", T->ws_sy);
|
||||
bs(b, ">\n");
|
||||
bindent(b, 3); bs(b, "<top>");
|
||||
for (int i = 0; i < T->top_n; i++) { char t[24]; snprintf(t, sizeof(t), "<i>%d</i>", T->top[i]); bs(b, t); }
|
||||
bs(b, "</top>\n");
|
||||
bindent(b, 3); bs(b, "<blocks>\n");
|
||||
for (int i = 0; i < TB_BP_MAX_WS; i++) {
|
||||
const TbBpBlock* bl = &T->blocks[i];
|
||||
if (!bl->used) continue;
|
||||
bindent(b, 4); bs(b, "<block");
|
||||
xattr_num(b, "i", i);
|
||||
xattr(b, "type", bl->type);
|
||||
xattr_real(b, "x", bl->x);
|
||||
xattr_real(b, "y", bl->y);
|
||||
xattr_num(b, "parent", bl->parent);
|
||||
xattr_num(b, "prel", bl->prel);
|
||||
xattr_num(b, "next", bl->next);
|
||||
bs(b, ">\n");
|
||||
bindent(b, 5); bs(b, "<sub>");
|
||||
for (int k = 0; k < 2; k++) { char t[24]; snprintf(t, sizeof(t), "<i>%d</i>", bl->sub[k]); bs(b, t); }
|
||||
bs(b, "</sub>\n");
|
||||
bindent(b, 5); bs(b, "<args>");
|
||||
for (int s = 0; s < TB_BP_MAX_INPUTS; s++) { char t[24]; snprintf(t, sizeof(t), "<i>%d</i>", bl->args[s]); bs(b, t); }
|
||||
bs(b, "</args>\n");
|
||||
bindent(b, 5); bs(b, "<in_text>");
|
||||
for (int s = 0; s < TB_BP_MAX_INPUTS; s++) { bs(b, "<s>"); esc_xml(b, bl->in_text[s]); bs(b, "</s>"); }
|
||||
bs(b, "</in_text>\n");
|
||||
bindent(b, 5); bs(b, "<lit>"); esc_xml(b, bl->lit); bs(b, "</lit>\n");
|
||||
bindent(b, 4); bs(b, "</block>\n");
|
||||
}
|
||||
bindent(b, 3); bs(b, "</blocks>\n");
|
||||
bindent(b, 2); bs(b, "</tab>\n");
|
||||
}
|
||||
bindent(b, 1); bs(b, "</tabs>\n");
|
||||
|
||||
bindent(b, 1); bs(b, "<user_blocks>\n");
|
||||
for (int u = 0; u < bp->user_block_n; u++) {
|
||||
const TbBpUserBlock* d = &bp->user_blocks[u];
|
||||
bindent(b, 2); bs(b, "<user_block");
|
||||
xattr(b, "name", d->name);
|
||||
xattr_num(b, "color", d->color_idx);
|
||||
xattr_num(b, "tab", d->tab_idx);
|
||||
bs(b, ">\n");
|
||||
bindent(b, 3); bs(b, "<inputs>");
|
||||
for (int i = 0; i < d->input_count; i++) { bs(b, "<s>"); esc_xml(b, d->input_names[i]); bs(b, "</s>"); }
|
||||
bs(b, "</inputs>\n");
|
||||
bindent(b, 3); bs(b, "<outputs>");
|
||||
for (int i = 0; i < d->output_count; i++) { bs(b, "<s>"); esc_xml(b, d->output_names[i]); bs(b, "</s>"); }
|
||||
bs(b, "</outputs>\n");
|
||||
bindent(b, 3); bs(b, "<parts>\n");
|
||||
for (int i = 0; i < d->part_n; i++) {
|
||||
bindent(b, 4); bs(b, "<part");
|
||||
xattr_num(b, "kind", d->parts[i].kind);
|
||||
xattr(b, "name", d->parts[i].name);
|
||||
bs(b, "/>\n");
|
||||
}
|
||||
bindent(b, 3); bs(b, "</parts>\n");
|
||||
bindent(b, 2); bs(b, "</user_block>\n");
|
||||
}
|
||||
bindent(b, 1); bs(b, "</user_blocks>\n");
|
||||
|
||||
bindent(b, 1); bs(b, "<variables>\n");
|
||||
for (int i = 0; i < bp->var_n; i++) {
|
||||
const TbBpVar* v = &bp->vars[i];
|
||||
bindent(b, 2); bs(b, "<var");
|
||||
xattr(b, "name", v->name);
|
||||
xattr_num(b, "kind", v->kind);
|
||||
xattr_real(b, "num", v->num);
|
||||
xattr(b, "text", v->text);
|
||||
bs(b, "/>\n");
|
||||
}
|
||||
bindent(b, 1); bs(b, "</variables>\n");
|
||||
bs(b, "</tinyblocks>\n");
|
||||
}
|
||||
|
||||
static bool csv_needs_quote(const char* s) {
|
||||
for (; *s; s++) if (*s == ',' || *s == '"' || *s == '\n' || *s == '\r') return true;
|
||||
return false;
|
||||
}
|
||||
static void csv_field(Buf* b, const char* s) {
|
||||
if (!csv_needs_quote(s)) { bs(b, s); return; }
|
||||
bc(b, '"');
|
||||
for (; *s; s++) { if (*s == '"') bc(b, '"'); bc(b, *s); }
|
||||
bc(b, '"');
|
||||
}
|
||||
/* data = array de objetos; colunas vem das chaves do primeiro. */
|
||||
static void csv_rec(Buf* b, const TinyBlocks_JsonValue* data) {
|
||||
if (tb_json_kind(data) != TB_JSON_ARRAY) return;
|
||||
TinyBlocks_JsonValue* first = tb_json_array_get(data, 0);
|
||||
if (!first) { tb_json_release(first); return; }
|
||||
uint32_t ncol = tb_json_object_length(first);
|
||||
for (uint32_t j = 0; j < ncol; j++) { if (j) bc(b, ','); csv_field(b, tb_json_object_key_at(first, j)); }
|
||||
bc(b, '\n');
|
||||
for (uint32_t i = 0; i < tb_json_array_length(data); i++) {
|
||||
TinyBlocks_JsonValue* o = tb_json_array_get(data, i);
|
||||
for (uint32_t j = 0; j < ncol; j++) {
|
||||
if (j) bc(b, ',');
|
||||
TinyBlocks_JsonValue* v = tb_json_object_get(o, tb_json_object_key_at(first, j));
|
||||
char num[32];
|
||||
const char* s = "";
|
||||
if (v && tb_json_kind(v) == TB_JSON_STRING) s = tb_json_as_string(v);
|
||||
else if (v && tb_json_kind(v) == TB_JSON_NUMBER) {
|
||||
double d = tb_json_as_number(v, 0);
|
||||
if (d == (double)(long long)d) snprintf(num, sizeof(num), "%lld", (long long)d);
|
||||
else snprintf(num, sizeof(num), "%g", d);
|
||||
s = num;
|
||||
} else if (v && tb_json_kind(v) == TB_JSON_BOOL) {
|
||||
s = tb_json_as_bool(v, false) ? "true" : "false";
|
||||
}
|
||||
csv_field(b, s);
|
||||
tb_json_release(v);
|
||||
}
|
||||
bc(b, '\n');
|
||||
tb_json_release(o);
|
||||
}
|
||||
tb_json_release(first);
|
||||
}
|
||||
|
||||
/* Tabela plana de blocos para o CSV. */
|
||||
static TinyBlocks_JsonValue* blueprint_csv_tree(const TbBlueprint* bp) {
|
||||
TinyBlocks_JsonValue* arr = tb_json_array();
|
||||
for (int ti = 0; ti < bp->tab_count; ti++) {
|
||||
const TbBpTab* T = &bp->tabs[ti];
|
||||
for (int i = 0; i < TB_BP_MAX_WS; i++) {
|
||||
const TbBpBlock* b = &T->blocks[i];
|
||||
if (!b->used) continue;
|
||||
TinyBlocks_JsonValue* o = tb_json_object();
|
||||
PUT(o, "aba", tb_json_string(T->name));
|
||||
PUT(o, "tipo", tb_json_string(b->type));
|
||||
PUT(o, "x", tb_json_number(b->x));
|
||||
PUT(o, "y", tb_json_number(b->y));
|
||||
PUT(o, "texto", tb_json_string(b->in_text[0]));
|
||||
tb_json_array_push(arr, o);
|
||||
tb_json_release(o);
|
||||
}
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
char* tb_blueprint_serialize(const TbBlueprint* bp, int fmt, bool pretty) {
|
||||
Buf b = {0};
|
||||
if (fmt == TB_BP_FMT_CSV) {
|
||||
TinyBlocks_JsonValue* data = blueprint_csv_tree(bp);
|
||||
csv_rec(&b, data);
|
||||
tb_json_release(data);
|
||||
} else if (fmt == TB_BP_FMT_XML) {
|
||||
blueprint_xml(&b, bp);
|
||||
} else {
|
||||
TinyBlocks_JsonValue* root = tb_blueprint_to_json(bp);
|
||||
json_rec(&b, root, pretty, 0); bc(&b, '\n');
|
||||
tb_json_release(root);
|
||||
}
|
||||
if (!b.p) { b.p = (char*)malloc(1); if (b.p) b.p[0] = 0; }
|
||||
return b.p;
|
||||
}
|
||||
|
||||
TbBlueprint* tb_blueprint_parse(const char* text, int fmt, char* err, size_t errcap) {
|
||||
if (fmt != TB_BP_FMT_JSON) {
|
||||
if (err && errcap) snprintf(err, errcap, "so o formato JSON e carregavel");
|
||||
return NULL;
|
||||
}
|
||||
TinyBlocks_JsonValue* root = tb_json_parse(text, err, errcap);
|
||||
if (!root) return NULL;
|
||||
TinyBlocks_JsonValue* fv = tb_json_object_get(root, "format");
|
||||
const char* fmt_s = fv ? tb_json_as_string(fv) : NULL;
|
||||
bool ok = fmt_s && strcmp(fmt_s, "tinyblocks-project") == 0;
|
||||
tb_json_release(fv);
|
||||
if (!ok) { if (err && errcap) snprintf(err, errcap, "formato de projeto desconhecido"); tb_json_release(root); return NULL; }
|
||||
TbBlueprint* bp = tb_blueprint_from_json(root);
|
||||
tb_json_release(root);
|
||||
return bp;
|
||||
}
|
||||
|
||||
bool tb_blueprint_save_file(const TbBlueprint* bp, const char* path, int fmt, bool pretty) {
|
||||
char* s = tb_blueprint_serialize(bp, fmt, pretty);
|
||||
if (!s) return false;
|
||||
FILE* f = fopen(path, "wb");
|
||||
if (!f) { free(s); return false; }
|
||||
size_t n = strlen(s);
|
||||
size_t w = fwrite(s, 1, n, f);
|
||||
fclose(f);
|
||||
free(s);
|
||||
return w == n;
|
||||
}
|
||||
|
||||
TbBlueprint* tb_blueprint_load_file(const char* path, char* err, size_t errcap) {
|
||||
FILE* f = fopen(path, "rb");
|
||||
if (!f) { if (err && errcap) snprintf(err, errcap, "nao abriu %s", path); return NULL; }
|
||||
fseek(f, 0, SEEK_END); long sz = ftell(f); fseek(f, 0, SEEK_SET);
|
||||
if (sz < 0 || sz > 256L * 1024 * 1024) { fclose(f); if (err && errcap) snprintf(err, errcap, "arquivo grande demais"); return NULL; }
|
||||
char* buf = (char*)malloc((size_t)sz + 1);
|
||||
if (!buf) { fclose(f); return NULL; }
|
||||
size_t rd = fread(buf, 1, (size_t)sz, f);
|
||||
buf[rd] = 0; fclose(f);
|
||||
TbBlueprint* bp = tb_blueprint_parse(buf, TB_BP_FMT_JSON, err, errcap);
|
||||
free(buf);
|
||||
return bp;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* TinyBlocks - tb_blueprint.c
|
||||
* Registro da library (categoria "Blueprint"). O modelo/serializacao fica em
|
||||
* blueprint.c/serialize.c; aqui so expomos o descritor e um bloco informativo.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include "tb_blueprint.h"
|
||||
|
||||
#define CAT_COLOR 110/255.f, 140/255.f, 255/255.f, 1.f
|
||||
#define CAT_DARK 70/255.f, 95/255.f, 210/255.f, 1.f
|
||||
|
||||
static void eval_format(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
(void)rt;
|
||||
tb_block_set_output(self, 0, tb_value_text("tinyblocks-project"));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void eval_version(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
(void)rt;
|
||||
tb_block_set_output(self, 0, tb_value_integer(1));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static bool blueprint_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
static const TinyBlocks_Category cat = { "Blueprint", {CAT_COLOR}, {CAT_DARK} };
|
||||
tb_library_set_category(lib, &cat);
|
||||
static const TinyBlocks_SlotDesc out_t[] = {{"formato", TB_VALUE_TEXT}};
|
||||
static const TinyBlocks_SlotDesc out_i[] = {{"versao", TB_VALUE_INTEGER}};
|
||||
TinyBlocks_TypeBlock* a = tb_type_block_create(rt, "Blueprint.Format", NULL, 0, out_t, 1, eval_format);
|
||||
TinyBlocks_TypeBlock* b = tb_type_block_create(rt, "Blueprint.Version", NULL, 0, out_i, 1, eval_version);
|
||||
return a && b && tb_library_add(lib, &a->base) && tb_library_add(lib, &b->base);
|
||||
}
|
||||
|
||||
static void blueprint_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { (void)rt; (void)lib; }
|
||||
|
||||
TINYBLOCKS_DEFINE_LIBRARY(blueprint,
|
||||
"tinyblocks-blueprint",
|
||||
"1.0.0",
|
||||
blueprint_init,
|
||||
blueprint_deinit)
|
||||
|
||||
const TinyBlocks_LibraryDescriptor* tb_blueprint_get_library(void) {
|
||||
return &_tb_lib_desc_blueprint;
|
||||
}
|
||||
Reference in new issue
Block a user