1.8 compatibility (blah)

This commit is contained in:
Jakub Kuźma 2011-06-01 14:20:30 +02:00
parent 3d56bc6ac4
commit b2a9410aa2
5 changed files with 52 additions and 19 deletions

7
README.rdoc Normal file
View file

@ -0,0 +1,7 @@
= SANE FFI
Scanner Access Now Easier in Ruby.
== Copyright
Copyright (c) 2011 Jakub Kuźma

View file

@ -15,7 +15,7 @@ class Sane
def self.open def self.open
instance.send(:init) instance.send(:init)
yield instance yield(instance)
ensure ensure
instance.send(:exit) instance.send(:exit)
end end
@ -52,12 +52,12 @@ class Sane
devices_pointer_pointer = FFI::MemoryPointer.new(:pointer) devices_pointer_pointer = FFI::MemoryPointer.new(:pointer)
check_status!(API.sane_get_devices(devices_pointer_pointer, 0)) check_status!(API.sane_get_devices(devices_pointer_pointer, 0))
devices_pointer = devices_pointer_pointer.read_pointer devices_pointer = devices_pointer_pointer.read_pointer
[].tap do |result| result = []
until devices_pointer.read_pointer.null? until devices_pointer.read_pointer.null?
result << API::Device.new(devices_pointer.read_pointer).to_hash result << API::Device.new(devices_pointer.read_pointer).to_hash
devices_pointer += FFI.type_size(:pointer) devices_pointer += FFI.type_size(:pointer)
end end
end result
end end
def open(device_name) def open(device_name)
@ -156,14 +156,14 @@ class Sane
end end
def ensure_not_initialized! def ensure_not_initialized!
raise "SANE library is already initialized" if initialized? raise("SANE library is already initialized") if initialized?
end end
def ensure_initialized! def ensure_initialized!
raise "SANE library is not initialized" if not_initialized? raise("SANE library is not initialized") if not_initialized?
end end
def check_status!(status) def check_status!(status)
raise Error.new(strstatus(status), status) unless status == :good raise(Error.new(strstatus(status), status)) unless status == :good
end end
end end

View file

@ -13,7 +13,14 @@ class Sane
class Device < FFI::Struct class Device < FFI::Struct
layout :name, :string, :vendor, :string, :model, :string, :type, :string layout :name, :string, :vendor, :string, :model, :string, :type, :string
def to_hash; {name: self[:name], vendor: self[:vendor], model: self[:model], type: self[:type]} end def to_hash
{
:name => self[:name],
:vendor => self[:vendor],
:model => self[:model],
:type => self[:type]
}
end
end end
class OptionDescriptor < FFI::Struct class OptionDescriptor < FFI::Struct
@ -22,13 +29,32 @@ class Sane
end end
layout :name, :string, :title, :string, :desc, :string, :type, :value_type, :unit, :unit, :size, :int, :cap, :int, :constraint_type, ConstraintType layout :name, :string, :title, :string, :desc, :string, :type, :value_type, :unit, :unit, :size, :int, :cap, :int, :constraint_type, ConstraintType
def to_hash; {name: self[:name], title: self[:title], desc: self[:desc], type: self[:type], unit: self[:unit], size: self[:size], cap: self[:cap]} end def to_hash;
{
:name => self[:name],
:title => self[:title],
:desc => self[:desc],
:type => self[:type],
:unit => self[:unit],
:size => self[:size],
:cap => self[:cap]
}
end
end end
class Parameters < FFI::Struct class Parameters < FFI::Struct
layout :format, :frame, :last_frame, :int, :bytes_per_line, :int, :pixels_per_line, :int, :lines, :int, :depth, :int layout :format, :frame, :last_frame, :int, :bytes_per_line, :int, :pixels_per_line, :int, :lines, :int, :depth, :int
def to_hash; {format: self[:format], last_frame: self[:last_frame], bytes_per_line: self[:bytes_per_line], pixels_per_line: self[:pixels_per_line], lines: self[:lines], depth: self[:depth]} end def to_hash
{
:format => self[:format],
:last_frame => self[:last_frame],
:bytes_per_line => self[:bytes_per_line],
:pixels_per_line => self[:pixels_per_line],
:lines => self[:lines],
:depth => self[:depth]
}
end
end end
# extern SANE_Status sane_init (SANE_Int * version_code, SANE_Auth_Callback authorize); # extern SANE_Status sane_init (SANE_Int * version_code, SANE_Auth_Callback authorize);

View file

@ -77,18 +77,20 @@ class Sane
begin begin
self[i] self[i]
rescue Error rescue Error
nil # we can't get values of some options, ignore them nil # we can't read values of some options (i.e. buttons), ignore them
end end
end end
end end
def options def options
{}.tap { |hash| option_count.times { |i| hash[option_names[i]] = option_values[i] } } result = {}
option_count.times { |i| hash[option_names[i]] = option_values[i] }
result
end end
def option_lookup(option_name) def option_lookup(option_name)
return option_name if (0..option_count).include?(option_name) return option_name if (0..option_count).include?(option_name)
option_descriptors.index { |option| option[:name] == option_name.to_s } or raise ArgumentError, "Option not found: #{option_name}" option_descriptors.index { |option| option[:name] == option_name.to_s } or raise(ArgumentError, "Option not found: #{option_name}")
end end
def describe(option) def describe(option)
@ -98,11 +100,11 @@ class Sane
private private
def ensure_closed! def ensure_closed!
raise "Device is already open" if open? raise("Device is already open") if open?
end end
def ensure_open! def ensure_open!
raise "Device is closed" if closed? raise("Device is closed") if closed?
end end
end end
end end

View file

@ -12,8 +12,6 @@ Gem::Specification.new do |s|
s.summary = %q{SANE bindings} s.summary = %q{SANE bindings}
s.description = %q{Scanner Access now Easier} s.description = %q{Scanner Access now Easier}
# s.rubyforge_project = "sane"
s.add_dependency "ffi" s.add_dependency "ffi"
s.files = `git ls-files`.split("\n") s.files = `git ls-files`.split("\n")