Class: StringIO

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb

Overview

MacRuby implementation of stringio.

This file is covered by the Ruby license. See COPYING for more details.

Copyright © 2012, The MacRuby Team. All rights reserved. Copyright © 2009-2011, Apple Inc. All rights reserved.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string = String.new, mode = nil) ⇒ StringIO

StringIO.new(string=“”[, mode])

Creates new StringIO instance from with string and mode.

Raises:

  • (Errno::EACCES)


56
57
58
59
60
61
62
63
64
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 56

def initialize(string = String.new, mode = nil)
  @string = string.to_str
  @pos = 0
  @lineno = 0
  define_mode(mode)

  raise Errno::EACCES if (@writable && string.frozen?)
  self
end

Instance Attribute Details

#linenoObject

strio.lineno -> integer

Returns the current line number in strio. The stringio must be opened for reading. lineno counts the number of times gets is called, rather than the number of newlines encountered. The two values will differ if gets is called with a separator other than newline. See also the $. variable.

strio.lineno = integer    -> integer

Manually sets the current line number to the given value. $. is updated only on the next read.



26
27
28
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 26

def lineno
  @lineno
end

#posObject

Returns the value of attribute pos.



10
11
12
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 10

def pos
  @pos
end

#stringObject

Returns the value of attribute string.



10
11
12
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 10

def string
  @string
end

Class Method Details

.open(*args) ⇒ Object

StringIO.open(string=“”[, mode]) {|strio| …}

Equivalent to StringIO.new except that when it is called with a block, it yields with the new instance and closes it, and returns the result which returned from the block.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 36

def self.open(*args)
  obj = new(*args)

  if block_given?
    begin
      yield obj
    ensure
      obj.close
      obj.instance_variable_set(:@string, nil)
      obj
    end
  else
    obj
  end
end

Instance Method Details

#<<(str) ⇒ Object

strio << obj -> strio

See IO#<<.



557
558
559
560
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 557

def <<(str)
  self.write(str)
  self
end

#binmodeObject



276
277
278
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 276

def binmode
  self
end

#closeObject

strio.close -> nil

Closes strio. The strio is unavailable for any further data operations; an IOError is raised if such an attempt is made.

Raises:

  • (IOError)


233
234
235
236
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 233

def close
  raise(IOError, "closed stream") if closed?
  @readable = @writable = nil
end

#close_readObject

strio.close_read -> nil

Closes the read end of a StringIO. Will raise an IOError if the strio is not readable.

Raises:

  • (IOError)


251
252
253
254
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 251

def close_read
  raise(IOError, "closing non-duplex IO for reading") unless @readable
  @readable = nil
end

#close_writeObject

Raises:

  • (IOError)


563
564
565
566
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 563

def close_write
  raise(IOError, "closing non-duplex IO for writing") unless @writable
  @writable = nil
end

#closed?Boolean

strio.closed? -> true or false

Returns true if strio is completely closed, false otherwise.

Returns:

  • (Boolean)


224
225
226
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 224

def closed?
  !@readable && !@writable
end

#closed_read?Boolean

strio.closed_read? -> true or false

Returns true if strio is not readable, false otherwise.

Returns:

  • (Boolean)


242
243
244
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 242

def closed_read?
  !@readable
end

#closed_write?Boolean

strio.closed_write? -> true or false

Returns true if strio is not writable, false otherwise.

Returns:

  • (Boolean)


260
261
262
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 260

def closed_write?
  !@writable
end

#each(sep = $/, limit = nil) ⇒ Object Also known as: each_line, lines

strio.each(sep=$/) {|line| block } -> strio

strio.each(limit) {|line| block }          -> strio
strio.each(sep, limit) {|line| block }     -> strio
strio.each_line(sep=$/) {|line| block }    -> strio
strio.each_line(limit) {|line| block }     -> strio
strio.each_line(sep,limit) {|line| block } -> strio

See IO#each.



