Class: IO
- Defined in:
- ext/enterprise_script_service/mruby/mrbgems/mruby-io/mrblib/io.rb
Direct Known Subclasses
Constant Summary collapse
- SEEK_SET =
0
- SEEK_CUR =
1
- SEEK_END =
2
- BUF_SIZE =
4096
Class Method Summary collapse
- .open(*args, &block) ⇒ Object
- .pipe(&block) ⇒ Object
- .popen(command, mode = 'r', opts = {}, &block) ⇒ Object
- .read(path, length = nil, offset = nil, opt = nil) ⇒ Object
Instance Method Summary collapse
- #<<(str) ⇒ Object
- #_read_buf ⇒ Object
-
#each(&block) ⇒ Object
(also: #each_line)
15.2.20.5.3.
-
#each_byte(&block) ⇒ Object
(also: #each_char)
15.2.20.5.4.
- #eof? ⇒ Boolean (also: #eof)
- #flush ⇒ Object
- #getc ⇒ Object
- #gets(*args) ⇒ Object
- #hash ⇒ Object
- #pos ⇒ Object (also: #tell)
- #pos=(i) ⇒ Object
- #print(*args) ⇒ Object
- #printf(*args) ⇒ Object
- #puts(*args) ⇒ Object
- #read(length = nil, outbuf = "") ⇒ Object
- #readchar ⇒ Object
- #readline(arg = "\n", limit = nil) ⇒ Object
- #readlines ⇒ Object
- #rewind ⇒ Object
- #seek(i, whence = SEEK_SET) ⇒ Object
- #ungetc(substr) ⇒ Object
- #write(string) ⇒ Object
Class Method Details
.open(*args, &block) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'ext/enterprise_script_service/mruby/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) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'ext/enterprise_script_service/mruby/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) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'ext/enterprise_script_service/mruby/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) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'ext/enterprise_script_service/mruby/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 |
Instance Method Details
#<<(str) ⇒ Object
135 136 137 138 |
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-io/mrblib/io.rb', line 135 def <<(str) write(str) self end |
#_read_buf ⇒ Object
172 173 174 175 |
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-io/mrblib/io.rb', line 172 def _read_buf return @buf if @buf && @buf.bytesize > 0 sysread(BUF_SIZE, @buf) end |
#each(&block) ⇒ Object Also known as: each_line
15.2.20.5.3
300 301 302 303 304 305 306 307 |
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-io/mrblib/io.rb', line 300 def each(&block) return to_enum unless block while line = self.gets block.call(line) end self end |
#each_byte(&block) ⇒ Object Also known as: each_char
15.2.20.5.4
310 311 312 313 314 315 316 317 |
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-io/mrblib/io.rb', line 310 def each_byte(&block) return to_enum(:each_byte) unless block while char = self.getc block.call(char) end self end |
#eof? ⇒ Boolean Also known as: eof
140 141 142 143 144 145 146 147 148 |
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-io/mrblib/io.rb', line 140 def eof? _check_readable begin _read_buf return @buf.empty? rescue EOFError return true end end |
#flush ⇒ Object
112 113 114 115 116 |
# File 'ext/enterprise_script_service/mruby/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 ⇒ Object
289 290 291 292 293 294 295 296 297 |
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-io/mrblib/io.rb', line 289 def getc begin readchar rescue EOFError c = @buf[0] @buf[0,1]="" if c nil end end |
#gets(*args) ⇒ Object
276 277 278 279 280 281 282 |
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-io/mrblib/io.rb', line 276 def gets(*args) begin readline(*args) rescue EOFError nil end end |
#hash ⇒ Object
118 119 120 121 122 |
# File 'ext/enterprise_script_service/mruby/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 ⇒ Object Also known as: tell
151 152 153 154 |
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-io/mrblib/io.rb', line 151 def pos raise IOError if closed? sysseek(0, SEEK_CUR) - @buf.bytesize end |
#pos=(i) ⇒ Object
157 158 159 |
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-io/mrblib/io.rb', line 157 def pos=(i) seek(i, SEEK_SET) end |
#print(*args) ⇒ Object
345 346 347 348 349 350 351 352 |
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-io/mrblib/io.rb', line 345 def print(*args) i = 0 len = args.size while i < len write args[i].to_s i += 1 end end |
#printf(*args) ⇒ Object
354 355 356 357 |
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-io/mrblib/io.rb', line 354 def printf(*args) write sprintf(*args) nil end |
#puts(*args) ⇒ Object
332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-io/mrblib/io.rb', line 332 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 = "") ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-io/mrblib/io.rb', line 187 def read(length = nil, outbuf = "") unless length.nil? unless length.is_a? Integer 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 ⇒ Object
284 285 286 287 |
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-io/mrblib/io.rb', line 284 def readchar _read_buf _readchar(@buf) end |
#readline(arg = "\n", limit = nil) ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-io/mrblib/io.rb', line 228 def readline(arg = "\n", limit = nil) case arg when String rs = arg when Integer 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 ⇒ Object
324 325 326 327 328 329 330 |
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-io/mrblib/io.rb', line 324 def readlines ary = [] while (line = gets) ary << line end ary end |
#rewind ⇒ Object
161 162 163 |
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-io/mrblib/io.rb', line 161 def rewind seek(0, SEEK_SET) end |
#seek(i, whence = SEEK_SET) ⇒ Object
165 166 167 168 169 170 |
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-io/mrblib/io.rb', line 165 def seek(i, whence = SEEK_SET) raise IOError if closed? sysseek(i, whence) @buf = '' 0 end |
#ungetc(substr) ⇒ Object
177 178 179 180 181 182 183 184 185 |
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-io/mrblib/io.rb', line 177 def ungetc(substr) raise TypeError.new "expect String, got #{substr.class}" unless substr.is_a?(String) if @buf.empty? @buf.replace(substr) else @buf[0,0] = substr end nil end |
#write(string) ⇒ Object
124 125 126 127 128 129 130 131 132 133 |
# File 'ext/enterprise_script_service/mruby/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 |