fiddley 0.0.5 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 89adacb7382de9ea109dda5d0a9aa9a0126a3198
4
- data.tar.gz: f8e12868b55cddad59d0981a01690d41b23888dc
3
+ metadata.gz: 40f86cac5170af47851db89e9cf863d1ed2d4cd0
4
+ data.tar.gz: 03d91eaeb1ca26ebb57e00e7ea22b23e626b50fd
5
5
  SHA512:
6
- metadata.gz: 1e455169f82ace8c779056efe3c6eda4f88ab6a5d13012a9d3bea5c59246876278e9cabccd73f72dab79e7151687a9af96493f7f97bc55bbcd45d922c8d8b829
7
- data.tar.gz: 03bc837b03cf53ccbe543efe6432596a34e158646bf91894ac7282c392cb14401cddc8f7f3c9516ede054a6194891ed3cc7d347bc5f61fc36c503fad30dc034d
6
+ metadata.gz: d907983cf014b4f1c9d03420232695a2a9dc41e84ad3e3a06b3219d11c5e50b1ca5a64fbfd1d95e03969484df192d378f971f3550a08935f3b4fa8ea03cca29f
7
+ data.tar.gz: 557d3d89020188e8d53ea4b844297ffe27b1642daa8e605b6c59db2153ea76b2fc5c7e45d05fb03891ec28d386e93240bcb5e037c4f17d4e7dda14a36234b76e
data/Rakefile CHANGED
@@ -1,10 +1,10 @@
1
- # -*- Ruby -*-
2
- require "bundler/gem_tasks"
3
- require "rake/testtask.rb"
4
-
5
- task :default => :test
6
-
7
- desc "Run all tests"
8
- task :test do
9
- ruby "-Ilib test/sample.rb"
10
- end
1
+ # -*- Ruby -*-
2
+ require "bundler/gem_tasks"
3
+ require "rake/testtask"
4
+
5
+ task :default => :test
6
+
7
+ desc "Run all tests"
8
+ task :test do
9
+ ruby "-Ilib test/test_*.rb"
10
+ end
@@ -1,7 +1,8 @@
1
1
  module Fiddley
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.7"
3
3
  end
4
4
 
5
+ require "fiddley/enum"
5
6
  require "fiddley/function"
6
7
  require "fiddley/memory_pointer"
7
8
  require "fiddley/library"
@@ -0,0 +1,46 @@
1
+ module Fiddley
2
+ class Enum
3
+ def initialize(*args)
4
+ info, @tag = *args
5
+ @map = {}
6
+ if info
7
+ last = nil
8
+ value = 0
9
+ info.each do |k|
10
+ if k.is_a?(Symbol)
11
+ @map[k] = value
12
+ last = k
13
+ value += 1
14
+ elsif k.is_a?(Integer)
15
+ @map[last] = k
16
+ value = k + 1
17
+ else
18
+ raise RuntimeError, "broken enum"
19
+ end
20
+ end
21
+ end
22
+ @rmap = @map.invert
23
+ end
24
+
25
+ attr_reader :tag
26
+
27
+ def symbols
28
+ @map.keys
29
+ end
30
+
31
+ def [](k)
32
+ if k.is_a?(Symbol)
33
+ @map[k]
34
+ elsif k.is_a?(Integer)
35
+ @rmap[k]
36
+ end
37
+ end
38
+ alias find []
39
+
40
+ def symbol_map
41
+ @map
42
+ end
43
+ alias to_h symbol_map
44
+ alias to_hash symbol_map
45
+ end
46
+ end
@@ -1,4 +1,5 @@
1
1
  require "fiddle/import"
2
+ require "fiddley/enum"
2
3
  require "fiddley/struct"
3
4
  require "fiddley/utils"
4
5
 
@@ -71,6 +72,34 @@ module Fiddley
71
72
  end
72
73
  end
73
74
 
75
+ def enum(*args)
76
+ if args[1].is_a?(Array)
77
+ tag = args.shift
78
+ else
79
+ tag = nil
80
+ end
81
+ if args[0].is_a?(Array)
82
+ args = args[0]
83
+ end
84
+ @enums ||= []
85
+ @enums << Fiddley::Enum.new(args, tag)
86
+ end
87
+
88
+ def enum_value(key)
89
+ @enums ||= []
90
+ enum = @enums.find{|enum| enum[key]}
91
+ if enum
92
+ enum[key]
93
+ else
94
+ nil
95
+ end
96
+ end
97
+
98
+ def enum_type(key)
99
+ @enums ||= []
100
+ @enums.find{|enum| enum.tag == key}
101
+ end
102
+
74
103
  case RUBY_PLATFORM
75
104
  when /cygwin/
