Class: IO

Inherits:
Object show all
Defined in:
opal/opal/corelib/io.rb

Direct Known Subclasses

File

Constant Summary collapse

SEEK_SET =
0
SEEK_CUR =
1
SEEK_END =
2
SEEK_DATA =
3
SEEK_HOLE =
4
READABLE =
1
WRITABLE =
4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fd, flags = 'r') ⇒ IO

Returns a new instance of IO.



11
12
13
14
15
16
17
18
19
20
21
# File 'opal/opal/corelib/io.rb', line 11

def initialize(fd, flags = 'r')
  @fd = fd
  @flags = flags
  @eof = false

  if flags.include?('r') && !flags.match?(/[wa+]/)
    @closed = :write
  elsif flags.match?(/[wa]/) && !flags.match?(/[r+]/)
    @closed = :read
  end
end

Instance Attribute Details

#eofObject (readonly) Also known as: eof?

Returns the value of attribute eof.



41
42
43
# File 'opal/opal/corelib/io.rb', line 41

def eof
  @eof
end

#read_procObject

Returns the value of attribute read_proc.



32
33
34
# File 'opal/opal/corelib/io.rb', line 32

def read_proc
  @read_proc
end

#syncObject

Returns the value of attribute sync.



39
40
41
# File 'opal/opal/corelib/io.rb', line 39

def sync
  @sync
end

#ttyObject

Returns the value of attribute tty.



39
40
41
# File 'opal/opal/corelib/io.rb', line 39

def tty
  @tty
end

#write_procObject

Returns the value of attribute write_proc.



31
32
33
# File 'opal/opal/corelib/io.rb', line 31

def write_proc
  @write_proc
end

Instance Method Details

#<<(string) ⇒ Object



48
49
50
51
# File 'opal/opal/corelib/io.rb', line 48

def <<(string)
  write(string)
  self
end

#check_readableObject



283
284
285
286
287
# File 'opal/opal/corelib/io.rb', line 283

def check_readable
  if closed_read?
    raise IOError, 'not opened for reading'
  end
end

#check_writableObject



276
277
278
279
280
# File 'opal/opal/corelib/io.rb', line 276

def check_writable
  if closed_write?
    raise IOError, 'not opened for writing'
  end
end

#closeObject

Closedness



243
244
245
# File 'opal/opal/corelib/io.rb', line 243

def close
  @closed = :both
end

#close_readObject



247
248
249
250
251
252
253
# File 'opal/opal/corelib/io.rb', line 247

def close_read
  if @closed == :write
    @closed = :both
  else
    @closed = :read
  end
end

#close_writeObject



255
256
257
258
259
260
261
# File 'opal/opal/corelib/io.rb', line 255

def close_write
  if @closed == :read
    @closed = :both
  else
    @closed = :write
  end
end

#closed?Boolean

Returns:



263
264
265
# File 'opal/opal/corelib/io.rb', line 263

def closed?
  @closed == :both
end

#closed_read?Boolean

Returns:



267
268
269
# File 'opal/opal/corelib/io.rb', line 267

def closed_read?
  @closed == :read || @closed == :both
end

#closed_write?Boolean

Returns:



271
272
273
# File 'opal/opal/corelib/io.rb', line 271

def closed_write?
  @closed == :write || @closed == :both
end

#each(sep = $/, *args, &block) ⇒ Object Also known as: each_line



209
210
211
212
213
214
215
216
217
# File 'opal/opal/corelib/io.rb', line 209

def each(sep = $/, *args, &block)
  return enum_for :each, sep, *args unless block_given?

  while (s = gets(sep, *args))
    yield(s)
  end

  self
end

#each_byte(&block) ⇒ Object



221
222
223
224
225
226
227
228
229
# File 'opal/opal/corelib/io.rb', line 221

def each_byte(&block)
  return enum_for :each_byte unless block_given?

  while (s = getbyte)
    yield(s)
  end

  self
end

#each_char(&block) ⇒ Object



231
232
233
234
235
236
237
238
239
# File 'opal/opal/corelib/io.rb', line 231

def each_char(&block)
  return enum_for :each_char unless block_given?

  while (s = getc)
    yield(s)
  end

  self
end

#filenoObject



23
24
25
# File 'opal/opal/corelib/io.rb', line 23

def fileno
  @fd
end

#flushObject



44
45
46
# File 'opal/opal/corelib/io.rb', line 44

def flush
  # noop
end

#getbyteObject



93
94
95
# File 'opal/opal/corelib/io.rb', line 93

def getbyte
  getc&.ord
end

#getcObject

Reading



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'opal/opal/corelib/io.rb', line 75

def getc
  @read_buffer ||= ''
  parts = ''

  # Will execure at most twice - one time reading from a buffer
  # second time
  begin
    @read_buffer += parts
    if @read_buffer != ''
      ret = @read_buffer[0]
      @read_buffer = @read_buffer[1..-1]
      return ret
    end
  end while parts = sysread_noraise(1)

  nil
