September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,14 +1,34 @@
|
|||
require 'ffi'
|
||||
/* rc_strdup.c */
|
||||
#include <stdlib.h> /* free() */
|
||||
#include <string.h> /* strdup() */
|
||||
#include <ruby.h>
|
||||
|
||||
module LibC
|
||||
extend FFI::Library
|
||||
ffi_lib FFI::Platform::LIBC
|
||||
static VALUE
|
||||
rc_strdup(VALUE obj, VALUE str_in)
|
||||
{
|
||||
VALUE str_out;
|
||||
char *c, *d;
|
||||
|
||||
attach_function :strdup, [:string], :pointer
|
||||
attach_function :free, [:pointer], :void
|
||||
end
|
||||
/*
|
||||
* Convert Ruby value to C string. May raise TypeError if the
|
||||
* value isn't a string, or ArgumentError if it contains '\0'.
|
||||
*/
|
||||
c = StringValueCStr(str_in);
|
||||
|
||||
string = "Hello, World!"
|
||||
duplicate = LibC.strdup(string)
|
||||
puts duplicate.get_string(0)
|
||||
LibC.free(duplicate)
|
||||
/* Call strdup() and perhaps raise Errno::ENOMEM. */
|
||||
d = strdup(c);
|
||||
if (d == NULL)
|
||||
rb_sys_fail(NULL);
|
||||
|
||||
/* Convert C string to Ruby string. */
|
||||
str_out = rb_str_new_cstr(d);
|
||||
free(d);
|
||||
return str_out;
|
||||
}
|
||||
|
||||
void
|
||||
Init_rc_strdup(void)
|
||||
{
|
||||
VALUE mRosettaCode = rb_define_module("RosettaCode");
|
||||
rb_define_module_function(mRosettaCode, "strdup", rc_strdup, 1);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,19 +1,3 @@
|
|||
require 'dl'
|
||||
require 'fiddle'
|
||||
|
||||
# Declare strdup().
|
||||
# * DL::Handle['strdup'] is the address of the function.
|
||||
# * The function takes a pointer and returns a pointer.
|
||||
strdup = Fiddle::Function.new(DL::Handle['strdup'],
|
||||
[DL::TYPE_VOIDP], DL::TYPE_VOIDP)
|
||||
|
||||
# Call strdup().
|
||||
# * Fiddle converts our Ruby string to a C string.
|
||||
# * Fiddle returns a DL::CPtr.
|
||||
duplicate = strdup.call("This is a string!")
|
||||
|
||||
# DL::CPtr#to_s converts our C string to a Ruby string.
|
||||
puts duplicate.to_s
|
||||
|
||||
# We must call free(), because strdup() allocated memory.
|
||||
DL.free duplicate
|
||||
# extconf.rb
|
||||
require 'mkmf'
|
||||
create_makefile('rc_strdup')
|
||||
|
|
|
|||
|
|
@ -1,33 +1,3 @@
|
|||
require 'rubygems'
|
||||
require 'inline'
|
||||
|
||||
class InlineTester
|
||||
def factorial_ruby(n)
|
||||
(1..n).inject(1, :*)
|
||||
end
|
||||
|
||||
inline do |builder|
|
||||
builder.c <<-'END_C'
|
||||
long factorial_c(int max) {
|
||||
long result = 1;
|
||||
int i;
|
||||
for (i = 1; i <= max; ++i)
|
||||
result *= i;
|
||||
return result;
|
||||
}
|
||||
END_C
|
||||
end
|
||||
|
||||
inline do |builder|
|
||||
builder.include %q("math.h")
|
||||
builder.c <<-'END_C'
|
||||
int my_ilogb(double value) {
|
||||
return ilogb(value);
|
||||
}
|
||||
END_C
|
||||
end
|
||||
end
|
||||
|
||||
t = InlineTester.new
|
||||
11.upto(14) {|n| p [n, t.factorial_ruby(n), t.factorial_c(n)]}
|
||||
p t.my_ilogb(1000)
|
||||
# demo.rb
|
||||
require 'rc_strdup'
|
||||
puts RosettaCode.strdup('This string gets duplicated.')
|
||||
|
|
|
|||
|
|
@ -0,0 +1,14 @@
|
|||
require 'ffi'
|
||||
|
||||
module LibC
|
||||
extend FFI::Library
|
||||
ffi_lib FFI::Platform::LIBC
|
||||
|
||||
attach_function :strdup, [:string], :pointer
|
||||
attach_function :free, [:pointer], :void
|
||||
end
|
||||
|
||||
string = "Hello, World!"
|
||||
duplicate = LibC.strdup(string)
|
||||
puts duplicate.get_string(0)
|
||||
LibC.free(duplicate)
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
require 'fiddle'
|
||||
|
||||
# Find strdup(). It takes a pointer and returns a pointer.
|
||||
strdup = Fiddle::Function
|
||||
.new(Fiddle::Handle['strdup'],
|
||||
[Fiddle::TYPE_VOIDP], Fiddle::TYPE_VOIDP)
|
||||
|
||||
# Call strdup().
|
||||
# - It converts our Ruby string to a C string.
|
||||
# - It returns a Fiddle::Pointer.
|
||||
duplicate = strdup.call("This is a string!")
|
||||
puts duplicate.to_s # Convert the C string to a Ruby string.
|
||||
Fiddle.free duplicate # free() the memory that strdup() allocated.
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
require 'fiddle'
|
||||
require 'fiddle/import'
|
||||
|
||||
module C
|
||||
extend Fiddle::Importer
|
||||
dlload Fiddle::Handle::DEFAULT
|
||||
extern 'char *strdup(char *)'
|
||||
end
|
||||
|
||||
duplicate = C.strdup("This is a string!")
|
||||
puts duplicate.to_s
|
||||
Fiddle.free duplicate
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
require 'rubygems'
|
||||
require 'inline'
|
||||
|
||||
class InlineTester
|
||||
def factorial_ruby(n)
|
||||
(1..n).inject(1, :*)
|
||||
end
|
||||
|
||||
inline do |builder|
|
||||
builder.c <<-'END_C'
|
||||
long factorial_c(int max) {
|
||||
long result = 1;
|
||||
int i;
|
||||
for (i = 1; i <= max; ++i)
|
||||
result *= i;
|
||||
return result;
|
||||
}
|
||||
END_C
|
||||
end
|
||||
|
||||
inline do |builder|
|
||||
builder.include %q("math.h")
|
||||
builder.c <<-'END_C'
|
||||
int my_ilogb(double value) {
|
||||
return ilogb(value);
|
||||
}
|
||||
END_C
|
||||
end
|
||||
end
|
||||
|
||||
t = InlineTester.new
|
||||
11.upto(14) {|n| p [n, t.factorial_ruby(n), t.factorial_c(n)]}
|
||||
p t.my_ilogb(1000)
|
||||
Loading…
Add table
Add a link
Reference in a new issue