From 80888c43e229d71fc9fce0500b4279f2f00ac55f 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 | 32 -------------------------------- src/node.jl | 38 +++++++++++++++++++++++++++----------- test/runtests.jl | 4 ++-- 4 files changed, 29 insertions(+), 46 deletions(-) delete mode 100644 src/buffer.jl diff --git a/src/EzXML.jl b/src/EzXML.jl index 08c226c..49df8f2 100644 --- a/src/EzXML.jl +++ b/src/EzXML.jl @@ -108,7 +108,6 @@ using XML2_jll: libxml2 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 c94b4fe..0000000 --- a/src/buffer.jl +++ /dev/null @@ -1,32 +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) - finalizer(finalize_buffer, buf) - 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 fe74d3a..e7d5c9a 100644 --- a/src/node.jl +++ b/src/node.jl @@ -323,21 +323,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 9cf40a5..a572232 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1327,7 +1327,7 @@ end doc = parsexml("") buf = IOBuffer() - print(buf, doc) + @test print(buf, doc) === nothing @test take!(buf) == b""" @@ -1335,7 +1335,7 @@ end doc = parsexml("") buf = IOBuffer() - prettyprint(buf, doc) + @test prettyprint(buf, doc) === nothing @test take!(buf) == b"""