In Files

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

IO

Public Class Methods

open(*args, &block) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/io.rb, line 14
def self.open(*args, &block)
  io = self.new(*args)

  return io unless block

  begin
    yield io
  ensure
    begin
      io.close unless io.closed?
    rescue StandardError
    end
  end
end
            
pipe(&block) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/io.rb, line 47
def self.pipe(&block)
  if !self.respond_to?(:_pipe)
    raise NotImplementedError, "pipe is not supported on this platform"
  end
  if block
    begin
      r, w = IO._pipe
      yield r, w
    ensure
      r.close unless r.closed?
      w.close unless w.closed?
    end
  else
    IO._pipe
  end
end
            
popen(command, mode = 'r', opts={}, &block) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/io.rb, line 29
def self.popen(command, mode = 'r', opts={}, &block)
  if !self.respond_to?(:_popen)
    raise NotImplementedError, "popen is not supported on this platform"
  end
  io = self._popen(command, mode, opts)
  return io unless block

  begin
    yield io
  ensure
    begin
      io.close unless io.closed?
    rescue IOError
      # nothing
    end
  end
end
            
read(path, length=nil, offset=nil, opt=nil) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/io.rb, line 64
def self.read(path, length=nil, offset=nil, opt=nil)
  if not opt.nil?        # 4 arguments
    offset ||= 0
  elsif not offset.nil?  # 3 arguments
    if offset.is_a? Hash
      opt = offset
      offset = 0
    else
      opt = {}
    end
  elsif not length.nil?  # 2 arguments
    if length.is_a? Hash
      opt = length
      offset = 0
      length = nil
    else
      offset = 0
      opt = {}
    end
  else                   # only 1 argument
    opt = {}
    offset = 0
    length = nil
  end

  str = ""
  fd = -1
  io = nil
  begin
    if path[0] == "|"
      io = IO.popen(path[1..-1], (opt[:mode] || "r"))
    else
      mode = opt[:mode] || "r"
      fd = IO.sysopen(path, mode)
      io = IO.open(fd, mode)
    end
    io.seek(offset) if offset > 0
    str = io.read(length)
  ensure
    if io
      io.close
    elsif fd != -1
      IO._sysclose(fd)
    end
  end
  str
end
            

Public Instance Methods

<<(str) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/io.rb, line 135
def <<(str)
  write(str)
  self
end
            
_read_buf() click to toggle source
 
               # File mrbgems/mruby-io/mrblib/io.rb, line 172
def _read_buf
  return @buf if @buf && @buf.bytesize >= 4 # maximum UTF-8 character is 4 bytes
  @buf ||= ""
  begin
    @buf += sysread(BUF_SIZE)
  rescue EOFError => e
    raise e if @buf.empty?
  end
end
            
each(&block) click to toggle source

15.2.20.5.3

 
               # File mrbgems/mruby-io/mrblib/io.rb, line 305
def each(&block)
  while line = self.gets
    block.call(line)
  end
  self
end
            
Also aliased as: each_line
each_byte(&block) click to toggle source

15.2.20.5.4

 
               # File mrbgems/mruby-io/mrblib/io.rb, line 313
def each_byte(&block)
  while char = self.getc
    block.call(char)
  end
  self
end
            
Also aliased as: each_char
each_char(&block) click to toggle source
Alias for: each_byte
each_line(&block) click to toggle source

15.2.20.5.5

Alias for: each
eof() click to toggle source
Alias for: eof?
eof?() click to toggle source
 
               # File mrbgems/mruby-io/mrblib/io.rb, line 140
def eof?
  _check_readable
  begin
    _read_buf
    return @buf.empty?
  rescue EOFError
    return true
  end
end
            
Also aliased as: eof
flush() click to toggle source
 
               # File mrbgems/mruby-io/mrblib/io.rb, line 112
def flush
  # mruby-io always writes immediately (no output buffer).
  raise IOError, "closed stream" if self.closed?
  self
end
            
getc() click to toggle source
 
               # File mrbgems/mruby-io/mrblib/io.rb, line 296
def getc
  begin
    readchar
  rescue EOFError
    nil
  end
end
            
gets(*args) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/io.rb, line 281
def gets(*args)
  begin
    readline(*args)
  rescue EOFError
    nil
  end
