root/platform/tdiary/util/convert_pstore.rb

Revision 14973, 1.3 kB (checked in by hsbt, 5 months ago)

add migrate_to_utf8.

Line 
1#
2# convert utf-8 in pstore.
3#
4# usage: convert_pstore.rb file1
5#
6
7$KCODE = 'u'
8
9require 'nkf'
10require "pstore"
11begin
12        require "iconv"
13rescue LoadError
14end
15
16def convert_pstore( file )
17        db = PStore.new( file )
18        begin
19                roots = db.transaction{ db.roots }
20        rescue ArgumentError
21                if /\Aundefined class\/module (.+?)(::)?\z/ =~ $!.message
22                        klass = $1
23                        if /EmptdiaryString\z/ =~ klass
24                                eval( "class #{klass} < String; end" )
25                        else
26                                eval( "class #{ klass}; end" )
27                        end
28                        retry
29                end
30        end
31        db.transaction do
32                roots.each do |root|
33                        convert_element( db[root] )
34                end
35        end
36end
37
38def convert_element( data )
39        case data
40        when Hash, Array
41                data.each_with_index do |e, i|
42                        if String === e
43                                data[i] = migrate_to_utf8( e )
44                        else
45                                convert_element( e )
46                        end
47                end
48        else
49                data.instance_variables.each do |e|
50                        var = data.instance_variable_get( e )
51                        if String === var
52                                data.instance_variable_set( e, migrate_to_utf8( var ) )
53                        else
54                                convert_element( var )
55                        end
56                end
57        end
58end
59
60def migrate_to_utf8( str )
61        to_native( str, 'EUC-JP' )
62end
63
64def to_native( str, charset = nil )
65        begin
66                Iconv.conv('utf-8', charset || 'utf-8', str)
67        rescue
68                from = case charset
69                        when /^utf-8$/i
70                                'W'
71                        when /^shift_jis/i
72                                'S'
73                        when /^EUC-JP/i
74                                'E'
75                        else
76                                ''
77                end
78                NKF::nkf("-m0 -#{from}w", str)
79        end
80end
81
82convert_pstore( ARGV[0] )
Note: See TracBrowser for help on using the browser.