summaryrefslogtreecommitdiffhomepage
path: root/mrblib
diff options
context:
space:
mode:
authorPaolo Bosetti <[email protected]>2014-03-03 13:15:57 +0100
committerPaolo Bosetti <[email protected]>2014-03-03 13:15:57 +0100
commitd9f3fd868a1c4778dbd7119a00c79ef020dc6048 (patch)
treee81ddb23d69030748e524148427fee7a5ba22f56 /mrblib
parentee2859de6e8c95335db65746775737ad1bff35c8 (diff)
parentf7c054bd39f0809641ab474771f0f85a5282c72e (diff)
downloadmruby-d9f3fd868a1c4778dbd7119a00c79ef020dc6048.tar.gz
mruby-d9f3fd868a1c4778dbd7119a00c79ef020dc6048.zip
Merge branch 'master' of https://github.com/iij/mruby-io into test
Conflicts: src/io.c
Diffstat (limited to 'mrblib')
-rw-r--r--mrblib/file.rb1
-rw-r--r--mrblib/io.rb55
2 files changed, 54 insertions, 2 deletions
diff --git a/mrblib/file.rb b/mrblib/file.rb
index aaa88616f..4087593a5 100644
--- a/mrblib/file.rb
+++ b/mrblib/file.rb
@@ -9,7 +9,6 @@ class File < IO
attr_accessor :path
def initialize(fd_or_path, mode = "r", perm = 0666)
- self._bless
if fd_or_path.kind_of? Fixnum
super(fd_or_path, mode)
else
diff --git a/mrblib/io.rb b/mrblib/io.rb
index ca687aee0..4c69a4b99 100644
--- a/mrblib/io.rb
+++ b/mrblib/io.rb
@@ -41,6 +41,59 @@ class IO
end
end
+
+ 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
+ fd = IO.sysopen(path)
+ io = IO.open(fd, opt[:mode] || "r")
+ 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
+
+ def flush
+ # mruby-io always writes immediately (no output buffer).
+ self
+ end
+
def write(string)
str = string.is_a?(String) ? string : string.to_s
return str.size unless str.size > 0
@@ -125,7 +178,7 @@ class IO
begin
_read_buf
rescue EOFError => e
- str = nil if str.empty?
+ str = nil if str.empty? and (not length.nil?) and length != 0
break
end