ffi 1.15.5-x64-mingw-ucrt

Sign up to get free protection for your applications and to get access to all the features.
Files changed (100) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +338 -0
  3. data/COPYING +49 -0
  4. data/Gemfile +14 -0
  5. data/LICENSE +24 -0
  6. data/LICENSE.SPECS +22 -0
  7. data/README.md +136 -0
  8. data/Rakefile +191 -0
  9. data/ffi.gemspec +42 -0
  10. data/lib/3.1/ffi_c.so +0 -0
  11. data/lib/ffi/abstract_memory.rb +44 -0
  12. data/lib/ffi/autopointer.rb +203 -0
  13. data/lib/ffi/buffer.rb +4 -0
  14. data/lib/ffi/callback.rb +4 -0
  15. data/lib/ffi/data_converter.rb +67 -0
  16. data/lib/ffi/enum.rb +296 -0
  17. data/lib/ffi/errno.rb +43 -0
  18. data/lib/ffi/ffi.rb +47 -0
  19. data/lib/ffi/io.rb +62 -0
  20. data/lib/ffi/library.rb +592 -0
  21. data/lib/ffi/managedstruct.rb +84 -0
  22. data/lib/ffi/memorypointer.rb +1 -0
  23. data/lib/ffi/platform/aarch64-darwin/types.conf +130 -0
  24. data/lib/ffi/platform/aarch64-freebsd/types.conf +128 -0
  25. data/lib/ffi/platform/aarch64-freebsd12/types.conf +181 -0
  26. data/lib/ffi/platform/aarch64-linux/types.conf +104 -0
  27. data/lib/ffi/platform/aarch64-openbsd/types.conf +134 -0
  28. data/lib/ffi/platform/arm-freebsd/types.conf +152 -0
  29. data/lib/ffi/platform/arm-freebsd12/types.conf +152 -0
  30. data/lib/ffi/platform/arm-linux/types.conf +132 -0
  31. data/lib/ffi/platform/i386-cygwin/types.conf +3 -0
  32. data/lib/ffi/platform/i386-darwin/types.conf +100 -0
  33. data/lib/ffi/platform/i386-freebsd/types.conf +152 -0
  34. data/lib/ffi/platform/i386-freebsd12/types.conf +152 -0
  35. data/lib/ffi/platform/i386-gnu/types.conf +107 -0
  36. data/lib/ffi/platform/i386-linux/types.conf +103 -0
  37. data/lib/ffi/platform/i386-netbsd/types.conf +126 -0
  38. data/lib/ffi/platform/i386-openbsd/types.conf +128 -0
  39. data/lib/ffi/platform/i386-solaris/types.conf +122 -0
  40. data/lib/ffi/platform/i386-windows/types.conf +52 -0
  41. data/lib/ffi/platform/ia64-linux/types.conf +104 -0
  42. data/lib/ffi/platform/mips-linux/types.conf +102 -0
  43. data/lib/ffi/platform/mips64-linux/types.conf +104 -0
  44. data/lib/ffi/platform/mips64el-linux/types.conf +104 -0
  45. data/lib/ffi/platform/mipsel-linux/types.conf +102 -0
  46. data/lib/ffi/platform/mipsisa32r6-linux/types.conf +102 -0
  47. data/lib/ffi/platform/mipsisa32r6el-linux/types.conf +102 -0
  48. data/lib/ffi/platform/mipsisa64r6-linux/types.conf +104 -0
  49. data/lib/ffi/platform/mipsisa64r6el-linux/types.conf +104 -0
  50. data/lib/ffi/platform/powerpc-aix/types.conf +180 -0
  51. data/lib/ffi/platform/powerpc-darwin/types.conf +100 -0
  52. data/lib/ffi/platform/powerpc-linux/types.conf +130 -0
  53. data/lib/ffi/platform/powerpc-openbsd/types.conf +156 -0
  54. data/lib/ffi/platform/powerpc64-linux/types.conf +104 -0
  55. data/lib/ffi/platform/powerpc64le-linux/types.conf +100 -0
  56. data/lib/ffi/platform/riscv64-linux/types.conf +104 -0
  57. data/lib/ffi/platform/s390-linux/types.conf +102 -0
  58. data/lib/ffi/platform/s390x-linux/types.conf +102 -0
  59. data/lib/ffi/platform/sparc-linux/types.conf +102 -0
  60. data/lib/ffi/platform/sparc-solaris/types.conf +128 -0
  61. data/lib/ffi/platform/sparc64-linux/types.conf +102 -0
  62. data/lib/ffi/platform/sparcv9-openbsd/types.conf +156 -0
  63. data/lib/ffi/platform/sparcv9-solaris/types.conf +128 -0
  64. data/lib/ffi/platform/x86_64-cygwin/types.conf +3 -0
  65. data/lib/ffi/platform/x86_64-darwin/types.conf +130 -0
  66. data/lib/ffi/platform/x86_64-dragonflybsd/types.conf +130 -0
  67. data/lib/ffi/platform/x86_64-freebsd/types.conf +128 -0
  68. data/lib/ffi/platform/x86_64-freebsd12/types.conf +158 -0
  69. data/lib/ffi/platform/x86_64-haiku/types.conf +117 -0
  70. data/lib/ffi/platform/x86_64-linux/types.conf +132 -0
  71. data/lib/ffi/platform/x86_64-msys/types.conf +119 -0
  72. data/lib/ffi/platform/x86_64-netbsd/types.conf +128 -0
  73. data/lib/ffi/platform/x86_64-openbsd/types.conf +134 -0
  74. data/lib/ffi/platform/x86_64-solaris/types.conf +122 -0
  75. data/lib/ffi/platform/x86_64-windows/types.conf +52 -0
  76. data/lib/ffi/platform.rb +185 -0
  77. data/lib/ffi/pointer.rb +167 -0
  78. data/lib/ffi/struct.rb +316 -0
  79. data/lib/ffi/struct_by_reference.rb +72 -0
  80. data/lib/ffi/struct_layout.rb +96 -0
  81. data/lib/ffi/struct_layout_builder.rb +227 -0
  82. data/lib/ffi/tools/const_generator.rb +232 -0
  83. data/lib/ffi/tools/generator.rb +105 -0
  84. data/lib/ffi/tools/generator_task.rb +32 -0
  85. data/lib/ffi/tools/struct_generator.rb +195 -0
  86. data/lib/ffi/tools/types_generator.rb +137 -0
  87. data/lib/ffi/types.rb +194 -0
  88. data/lib/ffi/union.rb +43 -0
  89. data/lib/ffi/variadic.rb +69 -0
  90. data/lib/ffi/version.rb +3 -0
  91. data/lib/ffi.rb +27 -0
  92. data/rakelib/ffi_gem_helper.rb +65 -0
  93. data/samples/getlogin.rb +8 -0
  94. data/samples/getpid.rb +8 -0
  95. data/samples/gettimeofday.rb +18 -0
  96. data/samples/hello.rb +8 -0
  97. data/samples/inotify.rb +60 -0
  98. data/samples/pty.rb +75 -0
  99. data/samples/qsort.rb +20 -0
  100. metadata +207 -0
