【问题标题】:Crystal-Lang C-Binding struct doesn't seem to pass null valueCrystal-Lang C-Binding 结构似乎没有传递空值
【发布时间】:2015-08-26 22:12:23
【问题描述】:

我目前正在尝试使用带有libxml2 的 c 绑定来添加对水晶语言的 c14n 支持。我已经成功地能够使用 xmlC14NDocSave 将规范的 xml 保存到文件中。我遇到的问题是 xmlC14NDocSaveTo 和 xmlC14NExecute 的 xmlOutputBufferPtr。

我收到的错误是(Mac 和 Linux)

xmlC14NExecute:输出缓冲区编码器!= NULL 但 C14N 需要 UTF8 输出

文档说明

这个缓冲区必须有 encoder==NULL 因为 C14N 需要 UTF-8 输出

src/C14N/lib_C14N.cr我有以下代码

type CharEncodingHandler = Void*
type Buff = Void*
#type OutputBuffer = Void*
struct OutputBuffer
  context : Void*
  writecallback : OutputWriteCallback
  closecallback : OutputCloseCallback
  encoder : CharEncodingHandler
  buffer  : Buff
  conv    : Buff
  written : Int32
  error   : Int32
end
....
fun xmlC14NDocSaveTo(doc : LibXML::Node*, nodes : LibXML::NodeSet*, mode : Mode, inclusive_ns_prefixes : UInt8**, with_comments : Int32, buf : OutputBuffer*) : Int32
fun xmlC14NExecute(doc : LibXML::Node*, is_visible_callback : IsVisibleCallback, user_data : Void*, mode : Mode, inclusive_ns_prefixes : UInt8**, with_comments : Int32, buf : OutputBuffer*) : Int32

src/C14N.cr

output = XML::LibC14N::OutputBuffer.new
p output.encoder
XML::LibC14N.xmlC14NDocSaveTo(@xml, nil, @mode, nil, 0, out output)

p ouput.encoder 的结果是Pointer(Void).null 所以看起来值是空的。

c14n.c 函数只是在 buf->encoder 结构上检查 null

if (buf->encoder != NULL) {
    xmlGenericError(xmlGenericErrorContext,
                    "xmlC14NExecute: output buffer encoder != NULL but C14N requires UTF8 output\n");
    return (-1);
}

任何帮助将不胜感激,代码可以在我的github 帐户上找到。克隆并运行crystal spec

【问题讨论】:

    标签: binding crystal-lang


    【解决方案1】:

    不要指定out output,它只是在堆栈上保留一个结构大小的内存块并传递一个指向它的指针。从 Crystal 0.7.6 开始,它并没有将其归零,所以你会传递垃圾。

    使用output = XML::LibC14N::OutputBuffer.new 已经是正确的第一步,因为这会将内存清零。现在要通过它,只需将out output 替换为pointerof(output)

    【讨论】:

    • 感谢您的帮助。出于某种原因,在阅读了文档后,我认为这只是 pointerof 的快捷方式。
    猜你喜欢
    • 1970-01-01
    • 2018-04-27
    • 2016-04-08
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    • 2015-09-01
    相关资源
    最近更新 更多