end
            
hash() click to toggle source
 
               # File mrbgems/mruby-io/mrblib/io.rb, line 118
def hash
  # We must define IO#hash here because IO includes Enumerable and
  # Enumerable#hash will call IO#read...
  self.__id__
end
            
pos() click to toggle source
 
               # File mrbgems/mruby-io/mrblib/io.rb, line 151
def pos
  raise IOError if closed?
  sysseek(0, SEEK_CUR) - @buf.bytesize
end
            
Also aliased as: tell
pos=(i) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/io.rb, line 157
def pos=(i)
  seek(i, SEEK_SET)
end
            
printf(*args) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/io.rb, line 355
def printf(*args)
  write sprintf(*args)
  nil
end
            
puts(*args) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/io.rb, line 333
def puts(*args)
  i = 0
  len = args.size
  while i < len
    s = args[i].to_s
    write s
    write "\n" if (s[-1] != "\n")
    i += 1
  end
  write "\n" if len == 0
  nil
end
            
read(length = nil, outbuf = "") click to toggle source
 
               # File mrbgems/mruby-io/mrblib/io.rb, line 192
def read(length = nil, outbuf = "")
  unless length.nil?
    unless length.is_a? Fixnum
      raise TypeError.new "can't convert #{length.class} into Integer"
    end
    if length < 0
      raise ArgumentError.new "negative length: #{length} given"
    end
    if length == 0
      return ""   # easy case
    end
  end

  array = []
  while 1
    begin
      _read_buf
    rescue EOFError
      array = nil if array.empty? and (not length.nil?) and length != 0
      break
    end

    if length
      consume = (length <= @buf.bytesize) ? length : @buf.bytesize
      array.push IO._bufread(@buf, consume)
      length -= consume
      break if length == 0
    else
      array.push @buf
      @buf = ''
    end
  end

  if array.nil?
    outbuf.replace("")
    nil
  else
    outbuf.replace(array.join)
  end
end
            
readchar() click to toggle source
 
               # File mrbgems/mruby-io/mrblib/io.rb, line 289
def readchar
  _read_buf
  c = @buf[0]
  @buf[0] = ""
  c
end
            
readline(arg = "\n", limit = nil) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/io.rb, line 233
def readline(arg = "\n", limit = nil)
  case arg
  when String
    rs = arg
  when Fixnum
    rs = "\n"
    limit = arg
  else
    raise ArgumentError
  end

  if rs.nil?
    return read
  end

  if rs == ""
    rs = "\n\n"
  end

  array = []
  while 1
    begin
      _read_buf
    rescue EOFError
      array = nil if array.empty?
      break
    end

    if limit && limit <= @buf.size
      array.push @buf[0, limit]
      @buf[0, limit] = ""
      break
    elsif idx = @buf.index(rs)
      len = idx + rs.size
      array.push @buf[0, len]
      @buf[0, len] = ""
      break
    else
      array.push @buf
      @buf = ''
    end
  end

  raise EOFError.new "end of file reached" if array.nil?

  array.join
end
            
readlines() click to toggle source
 
               # File mrbgems/mruby-io/mrblib/io.rb, line 325
def readlines
  ary = []
  while (line = gets)
    ary << line
  end
  ary
end
            
rewind() click to toggle source
 
               # File mrbgems/mruby-io/mrblib/io.rb, line 161
def rewind
  seek(0, SEEK_SET)
end
            
seek(i, whence = SEEK_SET) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/io.rb, line 165
def seek(i, whence = SEEK_SET)
  raise IOError if closed?
  sysseek(i, whence)
  @buf = ''
  0
end
            
tell() click to toggle source
Alias for: pos
ungetc(substr) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/io.rb, line 182
def ungetc(substr)
  raise TypeError.new "expect String, got #{substr.class}" unless substr.is_a?(String)
  if @buf.empty?
    @buf = substr.dup
  else
    @buf = substr + @buf
  end
  nil
end
            
write(string) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/io.rb, line 124
def write(string)
  str = string.is_a?(String) ? string : string.to_s
  return 0 if str.empty?
  unless @buf.empty?
    # reset real pos ignore buf
    seek(pos, SEEK_SET)
  end
  len = syswrite(str)
  len
end