From db3027b26e5ce1fe83784a4146822b51307a5e15 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Fri, 2 Jan 2015 12:40:51 +0100 Subject: [PATCH] description constraints --- lib/sane/api.rb | 60 +++++++++++++++++++++++++++++++++++++++++++----- sane-ffi.gemspec | 6 ++--- 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/lib/sane/api.rb b/lib/sane/api.rb index d538068..baf596d 100644 --- a/lib/sane/api.rb +++ b/lib/sane/api.rb @@ -11,6 +11,7 @@ class Sane enum :unit, [:none, 0, :pixel, :bit, :mm, :dpi, :percent, :microsecond] enum :action, [:get_value, 0, :set_value, :set_auto] enum :frame, [:gray, :rgb, :red, :green, :blue] + ConstraintType = enum :none, 0, :range, :word_list, :string_list class Device < FFI::Struct layout :name, :string, :vendor, :string, :model, :string, :type, :string @@ -26,12 +27,58 @@ class Sane end class OptionDescriptor < FFI::Struct - class ConstraintType < FFI::Union - layout :string_list, :pointer, :word_list, :pointer, :range, :pointer - end - layout :name, :string, :title, :string, :desc, :string, :type, :value_type, :unit, :unit, :size, :int, :cap, :int, :constraint_type, ConstraintType + class Range < Struct.new(:min, :max, :quant) + include Comparable + include Enumerable + def <=> x + (min..max) <=> x + end - def to_hash; + def include? x + (min..max).include?( x) and 0 == (x-min) % quant + end + + def === x + x.kind_of?( Numeric) and include?( x) + end + + def each + min.step( max, [1,quant].max) + end + end + + layout :name, :string, + :title, :string, + :desc, :string, + :type, :value_type, + :unit, :unit, + :size, :int, + :cap, :int, + :constraint_type, ConstraintType, + :constraint, :pointer + + alias method_missing [] + + def constraint + c = self[:constraint] + case self[:constraint_type] + when 0, :none then nil + when 1, :range + Range.new *c.read_array_of_int( 3) + when 2, :word_list + c[FFI::Type::INT.size].read_array_of_int c.read_int + when 3, :string_list + i, r, p = 0, [], nil + while 0 < (p = c[i].read_pointer).address + STDERR.puts p.inspect + r << p.read_string_to_null + i += FFI::Type::POINTER.size + end + r + end + end + + def to_hash { :name => self[:name], :title => self[:title], @@ -39,7 +86,8 @@ class Sane :type => self[:type], :unit => self[:unit], :size => self[:size], - :cap => self[:cap] + :cap => self[:cap], + :constraint => constraint } end end diff --git a/sane-ffi.gemspec b/sane-ffi.gemspec index 680d613..632d092 100644 --- a/sane-ffi.gemspec +++ b/sane-ffi.gemspec @@ -3,11 +3,11 @@ $:.push File.expand_path("../lib", __FILE__) require "sane/version" Gem::Specification.new do |s| - s.name = "sane-ffi" + s.name = "sane-ffi-denkn" s.version = Sane::VERSION s.platform = Gem::Platform::RUBY - s.authors = ["Jakub Kuźma"] - s.email = ["qoobaa@gmail.com"] + s.authors = ["Jakub Kuźma & Denis Knauf"] + s.email = ["qoobaa@gmail.com", %Q[de#{?n}]+'is'+?@+"d#{'en'}k"+?n+?.+%q[at]] s.homepage = "" s.summary = %q{SANE bindings} s.description = %q{Scanner Access now Easier}