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:
1 parent
3b37ac7cd6
commit
27245d9080
5 files changed
+78
-25
No files matched your search
@@ -130,6 +130,19 @@ TinyBlocks_JsonValue* tb_blueprint_to_json(const TbBlueprint* bp);
|
|||||||
TbBlueprint* tb_blueprint_from_json(const TinyBlocks_JsonValue* root);
|
TbBlueprint* tb_blueprint_from_json(const TinyBlocks_JsonValue* root);
|
||||||
|
|
||||||
/* Serializa para texto no formato pedido (malloc; libere com free). */
|
/* Serializa para texto no formato pedido (malloc; libere com free). */
|
||||||
|
/* Formatos de arquivo como interface: cada um sabe escrever (e talvez ler)
|
||||||
|
um projeto. serialize/parse/save/load despacham pela tabela. */
|
||||||
|
typedef struct TbBpFormat {
|
||||||
|
int id; /* TB_BP_FMT_* */
|
||||||
|
const char* name; /* "json" */
|
||||||
|
const char* ext; /* ".json" */
|
||||||
|
char* (*write)(const TbBlueprint* bp, bool pretty); /* malloc */
|
||||||
|
TbBlueprint* (*read)(const char* text, char* err, size_t errcap); /* NULL = so exporta */
|
||||||
|
} TbBpFormat;
|
||||||
|
|
||||||
|
const TbBpFormat* tb_blueprint_format(int fmt); /* NULL se nao existe */
|
||||||
|
const TbBpFormat* tb_blueprint_format_for_path(const char* path); /* pela extensao */
|
||||||
|
|
||||||
char* tb_blueprint_serialize(const TbBlueprint* bp, int fmt, bool pretty);
|
char* tb_blueprint_serialize(const TbBlueprint* bp, int fmt, bool pretty);
|
||||||
/* Le do texto. Suporta o formato nativo (JSON). */
|
/* Le do texto. Suporta o formato nativo (JSON). */
|
||||||
TbBlueprint* tb_blueprint_parse(const char* text, int fmt, char* err, size_t errcap);
|
TbBlueprint* tb_blueprint_parse(const char* text, int fmt, char* err, size_t errcap);
|
||||||
|
|||||||
@@ -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) {
|
const char* tb_blueprint_format_name(int fmt) {
|
||||||
switch (fmt) {
|
const TbBpFormat* f = tb_blueprint_format(fmt);
|
||||||
case TB_BP_FMT_XML: return "xml";
|
return f ? f->name : "json";
|
||||||
case TB_BP_FMT_CSV: return "csv";
|
|
||||||
default: return "json";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -4,6 +4,7 @@
|
|||||||
*/
|
*/
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <strings.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "blueprint.h"
|
#include "blueprint.h"
|
||||||
@@ -544,28 +545,22 @@ static TinyBlocks_JsonValue* blueprint_csv_tree(const TbBlueprint* bp) {
|
|||||||
return arr;
|
return arr;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* tb_blueprint_serialize(const TbBlueprint* bp, int fmt, bool pretty) {
|
/* -- 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
static char* json_write(const TbBlueprint* bp, bool pretty) {
|
||||||
Buf b = {0};
|
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);
|
TinyBlocks_JsonValue* root = tb_blueprint_to_json(bp);
|
||||||
json_rec(&b, root, pretty, 0); bc(&b, '\n');
|
json_rec(&b, root, pretty, 0); bc(&b, '\n');
|
||||||
tb_json_release(root);
|
tb_json_release(root);
|
||||||
}
|
return buf_take(&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) {
|
static TbBlueprint* json_read(const char* text, 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);
|
TinyBlocks_JsonValue* root = tb_json_parse(text, err, errcap);
|
||||||
if (!root) return NULL;
|
if (!root) return NULL;
|
||||||
TinyBlocks_JsonValue* fv = tb_json_object_get(root, "format");
|
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;
|
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) {
|
bool tb_blueprint_save_file(const TbBlueprint* bp, const char* path, int fmt, bool pretty) {
|
||||||
char* s = tb_blueprint_serialize(bp, fmt, pretty);
|
char* s = tb_blueprint_serialize(bp, fmt, pretty);
|
||||||
if (!s) return false;
|
if (!s) return false;
|
||||||
|
|||||||
@@ -753,7 +753,6 @@ typedef struct {
|
|||||||
} Toast;
|
} Toast;
|
||||||
|
|
||||||
/* Formatos de exportacao */
|
/* Formatos de exportacao */
|
||||||
enum { EX_JSON, EX_XML, EX_CSV };
|
|
||||||
|
|
||||||
typedef struct Ed {
|
typedef struct Ed {
|
||||||
TinyBlocks* rt;
|
TinyBlocks* rt;
|
||||||
|
|||||||
@@ -6081,9 +6081,9 @@ TB_ED_API void path_commit(Ed* ed, int mode, const char* p) {
|
|||||||
switch (mode) {
|
switch (mode) {
|
||||||
case PM_LOAD: project_load(ed, p); break;
|
case PM_LOAD: project_load(ed, p); break;
|
||||||
case PM_SAVE: project_save(ed, p); break;
|
case PM_SAVE: project_save(ed, p); break;
|
||||||
case PM_EXP_JSON: project_export(ed, p, EX_JSON); break;
|
case PM_EXP_JSON: project_export(ed, p, TB_BP_FMT_JSON); break;
|
||||||
case PM_EXP_XML: project_export(ed, p, EX_XML); break;
|
case PM_EXP_XML: project_export(ed, p, TB_BP_FMT_XML); break;
|
||||||
case PM_EXP_CSV: project_export(ed, p, EX_CSV); break;
|
case PM_EXP_CSV: project_export(ed, p, TB_BP_FMT_CSV); break;
|
||||||
case PM_EXP_SB: sb_export_file(ed, p); break;
|
case PM_EXP_SB: sb_export_file(ed, p); break;
|
||||||
case PM_IMGLOAD: if (CT.kind == TK_IMAGE) img_load_file(ed, p, -1); break;
|
case PM_IMGLOAD: if (CT.kind == TK_IMAGE) img_load_file(ed, p, -1); break;
|
||||||
case PM_IMSAVE: if (CT.kind == TK_IMAGE) img_save_out(ed, p); break;
|
case PM_IMSAVE: if (CT.kind == TK_IMAGE) img_save_out(ed, p); break;
|
||||||
|
|||||||
Reference in new issue
Block a user