Blueprint: formatos de arquivo como interface (TbBpFormat)

JSON, XML e CSV são TbBpFormat (id, nome, extensão, write, read) numa
tabela; tb_blueprint_serialize/parse/format_name despacham por ela e há
tb_blueprint_format_for_path. O editor usa os ids TB_BP_FMT_* em vez de
um enum próprio duplicado.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mguinhosandClaude Opus 5.5 committed 2026-10-22 19:04:00 -03:00
1 parent 3b37ac7cd6
commit 27245d9080
5 files changed
+82 -29

No files matched your search

@@ -123,9 +123,6 @@ void tb_blueprint_var_set_text(TbBlueprint* bp, const char* name, const char* va
}
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";
}
const TbBpFormat* f = tb_blueprint_format(fmt);
return f ? f->name : "json";
}
+64 -20
View File
@@ -4,6 +4,7 @@
*/
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include "blueprint.h"
@@ -544,28 +545,22 @@ static TinyBlocks_JsonValue* blueprint_csv_tree(const TbBlueprint* bp) {
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;
/* -- Formatos (TbBpFormat) ------------------------------------------------ */
static char* buf_take(Buf* b) {
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;
}
static char* json_write(const TbBlueprint* bp, bool pretty) {
Buf b = {0};
TinyBlocks_JsonValue* root = tb_blueprint_to_json(bp);
json_rec(&b, root, pretty, 0); bc(&b, '\n');
tb_json_release(root);
return buf_take(&b);
}
static TbBlueprint* json_read(const char* text, char* err, size_t errcap) {
TinyBlocks_JsonValue* root = tb_json_parse(text, err, errcap);
if (!root) return NULL;
TinyBlocks_JsonValue* fv = tb_json_object_get(root, "format");
@@ -578,6 +573,55 @@ TbBlueprint* tb_blueprint_parse(const char* text, int fmt, char* err, size_t err
return bp;
}
static char* xml_write(const TbBlueprint* bp, bool pretty) {
(void)pretty;
Buf b = {0};
blueprint_xml(&b, bp);
return buf_take(&b);
}
static char* csv_write(const TbBlueprint* bp, bool pretty) {
(void)pretty;
Buf b = {0};
TinyBlocks_JsonValue* data = blueprint_csv_tree(bp);
csv_rec(&b, data);
tb_json_release(data);
return buf_take(&b);
}
static const TbBpFormat s_formats[] = {
{ TB_BP_FMT_JSON, "json", ".json", json_write, json_read },
{ TB_BP_FMT_XML, "xml", ".xml", xml_write, NULL },
{ TB_BP_FMT_CSV, "csv", ".csv", csv_write, NULL },
};
const TbBpFormat* tb_blueprint_format(int fmt) {
for (size_t i = 0; i < sizeof(s_formats) / sizeof(s_formats[0]); i++)
if (s_formats[i].id == fmt) return &s_formats[i];
return NULL;
}
const TbBpFormat* tb_blueprint_format_for_path(const char* path) {
const char* dot = path ? strrchr(path, '.') : NULL;
for (size_t i = 0; dot && i < sizeof(s_formats) / sizeof(s_formats[0]); i++)
if (strcasecmp(dot, s_formats[i].ext) == 0) return &s_formats[i];
return NULL;
}
char* tb_blueprint_serialize(const TbBlueprint* bp, int fmt, bool pretty) {
const TbBpFormat* f = tb_blueprint_format(fmt);
return (f ? f : &s_formats[0])->write(bp, pretty);
}
TbBlueprint* tb_blueprint_parse(const char* text, int fmt, char* err, size_t errcap) {
const TbBpFormat* f = tb_blueprint_format(fmt);
if (!f || !f->read) {
if (err && errcap) snprintf(err, errcap, "o formato %s so exporta", f ? f->name : "?");
return NULL;
}
return f->read(text, err, errcap);
}
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;