4 # Platform specific behavior for the File class.
11 def self.dirname(path)
12 if(!path.match(/#{SEPARATOR}/) || path.match(/^\.+#{SEPARATOR}+$/))
15 # strip the basename off the end and clean up the ending separators
16 path = path.sub(/#{SEPARATOR}*[^#{SEPARATOR}]*#{SEPARATOR}*$/,'')
25 def self.basename(path,ext)
26 path.gsub!(/([^#{SEPARATOR}])#{SEPARATOR}\z/, "\\1")
27 basename = if(m = path.match(/#{SEPARATOR}+([^#{SEPARATOR}]*)#{SEPARATOR}*$/))
28 m[1] == '' ? SEPARATOR : m[1]
32 ext = Regexp.quote(ext) # convert everything to literal characters
33 ext.sub!(/\\\*/,'[^\.]+?') # convert .*'s back into a general match expression
34 return basename.sub(/#{ext}$/,'')
37 # FIXME: this is awful
38 def self.expand_path(path, dir_string = nil)
39 path = StringValue(path)
43 dir_string = StringValue(dir_string)
46 path.gsub!(/~(#{ENV['USER']})/, "~/")
48 raise ArgumentError, "user #{path}" if path.match(/~([^\/])/)
52 elsif(dir_string[0].chr == '~')
53 dir_string = ENV['HOME'] + dir_string[1..-1]
54 elsif(dir_string[0].chr != '/')
55 dir_string = Dir.pwd + "/" + dir_string
58 dirs = path.split('/')
59 if path == '' || (dirs.empty? && path[0].chr != '/')
62 first = case dirs.first
63 when '..'; dir_string.split('/')[0...-1].join('/')
68 match = /(\/+)/.match(path)
69 prefix = match[0] if match
72 dir_string + '/' + dirs.first
76 paths = first.split('/')
78 next if dir == '.' || dir == ''
79 dir == '..' ? paths.pop : paths.push(dir)
81 string = paths.empty? ? '' : paths.join("/")
82 return !string.empty? && string[0].chr == '/' ? string : prefix || '/' +string