458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 458

def each(sep=$/, limit=nil)
  if block_given?
    raise(IOError, "not opened for reading") unless @readable
    sep, limit = getline_args(sep, limit)
    if limit == 0
      raise ArgumentError, "invalid limit: 0 for each and family"
    end
    while line = getline(sep, limit)
      yield(line)
    end
    self
  else
    to_enum(:each, sep, limit)
  end
end

#each_byteObject Also known as: bytes

strio.each_byte {|byte| block } -> strio

See IO#each_byte.

Raises:

  • (IOError)


436
437
438
439
440
441
442
443
444
445
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 436

def each_byte
  raise(IOError, "not opened for reading") unless @readable
  return self if (@pos > @string.length)
  if block_given?
    @string.each_byte{|b| @pos += 1; yield(b)}
    self
  else
    @string.each_byte
  end
end

#each_charObject Also known as: chars

strio.each_char {|char| block } -> strio

See IO#each_char.

Raises:

  • (IOError)


407
408
409
410
411
412
413
414
415
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 407

def each_char
  raise(IOError, "not opened for reading") unless @readable
  if block_given?
    @string.each_char{|c| yield(c)}
    self
  else
    @string.each_char
  end
end

#eof?Boolean Also known as: eof

strio.eof -> true or false

strio.eof?    -> true or false

Returns true if strio is at end of file. The stringio must be opened for reading or an IOError will be raised.

Returns:

  • (Boolean)

Raises:

  • (IOError)


270
271
272
273
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 270

def eof?
  raise(IOError, "not opened for reading") unless @readable
  @pos >= @string.length
end

#external_encodingObject



713
714
715
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 713

def external_encoding
  @string ? @string.encoding : nil
end

#fcntlObject

Raises:

  • (NotImplementedError)


280
281
282
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 280

def fcntl
  raise NotImplementedError, "StringIO#fcntl is not implemented"
end

#filenoObject

strio.fileno -> nil



318
319
320
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 318

def fileno
  nil
end

#flushObject



284
285
286
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 284

def flush
  self
end

#fsyncObject



288
289
290
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 288

def fsync
  0
end

#getbyteObject

strio.getbyte -> fixnum or nil

See IO#getbyte.

Raises:

  • (IOError)


421
422
423
424
425
426
427
428
429
430
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 421

def getbyte
  raise(IOError, "not opened for reading") unless @readable
  # Because we currently don't support bytes access
  # the following code isn't used
  # instead we are dealing with chars
  result = @string.bytes.to_a[@pos]
  @pos += 1 unless eof?
  result
  # getc
end

#getcObject

strio.getc -> string or nil

Gets the next character from io. Returns nil if called at end of file

Raises:

  • (IOError)


338
339
340
341
342
343
344
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 338

def getc
  raise(IOError, "not opened for reading") unless @readable
  return nil if eof?
  result = @string[@pos]
  @pos += 1
  result
end

#gets(sep = $/, limit = nil) ⇒ Object

strio.gets(sep=$/) -> string or nil

strio.gets(limit)      -> string or nil
strio.gets(sep, limit) -> string or nil

See IO#gets.



482
483
484
485
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 482

def gets(sep=$/, limit=nil)
  sep, limit = getline_args(sep, limit)
  $_ = getline(sep, limit)
end

#initialize_copy(from) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 66

def initialize_copy(from)
  from = from.to_strio
  self.taint if from.tainted?

  @string   = from.instance_variable_get(:@string).dup
  # mode
  @append   = from.instance_variable_get(:@append)
  @readable = from.instance_variable_get(:@readable)
  @writable = from.instance_variable_get(:@writable)

  @pos = from.instance_variable_get(:@pos)
  @lineno = from.instance_variable_get(:@lineno)

  self
end

#internal_encodingObject



717
718
719
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 717

def internal_encoding
  nil
end

#isattyObject Also known as: tty?

strio.isatty -> nil

