Skip to content

Commit

Permalink
fix buffer interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
bicycle1885 committed Jan 23, 2018
1 parent 3a7ea72 commit 3227651
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 50 deletions.
1 change: 0 additions & 1 deletion src/EzXML.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ end
include("error.jl")
include("node.jl")
include("document.jl")
include("buffer.jl")
include("xpath.jl")
include("streamreader.jl")

Expand Down
36 changes: 0 additions & 36 deletions src/buffer.jl

This file was deleted.

38 changes: 27 additions & 11 deletions src/node.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1158,15 +1158,15 @@ end

doc = parsexml("<e1><e2/></e1>")
buf = IOBuffer()
print(buf, doc)
@test print(buf, doc) === nothing
@test take!(buf) == b"""
<?xml version="1.0" encoding="UTF-8"?>
<e1><e2/></e1>
"""

doc = parsexml("<e1><e2/></e1>")
buf = IOBuffer()
prettyprint(buf, doc)
@test prettyprint(buf, doc) === nothing
@test take!(buf) == b"""
<?xml version="1.0" encoding="UTF-8"?>
<e1>
Expand Down

0 comments on commit 3227651

Please sign in to comment.