data/lib/ffi/enum.rb ADDED
@@ -0,0 +1,296 @@
1
+ #
2
+ # Copyright (C) 2009, 2010 Wayne Meissner
3
+ # Copyright (C) 2009 Luc Heinrich
4
+ #
5
+ # This file is part of ruby-ffi.
6
+ #
7
+ # All rights reserved.
8
+ #
9
+ # Redistribution and use in source and binary forms, with or without
10
+ # modification, are permitted provided that the following conditions are met:
11
+ #
12
+ # * Redistributions of source code must retain the above copyright notice, this
13
+ # list of conditions and the following disclaimer.
14
+ # * Redistributions in binary form must reproduce the above copyright notice
15
+ # this list of conditions and the following disclaimer in the documentation
16
+ # and/or other materials provided with the distribution.
17
+ # * Neither the name of the Ruby FFI project nor the names of its contributors
18
+ # may be used to endorse or promote products derived from this software
19
+ # without specific prior written permission.
20
+ #
21
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
25
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ #
32
+
33
+ module FFI
34
+
35
+ # An instance of this class permits to manage {Enum}s. In fact, Enums is a collection of {Enum}s.
36
+ class Enums
37
+
38
+ # @return [nil]
39
+ def initialize
40
+ @all_enums = Array.new
41
+ @tagged_enums = Hash.new
42
+ @symbol_map = Hash.new
43
+ end
44
+
45
+ # @param [Enum] enum
46
+ # Add an {Enum} to the collection.
47
+ def <<(enum)
48
+ @all_enums << enum
49
+ @tagged_enums[enum.tag] = enum unless enum.tag.nil?
50
+ @symbol_map.merge!(enum.symbol_map)
51
+ end
52
+
53
+ # @param query enum tag or part of an enum name
54
+ # @return [Enum]
55
+ # Find a {Enum} in collection.
56
+ def find(query)
57
+ if @tagged_enums.has_key?(query)
58
+ @tagged_enums[query]
59
+ else
60
+ @all_enums.detect { |enum| enum.symbols.include?(query) }
61
+ end
62
+ end
63
+
64
+ # @param symbol a symbol to find in merge symbol maps of all enums.
65
+ # @return a symbol
66
+ def __map_symbol(symbol)
67
+ @symbol_map[symbol]
68
+ end
69
+
70
+ end
71
+
72
+ # Represents a C enum.
73
+ #
74
+ # For a C enum:
75
+ # enum fruits {
76
+ # apple,
77
+ # banana,
78
+ # orange,
79
+ # pineapple
80
+ # };
81
+ # are defined this vocabulary:
82
+ # * a _symbol_ is a word from the enumeration (ie. _apple_, by example);
83
+ # * a _value_ is the value of a symbol in the enumeration (by example, apple has value _0_ and banana _1_).
84
+ class Enum
85
+ include DataConverter
86
+
87
+ attr_reader :tag
88
+ attr_reader :native_type
89
+
90
+ # @overload initialize(info, tag=nil)
91
+ # @param [nil, Enumerable] info
92
+ # @param [nil, Symbol] tag enum tag
93
+ # @overload initialize(native_type, info, tag=nil)
94
+ # @param [FFI::Type] native_type Native type for new Enum
95
+ # @param [nil, Enumerable] info symbols and values for new Enum
96
+ # @param [nil, Symbol] tag name of new Enum
97
+ def initialize(*args)
98
+ @native_type = args.first.kind_of?(FFI::Type) ? args.shift : Type::INT
99
+ info, @tag = *args
100
+ @kv_map = Hash.new
101
+ unless info.nil?
102
+ last_cst = nil
103
+ value = 0
104
+ info.each do |i|
105
+ case i
106
+ when Symbol
107
+ raise ArgumentError, "duplicate enum key" if @kv_map.has_key?(i)
108
+ @kv_map[i] = value
109
+ last_cst = i
110
+ value += 1
111
+ when Integer
112
+ @kv_map[last_cst] = i
113
+ value = i+1
114
+ end
115
+ end
116
+ end
117
+ @vk_map = @kv_map.invert
118
+ end
119
+
120
+ # @return [Array] enum symbol names
121
+ def symbols
122
+ @kv_map.keys
123
+ end
124
+
125
+ # Get a symbol or a value from the enum.
126
+ # @overload [](query)
127
+ # Get enum value from symbol.
128
+ # @param [Symbol] query
129
+ # @return [Integer]
130
+ # @overload [](query)
131
+ # Get enum symbol from value.
132
+ # @param [Integer] query
133
+ # @return [Symbol]
134
+ def [](query)
135
+ case query
136
+ when Symbol
137
+ @kv_map[query]
138
+ when Integer
139
+ @vk_map[query]
140
+ end
141
+ end
142
+ alias find []
143
+
144
+ # Get the symbol map.
145
+ # @return [Hash]
146
+ def symbol_map
147
+ @kv_map
148
+ end
149
+
150
+ alias to_h symbol_map
151
+ alias to_hash symbol_map
152
+
153
+ # @param [Symbol, Integer, #to_int] val
154
+ # @param ctx unused
155
+ # @return [Integer] value of a enum symbol
156
+ def to_native(val, ctx)
157
+ @kv_map[val] || if val.is_a?(Integer)
158
+ val
159
+ elsif val.respond_to?(:to_int)
160
+ val.to_int
161
+ else
162
+ raise ArgumentError, "invalid enum value, #{val.inspect}"
163
+ end
164
+ end
165
+
166
+ # @param val
167
+ # @return symbol name if it exists for +val+.
168
+ def from_native(val, ctx)
169
+ @vk_map[val] || val
170
+ end
171
+ end
172
+
173
+ # Represents a C enum whose values are power of 2
174
+ #
175
+ # @example
176
+ # enum {
177
+ # red = (1<<0),
178
+ # green = (1<<1),
179
+ # blue = (1<<2)
180
+ # }
181
+ #
182
+ # Contrary to classical enums, bitmask values are usually combined
183
+ # when used.
184
+ class Bitmask < Enum
185
+
186
+ # @overload initialize(info, tag=nil)
187
+ # @param [nil, Enumerable] info symbols and bit rank for new Bitmask
188
+ # @param [nil, Symbol] tag name of new Bitmask
189
+ # @overload initialize(native_type, info, tag=nil)
190
+ # @param [FFI::Type] native_type Native type for new Bitmask
191
+ # @param [nil, Enumerable] info symbols and bit rank for new Bitmask
192
+ # @param [nil, Symbol] tag name of new Bitmask
193
+ def initialize(*args)
194
+ @native_type = args.first.kind_of?(FFI::Type) ? args.shift : Type::INT
195
+ info, @tag = *args
196
+ @kv_map = Hash.new
197
+ unless info.nil?
198
+ last_cst = nil
199
+ value = 0
200
+ info.each do |i|
201
+ case i
202
+ when Symbol
203
+ raise ArgumentError, "duplicate bitmask key" if @kv_map.has_key?(i)
204
+ @kv_map[i] = 1 << value
205
+ last_cst = i
206
+ value += 1
207
+ when Integer
208
+ raise ArgumentError, "bitmask index should be positive" if i<0
209
+ @kv_map[last_cst] = 1 << i
210
+ value = i+1
211
+ end
212
+ end
213
+ end
214
+ @vk_map = @kv_map.invert
215
+ end
216
+
217
+ # Get a symbol list or a value from the bitmask
218
+ # @overload [](*query)
219
+ # Get bitmask value from symbol list
220
+ # @param [Symbol] query
221
+ # @return [Integer]
222
+ # @overload [](query)
223
+ # Get bitmaks value from symbol array
224
+ # @param [Array<Symbol>] query
225
+ # @return [Integer]
226
+ # @overload [](*query)
227
+ # Get a list of bitmask symbols corresponding to
228
+ # the or reduction of a list of integer
229
+ # @param [Integer] query
230
+ # @return [Array<Symbol>]
231
+ # @overload [](query)
232
+ # Get a list of bitmask symbols corresponding to
233
+ # the or reduction of a list of integer
234
+ # @param [Array<Integer>] query
235
+ # @return [Array<Symbol>]
236
+ def [](*query)
237
+ flat_query = query.flatten
238
+ raise ArgumentError, "query should be homogeneous, #{query.inspect}" unless flat_query.all? { |o| o.is_a?(Symbol) } || flat_query.all? { |o| o.is_a?(Integer) || o.respond_to?(:to_int) }
239
+ case flat_query[0]
240
+ when Symbol
241
+ flat_query.inject(0) do |val, o|
242
+ v = @kv_map[o]
243
+ if v then val |= v else val end
244
+ end
245
+ when Integer, ->(o) { o.respond_to?(:to_int) }
246
+ val = flat_query.inject(0) { |mask, o| mask |= o.to_int }
247
+ @kv_map.select { |_, v| v & val != 0 }.keys
248
+ end
249
+ end
250
+
251
+ # Get the native value of a bitmask
252
+ # @overload to_native(query, ctx)
253
+ # @param [Symbol, Integer, #to_int] query
254
+ # @param ctx unused
255
+ # @return [Integer] value of a bitmask
256
+ # @overload to_native(query, ctx)
257
+ # @param [Array<Symbol, Integer, #to_int>] query
258
+ # @param ctx unused
259
+ # @return [Integer] value of a bitmask
260
+ def to_native(query, ctx)
261
+ return 0 if query.nil?
262
+ flat_query = [query].flatten
263
+ flat_query.inject(0) do |val, o|
264
+ case o
265
+ when Symbol
266
+ v = @kv_map[o]
267
+ raise ArgumentError, "invalid bitmask value, #{o.inspect}" unless v
268
+ val |= v
269
+ when Integer
270
+ val |= o
271
+ when ->(obj) { obj.respond_to?(:to_int) }
272
+ val |= o.to_int
273
+ else
274
+ raise ArgumentError, "invalid bitmask value, #{o.inspect}"
275
+ end
276
+ end
277
+ end
278
+
279
+ # @param [Integer] val
280
+ # @param ctx unused
281
+ # @return [Array<Symbol, Integer>] list of symbol names corresponding to val, plus an optional remainder if some bits don't match any constant
282
+ def from_native(val, ctx)
283
+ list = @kv_map.select { |_, v| v & val != 0 }.keys
284
+ # If there are unmatch flags,
285
+ # return them in an integer,
286
+ # else information can be lost.
287
+ # Similar to Enum behavior.
288
+ remainder = val ^ list.inject(0) do |tmp, o|
289
+ v = @kv_map[o]
290
+ if v then tmp |= v else tmp end
291
+ end
292
+ list.push remainder unless remainder == 0
293
+ return list
294
+ end
295
+ end
296
+ end
data/lib/ffi/errno.rb ADDED
@@ -0,0 +1,43 @@
1
+ #
2
+ # Copyright (C) 2008-2010 Wayne Meissner
3
+ #
4
+ # This file is part of ruby-ffi.
5
+ #
6
+ # All rights reserved.
7
+ #
8
+ # Redistribution and use in source and binary forms, with or without
9
+ # modification, are permitted provided that the following conditions are met:
10
+ #
11
+ # * Redistributions of source code must retain the above copyright notice, this
12
+ # list of conditions and the following disclaimer.
13
+ # * Redistributions in binary form must reproduce the above copyright notice
14
+ # this list of conditions and the following disclaimer in the documentation
15
+ # and/or other materials provided with the distribution.
16
+ # * Neither the name of the Ruby FFI project nor the names of its contributors
17
+ # may be used to endorse or promote products derived from this software
18
+ # without specific prior written permission.
19
+ #
20
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
24
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.#
30
+
31
+ module FFI
32
+ # @return (see FFI::LastError.error)
33
+ # @see FFI::LastError.error
34
+ def self.errno
35
+ FFI::LastError.error
36
+ end
37
+ # @param error (see FFI::LastError.error=)
38
+ # @return (see FFI::LastError.error=)
39
+ # @see FFI::LastError.error=
40
+ def self.errno=(error)
41
+ FFI::LastError.error = error
42
+ end
43
+ end
data/lib/ffi/ffi.rb ADDED
@@ -0,0 +1,47 @@
1
+ #
2
+ # Copyright (C) 2008-2010 JRuby project
3
+ #
4
+ # This file is part of ruby-ffi.
5
+ #
6
+ # All rights reserved.
7
+ #
8
+ # Redistribution and use in source and binary forms, with or without
9
+ # modification, are permitted provided that the following conditions are met:
10
+ #
11
+ # * Redistributions of source code must retain the above copyright notice, this
12
+ # list of conditions and the following disclaimer.
13
+ # * Redistributions in binary form must reproduce the above copyright notice
14
+ # this list of conditions and the following disclaimer in the documentation
15
+ # and/or other materials provided with the distribution.
16
+ # * Neither the name of the Ruby FFI project nor the names of its contributors
17
+ # may be used to endorse or promote products derived from this software
18
+ # without specific prior written permission.
19
+ #
20
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
24
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+
31
+ require 'ffi/platform'
32
+ require 'ffi/data_converter'
33
+ require 'ffi/types'
34
+ require 'ffi/library'
35
+ require 'ffi/errno'
36
+ require 'ffi/abstract_memory'
37
+ require 'ffi/pointer'
38
+ require 'ffi/memorypointer'
39
+ require 'ffi/struct'
40
+ require 'ffi/union'
41
+ require 'ffi/managedstruct'
42
+ require 'ffi/callback'
43
+ require 'ffi/io'
44
+ require 'ffi/autopointer'
45
+ require 'ffi/variadic'
46
+ require 'ffi/enum'
47
+ require 'ffi/version'
data/lib/ffi/io.rb ADDED
@@ -0,0 +1,62 @@
1
+ #
2
+ # Copyright (C) 2008, 2009 Wayne Meissner
3
+ #
4
+ # This file is part of ruby-ffi.
5
+ #
6
+ # All rights reserved.
7
+ #
8
+ # Redistribution and use in source and binary forms, with or without
9
+ # modification, are permitted provided that the following conditions are met:
10
+ #
11
+ # * Redistributions of source code must retain the above copyright notice, this
12
+ # list of conditions and the following disclaimer.
13
+ # * Redistributions in binary form must reproduce the above copyright notice
14
+ # this list of conditions and the following disclaimer in the documentation
15
+ # and/or other materials provided with the distribution.
16
+ # * Neither the name of the Ruby FFI project nor the names of its contributors
17
+ # may be used to endorse or promote products derived from this software
18
+ # without specific prior written permission.
19
+ #
20
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
24
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.#
30
+
31
+ module FFI
32
+
33
+ # This module implements a couple of class methods to play with IO.
34
+ module IO
35
+ # @param [Integer] fd file decriptor
36
+ # @param [String] mode mode string
37
+ # @return [::IO]
38
+ # Synonym for IO::for_fd.
39
+ def self.for_fd(fd, mode = "r")
40
+ ::IO.for_fd(fd, mode)
41
+ end
42
+
43
+ # @param [#read] io io to read from
44
+ # @param [AbstractMemory] buf destination for data read from +io+
45
+ # @param [nil, Numeric] len maximul number of bytes to read from +io+. If +nil+,
46
+ # read until end of file.
47
+ # @return [Numeric] length really read, in bytes
48
+ #
49
+ # A version of IO#read that reads data from an IO and put then into a native buffer.
50
+ #
51
+ # This will be optimized at some future time to eliminate the double copy.
52
+ #
53
+ def self.native_read(io, buf, len)
54
+ tmp = io.read(len)
55
+ return -1 unless tmp
56
+ buf.put_bytes(0, tmp)
57
+ tmp.length
58
+ end
59
+
60
+ end
61
+ end
62
+