From 32276510a11e9210f136a2b8c6f4f4bcf19870f6 Mon Sep 17 00:00:00 2001 From: Kenta Sato Date: Tue, 23 Jan 2018 11:45:50 +0900 Subject: [PATCH] fix buffer interfaces --- src/EzXML.jl | 1 - src/buffer.jl | 36 ------------------------------------ src/node.jl | 38 +++++++++++++++++++++++++++----------- test/runtests.jl | 4 ++-- 4 files changed, 29 insertions(+), 50 deletions(-) delete mode 100644 src/buffer.jl diff --git a/src/EzXML.jl b/src/EzXML.jl index 4f8caf3..a77ec19 100644 --- a/src/EzXML.jl +++ b/src/EzXML.jl @@ -114,7 +114,6 @@ end include("error.jl") include("node.jl") include("document.jl") -include("buffer.jl") include("xpath.jl") include("streamreader.jl") diff --git a/src/buffer.jl b/src/buffer.jl deleted file mode 100644 index add013d..0000000 --- a/src/buffer.jl +++ /dev/null @@ -1,36 +0,0 @@ -# Buffer -# ====== - -struct _Buffer - content::Cstring - use::Cuint - size::Cuint - alloc::Ptr{Cvoid} - contentIO::Cstring -end - -mutable struct Buffer - ptr::Ptr{_Buffer} - - function Buffer() - buf_ptr = ccall( - (:xmlBufferCreate, libxml2), - Ptr{_Buffer}, - ()) - buf = new(buf_ptr) - if VERSION > v"0.7-" - finalizer(finalize_buffer, buf) - else - finalizer(buf, finalize_buffer) - end - return buf - end -end - -function finalize_buffer(buf) - ccall( - (:xmlBufferFree, libxml2), - Cvoid, - (Ptr{Cvoid},), - buf.ptr) -end diff --git a/src/node.jl b/src/node.jl index 46663b3..10e5f3a 100644 --- a/src/node.jl +++ b/src/node.jl @@ -275,21 +275,37 @@ function prettyprint(io::IO, node::Node) dump_node(io, node, true) end -# Dump `node` to `io`. -function dump_node(io, node, format) +function dump_node(io::IO, node::Node, format::Bool) if hasdocument(node) doc_ptr = document(node).node.ptr else - doc_ptr = C_NULL + doc_ptr = convert(Ptr{_Node}, C_NULL) + end + buf_ptr = @check ccall( + (:xmlBufCreate, libxml2), + Ptr{Cvoid}, + ()) != C_NULL + try + level = 0 + size = ccall( + (:xmlBufNodeDump, libxml2), + Csize_t, + (Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Cint, Cint), + buf_ptr, doc_ptr, node.ptr, level, format) + content_ptr = @check ccall( + (:xmlBufContent, libxml2), + Ptr{UInt8}, + (Ptr{Cvoid},), + buf_ptr) != C_NULL + unsafe_write(io, content_ptr, size) + return nothing + finally + ccall( + (:xmlBufFree, libxml2), + Cvoid, + (Ptr{Cvoid},), + buf_ptr) end - buf = Buffer() - level = 0 - len = @check ccall( - (:xmlNodeDump, libxml2), - Cint, - (Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Cint, Cint), - buf.ptr, doc_ptr, node.ptr, level, format) != -1 - print(io, unsafe_string(unsafe_load(buf.ptr).content)) end function Base.:(==)(n1::Node, n2::Node) diff --git a/test/runtests.jl b/test/runtests.jl index 7b4d6d0..9be2685 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1158,7 +1158,7 @@ end doc = parsexml("") buf = IOBuffer() - print(buf, doc) + @test print(buf, doc) === nothing @test take!(buf) == b""" @@ -1166,7 +1166,7 @@ end doc = parsexml("") buf = IOBuffer() - prettyprint(buf, doc) + @test prettyprint(buf, doc) === nothing @test take!(buf) == b"""