// VDE 插件 SDK 模板:format(文件格式读写骨架;纯 C ABI,不链接 VDE 内核) // // 演示: // - 最小 VdeFormatPlugin 注册(format_id/extensions/probe/read/write/describe); // - 通过 VdePluginHost 宿主服务创建/查询/序列化实体(body_create_box/body_bbox/ // body_serialize/free_buffer),不直接链接内核; // - 跨 ABI 内存所有权:输出数组/缓冲区用 host->alloc,宿主用 host->free_buffer/free; // - 失败路径:负错误码 + host->set_error(不跨 C ABI 抛异常)。 // // 本模板的 .example 格式(演示用,可替换为真实格式): // VDE-EXAMPLE 1 // box // 读取时解析 box 尺寸并调用 host->body_create_box 创建实体;写入时输出同样两行 // (尺寸取实体 bbox),即一个最小的“可读可写”格式插件。 #include #include #include #include #include "vde/plugin/vde_plugin_api.h" namespace { constexpr const char* kFormatId = "example"; constexpr const char* kMagic = "VDE-EXAMPLE"; int fmt_fail(const VdePluginHost* host, int code, const char* message) { if (host && host->set_error) host->set_error(message); return code; } /// 校验宿主服务表:本模板需要创建/查询/序列化实体与缓冲区管理。 int validate_host(const VdePluginHost* host) { if (!host) return VDE_PLUGIN_ERR_INVALID_ARG; if (host->abi_major != VDE_PLUGIN_ABI_MAJOR) return fmt_fail(host, VDE_PLUGIN_ERR_ABI, "format template: host ABI major mismatch"); if (!host->alloc || !host->free || !host->set_error || !host->free_buffer) return fmt_fail(host, VDE_PLUGIN_ERR_UNSUPPORTED, "format template: host lacks alloc/free/set_error/free_buffer"); if (!host->body_create_box || !host->body_bbox || !host->body_serialize) return fmt_fail(host, VDE_PLUGIN_ERR_UNSUPPORTED, "format template: host lacks body_create_box/body_bbox/body_serialize"); return VDE_PLUGIN_OK; } int probe(const void* head, size_t head_size) { if (!head || head_size < std::strlen(kMagic)) return 0; return std::memcmp(head, kMagic, std::strlen(kMagic)) == 0 ? 100 : 0; } int read_memory(const VdePluginHost* host, const void* data, size_t size, VdeBody** bodies, size_t* count) { if (bodies) *bodies = nullptr; if (count) *count = 0; const int host_rc = validate_host(host); if (host_rc != VDE_PLUGIN_OK) return host_rc; if (!data || size == 0) return fmt_fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "format template: empty input"); if (!bodies || !count) return fmt_fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "format template: null output"); const std::string text(static_cast(data), size); if (text.compare(0, std::strlen(kMagic), kMagic) != 0) return fmt_fail(host, VDE_PLUGIN_ERR_PARSE, "format template: missing VDE-EXAMPLE header"); double sx = 0.0, sy = 0.0, sz = 0.0; if (std::sscanf(text.c_str(), "VDE-EXAMPLE 1 box %lf %lf %lf", &sx, &sy, &sz) != 3 || !(sx > 0.0) || !(sy > 0.0) || !(sz > 0.0)) { return fmt_fail(host, VDE_PLUGIN_ERR_PARSE, "format template: expected 'VDE-EXAMPLE 1 box '"); } VdeBody body = host->body_create_box(sx, sy, sz); if (!body) return fmt_fail(host, VDE_PLUGIN_ERR_INTERNAL, "format template: body_create_box failed"); VdeBody* array = static_cast(host->alloc(sizeof(VdeBody))); if (!array) { host->body_destroy(body); return fmt_fail(host, VDE_PLUGIN_ERR_BUFFER, "format template: body array alloc failed"); } array[0] = body; *bodies = array; *count = 1; return VDE_PLUGIN_OK; } int write_memory(const VdePluginHost* host, const VdeBody* bodies, size_t count, VdeBuffer* out) { if (out) { out->data = nullptr; out->size = 0; } const int host_rc = validate_host(host); if (host_rc != VDE_PLUGIN_OK) return host_rc; if (!out) return fmt_fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "format template: null output buffer"); if (!bodies || count == 0) return fmt_fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "format template: no bodies to write"); double bmin[3] = {0.0, 0.0, 0.0}; double bmax[3] = {0.0, 0.0, 0.0}; int rc = host->body_bbox(bodies[0], bmin, bmax); if (rc != VDE_PLUGIN_OK) return fmt_fail(host, VDE_PLUGIN_ERR_INTERNAL, "format template: body_bbox failed"); char header[256]; const int written = std::snprintf(header, sizeof(header), "VDE-EXAMPLE 1\nbox %.10g %.10g %.10g\n", bmax[0] - bmin[0], bmax[1] - bmin[1], bmax[2] - bmin[2]); if (written <= 0) return fmt_fail(host, VDE_PLUGIN_ERR_INTERNAL, "format template: header format failed"); const std::size_t total = static_cast(written); void* buffer = host->alloc(total + 1); if (!buffer) return fmt_fail(host, VDE_PLUGIN_ERR_BUFFER, "format template: output alloc failed"); std::memcpy(buffer, header, total); static_cast(buffer)[total] = '\0'; out->data = buffer; out->size = total; return VDE_PLUGIN_OK; } int read_file(const VdePluginHost* host, const char* path, VdeBody** bodies, size_t* count) { if (!path) return fmt_fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "format template: null path"); std::FILE* file = std::fopen(path, "rb"); if (!file) return fmt_fail(host, VDE_PLUGIN_ERR_IO, "format template: cannot open file"); std::fseek(file, 0, SEEK_END); const long length = std::ftell(file); std::fseek(file, 0, SEEK_SET); if (length <= 0) { std::fclose(file); return fmt_fail(host, VDE_PLUGIN_ERR_PARSE, "format template: empty file"); } std::string text(static_cast(length), '\0'); const std::size_t read = std::fread(text.data(), 1, text.size(), file); std::fclose(file); if (read != text.size()) return fmt_fail(host, VDE_PLUGIN_ERR_IO, "format template: short read"); return read_memory(host, text.data(), text.size(), bodies, count); } int write_file(const VdePluginHost* host, const char* path, const VdeBody* bodies, size_t count) { if (!path) return fmt_fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "format template: null path"); VdeBuffer buffer{}; const int rc = write_memory(host, bodies, count, &buffer); if (rc != VDE_PLUGIN_OK) return rc; std::FILE* file = std::fopen(path, "wb"); if (!file) { if (buffer.data && host->free_buffer) host->free_buffer(buffer.data); return fmt_fail(host, VDE_PLUGIN_ERR_IO, "format template: cannot create file"); } const std::size_t written = std::fwrite(buffer.data, 1, buffer.size, file); std::fclose(file); if (host->free_buffer) host->free_buffer(buffer.data); if (written != buffer.size) return fmt_fail(host, VDE_PLUGIN_ERR_IO, "format template: short write"); return VDE_PLUGIN_OK; } const char* describe() { return "VDE format plugin template (.example: VDE-EXAMPLE 1 / box sx sy sz)"; } const VdeFormatPlugin kFormat = [] { VdeFormatPlugin plugin{}; plugin.struct_size = static_cast(sizeof(VdeFormatPlugin)); plugin.format_id = kFormatId; plugin.extensions = ".example"; plugin.probe = &probe; plugin.read_file = &read_file; plugin.read_memory = &read_memory; plugin.write_file = &write_file; plugin.write_memory = &write_memory; plugin.describe = &describe; return plugin; }(); } // namespace extern "C" { VDE_PLUGIN_EXPORT int vde_plugin_query(uint32_t host_abi_major, VdePluginInfo* info) { if (!info || info->struct_size < sizeof(VdePluginInfo)) return VDE_PLUGIN_ERR_ABI; if (host_abi_major != VDE_PLUGIN_ABI_MAJOR) return VDE_PLUGIN_ERR_ABI; info->id = "com.example.vde.format"; info->name = "VDE Format Plugin Template"; info->version = "0.1.0"; info->kind = VDE_PLUGIN_KIND_FORMAT; info->abi_major = VDE_PLUGIN_ABI_MAJOR; info->abi_minor = VDE_PLUGIN_ABI_MINOR; info->capabilities = "format:example.read;format:example.write"; info->license = "Apache-2.0"; return VDE_PLUGIN_OK; } VDE_PLUGIN_EXPORT int vde_plugin_init(const VdePluginHost* host, VdePluginRegistration* reg) { if (!reg || reg->struct_size < sizeof(VdePluginRegistration)) return VDE_PLUGIN_ERR_ABI; const int host_rc = validate_host(host); if (host_rc != VDE_PLUGIN_OK) return host_rc; reg->kind = VDE_PLUGIN_KIND_FORMAT; reg->format = &kFormat; reg->generic = nullptr; reg->cae_solver = nullptr; reg->cam_strategy = nullptr; reg->gcs_solver = nullptr; return VDE_PLUGIN_OK; } VDE_PLUGIN_EXPORT void vde_plugin_shutdown() {} } // extern "C"