Project

General

Profile

Feature #3356 » pathname-shortpath.rb

Monkey patch to pathname - docwhat (Christian Höltje), 05/28/2010 01:42 AM

 
1
##
2
# Pathname monkey-patch -- adds shortpath to Pathname
3
begin
4
  require "Win32API"
5
rescue LoadError
6
  Win32API = nil
7
end
8
require 'pathname'
9

    
10
class Pathname
11
  # Returns a pathname object containing the windows short path.
12
  # In unix, it returns a new pathname object.
13
  def shortpath
14
    if Win32API.nil?
15
      return self.class.new(@path)
16
    else
17
      path = @path
18
      win_func = Win32API.new("kernel32","GetShortPathName","PPL"," L")
19
      max_path = 260
20

    
21
      buf = 0.chr * max_path
22
      buf[0..path.length-1] = path
23
      win_func.call(path, buf, buf.length)
24
      return self.class.new(buf.split(0.chr).first)
25
    end
26
  end
27
end