Skip to content

Commit 9a5f8c2

Browse files
committed
enable precompilation (fix #30)
1 parent bd448e9 commit 9a5f8c2

File tree

7 files changed

+93
-147
lines changed

7 files changed

+93
-147
lines changed

src/LightXML.jl

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
VERSION >= v"0.4.0-dev+6521" && __precompile__(false)
1+
VERSION >= v"0.4.0-dev+6521" && __precompile__()
22

33
module LightXML
44

5-
export
5+
using Compat
6+
7+
const libxml2 = @windows? Pkg.dir("WinRPM","deps","usr","$(Sys.ARCH)-w64-mingw32","sys-root","mingw","bin","libxml2-2") : "libxml2"
8+
9+
export
610

711
# common
812
name, free,
@@ -22,12 +26,23 @@ module LightXML
2226
XMLDocument, version, encoding, compression, standalone, root,
2327
parse_file, parse_string, save_file, set_root, create_root
2428

25-
using Compat
26-
include("clib.jl")
27-
include("errors.jl")
29+
typealias Xchar UInt8
30+
typealias Xstr Ptr{Xchar}
31+
32+
# opaque pointer type (do not dereference!) corresponding to xmlBufferPtr in C
33+
immutable xmlBuffer end
34+
typealias Xptr Ptr{xmlBuffer}
35+
36+
# pre-condition: p is not null
37+
# (After tests, it seems that free in libc instead of xmlFree
38+
# should be used here.)
39+
_xcopystr(p::Xstr) = (r = bytestring(p); Libc.free(p); r)
40+
41+
include("errors.jl")
42+
43+
include("utils.jl")
44+
include("nodes.jl")
45+
include("document.jl")
46+
include("cdata.jl")
2847

29-
include("utils.jl")
30-
include("nodes.jl")
31-
include("document.jl")
32-
include("cdata.jl")
3348
end

src/cdata.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function new_cdatanode(xdoc::XMLDocument, txt::ASCIIString)
2-
p = ccall(xmlNewCDataBlock, Xptr, (Xptr, Xstr, Cint), xdoc.ptr, txt, length(txt)+1)
2+
p = ccall((:xmlNewCDataBlock,libxml2), Xptr, (Xptr, Xstr, Cint), xdoc.ptr, txt, length(txt)+1)
33
XMLNode(p)
44
end
55

src/clib.jl

Lines changed: 0 additions & 69 deletions
This file was deleted.

src/document.jl

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ immutable _XMLDocStruct # use the same layout as C
55
# common part
66
_private::Ptr{Void}
77
nodetype::Cint
8-
name::Ptr{Cchar}
8+
name::Xstr
99
children::Xptr
1010
last::Xptr
1111
parent::Xptr
@@ -48,7 +48,7 @@ type XMLDocument
4848

4949
function XMLDocument()
5050
# create an empty document
51-
ptr = ccall(xmlNewDoc, Xptr, (Ptr{Cchar},), "1.0")
51+
ptr = ccall((:xmlNewDoc,libxml2), Xptr, (Cstring,), "1.0")
5252
XMLDocument(ptr)
5353
end
5454
end
@@ -59,59 +59,59 @@ compression(xdoc::XMLDocument) = @compat Int(xdoc._struct.compression)
5959
standalone(xdoc::XMLDocument) = @compat Int(xdoc._struct.standalone)
6060

6161
function root(xdoc::XMLDocument)
62-
pr = ccall(xmlDocGetRootElement, Ptr{Void}, (Ptr{Void},), xdoc.ptr)
63-
pr != nullptr || throw(XMLNoRootError())
62+
pr = ccall((:xmlDocGetRootElement,libxml2), Xptr, (Xptr,), xdoc.ptr)
63+
pr != C_NULL || throw(XMLNoRootError())
6464
XMLElement(pr)
6565
end
6666

6767

6868
#### construction & free
6969

7070
function free(xdoc::XMLDocument)
71-
ccall(xmlFreeDoc, Void, (Ptr{Void},), xdoc.ptr)
72-
xdoc.ptr = nullptr
71+
ccall((:xmlFreeDoc,libxml2), Void, (Xptr,), xdoc.ptr)
72+
xdoc.ptr = C_NULL
7373
end
7474

7575
function set_root(xdoc::XMLDocument, xroot::XMLElement)
76-
ccall(xmlDocSetRootElement, Xptr, (Xptr, Xptr), xdoc.ptr, xroot.node.ptr)
76+
ccall((:xmlDocSetRootElement,libxml2), Xptr, (Xptr, Xptr), xdoc.ptr, xroot.node.ptr)
7777
end
7878

79-
function create_root(xdoc::XMLDocument, name::String)
79+
function create_root(xdoc::XMLDocument, name::AbstractString)
8080
xroot = new_element(name)
8181
set_root(xdoc, xroot)
8282
return xroot
8383
end
8484

8585
#### parse and free
8686

87-
function parse_file(filename::String)
88-
p = ccall(xmlParseFile, Xptr, (Ptr{Cchar},), filename)
89-
p != nullptr || throw(XMLParseError("Failure in parsing an XML file."))
87+
function parse_file(filename::AbstractString)
88+
p = ccall((:xmlParseFile,libxml2), Xptr, (Cstring,), filename)
89+
p != C_NULL || throw(XMLParseError("Failure in parsing an XML file."))
9090
XMLDocument(p)
9191
end
9292

93-
function parse_string(s::String)
94-
p = ccall(xmlParseMemory, Xptr, (Ptr{Cchar}, Cint), s, sizeof(s) + 1)
95-
p != nullptr || throw(XMLParseError("Failure in parsing an XML string."))
93+
function parse_string(s::AbstractString)
94+
p = ccall((:xmlParseMemory,libxml2), Xptr, (Xstr, Cint), s, sizeof(s) + 1)
95+
p != C_NULL || throw(XMLParseError("Failure in parsing an XML string."))
9696
XMLDocument(p)
9797
end
9898

9999

100100
#### output
101101

102-
function save_file(xdoc::XMLDocument, filename::String; encoding::String="utf-8")
103-
ret = ccall(xmlSaveFormatFileEnc, Cint, (Ptr{Cchar}, Xptr, Ptr{Cchar}, Cint),
102+
function save_file(xdoc::XMLDocument, filename::AbstractString; encoding::AbstractString="utf-8")
103+
ret = ccall((:xmlSaveFormatFileEnc,libxml2), Cint, (Cstring, Xptr, Cstring, Cint),
104104
filename, xdoc.ptr, encoding, 1)
105105
if ret < 0
106106
throw(XMLWriteError("Failed to save XML to file $filename"))
107107
end
108108
return @compat Int(ret) # number of bytes written
109109
end
110110

111-
function Base.string(xdoc::XMLDocument; encoding::String="utf-8")
111+
function Base.string(xdoc::XMLDocument; encoding::AbstractString="utf-8")
112112
buf_out = Array(Xstr, 1)
113113
len_out = Array(Cint, 1)
114-
ccall(xmlDocDumpFormatMemoryEnc, Void, (Xptr, Ptr{Xstr}, Ptr{Cint}, Ptr{Cchar}, Cint),
114+
ccall((:xmlDocDumpFormatMemoryEnc,libxml2), Void, (Xptr, Ptr{Xstr}, Ptr{Cint}, Cstring, Cint),
115115
xdoc.ptr, buf_out, len_out, encoding, 1)
116116
_xcopystr(buf_out[1])
117117
end

src/errors.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

22
abstract XMLError <: Exception
33

4-
immutable XMLParseError <: XMLError
5-
msg::String
4+
immutable XMLParseError{T<:AbstractString} <: XMLError
5+
msg::T
66
end
77

88
immutable XMLNoRootError <: XMLError
@@ -11,12 +11,12 @@ end
1111
immutable XMLAttributeNotFound <: XMLError
1212
end
1313

14-
immutable XMLWriteError <: XMLError
15-
msg::String
14+
immutable XMLWriteError{T<:AbstractString} <: XMLError
15+
msg::T
1616
end
1717

18-
immutable XMLTreeError <: XMLError
19-
msg::String
18+
immutable XMLTreeError{T<:AbstractString} <: XMLError
19+
msg::T
2020
end
2121

2222
const dom_exception_causes = [
@@ -33,9 +33,9 @@ const dom_exception_causes = [
3333
]
3434

3535

36-
immutable DOMException <: XMLError
36+
immutable DOMException{T<:AbstractString} <: XMLError
3737
code::Int
38-
cause::String
38+
cause::T
3939

4040
DOMException(code::Int) = new(code, dom_exception_causes[code])
4141
end

0 commit comments

Comments
 (0)