Class: RbSDL2::RWOperator::ReadCallback

Inherits:
FFI::Function
  • Object
show all
Defined in:
lib/rb_sdl2/rw_ops/rw_operator.rb

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ ReadCallback

Returns a new instance of ReadCallback.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rb_sdl2/rw_ops/rw_operator.rb', line 20

def initialize(obj)
  # size_t (* read) (struct SDL_RWops * context, void *ptr, size_t size, size_t maxnum);

  super(:size_t, [:pointer, :pointer, :size_t, :size_t]) do |_context, ptr, size, max_num|
    return 0 if ptr.null?
    max = size * max_num
    str = obj.read(max)
    len = str.size
    # len > max は obj.read が壊れている。

    return 0 if str.nil? || len > max
    ptr.write_bytes(str, 0, len)
    len / size
  rescue
    0
  end
end