Crystal-Lang C-Binding 结构似乎没有传递空值
Crystal-Lang C-Binding struct doesn't seem to pass null value
我目前正在尝试使用 libxml2 的 c 绑定来添加对 crystal-lang 的 c14n 支持。我已经成功地能够使用 xmlC14NDocSave 将规范 xml 保存到文件中。我遇到的问题是 xmlOutputBufferPtr 对于 xmlC14NDocSaveTo 和 xmlC14NExecute。
我收到的错误是(Mac 和 Linux)
xmlC14NExecute: output buffer encoder != NULL but C14N requires UTF8 output
文档说明
this buffer MUST have encoder==NULL because C14N requires UTF-8 output
在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
不要指定out output
,它只是在堆栈上保留一个结构大小的内存块并传递一个指向它的指针。从 Crystal 0.7.6 开始,它没有将它清零,所以你通过了垃圾。
使用output = XML::LibC14N::OutputBuffer.new
已经是正确的第一步,因为这会将内存归零。现在要传递它,只需将 out output
替换为 pointerof(output)
.
我目前正在尝试使用 libxml2 的 c 绑定来添加对 crystal-lang 的 c14n 支持。我已经成功地能够使用 xmlC14NDocSave 将规范 xml 保存到文件中。我遇到的问题是 xmlOutputBufferPtr 对于 xmlC14NDocSaveTo 和 xmlC14NExecute。
我收到的错误是(Mac 和 Linux)
xmlC14NExecute: output buffer encoder != NULL but C14N requires UTF8 output
文档说明
this buffer MUST have encoder==NULL because C14N requires UTF-8 output
在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
不要指定out output
,它只是在堆栈上保留一个结构大小的内存块并传递一个指向它的指针。从 Crystal 0.7.6 开始,它没有将它清零,所以你通过了垃圾。
使用output = XML::LibC14N::OutputBuffer.new
已经是正确的第一步,因为这会将内存归零。现在要传递它,只需将 out output
替换为 pointerof(output)
.