Skip to content

Commit

Permalink
FFI: IO reader callbacks now use AbstractMemory#put_bytes instead of …
Browse files Browse the repository at this point in the history
…memcpy, per Wayne's advice. along the way, refactored callbacks. closes gh-43.
  • Loading branch information
flavorjones committed May 11, 2009
1 parent df6b8b2 commit 0b87111
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 46 deletions.
4 changes: 2 additions & 2 deletions FFI-TODO
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- [X] XML::RelaxNG
- [X] SAX::PushParser
- [X] ProcessingInstruction
* DONE refactor reader/writer/closer io callbacks
* miscellaneous issues
*** DONE blanks being escaped in output
CLOSED: [2009-04-30 Thu 19:03]
Expand Down Expand Up @@ -60,9 +61,8 @@ RangeError: 0xdb387178 is recycled object
*** TODO then remove the 'rake ffi' task and supporting functions

* TODO open jira tickets: support varags in callbacks (needed for syntax error and sax error)
* TODO rdoc documentation
* figure out how to cache the ruby objects (nodes and documents) without using Objectspace.

* use (new) nested struct support in FFI?
* do we have string encoding issues?
* i feel like we should refactor reader/writer/closer io callbacks
* rdoc documentation? shit.
10 changes: 1 addition & 9 deletions lib/nokogiri/ffi/html/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,7 @@ def self.new(uri=nil, external_id=nil)

def self.read_io(io, url, encoding, options)
wrap_with_error_handling(HTML_DOCUMENT_NODE) do
reader = lambda do |ctx, buffer, len|
string = io.read(len)
return 0 if string.nil?
LibXML.memcpy(buffer, string, string.length)
string.length
end
closer = lambda { |ctx| 0 } # coffee is for closers.

LibXML.htmlReadIO(reader, closer, nil, url, encoding, options)
LibXML.htmlReadIO(IoCallbacks.reader(io), IoCallbacks.closer(io), nil, url, encoding, options)
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/nokogiri/ffi/libxml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ module LibXML # :nodoc:
# libc
attach_function :calloc, [:int, :int], :pointer
attach_function :free, [:pointer], :void
attach_function :memcpy, [:pointer, :pointer, :int], :pointer

# HTMLparser.c
attach_function :htmlReadMemory, [:string, :int, :string, :string, :int], :pointer
Expand Down Expand Up @@ -282,7 +281,8 @@ def self.xmlFree(pointer)
require 'nokogiri/syntax_error'
require 'nokogiri/xml/syntax_error'

[ "structs/common_node",
[ "io_callbacks",
"structs/common_node",
"structs/xml_alloc",
"structs/xml_document",
"structs/xml_node",
Expand Down
10 changes: 1 addition & 9 deletions lib/nokogiri/ffi/xml/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,7 @@ def encoding

def self.read_io(io, url, encoding, options)
wrap_with_error_handling(DOCUMENT_NODE) do
reader = lambda do |ctx, buffer, len|
string = io.read(len)
return 0 if string.nil?
LibXML.memcpy(buffer, string, string.length)
string.length
end
closer = lambda { |ctx| 0 } # coffee is for closers.

LibXML.xmlReadIO(reader, closer, nil, url, encoding, options)
LibXML.xmlReadIO(IoCallbacks.reader(io), IoCallbacks.closer(io), nil, url, encoding, options)
end
end

Expand Down
7 changes: 1 addition & 6 deletions lib/nokogiri/ffi/xml/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,7 @@ def add_previous_sibling(prev_sibling)
def native_write_to(io, encoding, indent_string, options)
set_xml_indent_tree_output 1
set_xml_tree_indent_string indent_string
writer = lambda do |context, buffer, len|
io.write buffer
len
end
closer = lambda { |ctx| 0 } # coffee is for closers.
savectx = LibXML.xmlSaveToIO(writer, closer, nil, encoding, options)
savectx = LibXML.xmlSaveToIO(IoCallbacks.writer(io), IoCallbacks.closer(io), nil, encoding, options)
LibXML.xmlSaveTree(savectx, cstruct)
LibXML.xmlSaveClose(savectx)
io
Expand Down
10 changes: 1 addition & 9 deletions lib/nokogiri/ffi/xml/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,7 @@ def self.from_memory(buffer, url=nil, encoding=nil, options=0)
def self.from_io(io, url=nil, encoding=nil, options=0)
raise(ArgumentError, "io cannot be nil") if io.nil?

reader = lambda do |ctx, buffer, len|
string = io.read(len)
return 0 if string.nil?
LibXML.memcpy(buffer, string, string.length)
string.length
end
closer = lambda { |ctx| 0 } # coffee is for closers.

reader_ptr = LibXML.xmlReaderForIO(reader, closer, nil, url, encoding, options)
reader_ptr = LibXML.xmlReaderForIO(IoCallbacks.reader(io), IoCallbacks.closer(io), nil, url, encoding, options)
raise "couldn't create a parser" if reader_ptr.null?

reader = allocate
Expand Down
10 changes: 1 addition & 9 deletions lib/nokogiri/ffi/xml/sax/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,7 @@ def parse_memory(data)
end

def native_parse_io(io, encoding)
reader = lambda do |ctx, buffer, len|
string = io.read(len)
return 0 if string.nil?
LibXML.memcpy(buffer, string, string.length)
string.length
end
closer = lambda { |ctx| return 0 }

sax_ctx = LibXML.xmlCreateIOParserCtxt(cstruct, nil, reader, closer, nil, encoding)
sax_ctx = LibXML.xmlCreateIOParserCtxt(cstruct, nil, IoCallbacks.reader(io), IoCallbacks.closer(io), nil, encoding)
LibXML.xmlParseDocument(sax_ctx)
LibXML.xmlFreeParserCtxt(sax_ctx)
io
Expand Down

0 comments on commit 0b87111

Please sign in to comment.