Class StringInput
In: lib/tmail/stringio.rb
Parent: Object

stringio.rb

Copyright © 1999-2004 Minero Aoki

This program is free software. You can distribute/modify this program under the terms of the GNU Lesser General Public License version 2.1.

$amstdId: stringio.rb,v 1.12 2004/02/20 00:31:14 aamine Exp $

Methods

close   closed?   each   eof?   getc   gets   inspect   new   new   pos   read   read_all   rewind   seek   stream_check!   string   sysread   tell  

Included Modules

Enumerable

External Aliases

new -> open

Attributes

lineno  [R] 

Public Class methods

[Source]

# File lib/tmail/stringio.rb, line 34
  def initialize(str)
    @src = str
    @pos = 0
    @closed = false
    @lineno = 0
  end

[Source]

# File lib/tmail/stringio.rb, line 18
    def new(str)
      if block_given?
        begin
          f = super
          yield f
        ensure
          f.close if f
        end
      else
        super
      end
    end

Public Instance methods

[Source]

# File lib/tmail/stringio.rb, line 51
  def close
    stream_check!
    @pos = nil
    @closed = true
  end

[Source]

# File lib/tmail/stringio.rb, line 57
  def closed?
    @closed
  end

[Source]

# File lib/tmail/stringio.rb, line 95
  def each(&block)
    stream_check!
    begin
      @src.each(&block)
    ensure
      @pos = 0
    end
  end

[Source]

# File lib/tmail/stringio.rb, line 90
  def eof?
    stream_check!
    @pos > @src.size
  end

[Source]

# File lib/tmail/stringio.rb, line 120
  def getc
    stream_check!
    ch = @src[@pos]
    @pos += 1
    @pos += 1 if @pos == @src.size
    ch
  end

[Source]

# File lib/tmail/stringio.rb, line 104
  def gets
    stream_check!
    if idx = @src.index(?\n, @pos)
      idx += 1  # "\n".size
      line = @src[ @pos ... idx ]
      @pos = idx
      @pos += 1 if @pos == @src.size
    else
      line = @src[ @pos .. -1 ]
      @pos = @src.size + 1
    end
    @lineno += 1

    line
  end

[Source]

# File lib/tmail/stringio.rb, line 47
  def inspect
    "#<#{self.class}:#{@closed ? 'closed' : 'open'},src=#{@src[0,30].inspect}>"
  end

[Source]

# File lib/tmail/stringio.rb, line 61
  def pos
    stream_check!
    [@pos, @src.size].min
  end

[Source]

# File lib/tmail/stringio.rb, line 128
  def read(len = nil)
    stream_check!
    return read_all() unless len
    str = @src[@pos, len]
    @pos += len
    @pos += 1 if @pos == @src.size
    str
  end

[Source]

# File lib/tmail/stringio.rb, line 139
  def read_all
    stream_check!
    return nil if eof?
    rest = @src[@pos ... @src.size]
    @pos = @src.size + 1
    rest
  end

[Source]

# File lib/tmail/stringio.rb, line 85
  def rewind
    stream_check!
    @pos = 0
  end

[Source]

# File lib/tmail/stringio.rb, line 68
  def seek(offset, whence = IO::SEEK_SET)
    stream_check!
    case whence
    when IO::SEEK_SET
      @pos = offset
    when IO::SEEK_CUR
      @pos += offset
    when IO::SEEK_END
      @pos = @src.size - offset
    else
      raise ArgumentError, "unknown seek flag: #{whence}"
    end
    @pos = 0 if @pos < 0
    @pos = [@pos, @src.size + 1].min
    offset
  end

[Source]

# File lib/tmail/stringio.rb, line 147
  def stream_check!
    @closed and raise IOError, 'closed stream'
  end

[Source]

# File lib/tmail/stringio.rb, line 43
  def string
    @src
  end
sysread(len = nil)

Alias for read

tell()

Alias for pos

[Validate]