From bc8b42fc1eb3f1adf9d170dc353a33f53bdab92f Mon Sep 17 00:00:00 2001 From: nagai Date: Thu, 31 Jul 2003 23:04:45 +0000 Subject: [PATCH] * bug fix : wrong resource file format (resource.{en,jp}) * add Tk::Encoding.{encoding_convertfrom, encoding_convertto} * add TkOptionDB.read_with_encoding to read non-utf8 resource file git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4250 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/tk/lib/multi-tk.rb | 2 -- ext/tk/lib/tk.rb | 66 +++++++++++++++++++++++++++++++++++++-- ext/tk/sample/resource.en | 6 ++-- ext/tk/sample/resource.ja | 6 ++-- ext/tk/sample/tkoptdb.rb | 4 ++- 5 files changed, 72 insertions(+), 12 deletions(-) diff --git a/ext/tk/lib/multi-tk.rb b/ext/tk/lib/multi-tk.rb index af94204252..c74b5714b8 100644 --- a/ext/tk/lib/multi-tk.rb +++ b/ext/tk/lib/multi-tk.rb @@ -1155,8 +1155,6 @@ class MultiTkIp # from tkencoding.rb by ttate@jaist.ac.jp alias __eval _eval alias __invoke _invoke - private :__eval - private :__invoke def encoding @encoding[0] diff --git a/ext/tk/lib/tk.rb b/ext/tk/lib/tk.rb index 0a9aacaaa5..d22f7b43d7 100644 --- a/ext/tk/lib/tk.rb +++ b/ext/tk/lib/tk.rb @@ -1509,8 +1509,6 @@ if /^8\.[1-9]/ =~ Tk::TCL_VERSION && !Tk::JAPANIZED_TK # from tkencoding.rb by ttate@jaist.ac.jp alias __eval _eval alias __invoke _invoke - private :__eval - private :__invoke attr_accessor :encoding @@ -1521,7 +1519,7 @@ if /^8\.[1-9]/ =~ Tk::TCL_VERSION && !Tk::JAPANIZED_TK __eval(cmd) end end - + def _invoke(*cmds) if defined? @encoding cmds = cmds.collect{|cmd| _toUTF8(cmd, @encoding)} @@ -1557,6 +1555,16 @@ if /^8\.[1-9]/ =~ Tk::TCL_VERSION && !Tk::JAPANIZED_TK def encoding_system=(enc) tk_call('encoding', 'system', enc) end + + def encoding_convertfrom(str, enc=None) + TkCore::INTERP.__invoke('encoding', 'convertfrom', enc, str) + end + alias encoding_convert_from encoding_convertfrom + + def encoding_convertto(str, enc=None) + TkCore::INTERP.__invoke('encoding', 'convertto', enc, str) + end + alias encoding_convert_to encoding_convertto end extend Encoding @@ -1580,6 +1588,11 @@ if /^8\.[1-9]/ =~ Tk::TCL_VERSION && !Tk::JAPANIZED_TK else # dummy methods + class TclTkIp + alias __eval _eval + alias __invoke _invoke + end + module Tk module Encoding extend Encoding @@ -1599,6 +1612,16 @@ else def encoding_system=(enc) nil end + + def encoding_convertfrom(str, enc=None) + str + end + alias encoding_convert_from encoding_convertfrom + + def encoding_convertto(str, enc=None) + str + end + alias encoding_convert_to encoding_convertto end extend Encoding @@ -2848,6 +2871,43 @@ module TkOptionDB tk_call 'option', 'readfile', file, pri end module_function :add, :clear, :get, :readfile + + def read_with_encoding(file, f_enc=nil, pri=None) + i_enc = Tk.encoding() + + unless f_enc + f_enc = i_enc + end + + cline = '' + open(file, 'r') {|f| + while line = f.gets + cline += line.chomp! + case cline + when /\\$/ # continue + cline.chop! + next + when /^!/ # coment + cline = '' + next + when /^([^:]+):\s(.*)$/ + pat = $1 + val = $2 + p "ResourceDB: #{[pat, val].inspect}" if $DEBUG + pat = TkCore::INTERP._toUTF8(pat, f_enc) + pat = TkCore::INTERP._fromUTF8(pat, i_enc) + val = TkCore::INTERP._toUTF8(val, f_enc) + val = TkCore::INTERP._fromUTF8(val, i_enc) + add(pat, val, pri) + cline = '' + else # unknown --> ignore + cline = '' + next + end + end + } + end + module_function :read_with_encoding # support procs on the resource database @@resource_proc_class = Class.new diff --git a/ext/tk/sample/resource.en b/ext/tk/sample/resource.en index 29bd587ea2..bfdc809278 100644 --- a/ext/tk/sample/resource.en +++ b/ext/tk/sample/resource.en @@ -1,6 +1,6 @@ -# -# see Tcl/Tk's "options" manual for "Database Name" and "Database Class" -# +! +! see Tcl/Tk's "options" manual for "Database Name" and "Database Class" +! *BtnFrame.borderWidth: 5 *BtnFrame.relief: ridge *BtnFrame.Button.background: wheat diff --git a/ext/tk/sample/resource.ja b/ext/tk/sample/resource.ja index 61098db4f8..8b715f36b0 100644 --- a/ext/tk/sample/resource.ja +++ b/ext/tk/sample/resource.ja @@ -1,6 +1,6 @@ -# -# see Tcl/Tk's "options" manual for "Database Name" and "Database Class" -# +! +! see Tcl/Tk's "options" manual for "Database Name" and "Database Class" +! *BtnFrame.borderWidth: 5 *BtnFrame.relief: ridge *BtnFrame.Button.background: wheat diff --git a/ext/tk/sample/tkoptdb.rb b/ext/tk/sample/tkoptdb.rb index 066bd9501a..6cb3d17993 100644 --- a/ext/tk/sample/tkoptdb.rb +++ b/ext/tk/sample/tkoptdb.rb @@ -10,7 +10,9 @@ require "tk" if ENV['LANG'] =~ /^ja/ # read Japanese resource - TkOptionDB.readfile(File.expand_path('resource.ja', File.dirname(__FILE__))) + TkOptionDB.read_with_encoding(File.expand_path('resource.ja', + File.dirname(__FILE__)), + 'euc-jp') else # read English resource TkOptionDB.readfile(File.expand_path('resource.en', File.dirname(__FILE__)))