PageRenderTime 142ms CodeModel.GetById 100ms app.highlight 4ms RepoModel.GetById 36ms app.codeStats 1ms

/tools/Ruby/lib/ruby/1.8/xsd/iconvcharset.rb

http://github.com/agross/netopenspace
Ruby | 33 lines | 19 code | 9 blank | 5 comment | 0 complexity | c19c7052a66551bc95e8b9c209ce06b5 MD5 | raw file
 1# XSD4R - Charset handling with iconv.
 2# Copyright (C) 2003  NAKAMURA, Hiroshi <nahi@ruby-lang.org>.
 3
 4# This program is copyrighted free software by NAKAMURA, Hiroshi.  You can
 5# redistribute it and/or modify it under the same terms of Ruby's license;
 6# either the dual license version in 2003, or any later version.
 7
 8
 9require 'iconv'
10
11
12module XSD
13
14
15class IconvCharset
16  def self.safe_iconv(to, from, str)
17    iconv = Iconv.new(to, from)
18    out = ""
19    begin
20      out << iconv.iconv(str)
21    rescue Iconv::IllegalSequence => e
22      out << e.success
23      ch, str = e.failed.split(//, 2)
24      out << '?'
25      warn("Failed to convert #{ch}")
26      retry
27    end
28    return out
29  end
30end
31
32
33end