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 +4 -4
- data/Rakefile +10 -10
- data/lib/fiddley.rb +2 -1
- data/lib/fiddley/enum.rb +46 -0
- data/lib/fiddley/library.rb +29 -0
- data/lib/fiddley/utils.rb +5 -1
- data/test/test_enum.rb +68 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 40f86cac5170af47851db89e9cf863d1ed2d4cd0
|
4
|
+
data.tar.gz: 03d91eaeb1ca26ebb57e00e7ea22b23e626b50fd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
4
|
-
|
5
|
-
task :default => :test
|
6
|
-
|
7
|
-
desc "Run all tests"
|
8
|
-
task :test do
|
9
|
-
ruby "-Ilib test/
|
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
|
data/lib/fiddley.rb
CHANGED
data/lib/fiddley/enum.rb
ADDED
@@ -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
|
data/lib/fiddley/library.rb
CHANGED
@@ -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"
|
data/lib/fiddley/utils.rb
CHANGED
data/test/test_enum.rb
ADDED
@@ -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.
|
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-
|
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
|