Changeset 14973 for platform/tdiary/util

Show
Ignore:
Timestamp:
07/01/08 07:11:12 (5 months ago)
Author:
hsbt
Message:

add migrate_to_utf8.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • platform/tdiary/util/convert_pstore.rb

    r14886 r14973  
    22# convert utf-8 in pstore. 
    33# 
     4# usage: convert_pstore.rb file1 
     5# 
     6 
     7$KCODE = 'u' 
     8 
     9require 'nkf' 
     10require "pstore" 
     11begin 
     12        require "iconv" 
     13rescue LoadError 
     14end 
    415 
    516def convert_pstore( file ) 
    6         require "pstore" 
    7  
    817        db = PStore.new( file ) 
    918        begin 
     
    3241                data.each_with_index do |e, i| 
    3342                        if String === e 
    34                                 data[i] = @conf.migrate_to_utf8( e ) 
     43                                data[i] = migrate_to_utf8( e ) 
    3544                        else 
    3645                                convert_element( e ) 
     
    4150                        var = data.instance_variable_get( e ) 
    4251                        if String === var 
    43                                 data.instance_variable_set( e, @conf.migrate_to_utf8( var ) ) 
     52                                data.instance_variable_set( e, migrate_to_utf8( var ) ) 
    4453                        else 
    4554                                convert_element( var ) 
     
    4958end 
    5059 
    51 convert_pstore(ARGV[0]) 
     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] )