strio.tty? -> nil


324
325
326
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 324

def isatty
  false
end

#lengthObject Also known as: size



329
330
331
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 329

def length
  @string.length
end

#pidObject

strio.pid -> nil



294
295
296
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 294

def pid
  nil
end

strio.print() => nil

   strio.print(obj, ...)     => nil

Writes the given object(s) to <em>strio</em>. The stream must be
opened for writing. If the output record separator (<code>$\\</code>)
is not <code>nil</code>, it will be appended to the output. If no
arguments are given, prints <code>$_</code>. Objects that aren't
strings will be converted by calling their <code>to_s</code> method.
With no argument, prints the contents of the variable <code>$_</code>.
Returns <code>nil</code>.

   io.print("This is ", 100, " percent.\n")

<em>produces:</em>

   This is 100 percent.

Raises:

  • (IOError)


688
689
690
691
692
693
694
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 688

def print(*args)
  raise IOError, "not opened for writing" unless @writable
  args << $_ if args.empty?
  args.map! { |x| (x == nil) ? "nil" : x }
  write((args << $\).flatten.join)
  nil
end

#printf(*args) ⇒ Object

printf(strio, string [, obj … ] ) => nil

Equivalent to:
   strio.write(sprintf(string, obj, ...)

Raises:

  • (IOError)


701
702
703
704
705
706
707
708
709
710
711
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 701

def printf(*args)
  raise IOError, "not opened for writing" unless @writable

  if args.size > 1
    write(args.shift % args)
  else
    write(args.first)
  end

  nil
end

#putc(obj) ⇒ Object

strio.putc(obj) -> obj

If <i>obj</i> is <code>Numeric</code>, write the character whose
code is <i>obj</i>, otherwise write the first character of the
string representation of  <i>obj</i> to <em>strio</em>.

Raises:

  • (IOError)


645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 645

def putc(obj)
  raise(IOError, "not opened for writing") unless @writable

  if obj.is_a?(String)
    char = obj[0]
  else
    raise TypeError unless obj.respond_to?(:to_int)
    char = obj.to_int % 256
  end

  if @append || @pos == @string.length
    @string << char
    @pos = @string.length
  elsif @pos > @string.length
    @string = @string.ljust(@pos, "\000")
    @string << char
    @pos = @string.length
  else
    @string[@pos] = ("" << char)
    @pos += 1
  end

  obj
end

#puts(*args) ⇒ Object

strio.puts(obj, …) -> nil

Writes the given objects to <em>strio</em> as with
<code>IO#print</code>. Writes a record separator (typically a
newline) after any that do not already end with a newline sequence.
If called with an array argument, writes each element on a new line.
If called without arguments, outputs a single record separator.

   io.puts("this", "is", "a", "test")

<em>produces:</em>

   this
   is
   a
   test


604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 604

def puts(*args)
  if args.empty?
    write("\n")
  else
    args.each do |arg|
      if arg == nil
        line = "nil"
      else
        begin
          arg = arg.to_ary
          recur = false
          arg.each do |a|
            if arg == a
              recur = true
              break
            else
              puts a
            end
          end
          next unless recur
          line = '[...]'
        rescue
          line = arg.to_s
        end
      end

      write(line)
      write("\n") if line[-1] != ?\n
    end
  end

  nil
end

#read(length = nil, buffer = String.new) ⇒ Object

strio.read([length [, buffer]]) -> string, buffer, or nil

See IO#read.

Raises:

  • (IOError)


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
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 130

def read(length = nil, buffer = String.new)
  raise IOError, "not opened for reading" unless @readable
  raise TypeError unless buffer.respond_to?(:to_str)
  buffer = buffer.to_str

  if length.nil?
    return buffer.replace("") if self.eof?
    buffer.replace(@string[@pos..-1])
    @pos = @string.size
  else
    raise TypeError unless length.respond_to?(:to_int)
    length = length.to_int
    raise ArgumentError if length < 0

    if length == 0
      buffer.replace("")
    else
      if self.eof?
        buffer.replace("")
        return nil
      end
      buffer.replace(@string[@pos, length])
      @pos += buffer.length
    end
    buffer.force_encoding('BINARY')
 end


  buffer
