i
[chise/ruby.git] / chise / util.rb
index b8e2918..8a34ea7 100644 (file)
@@ -1,20 +1,53 @@
 # Copyright (C) 2002-2004 Kouichirou Eto, All rights reserved.
 
-module CHISE
-  def unix_to_win(unix_path)
-    win = unix_path.gsub(/</, "(")
+require "pathname"
+require "chise/config"
+
+class String
+  def path
+    Pathname.new(self)
+  end
+end
+
+class Pathname
+  def escape # copied from cgi.rb
+    s = @path.gsub(/([\/%]+)/n){
+      "%" + $1.unpack("H2" * $1.size).join("%").upcase
+    }
+    Pathname.new(s)
+  end
+
+  def unescape # copied from cgi.rb
+    s = @path.tr("+", " ").gsub(/((?:%[0-9a-fA-F]{2})+)/n) {
+      [$1.delete("%")].pack("H*")
+    }
+    Pathname.new(s)
+  end
+
+  # translate file name for deal with the restriction of Windows file system.
+  def unix_to_win
+    win = @path.gsub(/</, "(")
     win = win.gsub(/>/, ")")
     win = win.gsub(/\*/, "+")
     win = win.gsub(/\?/, "!")
-    win
+    Pathname.new(win)
   end
 
-  def win_to_unix(win_path)
-    unix = win_path.gsub(/\)/, ">")
+  def win_to_unix
+    unix = @path.gsub(/\)/, ">")
     unix = unix.gsub(/\(/, "<")
     unix = unix.gsub(/\!/, "?")
     unix = unix.gsub(/\+/, "*")
-    unix
+    Pathname.new(unix)
+  end
+
+  def escape_win_filename
+    return self.unix_to_win if CHISE.windows?
+    self
+  end
+
+  def unescape_win_filename
+    return self.win_to_unix if CHISE.windows?
+    self
   end
-  module_function :unix_to_win, :win_to_unix
 end