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 $
new | -> | open |
lineno | [R] |
# File lib/tmail/stringio.rb, line 34 def initialize(str) @src = str @pos = 0 @closed = false @lineno = 0 end
# 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
# File lib/tmail/stringio.rb, line 95 def each(&block) stream_check! begin @src.each(&block) ensure @pos = 0 end end
# File lib/tmail/stringio.rb, line 120 def getc stream_check! ch = @src[@pos] @pos += 1 @pos += 1 if @pos == @src.size ch end
# 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
# File lib/tmail/stringio.rb, line 47 def inspect "#<#{self.class}:#{@closed ? 'closed' : 'open'},src=#{@src[0,30].inspect}>" end
# 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
# 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
# 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