In Files

  • mrbgems/mruby-io/mrblib/file.rb
  • mrbgems/mruby-io/mrblib/file_constants.rb
  • mrbgems/mruby-io/test/file.rb

File

Attributes

path[RW]

Public Class Methods

directory?(file) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/file.rb, line 152
def self.directory?(file)
  FileTest.directory?(file)
end
            
exist?(file) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/file.rb, line 156
def self.exist?(file)
  FileTest.exist?(file)
end
            
exists?(file) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/file.rb, line 160
def self.exists?(file)
  FileTest.exists?(file)
end
            
expand_path(path, default_dir = '.') click to toggle source
 
               # File mrbgems/mruby-io/mrblib/file.rb, line 63
def self.expand_path(path, default_dir = '.')
  def concat_path(path, base_path)
    if path[0] == "/" || path[1] == ':' # Windows root!
      expanded_path = path
    elsif path[0] == "~"
      if (path[1] == "/" || path[1] == nil)
        dir = path[1, path.size]
        home_dir = _gethome

        unless home_dir
          raise ArgumentError, "couldn't find HOME environment -- expanding '~'"
        end

        expanded_path = home_dir
        expanded_path += dir if dir
        expanded_path += "/"
      else
        splitted_path = path.split("/")
        user = splitted_path[0][1, splitted_path[0].size]
        dir = "/" + splitted_path[1, splitted_path.size].join("/")

        home_dir = _gethome(user)

        unless home_dir
          raise ArgumentError, "user #{user} doesn't exist"
        end

        expanded_path = home_dir
        expanded_path += dir if dir
        expanded_path += "/"
      end
    else
      expanded_path = concat_path(base_path, _getwd)
      expanded_path += "/" + path
    end

    expanded_path
  end

  expanded_path = concat_path(path, default_dir)
  drive_prefix = ""
  if File::ALT_SEPARATOR && expanded_path.size > 2 &&
      ("A".."Z").include?(expanded_path[0].upcase) && expanded_path[1] == ":"
    drive_prefix = expanded_path[0, 2]
    expanded_path = expanded_path[2, expanded_path.size]
  end
  expand_path_array = []
  if File::ALT_SEPARATOR && expanded_path.include?(File::ALT_SEPARATOR)
    expanded_path.gsub!(File::ALT_SEPARATOR, '/')
  end
  while expanded_path.include?('//')
    expanded_path = expanded_path.gsub('//', '/')
  end

  if expanded_path != "/"
    expanded_path.split('/').each do |path_token|
      if path_token == '..'
        if expand_path_array.size > 1
          expand_path_array.pop
        end
      elsif path_token == '.'
        # nothing to do.
      else
        expand_path_array << path_token
      end
    end

    expanded_path = expand_path_array.join("/")
    if expanded_path.empty?
      expanded_path = '/'
    end
  end
  if drive_prefix.empty?
    expanded_path
  else
    drive_prefix + expanded_path.gsub("/", File::ALT_SEPARATOR)
  end
end
            
extname(filename) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/file.rb, line 192
def self.extname(filename)
  fname = self.basename(filename)
  return '' if fname[0] == '.' || fname.index('.').nil?
  ext = fname.split('.').last
  ext.empty? ? '' : ".#{ext}"
end
            
file?(file) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/file.rb, line 164
def self.file?(file)
  FileTest.file?(file)
end
            
foreach(file) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/file.rb, line 142
def self.foreach(file)
  if block_given?
    self.open(file) do |f|
      f.each {|l| yield l}
    end
  else
    return self.new(file)
  end
end
            
join(*names) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/file.rb, line 19
def self.join(*names)
  return "" if names.empty?

  names.map! do |name|
    case name
    when String
      name
    when Array
      if names == name
        raise ArgumentError, "recursive array"
      end
      join(*name)
    else
      raise TypeError, "no implicit conversion of #{name.class} into String"
    end
  end

  return names[0] if names.size == 1

  if names[0][-1] == File::SEPARATOR
    s = names[0][0..-2]
  else
    s = names[0].dup
  end

  (1..names.size-2).each { |i|
    t = names[i]
    if t[0] == File::SEPARATOR and t[-1] == File::SEPARATOR
      t = t[1..-2]
    elsif t[0] == File::SEPARATOR
      t = t[1..-1]
    elsif t[-1] == File::SEPARATOR
      t = t[0..-2]
    end
    s += File::SEPARATOR + t if t != ""
  }
  if names[-1][0] == File::SEPARATOR
    s += File::SEPARATOR + names[-1][1..-1]
  else
    s += File::SEPARATOR + names[-1]
  end
  s
end
            
new(fd_or_path, mode = "r", perm = 0666) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/file.rb, line 9
def initialize(fd_or_path, mode = "r", perm = 0666)
  if fd_or_path.kind_of? Fixnum
    super(fd_or_path, mode)
  else
    @path = fd_or_path
    fd = IO.sysopen(@path, mode, perm)
    super(fd, mode)
  end
end
            
path(filename) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/file.rb, line 199
def self.path(filename)
  if filename.kind_of?(String)
    filename
  elsif filename.respond_to?(:to_path)
    filename.to_path
  else
    raise TypeError, "no implicit conversion of #{filename.class} into String"
  end
end
            
pipe?(file) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/file.rb, line 168
def self.pipe?(file)
  FileTest.pipe?(file)
end
            
size(file) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/file.rb, line 172
def self.size(file)
  FileTest.size(file)
end
            
size?(file) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/file.rb, line 176
def self.size?(file)
  FileTest.size?(file)
end
            
socket?(file) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/file.rb, line 180
def self.socket?(file)
  FileTest.socket?(file)
end
            
zero?(file) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/file.rb, line 188
def self.zero?(file)
  FileTest.zero?(file)
end
            

Public Instance Methods

concat_path(path, base_path) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/file.rb, line 64
def concat_path(path, base_path)
  if path[0] == "/" || path[1] == ':' # Windows root!
    expanded_path = path
  elsif path[0] == "~"
    if (path[1] == "/" || path[1] == nil)
      dir = path[1, path.size]
      home_dir = _gethome

      unless home_dir
        raise ArgumentError, "couldn't find HOME environment -- expanding '~'"
      end

      expanded_path = home_dir
      expanded_path += dir if dir
      expanded_path += "/"
    else
      splitted_path = path.split("/")
      user = splitted_path[0][1, splitted_path[0].size]
      dir = "/" + splitted_path[1, splitted_path.size].join("/")

      home_dir = _gethome(user)

      unless home_dir
        raise ArgumentError, "user #{user} doesn't exist"
      end

      expanded_path = home_dir
      expanded_path += dir if dir
      expanded_path += "/"
    end
  else
    expanded_path = concat_path(base_path, _getwd)
    expanded_path += "/" + path
  end

  expanded_path
end