| 1 |
## |
| 2 |
# IO |
| 3 |
# |
| 4 |
# ISO 15.2.20 |
| 5 |
|
| 6 |
class IOError < StandardError; end |
| 7 |
class EOFError < IOError; end |
| 8 |
|
| 9 |
class IO |
| 10 |
def self.open(*args, &block) |
| 11 |
io = self.new(*args) |
| 12 |
|
| 13 |
return io unless block |
| 14 |
|
| 15 |
begin |
| 16 |
yield io |
| 17 |
ensure |
| 18 |
begin |
| 19 |
io.close unless io.closed? |
| 20 |
rescue StandardError |
| 21 |
end |
| 22 |
end |
| 23 |
end |
| 24 |
|
| 25 |
def self.popen(command, mode = 'r', **opts, &block) |
| 26 |
if !self.respond_to?(:_popen) |
| 27 |
raise NotImplementedError, "popen is not supported on this platform" |
| 28 |
end |
| 29 |
io = self._popen(command, mode, **opts) |
| 30 |
return io unless block |
| 31 |
|
| 32 |
begin |
| 33 |
yield io |
| 34 |
ensure |
| 35 |
begin |
| 36 |
io.close unless io.closed? |
| 37 |
rescue IOError |
| 38 |
# nothing |
| 39 |
end |
| 40 |
end |
| 41 |
end |
| 42 |
|
| 43 |
def self.pipe(&block) |
| 44 |
if !self.respond_to?(:_pipe) |
| 45 |
raise NotImplementedError, "pipe is not supported on this platform" |
| 46 |
end |
| 47 |
if block |
| 48 |
begin |
| 49 |
r, w = IO._pipe |
| 50 |
yield r, w |
| 51 |
ensure |
| 52 |
r.close unless r.closed? |
| 53 |
w.close unless w.closed? |
| 54 |
end |
| 55 |
else |
| 56 |
IO._pipe |
| 57 |
end |
| 58 |
end |
| 59 |
|
| 60 |
def self.read(path, length=nil, offset=0, mode: "r") |
| 61 |
str = "" |
| 62 |
fd = -1 |
| 63 |
io = nil |
| 64 |
begin |
| 65 |
fd = IO.sysopen(path, mode) |
| 66 |
io = IO.open(fd, mode) |
| 67 |
io.seek(offset) if offset > 0 |
| 68 |
str = io.read(length) |
| 69 |
ensure |
| 70 |
if io |
| 71 |
io.close |
| 72 |
elsif fd != -1 |
| 73 |
IO._sysclose(fd) |
| 74 |
end |
| 75 |
end |
| 76 |
str |
| 77 |
end |
| 78 |
|
| 79 |
def hash |
| 80 |
# We must define IO#hash here because IO includes Enumerable and |
| 81 |
# Enumerable#hash will call IO#read() otherwise |
| 82 |
self.__id__ |
| 83 |
end |
| 84 |
|
| 85 |
def <<(str) |
| 86 |
write(str) |
| 87 |
self |
| 88 |
end |
| 89 |
|
| 90 |
alias_method :eof, :eof? |
| 91 |
alias_method :tell, :pos |
| 92 |
|
| 93 |
def pos=(i) |
| 94 |
seek(i, SEEK_SET) |
| 95 |
end |
| 96 |
|
| 97 |
def rewind |
| 98 |
seek(0, SEEK_SET) |
| 99 |
end |
| 100 |
|
| 101 |
def ungetbyte(c) |
| 102 |
if c.is_a? String |
| 103 |
c = c.getbyte(0) |
| 104 |
else |
| 105 |
c &= 0xff |
| 106 |
end |
| 107 |
s = " " |
| 108 |
s.setbyte(0,c) |
| 109 |
ungetc s |
| 110 |
end |
| 111 |
|
| 112 |
# 15.2.20.5.3 |
| 113 |
def each(&block) |
| 114 |
return to_enum unless block |
| 115 |
|
| 116 |
while line = self.gets |
| 117 |
block.call(line) |
| 118 |
end |
| 119 |
self |
| 120 |
end |
| 121 |
|
| 122 |
# 15.2.20.5.4 |
| 123 |
def each_byte(&block) |
| 124 |
return to_enum(:each_byte) unless block |
| 125 |
|
| 126 |
while byte = self.getbyte |
| 127 |
block.call(byte) |
| 128 |
end |
| 129 |
self |
| 130 |
end |
| 131 |
|
| 132 |
# 15.2.20.5.5 |
| 133 |
alias each_line each |
| 134 |
|
| 135 |
def each_char(&block) |
| 136 |
return to_enum(:each_char) unless block |
| 137 |
|
| 138 |
while char = self.getc |
| 139 |
block.call(char) |
| 140 |
end |
| 141 |
self |
| 142 |
end |
| 143 |
|
| 144 |
def puts(*args) |
| 145 |
i = 0 |
| 146 |
len = args.size |
| 147 |
while i < len |
| 148 |
s = args[i] |
| 149 |
if s.kind_of?(Array) |
| 150 |
puts(*s) |
| 151 |
else |
| 152 |
s = s.to_s |
| 153 |
write s |
| 154 |
write "\n" if (s[-1] != "\n") |
| 155 |
end |
| 156 |
i += 1 |
| 157 |
end |
| 158 |
write "\n" if len == 0 |
| 159 |
nil |
| 160 |
end |
| 161 |
|
| 162 |
def print(*args) |
| 163 |
i = 0 |
| 164 |
len = args.size |
| 165 |
while i < len |
| 166 |
write args[i].to_s |
| 167 |
i += 1 |
| 168 |
end |
| 169 |
end |
| 170 |
|
| 171 |
def printf(*args) |
| 172 |
write sprintf(*args) |
| 173 |
nil |
| 174 |
end |
| 175 |
|
| 176 |
alias_method :to_i, :fileno |
| 177 |
alias_method :tty?, :isatty |
| 178 |
end |
| 179 |
|
| 180 |
STDIN = IO.open(0, "r") |
| 181 |
STDOUT = IO.open(1, "w") |
| 182 |
STDERR = IO.open(2, "w") |
| 183 |
|
| 184 |
$stdin = STDIN |
| 185 |
$stdout = STDOUT |
| 186 |
$stderr = STDERR |