end

#gets(sep = false, limit = nil, opts = {}) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'opal/opal/corelib/io.rb', line 109

def gets(sep = false, limit = nil, opts = {})
  if `sep.$$is_number` && !limit
    sep, limit, opts = false, sep, limit
  end
  if `sep.$$is_hash` && !limit && opts == {}
    sep, limit, opts = false, nil, sep
  elsif `limit.$$is_hash` && opts == {}
    sep, limit, opts = sep, nil, limit
  end

  orig_sep = sep

  sep = $/ if sep == false
  sep = /\r?\n\r?\n/ if sep == ''
  sep ||= ''
  sep = sep.to_str unless orig_sep == ''

  # Try to deduce length of a regexp
  seplen = `orig_sep == '' ? 2 : sep.length`

  sep = / / if sep == ' ' # WTF is this, String#split(" ") matches all whitespaces???

  @read_buffer ||= ''
  data = ''
  ret = nil

  begin
    @read_buffer += data
    if sep != '' && (`sep.$$is_regexp` ? @read_buffer.match?(sep) : @read_buffer.include?(sep))
      orig_buffer = @read_buffer
      ret, @read_buffer = @read_buffer.split(sep, 2)
      ret += orig_buffer[ret.length, seplen] if ret != orig_buffer
      break
    end
  end while data = sysread_noraise(sep == '' ? 65_536 : 1)

  unless ret
    ret, @read_buffer = (@read_buffer || ''), ''
    ret = nil if ret == ''
  end

  if ret
    if limit
      ret = ret[0...limit]
      @read_buffer = ret[limit..-1] + @read_buffer
    end
    ret = ret.sub(/\r?\n\z/, '') if opts[:chomp]
    ret = ret.sub(/\A[\r\n]+/, '') if orig_sep == ''
  end

  $_ = ret if orig_sep == false
  ret
end


53
54
55
56
57
58
59
60
61
# File 'opal/opal/corelib/io.rb', line 53

def print(*args)
  %x{
    for (var i = 0, ii = args.length; i < ii; i++) {
      args[i] = #{String(`args[i]`)}
    }
    self.$write(args.join(#{$,}));
  }
  nil
end

#puts(*args) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'opal/opal/corelib/io.rb', line 63

def puts(*args)
  %x{
    for (var i = 0, ii = args.length; i < ii; i++) {
      args[i] = #{String(`args[i]`).chomp}
    }
    self.$write(args.concat([nil]).join(#{$/}));
  }
  nil
end

#read(integer = nil) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'opal/opal/corelib/io.rb', line 186

def read(integer = nil)
  @read_buffer ||= ''
  parts = ''
  ret = nil

  begin
    @read_buffer += parts
    if integer && @read_buffer.length > integer
      ret, @read_buffer = @read_buffer[0...integer], @read_buffer[integer..-1]
      return ret
    end
  end while parts = sysread_noraise(integer || 65_536)

  ret, @read_buffer = @read_buffer, ''
  ret
end

#readbyteObject



97
98
99
# File 'opal/opal/corelib/io.rb', line 97

def readbyte
  readchar.ord
end

#readcharObject



101
102
103
# File 'opal/opal/corelib/io.rb', line 101

def readchar
  getc || raise(EOFError, 'end of file reached')
end

#readline(*args) ⇒ Object



105
106
107
# File 'opal/opal/corelib/io.rb', line 105

def readline(*args)
  gets(*args) || raise(EOFError, 'end of file reached')
end

#readlines(separator = $/) ⇒ Object

Eaches



205
206
207
# File 'opal/opal/corelib/io.rb', line 205

def readlines(separator = $/)
  each_line(separator).to_a
end

#readpartial(integer) ⇒ Object



178
179
180
181
182
183
184
# File 'opal/opal/corelib/io.rb', line 178

def readpartial(integer)
  @read_buffer ||= ''
  part = sysread(integer)
  ret, @read_buffer = @read_buffer + (part || ''), ''
  ret = nil if ret == ''
  ret
end

#sysread(integer) ⇒ Object

This method is to be overloaded, or read_proc can be changed



164
165
166
167
168
169
# File 'opal/opal/corelib/io.rb', line 164

def sysread(integer)
  `self.read_proc(integer)` || begin
    @eof = true
    raise EOFError, 'end of file reached'
  end
end

#sysread_noraise(integer) ⇒ Object



172
173
174
175
176
# File 'opal/opal/corelib/io.rb', line 172

def sysread_noraise(integer)
  sysread(integer)
rescue EOFError
  nil
end

#tty?Boolean

Returns:



27
28
29
# File 'opal/opal/corelib/io.rb', line 27

def tty?
  `self.tty == true`
end

#write(string) ⇒ Object



34
35
36
37
# File 'opal/opal/corelib/io.rb', line 34

def write(string)
  `self.write_proc(string)`
  string.size
end