end

#readbyteObject

strio.readbyte -> fixnum

See IO#readbyte.

Raises:

  • (IO::EOFError)


177
178
179
180
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 177

def readbyte
  raise(IO::EOFError, "end of file reached") if eof?
  getbyte
end

#readcharObject

strio.readchar -> fixnum

See IO#readchar.

Raises:

  • (IO::EOFError)


398
399
400
401
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 398

def readchar
  raise(IO::EOFError, "end of file reached") if eof?
  getc
end

#readline(sep = $/, limit = nil) ⇒ Object

strio.readline(sep=$/) -> string

strio.readline(limit)      -> string or nil
strio.readline(sep, limit) -> string or nil

See IO#readline.

Raises:

  • (IO::EOFError)


492
493
494
495
496
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 492

def readline(sep=$/, limit=nil)
  raise(IO::EOFError, 'end of file reached') if eof?
  sep, limit = getline_args(sep, limit)
  $_ = getline(sep, limit)
end

#readlines(sep = $/, limit = nil) ⇒ Object

strio.readlines(sep=$/) -> array

strio.readlines(limit)     ->   array
strio.readlines(sep,limit) ->   array

See IO#readlines.

Raises:

  • (IOError)


504
505
506
507
508
509
510
511
512
513
514
515
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 504

def readlines(sep=$/, limit=nil)
  raise IOError, "not opened for reading" unless @readable
  ary = []
  sep, limit = getline_args(sep, limit)
  if limit == 0
    raise ArgumentError, "invalid limit: 0 for readlines"
  end
  while line = getline(sep, limit)
    ary << line
  end
  ary
end

#reopen(str = nil, mode = nil) ⇒ Object

strio.reopen(other_StrIO) -> strio

strio.reopen(string, mode)    -> strio

