sitar 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/.travis.yml +10 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +107 -0
- data/Rakefile +7 -0
- data/lib/sitar.rb +53 -0
- data/lib/sitar/binder.rb +67 -0
- data/lib/sitar/executor.rb +103 -0
- data/lib/sitar/helper.rb +34 -0
- data/lib/sitar/types.rb +13 -0
- data/lib/sitar/types/aliases.rb +25 -0
- data/lib/sitar/types/pointer.rb +21 -0
- data/lib/sitar/types/struct_factory.rb +79 -0
- data/lib/sitar/types/wide_char.rb +60 -0
- data/lib/sitar/version.rb +4 -0
- data/sitar.gemspec +26 -0
- metadata +105 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c92941d27abd491805383eec1ef8cdb6bb3c1365
|
4
|
+
data.tar.gz: 62d445d65ab7b13b775b058017e3a79444d17a99
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c830628399e594f378ffaba57bca4aaf8b30cf3639441f38fa88027989eb0a8e4c130b63cb4bf3a4cc95f3d1ff4e6e27cf2759a218596c0f6390d369818d079f
|
7
|
+
data.tar.gz: d153bc4f14ceb763cab6da453ed8201cde48e095ef97955aaf4ac7a4759e653dcd90755010c3063c32b6c7848c37147e8f2de82865f4388032cc7ed37d32327a
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at slowhand0309@gmail.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Slowhand0309
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
# Sitar
|
2
|
+
|
3
|
+
Sitar is easy to use the [fiddle](https://github.com/ruby/ruby/tree/trunk/ext/fiddle).
|
4
|
+
|
5
|
+
[![Build Status](https://travis-ci.org/Slowhand0309/sitar.svg?branch=master)](https://travis-ci.org/Slowhand0309/sitar)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'sitar'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install sitar
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
You can used C method only to extend sitar and load shared library like follow:
|
26
|
+
|
27
|
+
C function in `libsample.so`
|
28
|
+
|
29
|
+
```c
|
30
|
+
int add(int a, int b) {
|
31
|
+
return a + b;
|
32
|
+
}
|
33
|
+
```
|
34
|
+
|
35
|
+
Use in ruby.
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
module M
|
39
|
+
extend Sitar
|
40
|
+
|
41
|
+
load 'libsample.so'
|
42
|
+
end
|
43
|
+
|
44
|
+
M.add(1, 2).ret_i # => 3
|
45
|
+
```
|
46
|
+
|
47
|
+
* List of function return type mthod like ret_XXX.
|
48
|
+
|
49
|
+
|Method|Return Type in C|Return Type in Ruby|Description|
|
50
|
+
|:-----|:------|:----------|:----------|
|
51
|
+
|call|void|nil|Not returned value.|
|
52
|
+
|ret_i|int|Integer|Returned int value.|
|
53
|
+
|ret_s|char *|String|Return string pointer.|
|
54
|
+
|ret_ws|wchar_t *|String|Return wide char string pointer.|
|
55
|
+
|ret_f|float|Float|Return float value.|
|
56
|
+
|ret_d|double|Float|Return double value.|
|
57
|
+
|
58
|
+
## Struct
|
59
|
+
|
60
|
+
You can easy to use struct by `Sitar::Types::StructFactory`.
|
61
|
+
|
62
|
+
e.g.
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
module M
|
66
|
+
extend Sitar
|
67
|
+
extend Sitar::Types::StructFactory
|
68
|
+
|
69
|
+
# struct Car {
|
70
|
+
# int id;
|
71
|
+
# char name[20];
|
72
|
+
# };
|
73
|
+
define 'Car', ["int id", "char name[20]"]
|
74
|
+
|
75
|
+
# struct Owner {
|
76
|
+
# int seq;
|
77
|
+
# struct Car cars[3];
|
78
|
+
# };
|
79
|
+
define 'Owner', ["int seq", "Car cars[3]"]
|
80
|
+
|
81
|
+
define_eval(self)
|
82
|
+
end
|
83
|
+
|
84
|
+
M::CAR.malloc
|
85
|
+
|
86
|
+
owner = M::OWNER.malloc
|
87
|
+
# Car cars[3] is expanded like follow:
|
88
|
+
# struct Owner {
|
89
|
+
# int seq;
|
90
|
+
# int id_1;
|
91
|
+
# char name_1[20];
|
92
|
+
# int id_2;
|
93
|
+
# char name_2[20];
|
94
|
+
# int id_3;
|
95
|
+
# char name_3[20];
|
96
|
+
# }
|
97
|
+
owner.id_1
|
98
|
+
owner.name_1
|
99
|
+
```
|
100
|
+
|
101
|
+
## License
|
102
|
+
|
103
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
104
|
+
|
105
|
+
## Author
|
106
|
+
|
107
|
+
[slowhand0309](https://github.com/Slowhand0309)
|
data/Rakefile
ADDED
data/lib/sitar.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'sitar/binder'
|
3
|
+
|
4
|
+
module Sitar
|
5
|
+
|
6
|
+
# == Description
|
7
|
+
#
|
8
|
+
# Load shared librarys (.so, .dll).
|
9
|
+
# And extend typealias.
|
10
|
+
#
|
11
|
+
# * +library+ is the specfic library path's
|
12
|
+
#
|
13
|
+
# == Example
|
14
|
+
#
|
15
|
+
# module M
|
16
|
+
# extend Sitar
|
17
|
+
# load 'path/liba.so', 'path/libb.so'
|
18
|
+
# end
|
19
|
+
def load(*library)
|
20
|
+
Binder::dlload(*library)
|
21
|
+
Binder::typealias_ext
|
22
|
+
end
|
23
|
+
|
24
|
+
# Implemention method_missing.
|
25
|
+
#
|
26
|
+
def method_missing(name, *args)
|
27
|
+
e = Binder::at(name, *args)
|
28
|
+
unless e
|
29
|
+
super(name, *args) # No method error.
|
30
|
+
else
|
31
|
+
e
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Create C struct.
|
36
|
+
#
|
37
|
+
def struct(signature)
|
38
|
+
Binder::struct(signature)
|
39
|
+
end
|
40
|
+
|
41
|
+
# Create C union.
|
42
|
+
#
|
43
|
+
def union(signature)
|
44
|
+
Binder::union(signature)
|
45
|
+
end
|
46
|
+
|
47
|
+
# Declare typealias.
|
48
|
+
#
|
49
|
+
def typealias(alias_type, orig_type)
|
50
|
+
Binder::typealias(alias_type, orig_type)
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
data/lib/sitar/binder.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'fiddle'
|
3
|
+
require 'fiddle/import'
|
4
|
+
require 'sitar/executor'
|
5
|
+
require 'sitar/types'
|
6
|
+
|
7
|
+
module Sitar
|
8
|
+
module Binder
|
9
|
+
extend Fiddle::Importer
|
10
|
+
|
11
|
+
class << self
|
12
|
+
|
13
|
+
# Bind at function.
|
14
|
+
#
|
15
|
+
# * +name+ is the function name
|
16
|
+
# * +args+ is the function args
|
17
|
+
#
|
18
|
+
# Return the executor class instance.
|
19
|
+
def at(name, *args)
|
20
|
+
address = nil
|
21
|
+
argv = []
|
22
|
+
begin
|
23
|
+
address = handler.sym(name.to_s)
|
24
|
+
argv = parse_arguments(*args)
|
25
|
+
rescue Fiddle::DLError => e
|
26
|
+
$stderr.puts "Fiddle::DLError #{e}"
|
27
|
+
end
|
28
|
+
if address
|
29
|
+
Executor.new(address, argv, *args)
|
30
|
+
else
|
31
|
+
nil # If invalid symbol name, return nil.
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def typealias_ext
|
36
|
+
include Types::Aliases
|
37
|
+
end
|
38
|
+
|
39
|
+
def parse_arguments(*args)
|
40
|
+
values = *args
|
41
|
+
# No have argument.
|
42
|
+
if values.length == 0
|
43
|
+
return []
|
44
|
+
end
|
45
|
+
|
46
|
+
argv = []
|
47
|
+
values.each do |arg|
|
48
|
+
# Decide arg type.
|
49
|
+
if arg.kind_of?(Integer)
|
50
|
+
argv << Fiddle::TYPE_INT
|
51
|
+
elsif arg.kind_of?(Float)
|
52
|
+
argv << Fiddle::TYPE_DOUBLE
|
53
|
+
elsif arg.kind_of?(String)
|
54
|
+
argv << Fiddle::TYPE_VOIDP
|
55
|
+
elsif arg.kind_of?(Fiddle::Pointer)
|
56
|
+
argv << Fiddle::TYPE_VOIDP
|
57
|
+
else
|
58
|
+
argv << Fiddle::TYPE_VOIDP
|
59
|
+
end
|
60
|
+
end
|
61
|
+
argv
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'fiddle/import'
|
3
|
+
require 'sitar/types'
|
4
|
+
|
5
|
+
module Sitar
|
6
|
+
class Executor
|
7
|
+
|
8
|
+
# === Description
|
9
|
+
#
|
10
|
+
# Construct new object.
|
11
|
+
#
|
12
|
+
# * +address+ is the shared library memory address.
|
13
|
+
def initialize(address, argv, *args)
|
14
|
+
@address = address
|
15
|
+
@argv = argv
|
16
|
+
@args = args
|
17
|
+
end
|
18
|
+
|
19
|
+
# === Description
|
20
|
+
#
|
21
|
+
# Call function with no returned value.
|
22
|
+
def call
|
23
|
+
func = get_func(Fiddle::TYPE_VOID)
|
24
|
+
if func
|
25
|
+
func.call(*@args)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# === Description
|
30
|
+
#
|
31
|
+
# Call function with integer returned value.
|
32
|
+
# If failed call, returned -1.
|
33
|
+
def ret_i
|
34
|
+
result = -1
|
35
|
+
func = get_func(Fiddle::TYPE_LONG)
|
36
|
+
if func
|
37
|
+
result = func.call(*@args).to_i
|
38
|
+
end
|
39
|
+
result
|
40
|
+
end
|
41
|
+
|
42
|
+
# === Description
|
43
|
+
#
|
44
|
+
# Call function with (char *) returned value.
|
45
|
+
# If failed call, returned ''.
|
46
|
+
def ret_s
|
47
|
+
result = ''
|
48
|
+
func = get_func(Fiddle::TYPE_LONG)
|
49
|
+
if func
|
50
|
+
result = Fiddle::Pointer.new(func.call(*@args)).to_s
|
51
|
+
end
|
52
|
+
result
|
53
|
+
end
|
54
|
+
|
55
|
+
# === Description
|
56
|
+
#
|
57
|
+
# Call function with (wchar_t *) returned value.
|
58
|
+
# If failed call, returned ''.
|
59
|
+
def ret_ws
|
60
|
+
result = ''
|
61
|
+
func = get_func(Fiddle::TYPE_LONG)
|
62
|
+
if func
|
63
|
+
result = Types::WideChar.from_ptr(func.call(*@args))
|
64
|
+
end
|
65
|
+
result
|
66
|
+
end
|
67
|
+
|
68
|
+
# === Description
|
69
|
+
#
|
70
|
+
# Call function with float returned value.
|
71
|
+
# If failed call, returned 0.0.
|
72
|
+
def ret_f
|
73
|
+
result = 0.0
|
74
|
+
func = get_func(Fiddle::TYPE_FLOAT)
|
75
|
+
if func
|
76
|
+
result = func.call(*@args).to_f
|
77
|
+
end
|
78
|
+
result
|
79
|
+
end
|
80
|
+
|
81
|
+
# === Description
|
82
|
+
#
|
83
|
+
# Call function with double returned value.
|
84
|
+
# If failed call, returned 0.0.
|
85
|
+
def ret_d
|
86
|
+
result = 0.0
|
87
|
+
func = get_func(Fiddle::TYPE_DOUBLE)
|
88
|
+
if func
|
89
|
+
result = func.call(*@args).to_f
|
90
|
+
end
|
91
|
+
result
|
92
|
+
end
|
93
|
+
|
94
|
+
private
|
95
|
+
|
96
|
+
def get_func(type)
|
97
|
+
unless @address
|
98
|
+
return nil
|
99
|
+
end
|
100
|
+
Fiddle::Function.new(@address, @argv, type)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
data/lib/sitar/helper.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
module Sitar
|
4
|
+
class Helper
|
5
|
+
|
6
|
+
class << self
|
7
|
+
|
8
|
+
# Platform to check whether windows.
|
9
|
+
#
|
10
|
+
# === Return
|
11
|
+
# If return true, platform is windows.
|
12
|
+
def platform_windows?
|
13
|
+
RUBY_PLATFORM =~ /cygwin|mswin|mingw|bccwin|wince|emx/
|
14
|
+
end
|
15
|
+
|
16
|
+
# Platform to check whether mac os.
|
17
|
+
#
|
18
|
+
# === Return
|
19
|
+
# If return true, platform is mac os.
|
20
|
+
def platform_mac?
|
21
|
+
RUBY_PLATFORM =~ /darwin/
|
22
|
+
end
|
23
|
+
|
24
|
+
# Platform to check whether linux.
|
25
|
+
#
|
26
|
+
# === Return
|
27
|
+
# If return true, platform is linux.
|
28
|
+
def platform_linux?
|
29
|
+
RUBY_PLATFORM =~ /linux/
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
data/lib/sitar/types.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'sitar/helper'
|
3
|
+
|
4
|
+
module Sitar
|
5
|
+
module Types
|
6
|
+
|
7
|
+
module Aliases
|
8
|
+
|
9
|
+
def self.included(m)
|
10
|
+
m.module_eval{
|
11
|
+
if Helper.platform_windows?
|
12
|
+
# Windows UTF-16 2-byte wide chars.
|
13
|
+
typealias "wchar_t", "short"
|
14
|
+
else
|
15
|
+
# Linux and OS X is 4 bytes UTF-32.
|
16
|
+
typealias "wchar_t", "int"
|
17
|
+
end
|
18
|
+
typealias "bool", "unsigned char"
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'fiddle'
|
3
|
+
|
4
|
+
module Sitar
|
5
|
+
module Types
|
6
|
+
|
7
|
+
class Pointer
|
8
|
+
|
9
|
+
class << self
|
10
|
+
|
11
|
+
# Cretae double pointer value.
|
12
|
+
# e.g. void **double_ptr;
|
13
|
+
def pptr
|
14
|
+
Fiddle::Pointer.malloc(Fiddle::SIZEOF_VOIDP)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'fiddle/import'
|
3
|
+
|
4
|
+
module Sitar
|
5
|
+
module Types
|
6
|
+
|
7
|
+
module StructFactory
|
8
|
+
|
9
|
+
# Define struct.
|
10
|
+
#
|
11
|
+
# == Example
|
12
|
+
#
|
13
|
+
# module M
|
14
|
+
# extend Sitar::Types::StructFactory
|
15
|
+
#
|
16
|
+
# define 'struct', ["int no", "char *name"]
|
17
|
+
# end
|
18
|
+
def define(struct_name, members)
|
19
|
+
@structs ||= {}
|
20
|
+
@structs[struct_name] = members
|
21
|
+
end
|
22
|
+
|
23
|
+
# Apply struct via define method.
|
24
|
+
def define_eval(m)
|
25
|
+
@structs.each do |k, v|
|
26
|
+
# Declare struct.
|
27
|
+
name = k.upcase
|
28
|
+
members = parse(k, v)
|
29
|
+
m.module_eval <<-EOF
|
30
|
+
#{name} = struct(#{members})
|
31
|
+
EOF
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def parse(struct_name, members)
|
38
|
+
parse_inner_struct(1, struct_name, members)
|
39
|
+
end
|
40
|
+
|
41
|
+
def parse_inner_struct(count, struct_name, members)
|
42
|
+
inners = []
|
43
|
+
1.upto count do |idx|
|
44
|
+
members.each do |member|
|
45
|
+
# Parse inner struct.
|
46
|
+
type = member.split(' ')[0]
|
47
|
+
value = member.split(' ')[1]
|
48
|
+
|
49
|
+
inner_struct = @structs[type]
|
50
|
+
if type != struct_name && inner_struct
|
51
|
+
array_count = 1
|
52
|
+
if /\[(\d+)\]/ =~ value
|
53
|
+
array_count = $1.to_i
|
54
|
+
end
|
55
|
+
inners << parse_inner_struct(array_count, type, inner_struct)
|
56
|
+
else
|
57
|
+
if count > 1
|
58
|
+
# Add member prefix. XX_1, XX_2
|
59
|
+
if /([a-zA-Z]+)\[(\d+)\]/ =~ value
|
60
|
+
# Replace XXX[20] -> XXX_1[20]
|
61
|
+
value = "#{$1}_#{idx}[#{$2}]"
|
62
|
+
else
|
63
|
+
value = "#{value}_#{idx}"
|
64
|
+
end
|
65
|
+
|
66
|
+
inners << "#{type} #{value}"
|
67
|
+
else
|
68
|
+
inners << member
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
inners.flatten
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'fiddle'
|
3
|
+
require 'sitar/helper'
|
4
|
+
|
5
|
+
module Sitar
|
6
|
+
module Types
|
7
|
+
class WideChar
|
8
|
+
|
9
|
+
NULL_STRING = "\u0000".freeze
|
10
|
+
|
11
|
+
class << self
|
12
|
+
|
13
|
+
# Check '\u0000' charactor.
|
14
|
+
#
|
15
|
+
# === Return
|
16
|
+
# If return true, value is '\u0000'.
|
17
|
+
def null?(value)
|
18
|
+
value == NULL_STRING
|
19
|
+
end
|
20
|
+
|
21
|
+
# Convert array to unicode string.
|
22
|
+
#
|
23
|
+
# === Example
|
24
|
+
#
|
25
|
+
# from_array([119]) => 'w'
|
26
|
+
def from_array(array)
|
27
|
+
array.pack('U*')
|
28
|
+
end
|
29
|
+
|
30
|
+
# Convert pointer to unicode string.
|
31
|
+
def from_ptr(ptr)
|
32
|
+
pointer = Fiddle::Pointer.new(ptr)
|
33
|
+
size = 4 # Linux and OS X is 4 bytes UTF-32.
|
34
|
+
if Helper.platform_windows?
|
35
|
+
size = 2 # Windows UTF-16 2-byte wide chars.
|
36
|
+
end
|
37
|
+
|
38
|
+
index = 0
|
39
|
+
result = ''
|
40
|
+
loop do
|
41
|
+
val = []
|
42
|
+
size.times do |i|
|
43
|
+
n = pointer[index + i].to_i
|
44
|
+
val << n if n != 0
|
45
|
+
end
|
46
|
+
break if val.empty?
|
47
|
+
|
48
|
+
index = index + size
|
49
|
+
|
50
|
+
str = from_array(val)
|
51
|
+
result = result + str
|
52
|
+
end
|
53
|
+
result
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/sitar.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sitar/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "sitar"
|
8
|
+
spec.version = Sitar::VERSION
|
9
|
+
spec.authors = ["Slowhand0309"]
|
10
|
+
spec.email = ["slowhand0309@gmail.com"]
|
11
|
+
|
12
|
+
spec.required_ruby_version = ">= 2.0.0"
|
13
|
+
|
14
|
+
spec.summary = %q{Sitar is easy to use the fiddle}
|
15
|
+
spec.description = %q{You can used C method only to extend module with Sitar and load shared library.}
|
16
|
+
spec.homepage = "https://github.com/Slowhand0309/sitar"
|
17
|
+
spec.license = "MIT"
|
18
|
+
|
19
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_runtime_dependency "activesupport", "~> 4.0"
|
23
|
+
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sitar
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Slowhand0309
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-08-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: You can used C method only to extend module with Sitar and load shared
|
56
|
+
library.
|
57
|
+
email:
|
58
|
+
- slowhand0309@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".travis.yml"
|
65
|
+
- CODE_OF_CONDUCT.md
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- lib/sitar.rb
|
71
|
+
- lib/sitar/binder.rb
|
72
|
+
- lib/sitar/executor.rb
|
73
|
+
- lib/sitar/helper.rb
|
74
|
+
- lib/sitar/types.rb
|
75
|
+
- lib/sitar/types/aliases.rb
|
76
|
+
- lib/sitar/types/pointer.rb
|
77
|
+
- lib/sitar/types/struct_factory.rb
|
78
|
+
- lib/sitar/types/wide_char.rb
|
79
|
+
- lib/sitar/version.rb
|
80
|
+
- sitar.gemspec
|
81
|
+
homepage: https://github.com/Slowhand0309/sitar
|
82
|
+
licenses:
|
83
|
+
- MIT
|
84
|
+
metadata: {}
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 2.0.0
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
requirements: []
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 2.4.6
|
102
|
+
signing_key:
|
103
|
+
specification_version: 4
|
104
|
+
summary: Sitar is easy to use the fiddle
|
105
|
+
test_files: []
|