76
105
  libprefix = "cyg"
@@ -181,7 +181,11 @@ module Fiddley
181
181
  when :string, :pointer
182
182
  "void *"
183
183
  else
184
- type.to_s
184
+ if @enums && @enums.find{|e| type == e.tag}
185
+ "int"
186
+ else
187
+ type.to_s
188
+ end
185
189
  end
186
190
  end
187
191
 
@@ -0,0 +1,68 @@
1
+ require "fiddley"
2
+ require "test/unit"
3
+
4
+ class TestFiddleyEnum < Test::Unit::TestCase
5
+ module EnumTest
6
+ extend Fiddley::Library
7
+ ffi_lib Fiddley::Library::LIBC
8
+
9
+ enum :enum1
10
+ enum :enum2, :a
11
+ enum :enum3, [:b]
12
+ enum :enum4, 123
13
+ enum :enum5, :c, 456
14
+ enum :enum6, [:d, 789]
15
+ enum :enum7, [:e, 0x10, :f, :g, 0x100, :h]
16
+
17
+ attach_function :puts, [:enum7], :int
18
+ end
19
+
20
+ test "enum_value" do
21
+ assert_equal 0, EnumTest.enum_value(:enum1)
22
+ assert_equal 0, EnumTest.enum_value(:enum2)
23
+ assert_nil EnumTest.enum_value(:enum3)
24
+ assert_equal 1, EnumTest.enum_value(:a)
25
+ assert_equal 0, EnumTest.enum_value(:b)
26
+ assert_equal 123, EnumTest.enum_value(:enum4)
27
+ assert_equal 0, EnumTest.enum_value(:enum5)
28
+ assert_equal 456, EnumTest.enum_value(:c)
29
+ assert_nil EnumTest.enum_value(:enum6)
30
+ assert_equal 789, EnumTest.enum_value(:d)
31
+ assert_nil EnumTest.enum_value(:enum7)
32
+ assert_equal 0x10, EnumTest.enum_value(:e)
33
+ assert_equal 0x11, EnumTest.enum_value(:f)
34
+ assert_equal 0x100, EnumTest.enum_value(:g)
35
+ assert_equal 0x101, EnumTest.enum_value(:h)
36
+ end
37
+
38
+ test "enum_type" do
39
+ assert_nil EnumTest.enum_type(:enum1)
40
+ assert_nil EnumTest.enum_type(:enum2)
41
+ assert_kind_of Fiddley::Enum, EnumTest.enum_type(:enum3)
42
+ assert_nil EnumTest.enum_type(:enum4)
43
+ assert_nil EnumTest.enum_type(:enum5)
44
+ assert_kind_of Fiddley::Enum, EnumTest.enum_type(:enum6)
45
+ assert_kind_of Fiddley::Enum, EnumTest.enum_type(:enum7)
46
+
47
+ enum = EnumTest.enum_type(:enum3)
48
+ assert_equal 1, enum.symbols.length
49
+ assert_equal 0, enum[:b]
50
+ assert_equal :b, enum[0]
51
+
52
+ enum = EnumTest.enum_type(:enum6)
53
+ assert_equal 1, enum.symbols.length
54
+ assert_equal 789, enum[:d]
55
+ assert_equal :d, enum[789]
56
+
57
+ enum = EnumTest.enum_type(:enum7)
58
+ assert_equal 4, enum.symbols.length
59
+ assert_equal 0x10, enum[:e]
60
+ assert_equal :e, enum[0x10]
61
+ assert_equal 0x11, enum[:f]
62
+ assert_equal :f, enum[0x11]
63
+ assert_equal 0x100, enum[:g]
64
+ assert_equal :g, enum[0x100]
65
+ assert_equal 0x101, enum[:h]
66
+ assert_equal :h, enum[0x101]
67
+ end
68
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fiddley
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - U.Nakamura
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-21 00:00:00.000000000 Z
11
+ date: 2017-06-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Use Fiddle instead of Ruby-FFI !!!
14
14
  email:
@@ -22,6 +22,7 @@ files:
22
22
  - fiddley.gemspec
23
23
  - lib/ffi.rb
24
24
  - lib/fiddley.rb
25
+ - lib/fiddley/enum.rb
25
26
  - lib/fiddley/function.rb
26
27
  - lib/fiddley/library.rb
27
28
  - lib/fiddley/memory_pointer.rb
@@ -29,6 +30,7 @@ files:
29
30
  - lib/fiddley/utils.rb
30
31
  - test/sample.rb
31
32
  - test/sample2.rb
33
+ - test/test_enum.rb
32
34
  homepage: https://github.com/unak/fiddley
33
35
  licenses:
34
36
  - BSD-2-Clause