1.8 compatibility (blah)

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

View File

@ -13,7 +13,14 @@ class Sane
class Device < FFI::Struct
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
class OptionDescriptor < FFI::Struct
@ -22,13 +29,32 @@ class Sane
end
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
class Parameters < FFI::Struct
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
# extern SANE_Status sane_init (SANE_Int * version_code, SANE_Auth_Callback authorize);

View File

@ -77,18 +77,20 @@ class Sane
begin
self[i]
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
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
def option_lookup(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
def describe(option)
@ -98,11 +100,11 @@ class Sane
private
def ensure_closed!
raise "Device is already open" if open?
raise("Device is already open") if open?
end
def ensure_open!
raise "Device is closed" if closed?
raise("Device is closed") if closed?
end
end
end

View File

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