Module: Nokogiri::IoCallbacks

Defined in:
lib/nokogiri/ffi/io_callbacks.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.plain_old_reader(io) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/nokogiri/ffi/io_callbacks.rb', line 7

def plain_old_reader(io)
  lambda do |ctx, buffer, len|
    string = io.read(len)
    return 0 if string.nil?
    buffer.put_bytes(0, string, 0, string.length)
    string.length
  end
end

.writer(io) ⇒ Object



32
33
34
35
36
37
# File 'lib/nokogiri/ffi/io_callbacks.rb', line 32

def writer(io)
  lambda do |context, buffer, len|
    io.write buffer
    len
  end
end

Instance Method Details

#ffi_io_native_reader(io) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/nokogiri/ffi/io_callbacks.rb', line 17

def ffi_io_native_reader(io)
  if io.is_a?(StringIO)
    plain_old_reader(io)
  else
    lambda do |ctx, buffer, len|
      rcode = FFI::IO.native_read(io, buffer, len)
      (rcode < 0) ? 0 : rcode
    end
  end
end