Reinitializes strio with the given other_StrIO or string and mode (see StringIO#new).



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 88

def reopen(str=nil, mode=nil)
  if str == nil && mode == nil
    mode = 'w+'
  elsif !str.kind_of?(String) && mode == nil
    raise TypeError unless str.respond_to?(:to_strio)
    return initialize_copy(str)
  else
    raise TypeError unless str.respond_to?(:to_str)
    @string = str.to_str
  end

  define_mode(mode)
  @pos = 0
  @lineno = 0

  self
end

#rewindObject

strio.rewind -> 0

Positions strio to the beginning of input, resetting lineno to zero.



121
122
123
124
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 121

def rewind
  @pos = 0
  @lineno = 0
end

#seek(offset, whence = ::IO::SEEK_SET) ⇒ Object

strio.seek(amount, whence=SEEK_SET) -> 0

Seeks to a given offset amount in the stream according to the value of whence (see IO#seek).

Raises:

  • (IOError)


187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 187

def seek(offset, whence = ::IO::SEEK_SET)
  raise(IOError, "closed stream") if closed?
  raise TypeError unless offset.respond_to?(:to_int)
  offset = offset.to_int

  case whence
  when ::IO::SEEK_CUR
    # Seeks to offset plus current position
    offset += @pos
  when ::IO::SEEK_END
    # Seeks to offet plus end of stream (usually offset is a negative value)
    offset += @string.size
  when ::IO::SEEK_SET, nil
    # Seeks to the absolute location given by offset
  else
    raise Errno::EINVAL, "invalid whence"
  end

  raise Errno::EINVAL if (offset < 0)
  @pos = offset

  0
end

#set_encoding(enc) ⇒ Object



721
722
723
724
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 721

def set_encoding(enc)
  @string = @string.encode(enc)
  self
end

#syncObject

strio.sync -> true

Returns true always.



302
303
304
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 302

def sync
  true
end

#sync=(value) ⇒ Object

strio.sync = boolean -> boolean



308
309
310
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 308

def sync=(value)
  value
end

#sysread(length = nil, buffer = String.new) ⇒ Object Also known as: readpartial, read_nonblock

strio.sysread(integer[, outbuf]) -> string

Similar to #read, but raises EOFError at end of string instead of returning nil, as well as IO#sysread does.



165
166
167
168
169
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 165

def sysread(length = nil, buffer = String.new)
  val = read(length, buffer)
  ( buffer.clear && raise(IO::EOFError, "end of file reached")) if val == nil
  val
end

#tellObject



312
313
314
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 312

def tell
  @pos
end

#truncate(len) ⇒ Object

strio.truncate(integer) -> 0

Truncates the buffer string to at most integer bytes. The strio must be opened for writing.

Raises:

  • (IOError)


573
574
575
576
577
578
579
580
581
582
583
584
585
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 573

def truncate(len)
  raise(IOError, "closing non-duplex IO for writing") unless @writable
  raise(TypeError) unless len.respond_to?(:to_int)
  length = len.to_int
  raise(Errno::EINVAL, "negative length") if (length < 0)
  if length < @string.size
    @string[length .. @string.size] = ""
  else
    @string = @string.ljust(length, "\000")
  end
  # send back what was passed, not our :to_int version
  len
end

#ungetbyte(bytes) ⇒ Object

strio.ungetbyte(fixnum) -> nil

See IO#ungetbyte

Raises:

  • (IOError)


376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 376

def ungetbyte(bytes)
  raise(IOError, "not opened for reading") unless @readable
  return nil if bytes == nil

  bytes = bytes.chr if bytes.kind_of?(Fixnum)
  raise TypeError unless bytes.respond_to?(:to_str)
  bytes = bytes.to_str

  if @pos == 0
    @string = bytes + @string
  elsif @pos > 0
    @pos -= 1
    @string[@pos] = bytes
  end

  nil
end

#ungetc(chars) ⇒ Object

strio.ungetc(string) -> nil

Pushes back one character (passed as a parameter) onto strio such that a subsequent buffered read will return it. Pushing back behind the beginning of the buffer string is not possible. Nothing will be done if such an attempt is made. In other case, there is no limitation for multiple pushbacks.

Raises:

  • (IOError)


354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 354

def ungetc(chars)
  raise(IOError, "not opened for reading") unless @readable
  return nil if chars == nil

  chars = chars.chr if chars.kind_of?(Fixnum)
  raise TypeError unless chars.respond_to?(:to_str)
  chars = chars.to_str

  if @pos == 0
    @string = chars + @string
  elsif @pos > 0
    @pos -= 1
    @string[@pos] = chars
  end

  nil
end

#write(str) ⇒ Object Also known as: syswrite, write_nonblock

strio.write(string) -> integer

strio.syswrite(string) -> integer

Appends the given string to the underlying buffer string of strio. The stream must be opened for writing. If the argument is not a string, it will be converted to a string using to_s. Returns the number of bytes written. See IO#write.

Raises:

  • (IOError)


526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
# File 'lib/motion-bundler/mocks/mac_ruby-0.12/stringio.rb', line 526

def write(str)
  raise(IOError, "not opened for writing") unless @writable
  raise(IOError, "not modifiable string") if @string.frozen?

  str = str.to_s
  return 0 if str.empty?

  if @append || (@pos >= @string.length)
    # add padding in case it's needed
    str = str.rjust((@pos + str.length) - @string.length, "\000") if (@pos > @string.length)
    enc1, enc2 = str.encoding, @string.encoding
    if enc1 != enc2
      str = str.dup.force_encoding(enc2)
    end
    @string << str
    @pos = @string.length
  else
    @string[@pos, str.length] = str
    @pos += str.length
    @string.taint if str.tainted?
  end

  str.length
end