summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/efl/ecore_getopt.rb38
-rw-r--r--spec/ecore_getopt_spec.rb38
2 files changed, 42 insertions, 34 deletions
diff --git a/lib/efl/ecore_getopt.rb b/lib/efl/ecore_getopt.rb
index fdc7ede..e403e22 100644
--- a/lib/efl/ecore_getopt.rb
+++ b/lib/efl/ecore_getopt.rb
@@ -91,6 +91,7 @@ module Efl
class REcoreGetopt
#
def initialize desc
+ @pts = []
@ecore_getopt_st = nil
@ecore_values_st = nil
@desc = desc
@@ -116,11 +117,14 @@ module Efl
:choice => [ :pointer, nil, :ptrp ]
}
end
- def p_from_string r
+ def want_p r
+ return r if r.is_a? FFI::Pointer
return FFI::Pointer::NULL if r.nil?
- FFI::MemoryPointer.from_string r
+ p = FFI::MemoryPointer.from_string r
+ @pts << p
+ p
end
- private :p_from_string
+ private :want_p
def set_option o
@options.insert -2, o
end
@@ -148,6 +152,7 @@ module Efl
p.send 'write_'+ptype.to_s, val unless val.nil?
r = @values[skey] = [ ptype, p ]
end
+ @pts << p
end
@values_order.insert -2, skey
r
@@ -166,7 +171,7 @@ module Efl
p = ptr.read_pointer
(p==FFI::Pointer::NULL ? nil : p.read_string )
when :pointer
- ptr
+ ptr.to_ptr
else
ptr.send 'read_'+ptype.to_s
end
@@ -187,9 +192,9 @@ module Efl
break
end
d[:shortname] = o[0].to_s.bytes.first
- d[:longname] = p_from_string o[1]
- d[:help] = p_from_string o[2]
- d[:metavar] = o[3]
+ d[:longname] = want_p o[1]
+ d[:help] = want_p o[2]
+ d[:metavar] = want_p o[3]
d[:action] = o[4]
k, v = o[5]
case k
@@ -205,7 +210,7 @@ module Efl
st = d[:action_param][:store]
st[:type] = v[0]
st[:arg_req] = v[1]
- st[:def][v[2]] = (v[2]==:strv ? p_from_string(v[3]) : v[3] ) unless v[3].nil?
+ st[:def][v[2]] = (v[2]==:strv ? want_p(v[3]) : v[3] ) unless v[3].nil?
when :store_const
d[:action_param][:store_const] = v
when :choices
@@ -228,7 +233,7 @@ module Efl
def parse argv
ptr = FFI::MemoryPointer.new(:pointer, argv.length+1)
argv.each_with_index do |s, i|
- ptr[i].put_pointer 0, p_from_string(s)
+ ptr[i].put_pointer 0, want_p(s)
end
ptr[argv.length].put_pointer 0, FFI::Pointer::NULL
Native.ecore_getopt_parse @ecore_getopt_st, @ecore_values_st, argv.length, ptr
@@ -283,10 +288,14 @@ module Efl
if def_val.nil?
p = FFI::Pointer::NULL
else
- require 'efl/eina_list'
- p = Efl::EinaList::REinaList.from_a def_val, sub_type
+ p = def_val.inject(FFI::Pointer::NULL) { |list,e|
+ ptr = FFI::MemoryPointer.new sub_type
+ @pts << ptr
+ ptr.send 'write_'+sub_type.to_s, e
+ Native.eina_list_append list, ptr
+ }
end
- set_value short, :list, [p,sub_type]
+ set_value short, :list, [want_p(p),sub_type]
set_option [ short, long, help, meta, :ecore_getopt_action_append, [:append,@types[sub_type][0]] ]
end
def append short, long, help, sub_type, def_val=nil
@@ -294,8 +303,9 @@ module Efl
end
def choice_meta short, long, help, meta, choices
p = FFI::MemoryPointer.new(:pointer, choices.length+1)
+ @pts << p
choices.each_with_index do |s, i|
- p[i].put_pointer 0, p_from_string(s)
+ p[i].put_pointer 0, want_p(s)
end
p[choices.length].put_pointer 0, FFI::Pointer::NULL
set_value short, :choice
@@ -307,7 +317,7 @@ module Efl
def callback_full short, long, help, meta, cb, data, arg_req, type, def_val
pt, ptr = set_value short, type, def_val
arg_req = ( arg_req ? :ecore_getopt_desc_arg_requirement_yes : :ecore_getopt_desc_arg_requirement_no )
- set_option [ short, long, help, meta, :ecore_getopt_action_callback, [:callback, [cb, data, arg_req, ptr ] ] ]
+ set_option [ short, long, help, meta, :ecore_getopt_action_callback, [:callback, [cb, want_p(data), arg_req, ptr ] ] ]
end
def callback_noargs short, long, help, cb, data=nil
callback_full short, long, help, FFI::Pointer::NULL, cb, data, false, :uchar, 0
diff --git a/spec/ecore_getopt_spec.rb b/spec/ecore_getopt_spec.rb
index e8fef67..7dff80d 100644
--- a/spec/ecore_getopt_spec.rb
+++ b/spec/ecore_getopt_spec.rb
@@ -14,8 +14,8 @@ describe Efl::EcoreGetopt do
}
before(:all) {
Efl::Ecore.init
- #
}
+ #
before(:each) do
#
@p = Efl::EcoreGetopt::REcoreGetopt.new :prog =>"Prog", :usage => "Usage", :version => "0.0.0", :copyright => "less", :license => "MIT", :description => "description", :strict => 1
@@ -25,14 +25,10 @@ describe Efl::EcoreGetopt do
Efl::Native::EcoreGetoptDesc.new(desc)[:shortname].chr.should == 'b'
string.should == "user_arg"
data.read_string.should == "cb_data"
- storage.read_pointer.read_int == 69
+ storage.read_pointer.read_int.should == 69
storage.read_pointer.write_int 666
true
end
- #
- @meta1 = FFI::MemoryPointer.from_string "My pretty meta"
- @meta2 = FFI::MemoryPointer.from_string "My precious meta"
- @cb_data = FFI::MemoryPointer.from_string "cb_data"
@g = Efl::Native::EinaRectangleStruct.new
@g[:x] = 100; @g[:y] = 200; @g[:w] = 300; @g[:h] = 400
#
@@ -42,18 +38,20 @@ describe Efl::EcoreGetopt do
@p.version 'V', 'version'
@p.help 'H', 'help'
@p.store 'i', 'int', 'store an integer', :int, 2
- @p.store_meta 'd', 'double', 'store an double+meta', @meta1, :double, 3.1415926
+ @p.store_meta 'd', 'double', 'store an double+meta', "My pretty meta", :double, 3.1415926
@p.store_def 's', 'string', 'store an string+default', :string, "default"
- @p.store_full 'l', 'long', 'store a long+full', @meta2, :ecore_getopt_desc_arg_requirement_yes, :long, 666
+ @p.store_full 'l', 'long', 'store a long+full', "My precious meta", :ecore_getopt_desc_arg_requirement_yes, :long, 666
@p.store_const 'c', 'const', 'store a constant', -666, 123456
@p.store_true 't', 'true', 'store true'
@p.store_false 'f', 'false', 'store false'
@p.count 'k', 'count', 'store count', 664
@p.append 'a', 'append', 'store append', :int, [1,2,3]
@p.choice 'm', 'many', 'store choice', ['ch1','ch2','ch3']
- @p.callback_noargs 'E', 'list-engines', 'list ecore-evas available engines', Efl::Native.method(:ecore_getopt_callback_ecore_evas_list_engines)
- @p.callback_args 'g', 'geometry', 'x:y:w:h', FFI::MemoryPointer.from_string("X:Y:W:H"), Efl::Native.method(:ecore_getopt_callback_geometry_parse), nil, :pointer, @g.to_ptr
- @p.callback_args 'b', 'callback', 'callback full', @meta1, @callback, @cb_data, :int, 69
+ @p.callback_args 'b', 'callback', 'callback full', "Another meta", @callback, "cb_data", :int, 69
+ @p.callback_noargs 'E', 'list-engines', 'list ecore-evas available engines',
+ Efl::Native.method(:ecore_getopt_callback_ecore_evas_list_engines)
+ @p.callback_args 'g', 'geometry', 'x:y:w:h', "X:Y:W:H",
+ Efl::Native.method(:ecore_getopt_callback_geometry_parse), nil, :pointer, @g
@p.create
#
end
@@ -148,15 +146,15 @@ describe Efl::EcoreGetopt do
args = @p.parse ["progname","-mch2"]
@p['m'].should == "ch2"
end
- it "should handle -E" do
- args = @p.parse ["My lovely prog name","-E"]
- @p['E'].should == 1
- end
it "should handle -b" do
@p['b'].should == 69
args = @p.parse ["progname","-buser_arg"]
@p['b'].should == 666
end
+ it "should handle -E" do
+ args = @p.parse ["My lovely prog name","-E"]
+ @p['E'].should == 1
+ end
it "should handle -g" do
g = Efl::Native::EinaRectangleStruct.new @p['g']
g[:x].should == 100
@@ -212,7 +210,7 @@ describe Efl::EcoreGetopt do
args = @p.parse ["progname","--count","--count"]
@p['k'].should == 666
end
- it "should handle -a" do
+ it "should handle --append" do
@p['a'].should == [1,2,3]
args = @p.parse ["progname","--append=10", "--append=20"]
@p['a'].should == [1,2,3,10,20]
@@ -222,15 +220,15 @@ describe Efl::EcoreGetopt do
args = @p.parse ["progname","--many=ch3"]
@p['m'].should == "ch3"
end
- it "should handle --list-engines" do
- args = @p.parse ["My lovely prog name","--list-engines"]
- @p['E'].should == 1
- end
it "should handle --callback" do
@p['b'].should == 69
args = @p.parse ["progname","--callback=user_arg"]
@p['b'].should == 666
end
+ it "should handle --list-engines" do
+ args = @p.parse ["My lovely prog name","--list-engines"]
+ @p['E'].should == 1
+ end
it "should handle --geometry" do
g = Efl::Native::EinaRectangleStruct.new @p['g']
g[:x].should == 100