[macruby-changes] [2846] MacRuby/trunk/lib/iconv.rb

source_changes at macosforge.org source_changes at macosforge.org
Sat Oct 17 21:15:56 PDT 2009


Revision: 2846
          http://trac.macosforge.org/projects/ruby/changeset/2846
Author:   lsansonetti at apple.com
Date:     2009-10-17 21:15:53 -0700 (Sat, 17 Oct 2009)
Log Message:
-----------
adding dummy pure-ruby iconv implementation

Added Paths:
-----------
    MacRuby/trunk/lib/iconv.rb

Added: MacRuby/trunk/lib/iconv.rb
===================================================================
--- MacRuby/trunk/lib/iconv.rb	                        (rev 0)
+++ MacRuby/trunk/lib/iconv.rb	2009-10-18 04:15:53 UTC (rev 2846)
@@ -0,0 +1,57 @@
+# MacRuby implementation of iconv.
+#
+# This file is covered by the Ruby license. See COPYING for more details.
+# 
+# Copyright (C) 2009, Apple Inc. All rights reserved.
+
+framework 'Foundation'
+
+class Iconv
+  # TODO: lots of things... help!
+
+  def self.iconv(to, from, str)
+    new(from, to).iconv(str)
+  end
+
+  def initialize(to, from, options=nil)
+    @to_enc = ns_encoding(to)
+    @from_enc = ns_encoding(from) 
+  end
+
+  def iconv(str, start=0, length=-1)
+    # Not sure if it's the right thing to do...
+    data = CFStringCreateExternalRepresentation(nil, str, @to_enc, 0)
+    if data.nil?
+      raise "can't retrieve data from `#{str}'"
+    end
+    dest = CFStringCreateFromExternalRepresentation(nil, data, @to_enc)
+    if dest.nil?
+      raise "can't convert data from `#{str}'"
+    end
+    CFRelease(data)
+    CFRelease(dest)
+    dest.mutableCopy 
+  end
+
+  private
+
+  ENCS = {}
+  def ns_encoding(str)
+    if ENCS.empty?
+      # Build database.
+      ptr = CFStringGetListOfAvailableEncodings()
+      i = 0
+      while (enc = ptr[i]) != KCFStringEncodingInvalidId
+        enc_name = CFStringConvertEncodingToIANACharSetName(enc)
+        ENCS[enc_name] = enc
+        i += 1
+      end
+    end
+    str = str.downcase
+    enc = ENCS[str]
+    if enc.nil?
+      raise "unrecognized encoding `#{enc}'"
+    end
+    return enc
+  end
+end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20091017/adeed48e/attachment-0001.html>


More information about the macruby-changes mailing list