convert encoding if defined Phi::DELPHI_ENCODING.
@@ -0,0 +1,30 @@ | ||
1 | +#!/usr/bin/ruby | |
2 | +require 'phi' | |
3 | + | |
4 | +require 'iconv' | |
5 | +module Phi | |
6 | + DELPHI_ENCODING = 'Shift_JIS' | |
7 | + RUBY_ENCODING = 'UTF-8' | |
8 | + RubyDelphiCodeConverter = Iconv.new(DELPHI_ENCODING, RUBY_ENCODING) | |
9 | + DelphiRubyCodeConverter = Iconv.new(RUBY_ENCODING, DELPHI_ENCODING) | |
10 | +module_function | |
11 | + def convert_encoding_from_ruby_to_delphi(str) | |
12 | + p [:ruby_to_delphi, str] | |
13 | + RubyDelphiCodeConverter.iconv(str) | |
14 | + end | |
15 | + def convert_encoding_from_delphi_to_ruby(str) | |
16 | + p [:delphi_to_ruby, str] | |
17 | + DelphiRubyCodeConverter.iconv(str) | |
18 | + end | |
19 | +end | |
20 | +# file encoding: UTF-8 | |
21 | +str = 'おはよう' # ohayou | |
22 | +p str | |
23 | +# delphi_str = Phi.convert_string_from_ruby_to_delphi(str) | |
24 | +# p delphi_str | |
25 | +form=Phi::Form.new(str) | |
26 | +form.show | |
27 | +caption = form.caption | |
28 | +# ruby_caption = Phi.convert_string_from_delphi_to_ruby(caption) | |
29 | +p caption | |
30 | +Phi.mainloop |
@@ -107,7 +107,7 @@ | ||
107 | 107 | |
108 | 108 | function ap_const_get(klass: Tvalue; const name: String): Tvalue; |
109 | 109 | begin |
110 | - result := rb_const_get(klass,rb_intern(PChar(name))); | |
110 | + result := rb_const_get(klass, rb_intern(PChar(name))); | |
111 | 111 | end; |
112 | 112 | |
113 | 113 | procedure ap_const_set(klass: Tvalue; const name: String; val: Tvalue); |
@@ -29,6 +29,8 @@ | ||
29 | 29 | function dl_Integer(v: Tvalue): Integer; |
30 | 30 | function ap_Float(v: Double): Tvalue; |
31 | 31 | function dl_Double(v: Tvalue): Double; |
32 | +function ap_String_0(S: string): Tvalue; | |
33 | +function dl_String_0(v: Tvalue): string; | |
32 | 34 | function ap_String(S: string): Tvalue; |
33 | 35 | function dl_String(v: Tvalue): string; |
34 | 36 | function ap_Variant(A: Variant): Tvalue; |
@@ -54,7 +56,7 @@ | ||
54 | 56 | |
55 | 57 | implementation |
56 | 58 | |
57 | -uses uDateTime; | |
59 | +uses uDateTime, uPhi; | |
58 | 60 | |
59 | 61 | function ap_Handle(h: THandle): Tvalue; |
60 | 62 | begin |
@@ -103,14 +105,44 @@ | ||
103 | 105 | result := rb_num2dbl(v); |
104 | 106 | end; |
105 | 107 | |
108 | +function ap_String_0(S: string): Tvalue; | |
109 | +begin | |
110 | + result := rb_str_new(PChar(S), length(S)); | |
111 | +end; | |
112 | + | |
113 | +function dl_String_0(v: Tvalue): string; | |
114 | +begin | |
115 | + Check_Type(v, T_STRING); | |
116 | + SetString(result, ap_str_ptr(v), ap_str_len(v)); | |
117 | +end; | |
118 | + | |
119 | +function EnableCodeConv: Boolean; | |
120 | +begin | |
121 | + result := rb_const_defined(mPhi, rb_intern('DELPHI_ENCODING')) <> 0; | |
122 | +end; | |
123 | + | |
124 | +function ConvertEncodingFromDelphiToRuby(v: Tvalue): Tvalue; | |
125 | +begin | |
126 | + result := rb_funcall2(mPhi, rb_intern('convert_encoding_from_delphi_to_ruby'), 1, @v); | |
127 | +end; | |
128 | + | |
129 | +function ConvertEncodingFromRubyToDelphi(v: Tvalue): Tvalue; | |
130 | +begin | |
131 | + result := rb_funcall2(mPhi, rb_intern('convert_encoding_from_ruby_to_delphi'), 1, @v); | |
132 | +end; | |
133 | + | |
106 | 134 | function ap_String(S: string): Tvalue; |
107 | 135 | begin |
108 | 136 | result := rb_str_new(PChar(S), length(S)); |
137 | + if EnableCodeConv then | |
138 | + result := ConvertEncodingFromDelphiToRuby(result); | |
109 | 139 | end; |
110 | 140 | |
111 | 141 | function dl_String(v: Tvalue): string; |
112 | 142 | begin |
113 | 143 | Check_Type(v, T_STRING); |
144 | + if EnableCodeConv then | |
145 | + v := ConvertEncodingFromRubyToDelphi(v); | |
114 | 146 | SetString(result, ap_str_ptr(v), ap_str_len(v)); |
115 | 147 | end; |
116 | 148 |
@@ -49,7 +49,7 @@ | ||
49 | 49 | begin |
50 | 50 | if RTYPE(str) <> T_STRING then str := rb_obj_as_string(str); { to_s } |
51 | 51 | len := ap_str_len(str); |
52 | - if len <> 0 then Stdout(dl_String(str)); | |
52 | + if len <> 0 then Stdout(dl_String_0(str)); | |
53 | 53 | end; |
54 | 54 | result := INT2FIX(len); |
55 | 55 | end; |
@@ -221,7 +221,7 @@ | ||
221 | 221 | name := last_func_name; |
222 | 222 | real_name := trimUnder(name); |
223 | 223 | S := GetStrProp(real, trimUnder(name)); |
224 | - result := rb_str_new2(PChar(S)); | |
224 | + result := ap_String(S); | |
225 | 225 | end; |
226 | 226 | |
227 | 227 | function Prop_set_path(This, v: Tvalue): Tvalue; cdecl; |